Skip to content

Commit

Permalink
fixed TypeScript definition for getTokenPrice method (#720)
Browse files Browse the repository at this point in the history
fixed TypeScript definition for the getTokenPrice method
  • Loading branch information
b4rtaz authored Oct 4, 2022
1 parent 555883f commit e5fb3d6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-eels-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/evm-api': patch
---

Fixed TypeScript definition for the `getTokenPrice` API method.
7 changes: 4 additions & 3 deletions packages/evmApi/src/resolvers/token/getTokenPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ type ApiParams = QueryParams & PathParams;

type ApiResult = operations[operation]['responses']['200']['content']['application/json'];

interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address' | 'to_block'>> {
chain?: EvmChainish;
address: EvmAddressish;
toBlock?: number;
}

export const getTokenPrice = createEndpointFactory((core) =>
createEndpoint({
name: 'getTokenPrice',
urlParams: ['address'],
getUrl: (params: ApiParams) => `/erc20/${params.address}/price`,
apiToResult: (data: ApiResult) => ({
getUrl: (params: Params) => `/erc20/${params.address}/price`,
apiToResult: (data: ApiResult, _: Params) => ({
...toCamelCase(data),
nativePrice: data.nativePrice?.value
? EvmNative.create(data.nativePrice?.value, data.nativePrice?.decimals)
Expand Down
36 changes: 28 additions & 8 deletions packages/integration/mockRequests/evmApi/getTokenPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { rest } from 'msw';
import { EVM_API_ROOT, MOCK_API_KEY } from '../config';

export const mockGetTokenPrice = rest.get(`${EVM_API_ROOT}/erc20/:address/price`, (req, res, ctx) => {
const address = req.params.address as string;
const apiKey = req.headers.get('x-api-key');

if (apiKey !== MOCK_API_KEY) {
return res(ctx.status(401));
}

if (address === '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce') {
function createSuccessRes() {
return res(
ctx.status(200),
ctx.json({
Expand All @@ -21,5 +14,32 @@ export const mockGetTokenPrice = rest.get(`${EVM_API_ROOT}/erc20/:address/price`
);
}

const address = req.params.address as string;
const apiKey = req.headers.get('x-api-key');

if (apiKey !== MOCK_API_KEY) {
return res(ctx.status(401));
}

if (address === '0x0000000000000000000000000000000000000001') {
return createSuccessRes();
}

if (address === '0x0000000000000000000000000000000000000002') {
const chain = req.url.searchParams.get('chain') as string;
if (chain !== '0x13881') {
throw new Error(`Expected chain=0x13881, got ${chain}`);
}
return createSuccessRes();
}

if (address === '0x0000000000000000000000000000000000000003') {
const toBlock = req.url.searchParams.get('to_block') as string;
if (toBlock !== '512') {
throw new Error(`Expected to_block=512, got ${toBlock}`);
}
return createSuccessRes();
}

throw new Error('getTokenPrice: Not supported scenario');
});
18 changes: 17 additions & 1 deletion packages/integration/test/evmApi/getTokenPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,27 @@ describe('getTokenPrice', () => {

it('returns an info', async () => {
const response = await evmApi.token.getTokenPrice({
address: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce',
address: '0x0000000000000000000000000000000000000001',
});

expect(response.result.usdPrice).toBe(0.000011961341215674);
expect(response.result.exchangeAddress?.lowercase).toBe('0x1f98431c8ad98523631ae4a59f267346ea31f984');
expect(response.result.exchangeName).toBe('Uniswap v3');
});

it('passes chain correctly', async () => {
const response = await evmApi.token.getTokenPrice({
address: '0x0000000000000000000000000000000000000002',
chain: 0x13881,
});
expect(response).toBeDefined();
});

it('passes toBlock correctly', async () => {
const response = await evmApi.token.getTokenPrice({
address: '0x0000000000000000000000000000000000000003',
toBlock: 512,
});
expect(response).toBeDefined();
});
});

0 comments on commit e5fb3d6

Please sign in to comment.