diff --git a/.changeset/grumpy-eels-mix.md b/.changeset/grumpy-eels-mix.md new file mode 100644 index 0000000000..ff5b44f0d1 --- /dev/null +++ b/.changeset/grumpy-eels-mix.md @@ -0,0 +1,5 @@ +--- +'@moralisweb3/evm-api': patch +--- + +Fixed TypeScript definition for the `getTokenPrice` API method. diff --git a/packages/evmApi/src/resolvers/token/getTokenPrice.ts b/packages/evmApi/src/resolvers/token/getTokenPrice.ts index 20a2690095..2d15678536 100644 --- a/packages/evmApi/src/resolvers/token/getTokenPrice.ts +++ b/packages/evmApi/src/resolvers/token/getTokenPrice.ts @@ -12,17 +12,18 @@ type ApiParams = QueryParams & PathParams; type ApiResult = operations[operation]['responses']['200']['content']['application/json']; -interface Params extends Camelize> { +export interface Params extends Camelize> { 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) diff --git a/packages/integration/mockRequests/evmApi/getTokenPrice.ts b/packages/integration/mockRequests/evmApi/getTokenPrice.ts index 6285993d80..2f288c985f 100644 --- a/packages/integration/mockRequests/evmApi/getTokenPrice.ts +++ b/packages/integration/mockRequests/evmApi/getTokenPrice.ts @@ -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({ @@ -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'); }); diff --git a/packages/integration/test/evmApi/getTokenPrice.test.ts b/packages/integration/test/evmApi/getTokenPrice.test.ts index b74c73b6e0..04de4204dc 100644 --- a/packages/integration/test/evmApi/getTokenPrice.test.ts +++ b/packages/integration/test/evmApi/getTokenPrice.test.ts @@ -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(); + }); });