diff --git a/packages/common/evmUtils/src/dataTypes/Erc20/Erc20.ts b/packages/common/evmUtils/src/dataTypes/Erc20/Erc20.ts index 5c172227d5..435196b86a 100644 --- a/packages/common/evmUtils/src/dataTypes/Erc20/Erc20.ts +++ b/packages/common/evmUtils/src/dataTypes/Erc20/Erc20.ts @@ -19,6 +19,7 @@ export class Erc20Token implements MoralisDataObject { * Create a new instance of Erc20Token from any valid Erc20Token input * * @param value - the Erc20Tokenish type + * @param core - The MoralisCore instance * @example * ```ts * const token = Erc20Token.create(value); diff --git a/packages/common/evmUtils/src/dataTypes/Erc20Value/Erc20Value.ts b/packages/common/evmUtils/src/dataTypes/Erc20Value/Erc20Value.ts index 9a143948ef..e496d035e1 100644 --- a/packages/common/evmUtils/src/dataTypes/Erc20Value/Erc20Value.ts +++ b/packages/common/evmUtils/src/dataTypes/Erc20Value/Erc20Value.ts @@ -1,4 +1,4 @@ -import { BigNumber, BigNumberish, CoreErrorCode, CoreError, MoralisData } from '@moralisweb3/common-core'; +import MoralisCore, { BigNumber, BigNumberish, CoreError, CoreErrorCode, MoralisData } from '@moralisweb3/common-core'; import { Erc20Token, Erc20Tokenish } from '../Erc20/Erc20'; const EVM_ERC20_DEFAULT_DECIMALS = 18; @@ -46,22 +46,23 @@ export class Erc20Value implements MoralisData { * Create a new instance of Erc20Value from any valid input * @param value - The value to create * @param options - The options for the token + * @param core - The MoralisCore instance * @example Erc20Value.create(1000, { decimals: 3 }); * @returns The created value * @throws CoreError if the value is invalid */ - static create(value: Erc20Valueish, options?: Erc20Options): Erc20Value { + static create(value: Erc20Valueish, options?: Erc20Options, core?: MoralisCore): Erc20Value { if (value instanceof Erc20Value) { return value; } - return new Erc20Value(value, options); + return new Erc20Value(value, options, core); } private _value: Erc20ValueData; private _token?: Erc20Token; - constructor(amount: Erc20ValueInputAmount, options?: Erc20Options) { + constructor(amount: Erc20ValueInputAmount, options?: Erc20Options, core?: MoralisCore) { this._value = Erc20Value.parse({ amount, decimals: options?.decimals ?? options?.token?.decimals ?? EVM_ERC20_DEFAULT_DECIMALS, @@ -69,7 +70,7 @@ export class Erc20Value implements MoralisData { }); if (options?.token) { - this._token = Erc20Token.create(options.token); + this._token = Erc20Token.create(options.token, core); } } diff --git a/packages/common/evmUtils/src/operations/nft/getNFTLowestPriceOperation.ts b/packages/common/evmUtils/src/operations/nft/getNFTLowestPriceOperation.ts index 692ae685d5..87aaff94e3 100644 --- a/packages/common/evmUtils/src/operations/nft/getNFTLowestPriceOperation.ts +++ b/packages/common/evmUtils/src/operations/nft/getNFTLowestPriceOperation.ts @@ -53,7 +53,7 @@ export const getNFTLowestPriceOperation: Operation< function getRequestUrlParams(request: GetNFTLowestPriceRequest, core: Core) { return { chain: EvmChainResolver.resolve(request.chain, core).apiHex, - address: EvmAddress.create(request.address, core).checksum, + address: EvmAddress.create(request.address, core).lowercase, days: maybe(request.days, String), provider_url: request.providerUrl, marketplace: request.marketplace, diff --git a/packages/common/evmUtils/src/operations/nft/getNFTOwnersOperation.ts b/packages/common/evmUtils/src/operations/nft/getNFTOwnersOperation.ts index 9907b64681..5597d303b1 100644 --- a/packages/common/evmUtils/src/operations/nft/getNFTOwnersOperation.ts +++ b/packages/common/evmUtils/src/operations/nft/getNFTOwnersOperation.ts @@ -60,7 +60,7 @@ export const getNFTOwnersOperation: PaginatedOperation< function getRequestUrlParams(request: GetNFTOwnersRequest, core: Core) { return { chain: EvmChainResolver.resolve(request.chain, core).apiHex, - address: EvmAddress.create(request.address, core).checksum, + address: EvmAddress.create(request.address, core).lowercase, format: request.format, limit: maybe(request.limit, String), cursor: request.cursor, diff --git a/packages/common/evmUtils/src/operations/nft/getNFTTransfersOperation.ts b/packages/common/evmUtils/src/operations/nft/getNFTTransfersOperation.ts index ef8e36afe9..8796f2359e 100644 --- a/packages/common/evmUtils/src/operations/nft/getNFTTransfersOperation.ts +++ b/packages/common/evmUtils/src/operations/nft/getNFTTransfersOperation.ts @@ -44,7 +44,7 @@ export const getNFTTransfersOperation: PaginatedOperation< name: 'getNFTTransfers', id: 'getNFTTransfers', groupName: 'nft', - urlPathPattern: '/nft/{address}/{token_id}/transfers', + urlPathPattern: '/nft/{address}/{tokenId}/transfers', urlPathParamNames: ['address', 'tokenId'], urlSearchParamNames: ['chain', 'format', 'limit', 'cursor'], firstPageIndex: 0, diff --git a/packages/common/evmUtils/src/operations/token/getWalletTokenBalancesOperation.ts b/packages/common/evmUtils/src/operations/token/getWalletTokenBalancesOperation.ts index 6a2eedd951..94c4f97182 100644 --- a/packages/common/evmUtils/src/operations/token/getWalletTokenBalancesOperation.ts +++ b/packages/common/evmUtils/src/operations/token/getWalletTokenBalancesOperation.ts @@ -67,18 +67,22 @@ function deserializeResponse( core: Core, ) { return (jsonResponse ?? []).map((token) => - Erc20Value.create(token.balance, { - decimals: token.decimals, - token: { + Erc20Value.create( + token.balance, + { decimals: token.decimals, - name: token.name, - symbol: token.symbol, - contractAddress: token.token_address, - logo: token.logo, - thumbnail: token.thumbnail, - chain: EvmChainResolver.resolve(request.chain, core), + token: { + decimals: token.decimals, + name: token.name, + symbol: token.symbol, + contractAddress: token.token_address, + logo: token.logo, + thumbnail: token.thumbnail, + chain: EvmChainResolver.resolve(request.chain, core), + }, }, - }), + core, + ), ); } diff --git a/packages/evmApi/integration/mocks/endpoints/getContractNFTs.ts b/packages/evmApi/integration/mocks/endpoints/getContractNFTs.ts index 450c27ff0c..743973b7b9 100644 --- a/packages/evmApi/integration/mocks/endpoints/getContractNFTs.ts +++ b/packages/evmApi/integration/mocks/endpoints/getContractNFTs.ts @@ -1,28 +1,71 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const tokens: Record = { - '0x7de3085b3190b3a787822ee16f23be010f5f8686': 741, -}; - -export const mockGetContractNFTs = rest.get(`${EVM_API_ROOT}/nft/:address`, (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)); - } - - const value = tokens[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetContractNFTs = MockScenarios.create( + { + method: 'get', + name: 'mockGetContractNFTs', + url: `/nft/:address`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + status: 'SYNCING', + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + token_id: '15', + contract_type: 'ERC721', + owner_of: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + block_number: '88256', + block_number_minted: '88256', + token_uri: 'string', + metadata: 'string', + normalized_metadata: { + name: 'Moralis Mug', + description: + 'Moralis Coffee nug 3D Asset that can be used in 3D worldspaces. This NFT is presented as a flat PNG, a Unity3D Prefab and a standard fbx.', + image: + 'https://arw2wxg84h6b.moralishost.com:2053/server/files/tNJatzsHirx4V2VAep6sc923OYGxvkpBeJttR7Ks/de504bbadadcbe30c86278342fcf2560_moralismug.png', + external_link: 'https://giphy.com/gifs/loop-recursion-ting-aaODAv1iuQdgI', + animation_url: 'https://giphy.com/gifs/food-design-donuts-o9ngTPVYW4qo8', + attributes: [ + { + trait_type: 'Eye Color', + value: 'hazel', + display_type: 'string', + max_value: 100, + trait_count: 7, + order: 1, + }, + ], + }, + amount: '1', + name: 'CryptoKitties', + symbol: 'RARI', + token_hash: '502cee781b0fb40ea02508b21d319ced', + last_token_uri_sync: '2021-02-24T00:47:26.647Z', + last_metadata_sync: '2021-02-24T00:47:26.647Z', + }, + ], + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTContractMetadata.ts b/packages/evmApi/integration/mocks/endpoints/getNFTContractMetadata.ts index 1bddfbeee2..a1e31ac1c9 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTContractMetadata.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTContractMetadata.ts @@ -1,30 +1,34 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockGetNFTContractMetadata = rest.get(`${EVM_API_ROOT}/nft/:address/metadata`, (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 === '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d') { - return res( - ctx.status(200), - ctx.json({ - token_address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', - name: 'BoredApeYachtClub', - symbol: 'BAYC', +export const mockGetNFTContractMetadata = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTContractMetadata', + url: `/nft/:address/metadata`, + getParams: (req) => ({ + address: req.params.address, + }), + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + token_address: '0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09', + name: 'KryptoKitties', + synced_at: 'string', + symbol: 'RARI', contract_type: 'ERC721', - synced_at: '2021-09-12T00:00:00.000Z', - }), - ); - } - - if (address === '0x4044044044044044044044044044044044044040') { - return res(ctx.status(404)); - } - - throw new Error('getNFTContractMetadata: Not supported scenario'); -}); + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTContractTransfers.ts b/packages/evmApi/integration/mocks/endpoints/getNFTContractTransfers.ts index 10a9484754..e3a307d4b1 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTContractTransfers.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTContractTransfers.ts @@ -1,28 +1,54 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const transfers: Record = { - '0x7de3085b3190b3a787822ee16f23be010f5f8686': 900, -}; - -export const mockGetNFTContractTransfers = rest.get(`${EVM_API_ROOT}/nft/:address/transfers`, (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)); - } - - const value = transfers[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetNFTContractTransfers = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTContractTransfers', + url: `/nft/:address/transfers`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + token_id: '15', + from_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + to_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + value: '1000000000000000', + amount: '1', + contract_type: 'ERC721', + block_number: '88256', + block_timestamp: '2021-06-04T16:00:15', + block_hash: 'string', + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_type: 'string', + transaction_index: 0, + log_index: 0, + operator: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }, + ], + block_exists: true, + index_complete: true, + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTLowestPrice.ts b/packages/evmApi/integration/mocks/endpoints/getNFTLowestPrice.ts index 311c32c8a5..7c8b8cc2d2 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTLowestPrice.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTLowestPrice.ts @@ -1,40 +1,39 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockGetNFTLowestPrice = rest.get(`${EVM_API_ROOT}/nft/:address/lowestprice`, (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 === '0x4044044044044044044044044044044044044040') { - return res(ctx.status(404)); - } - - if (address === '0x2000000000000000000404404404404404404404') { - return res(ctx.status(200), ctx.json(null)); - } - - if (address === '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D') { - return res( - ctx.status(200), - ctx.json({ - transaction_hash: '0x6abc903573c0a615797ff9b88564ea59de7bef48966e93d4d181ef42dbb86f18', - transaction_index: '310', - token_ids: ['127'], - seller_address: '0x1254958bd5073c6b238e516298f0c48f6f60a78e', - buyer_address: '0xcd2854b46c7e553d530eab029712ad205ba6ed02', - token_address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', - marketplace_address: '0x00000000006c3852cbef3e08e8df289169ede581', - price: '66990000000000000000', - block_timestamp: '2022-08-24T07:47:01.000Z', - block_number: '15401671', - block_hash: '0x69de52caa13ac1c165d6b408a47f7ed79cef12280d1d26099d9c0d2b63b52626', - }), - ); - } - - throw new Error('getNFTLowestPrice: Not supported scenario'); -}); +export const mockGetNFTLowestPrice = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTLowestPrice', + url: `/nft/:address/lowestprice`, + getParams: (req) => ({ + address: req.params.address, + }), + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_index: 'string', + token_ids: ['15', '54'], + seller_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + buyer_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + marketplace_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + price: '1000000000000000', + block_timestamp: '2021-06-04T16:00:15', + block_number: '13680123', + block_hash: '0x4a7c916ca4a970358b9df90051008f729685ff05e9724a9dddba32630c37cb96', + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTMetadata.ts b/packages/evmApi/integration/mocks/endpoints/getNFTMetadata.ts index e65d08b842..74e4e760de 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTMetadata.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTMetadata.ts @@ -1,46 +1,74 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockGetNFTMetadata = rest.get(`${EVM_API_ROOT}/nft/:address/:token_id`, (req, res, ctx) => { - const address = req.params.address as string; - const tokenId = req.params.token_id as string; - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - if ( - address === '0x2953399124f0cbb46d2cbacd8a89cf0599974963' && - tokenId === '113461209507512867518933452141320285231135646094834536306130710983923277496520' - ) { - return res( - ctx.status(200), - ctx.json({ - token_address: '0x2953399124f0cbb46d2cbacd8a89cf0599974963', - token_id: '113461209507512867518933452141320285231135646094834536306130710983923277496520', - transfer_index: [31480170, 38, 129, 0], - owner_of: '0x86d2b5f4af69458a22d69a7347b2133854933ba4', - block_number: '31480170', - block_number_minted: '27090571', - token_hash: '6a5c632686374a276c17510e45fa546f', +export const mockGetNFTMetadata = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTMetadata', + url: `/nft/:address/:tokenId`, + getParams: (req) => ({ + address: req.params.address, + tokenId: req.params.tokenId, + }), + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '15', + }, + response: { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + token_id: '15', + owner_of: '0x9c83ff0f1c8924da96cb2fcb7e093f78eb2e316b', + token_hash: '502cee781b0fb40ea02508b21d319ced', + block_number: '88256', + block_number_minted: '88256', + contract_type: 'ERC721', + token_uri: 'string', + metadata: 'string', + normalized_metadata: { + name: 'Moralis Mug', + description: + 'Moralis Coffee nug 3D Asset that can be used in 3D worldspaces. This NFT is presented as a flat PNG, a Unity3D Prefab and a standard fbx.', + image: + 'https://arw2wxg84h6b.moralishost.com:2053/server/files/tNJatzsHirx4V2VAep6sc923OYGxvkpBeJttR7Ks/de504bbadadcbe30c86278342fcf2560_moralismug.png', + external_link: 'https://giphy.com/gifs/loop-recursion-ting-aaODAv1iuQdgI', + animation_url: 'https://giphy.com/gifs/food-design-donuts-o9ngTPVYW4qo8', + attributes: [ + { + trait_type: 'Eye Color', + value: 'hazel', + display_type: 'string', + max_value: 100, + trait_count: 7, + order: 1, + }, + ], + }, + minter_address: '0x9c83ff0f1c8924da96cb2fcb7e093f78eb2e316b', + last_token_uri_sync: 'string', + last_metadata_sync: 'string', amount: '1', - contract_type: 'ERC1155', - name: 'OpenSea Collections', - symbol: 'OPENSTORE', - token_uri: - 'https://api.opensea.io/api/v2/metadata/matic/0x2953399124F0cBB46d2CbACD8A89cF0599974963/0xfad8c4d1b26f280ca7a3f3f05e0fba1954e69c930000000000001c00000000c8', - metadata: - '{"image":"https://lh3.googleusercontent.com/rp89xp0BIhvqaRPeSt-ONlBXyKb016HFAOr_f3HjkdQjBgcXmwZJXPafTlfft9qR6yKB7Ga7ycFtu2Oa4Aqder4_rBoKyMqL8b-e6Q","name":"Moralis Core Keycard","description":"The Core Team Keycard is used to identify a Moralis Core Team member.\\n\\nThe token is held proudly by Moralis developers, content creators, and supporting team members a like!\\n\\nKeep the hedgehog, push the flywheel together, be the movement: Moralis!","external_link":null,"animation_url":"https://openseauserdata.com/files/61af0e98bb91bee60234fcad25a9b343.mp4","traits":[]}', - last_token_uri_sync: null, - last_metadata_sync: null, - }), - ); - } - - if (address === '0x4044044044044044044044044044044044044040') { - return res(ctx.status(404)); - } - - throw new Error('getNFTMetadata: Not supported scenario'); -}); + name: 'CryptoKitties', + symbol: 'RARI', + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + tokenId: '15', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '000000215', + }, + responseStatus: 400, + response: createErrorResponse('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTOwners.ts b/packages/evmApi/integration/mocks/endpoints/getNFTOwners.ts index c037806afb..0f2a868b3d 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTOwners.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTOwners.ts @@ -1,59 +1,71 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const owner = { - token_address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', - token_id: '8152', - owner_of: '0x7ea7ccaecd27371be870b507e41058ddc5dea8f8', - block_number: '15458263', - block_number_minted: '12347191', - token_hash: 'c7218c4f0b780b7e7f17251ec12e94a2', - amount: '1', - contract_type: 'ERC721', - name: 'BoredApeYachtClub', - symbol: 'BAYC', - token_uri: 'https://ipfs.moralis.io:2053/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/8152', - metadata: - '{"image":"ipfs://Qma2RuGQTMuYhFVwn3D4ipdjmM37KUDwYFxFgLnnyddSx6","attributes":[{"trait_type":"Fur","value":"Blue"},{"trait_type":"Clothes","value":"Guayabera"},{"trait_type":"Background","value":"Army Green"},{"trait_type":"Mouth","value":"Bored"},{"trait_type":"Earring","value":"Silver Hoop"},{"trait_type":"Eyes","value":"Angry"}]}', - last_token_uri_sync: '2022-05-18T22:09:10.799Z', - last_metadata_sync: '2022-05-16T15:47:08.669Z', -}; - -export const mockGetNFTOwners = rest.get(`${EVM_API_ROOT}/nft/:address/owners`, (req, res, ctx) => { - const address = req.params.address as string; - const apiKey = req.headers.get('x-api-key'); - const cursor = req.url.searchParams.get('cursor'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - if (address === '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D') { - if (!cursor) { - return res( - ctx.status(200), - ctx.json({ - total: 150, - page: 1, - page_size: 100, - cursor: 'page_1', - result: Array(100).fill(owner), - }), - ); - } - if (cursor === 'page_1') { - return res( - ctx.status(200), - ctx.json({ - total: 150, - page: 2, - page_size: 100, - cursor: 'page_2', - result: Array(50).fill(owner), - }), - ); - } - } - - throw new Error('getNFTOwners: Not supported scenario'); -}); +export const mockGetNFTOwners = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTOwners', + url: `/nft/:address/owners`, + getParams: (req) => ({ + address: req.params.address, + }), + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + status: 'SYNCING', + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + token_id: '15', + contract_type: 'ERC721', + owner_of: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + block_number: '88256', + block_number_minted: '88256', + token_uri: 'string', + metadata: 'string', + normalized_metadata: { + name: 'Moralis Mug', + description: + 'Moralis Coffee nug 3D Asset that can be used in 3D worldspaces. This NFT is presented as a flat PNG, a Unity3D Prefab and a standard fbx.', + image: + 'https://arw2wxg84h6b.moralishost.com:2053/server/files/tNJatzsHirx4V2VAep6sc923OYGxvkpBeJttR7Ks/de504bbadadcbe30c86278342fcf2560_moralismug.png', + external_link: 'https://giphy.com/gifs/loop-recursion-ting-aaODAv1iuQdgI', + animation_url: 'https://giphy.com/gifs/food-design-donuts-o9ngTPVYW4qo8', + attributes: [ + { + trait_type: 'Eye Color', + value: 'hazel', + display_type: 'string', + max_value: 100, + trait_count: 7, + order: 1, + }, + ], + }, + amount: '1', + name: 'CryptoKitties', + symbol: 'RARI', + token_hash: '502cee781b0fb40ea02508b21d319ced', + last_token_uri_sync: '2021-02-24T00:47:26.647Z', + last_metadata_sync: '2021-02-24T00:47:26.647Z', + }, + ], + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTTokenIdOwners.ts b/packages/evmApi/integration/mocks/endpoints/getNFTTokenIdOwners.ts index 3dfd2a0d47..eaa0a046e3 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTTokenIdOwners.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTTokenIdOwners.ts @@ -1,28 +1,82 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const owners: Record = { - '0x7de3085b3190b3a787822ee16f23be010f5f8686': 10, -}; - -export const mockGetNFTTokenIdOwners = rest.get(`${EVM_API_ROOT}/nft/:address/:token_id/owners`, (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)); - } - - const value = owners[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetNFTTokenIdOwners = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTTokenIdOwners', + url: `/nft/:address/:tokenId/owners`, + getParams: (req) => ({ + address: req.params.address, + tokenId: req.params.tokenId, }), - ); -}); + }, + [ + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + tokenId: '15', + }, + response: { + status: 'SYNCING', + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + token_id: '15', + contract_type: 'ERC721', + owner_of: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + block_number: '88256', + block_number_minted: '88256', + token_uri: 'string', + metadata: 'string', + normalized_metadata: { + name: 'Moralis Mug', + description: + 'Moralis Coffee nug 3D Asset that can be used in 3D worldspaces. This NFT is presented as a flat PNG, a Unity3D Prefab and a standard fbx.', + image: + 'https://arw2wxg84h6b.moralishost.com:2053/server/files/tNJatzsHirx4V2VAep6sc923OYGxvkpBeJttR7Ks/de504bbadadcbe30c86278342fcf2560_moralismug.png', + external_link: 'https://giphy.com/gifs/loop-recursion-ting-aaODAv1iuQdgI', + animation_url: 'https://giphy.com/gifs/food-design-donuts-o9ngTPVYW4qo8', + attributes: [ + { + trait_type: 'Eye Color', + value: 'hazel', + display_type: 'string', + max_value: 100, + trait_count: 7, + order: 1, + }, + ], + }, + amount: '1', + name: 'CryptoKitties', + symbol: 'RARI', + token_hash: '502cee781b0fb40ea02508b21d319ced', + last_token_uri_sync: '2021-02-24T00:47:26.647Z', + last_metadata_sync: '2021-02-24T00:47:26.647Z', + }, + ], + }, + }, + { + condition: { + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88', + tokenId: '15', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + tokenId: '000000215', + }, + responseStatus: 400, + response: createErrorResponse('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTTrades.ts b/packages/evmApi/integration/mocks/endpoints/getNFTTrades.ts index 043912b229..cc96bc7dda 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTTrades.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTTrades.ts @@ -1,28 +1,46 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockGetNFTTradess: Record = { - '0x7de3085b3190b3a787822ee16f23be010f5f8686': 53, -}; - -export const mockGetNFTTrades = rest.get(`${EVM_API_ROOT}/nft/:address/trades`, (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)); - } - - const value = mockGetNFTTradess[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetNFTTrades = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTTrades', + url: `/nft/:address/trades`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + result: [ + { + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_index: 'string', + token_ids: ['15', '54'], + seller_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + buyer_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + marketplace_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + price: '1000000000000000', + block_timestamp: '2021-06-04T16:00:15', + block_number: '13680123', + block_hash: '0x4a7c916ca4a970358b9df90051008f729685ff05e9724a9dddba32630c37cb96', + }, + ], + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTTransfers.ts b/packages/evmApi/integration/mocks/endpoints/getNFTTransfers.ts index 19bfa5b666..21fd7b9130 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTTransfers.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTTransfers.ts @@ -1,29 +1,65 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const transfers: Record = { - '0x7de3085b3190b3a787822ee16f23be010f5f8686': 1, -}; - -export const mockGetNFTTransfers = rest.get(`${EVM_API_ROOT}/nft/:address/:token_id/transfers`, (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)); - } - - const value = transfers[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetNFTTransfers = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTTransfers', + url: `/nft/:address/:token_id/transfers`, + getParams: (req) => ({ + address: req.params.address, + token_id: req.params.token_id, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + token_id: '15', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + token_id: '15', + from_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + to_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + value: '1000000000000000', + amount: '1', + contract_type: 'ERC721', + block_number: '88256', + block_timestamp: '2021-06-04T16:00:15', + block_hash: 'string', + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_type: 'string', + transaction_index: 0, + log_index: 0, + operator: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }, + ], + block_exists: true, + index_complete: true, + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + token_id: '15', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + token_id: '000000215', + }, + responseStatus: 400, + response: createErrorResponse('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTTransfersByBlock.ts b/packages/evmApi/integration/mocks/endpoints/getNFTTransfersByBlock.ts index a5a0582d2d..ccd701aa9e 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTTransfersByBlock.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTTransfersByBlock.ts @@ -1,31 +1,54 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockGetNFTTransfersByBlocks: Record = { - '0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171': 3, -}; - -export const mockGetNFTTransfersByBlock = rest.get( - `${EVM_API_ROOT}/block/:blockNumberOrHash/nft/transfers`, - (req, res, ctx) => { - const blockNumberOrHash = req.params.blockNumberOrHash as string; - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - const value = mockGetNFTTransfersByBlocks[blockNumberOrHash]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, - }), - ); +export const mockGetNFTTransfersByBlock = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTTransfersByBlock', + url: `/block/:blockNumberOrHash/nft/transfers`, + getParams: (req) => ({ + blockNumberOrHash: req.params.blockNumberOrHash, + }), }, + [ + { + condition: { + blockNumberOrHash: '0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + token_id: '15', + from_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + to_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + value: '1000000000000000', + amount: '1', + contract_type: 'ERC721', + block_number: '88256', + block_timestamp: '2021-06-04T16:00:15', + block_hash: 'string', + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_type: 'string', + transaction_index: 0, + log_index: 0, + operator: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }, + ], + block_exists: true, + index_complete: true, + }, + }, + { + condition: { + blockNumberOrHash: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid block number or block hash provided'), + }, + ], ); diff --git a/packages/evmApi/integration/mocks/endpoints/getNFTTransfersFromToBlock.ts b/packages/evmApi/integration/mocks/endpoints/getNFTTransfersFromToBlock.ts index 9d08101e30..a999248203 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNFTTransfersFromToBlock.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNFTTransfersFromToBlock.ts @@ -1,28 +1,56 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const transfers: Record = { - '1': 118072744, -}; - -export const mockGetNFTTransfersFromToBlock = rest.get(`${EVM_API_ROOT}/nft/transfers`, (req, res, ctx) => { - const fromBlock = req.url.searchParams.get('from_block') as string; - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - const value = transfers[fromBlock]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetNFTTransfersFromToBlock = MockScenarios.create( + { + method: 'get', + name: 'mockGetNFTTransfersFromToBlock', + url: `/nft/transfers`, + getParams: (req) => ({ + fromBlock: req.url.searchParams.get('from_block'), + toBlock: req.url.searchParams.get('to_block'), }), - ); -}); + }, + [ + { + condition: { + fromBlock: '1', + toBlock: '2', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + token_id: '15', + from_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + to_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + value: '1000000000000000', + amount: '1', + contract_type: 'ERC721', + block_number: '88256', + block_timestamp: '2021-06-04T16:00:15', + block_hash: 'string', + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_type: 'string', + transaction_index: 0, + log_index: 0, + operator: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }, + ], + block_exists: true, + index_complete: true, + }, + }, + { + condition: { + fromBlock: '7', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid block number provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getNativeBalance.ts b/packages/evmApi/integration/mocks/endpoints/getNativeBalance.ts index 9a547d01f1..298d97565f 100644 --- a/packages/evmApi/integration/mocks/endpoints/getNativeBalance.ts +++ b/packages/evmApi/integration/mocks/endpoints/getNativeBalance.ts @@ -1,28 +1,21 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createBalanceResponse } from '../response/balanceResponse'; -export const mockGetNativeBalances: Record = { - '0x7dE3085b3190B3a787822Ee16F23be010f5F8686': '39600000000000000', -}; - -export const mockGetNativeBalance = rest.get(`${EVM_API_ROOT}/:address/balance`, (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)); - } - - const value = mockGetNativeBalances[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - balance: value, +export const mockGetNativeBalance = MockScenarios.create( + { + method: 'get', + name: 'mockGetNativeBalance', + url: '/:address/balance', + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x7dE3085b3190B3a787822Ee16F23be010f5F8686', + }, + response: createBalanceResponse('39600000000000000'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getPairAddress.ts b/packages/evmApi/integration/mocks/endpoints/getPairAddress.ts index 49c4ac1993..012f83f15b 100644 --- a/packages/evmApi/integration/mocks/endpoints/getPairAddress.ts +++ b/packages/evmApi/integration/mocks/endpoints/getPairAddress.ts @@ -4,8 +4,8 @@ export const mockGetPairAddress = MockScenarios.create( { name: 'mockGetPairAddress', getParams: (req) => ({ - token0_address: req.params['token0_address'], - token1_address: req.params['token1_address'], + token0_address: req.params.token0_address, + token1_address: req.params.token1_address, exchange: req.url.searchParams.get('exchange'), chain: req.url.searchParams.get('chain'), }), diff --git a/packages/evmApi/integration/mocks/endpoints/getPairReserves.ts b/packages/evmApi/integration/mocks/endpoints/getPairReserves.ts index 50754741f5..c73f266bf5 100644 --- a/packages/evmApi/integration/mocks/endpoints/getPairReserves.ts +++ b/packages/evmApi/integration/mocks/endpoints/getPairReserves.ts @@ -4,7 +4,7 @@ export const mockGetPairReserves = MockScenarios.create( { name: 'mockGetPairReserves', getParams: (req) => ({ - pair_address: req.params['pair_address'], + pair_address: req.params.pair_address, chain: req.url.searchParams.get('chain'), }), url: '/:pair_address/reserves', diff --git a/packages/evmApi/integration/mocks/endpoints/getTokenAllowance.ts b/packages/evmApi/integration/mocks/endpoints/getTokenAllowance.ts index 87611c4d0a..1894136f4b 100644 --- a/packages/evmApi/integration/mocks/endpoints/getTokenAllowance.ts +++ b/packages/evmApi/integration/mocks/endpoints/getTokenAllowance.ts @@ -1,18 +1,21 @@ -/* eslint-disable no-console */ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createAllowanceResponse } from '../response/allowanceResponse'; -export const mockGetTokenAllowance = rest.get(`${EVM_API_ROOT}/erc20/:address/allowance`, (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') { - return res(ctx.status(200), ctx.json({ allowance: '0' })); - } - - throw new Error('getTokenAllowance: Not supported scenario'); -}); +export const mockGetTokenAllowance = MockScenarios.create( + { + method: 'get', + name: 'mockGetTokenAllowance', + url: '/erc20/:address/allowance', + getParams: (req) => ({ + address: req.params.address, + }), + }, + [ + { + condition: { + address: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', + }, + response: createAllowanceResponse('0'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getTokenMetadata.ts b/packages/evmApi/integration/mocks/endpoints/getTokenMetadata.ts index cba53b0305..da25b3c8a3 100644 --- a/packages/evmApi/integration/mocks/endpoints/getTokenMetadata.ts +++ b/packages/evmApi/integration/mocks/endpoints/getTokenMetadata.ts @@ -1,33 +1,34 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createTokenMetadataResponse } from '../response/tokenMetadataResponse'; -export const mockGetTokenMetadata = rest.get(`${EVM_API_ROOT}/erc20/metadata`, (req, res, ctx) => { - const addresses = req.url.searchParams.get('addresses[]') as string; - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - if (addresses === '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce') { - return res( - ctx.status(200), - ctx.json([ - { - address: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', - name: 'SHIBA INU', - symbol: 'SHIB', - decimals: '18', - logo: 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce.png', - logo_hash: '0dba9c0d492b42b3a73c5ceee62b205568a8b5c1932cac048ccd71cbbe051690', - thumbnail: 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce_thumb.png', - block_number: null, - validated: null, - created_at: '2022-01-20T10:39:55.818Z', - }, - ]), - ); - } - - throw new Error('getTokenMetadata: Not supported scenario'); -}); +export const mockGetTokenMetadata = MockScenarios.create( + { + method: 'get', + name: 'mockGetTokenMetadata', + url: '/erc20/metadata', + getParams: (req) => ({ + addresses: req.url.searchParams.get('addresses[]'), + }), + }, + [ + { + condition: { + addresses: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', + }, + response: [ + createTokenMetadataResponse( + '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', + 'SHIBA INU', + 'SHIB', + '18', + 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce.png', + '0dba9c0d492b42b3a73c5ceee62b205568a8b5c1932cac048ccd71cbbe051690', + 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce_thumb.png', + null, + null, + '2022-01-20T10:39:55.818Z', + ), + ], + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getTokenMetadataBySymbol.ts b/packages/evmApi/integration/mocks/endpoints/getTokenMetadataBySymbol.ts index c65d0e961b..d37cfd27fb 100644 --- a/packages/evmApi/integration/mocks/endpoints/getTokenMetadataBySymbol.ts +++ b/packages/evmApi/integration/mocks/endpoints/getTokenMetadataBySymbol.ts @@ -1,33 +1,34 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createTokenMetadataResponse } from '../response/tokenMetadataResponse'; -export const mockGetTokenMetadataBySymbol = rest.get(`${EVM_API_ROOT}/erc20/metadata/symbols`, (req, res, ctx) => { - const symbols = req.url.searchParams.get('symbols[]') as string; - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - if (symbols === 'SHIBA INU') { - return res( - ctx.status(200), - ctx.json([ - { - address: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', - name: 'SHIBA INU', - symbol: 'SHIB', - decimals: '18', - logo: 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce.png', - logo_hash: '0dba9c0d492b42b3a73c5ceee62b205568a8b5c1932cac048ccd71cbbe051690', - thumbnail: 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce_thumb.png', - block_number: null, - validated: null, - created_at: '2022-01-20T10:39:55.818Z', - }, - ]), - ); - } - - throw new Error('getTokenMetadataBySymbol: Not supported scenario'); -}); +export const mockGetTokenMetadataBySymbols = MockScenarios.create( + { + method: 'get', + name: 'mockGetTokenMetadataBySymbol', + url: '/erc20/metadata/symbols', + getParams: (req) => ({ + symbols: req.url.searchParams.get('symbols[]'), + }), + }, + [ + { + condition: { + symbols: 'SHIBA INU', + }, + response: [ + createTokenMetadataResponse( + '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', + 'SHIBA INU', + 'SHIB', + '18', + 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce.png', + '0dba9c0d492b42b3a73c5ceee62b205568a8b5c1932cac048ccd71cbbe051690', + 'https://cdn.moralis.io/eth/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce_thumb.png', + null, + null, + '2022-01-20T10:39:55.818Z', + ), + ], + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getTokenPrice.ts b/packages/evmApi/integration/mocks/endpoints/getTokenPrice.ts index 4b45f47084..4d71fabe4c 100644 --- a/packages/evmApi/integration/mocks/endpoints/getTokenPrice.ts +++ b/packages/evmApi/integration/mocks/endpoints/getTokenPrice.ts @@ -1,58 +1,39 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createTokenPriceResponse } from '../response/tokenPriceResponse'; -function createResponse(exchangeAddress: string) { - return { - nativePrice: { value: '8244913831', decimals: 18, name: 'Ether', symbol: 'ETH' }, - usdPrice: 0.000011961341215674, - exchangeAddress, - exchangeName: 'Uniswap v3', - }; -} - -const scenarios = [ +export const mockGetTokenPrice = MockScenarios.create( { - url: { - address: '0x0000000000000000000000000000000000000001', - chain: '0x1', - toBlock: null, - }, - response: createResponse('0x0000000000000000000000000000000000000001'), + method: 'get', + name: 'mockGetTokenPrice', + url: '/erc20/:address/price', + getParams: (req) => ({ + address: req.params.address, + chain: req.url.searchParams.get('chain'), + toBlock: req.url.searchParams.get('to_block'), + }), }, - { - url: { - address: '0x0000000000000000000000000000000000000002', - chain: '0x13881', - toBlock: null, + [ + { + condition: { + address: '0x0000000000000000000000000000000000000001', + chain: '0x1', + }, + response: createTokenPriceResponse('0x0000000000000000000000000000000000000001'), }, - response: createResponse('0x0000000000000000000000000000000000000002'), - }, - { - url: { - address: '0x0000000000000000000000000000000000000003', - chain: '0x1', - toBlock: '512', + { + condition: { + address: '0x0000000000000000000000000000000000000002', + chain: '0x13881', + }, + response: createTokenPriceResponse('0x0000000000000000000000000000000000000002'), }, - response: createResponse('0x0000000000000000000000000000000000000003'), - }, -]; - -export const mockGetTokenPrice = rest.get(`${EVM_API_ROOT}/erc20/:address/price`, (req, res, ctx) => { - const apiKey = req.headers.get('x-api-key'); - const address = req.params.address as string; - const chain = req.url.searchParams.get('chain') as string | null; - const toBlock = req.url.searchParams.get('to_block') as string | null; - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - const scenario = scenarios.find( - (s) => s.url.address === address && s.url.chain === chain && s.url.toBlock === toBlock, - ); - if (scenario) { - return res(ctx.status(200), ctx.json(scenario.response)); - } - - throw new Error(`getTokenPrice: Not supported scenario (chain=${chain}, toBlock=${toBlock})`); -}); + { + condition: { + address: '0x0000000000000000000000000000000000000003', + chain: '0x1', + toBlock: '512', + }, + response: createTokenPriceResponse('0x0000000000000000000000000000000000000003'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getTokenTransfers.ts b/packages/evmApi/integration/mocks/endpoints/getTokenTransfers.ts index 7a4b6e170c..7999675954 100644 --- a/packages/evmApi/integration/mocks/endpoints/getTokenTransfers.ts +++ b/packages/evmApi/integration/mocks/endpoints/getTokenTransfers.ts @@ -1,28 +1,88 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createPaginatedResponse } from '../response/paginatedResponse'; +import { createTransferResponse } from '../response/transferResponse'; -const transfers: Record = { - '0xa2107fa5b38d9bbd2c461d6edf11b11a50f6b974': 807091, -}; - -export const mockGetTokenTransfers = rest.get(`${EVM_API_ROOT}/erc20/:address/transfers`, (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)); - } - - const value = transfers[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetTokenTransfers = MockScenarios.create( + { + method: 'get', + name: 'mockGetTokenAddressTransfers', + url: '/erc20/:address/transfers', + getParams: (req) => ({ + address: req.params.address, + limit: req.url.searchParams.get('limit'), }), - ); -}); + }, + [ + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + }, + response: createPaginatedResponse( + Array(12).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', + '347995260860000000000', + ), + ), + 12, + 0, + 100, + null, + ), + }, + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + limit: '6', + }, + response: createPaginatedResponse( + Array(6).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', + '347995260860000000000', + ), + ), + 12, + 0, + 6, + 'limit_6_page_0', + ), + }, + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + limit: '6', + }, + response: createPaginatedResponse( + Array(6).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', + '347995260860000000000', + ), + ), + 12, + 1, + 6, + null, + ), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getWalletNFTCollections.ts b/packages/evmApi/integration/mocks/endpoints/getWalletNFTCollections.ts index 276f36f1ba..875449e2a4 100644 --- a/packages/evmApi/integration/mocks/endpoints/getWalletNFTCollections.ts +++ b/packages/evmApi/integration/mocks/endpoints/getWalletNFTCollections.ts @@ -1,37 +1,42 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const collections: Record = { - '0x3514980793dceae1b34d0144e3ae725bee084a70': { - total: 1, - page_size: 100, - page: 1, - status: 'SYNCED', - cursor: null, - result: [ - { - token_address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', - contract_type: 'ERC721', - name: 'Test NFT', - symbol: 'TEST', - }, - ], +export const mockGetWalletNFTCollections = MockScenarios.create( + { + method: 'get', + name: 'mockGetWalletNFTCollections', + url: `/:address/nft/collections`, + getParams: (req) => ({ + address: req.params.address, + }), }, -}; - -export const mockGetWalletNFTCollections = rest.get(`${EVM_API_ROOT}/:address/nft/collections`, (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)); - } - - const value = collections[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res(ctx.status(200), ctx.json(value)); -}); + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + status: 'SYNCING', + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + contract_type: 'ERC721', + name: 'CryptoKitties', + symbol: 'RARI', + }, + ], + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getWalletNFTTransfers.ts b/packages/evmApi/integration/mocks/endpoints/getWalletNFTTransfers.ts index 172d858420..deaffeb33c 100644 --- a/packages/evmApi/integration/mocks/endpoints/getWalletNFTTransfers.ts +++ b/packages/evmApi/integration/mocks/endpoints/getWalletNFTTransfers.ts @@ -1,28 +1,54 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const transfers: Record = { - '0x75e3e9c92162e62000425c98769965a76c2e387a': 126, -}; - -export const mockGetWalletNFTTransfers = rest.get(`${EVM_API_ROOT}/:address/nft/transfers`, (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)); - } - - const value = transfers[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetWalletNFTTransfers = MockScenarios.create( + { + method: 'get', + name: 'mockGetWalletNFTTransfers', + url: `/:address/nft/transfers`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + token_id: '15', + from_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + to_address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + value: '1000000000000000', + amount: '1', + contract_type: 'ERC721', + block_number: '88256', + block_timestamp: '2021-06-04T16:00:15', + block_hash: 'string', + transaction_hash: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + transaction_type: 'string', + transaction_index: 0, + log_index: 0, + operator: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }, + ], + block_exists: true, + index_complete: true, + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getWalletNFTs.ts b/packages/evmApi/integration/mocks/endpoints/getWalletNFTs.ts index e938337a48..73440d3ad4 100644 --- a/packages/evmApi/integration/mocks/endpoints/getWalletNFTs.ts +++ b/packages/evmApi/integration/mocks/endpoints/getWalletNFTs.ts @@ -1,28 +1,71 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -const nfts: Record = { - '0x75e3e9c92162e62000425c98769965a76c2e387a': 112, -}; - -export const mockGetWalletNFTs = rest.get(`${EVM_API_ROOT}/:address/nft`, (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)); - } - - const value = nfts[address]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockGetWalletNFTs = MockScenarios.create( + { + method: 'get', + name: 'mockGetWalletNFTs', + url: `/:address/nft`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }, + response: { + status: 'SYNCING', + total: 2000, + page: 2, + page_size: 100, + cursor: 'string', + result: [ + { + token_address: '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + token_id: '15', + contract_type: 'ERC721', + owner_of: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + block_number: '88256', + block_number_minted: '88256', + token_uri: 'string', + metadata: 'string', + normalized_metadata: { + name: 'Moralis Mug', + description: + 'Moralis Coffee nug 3D Asset that can be used in 3D worldspaces. This NFT is presented as a flat PNG, a Unity3D Prefab and a standard fbx.', + image: + 'https://arw2wxg84h6b.moralishost.com:2053/server/files/tNJatzsHirx4V2VAep6sc923OYGxvkpBeJttR7Ks/de504bbadadcbe30c86278342fcf2560_moralismug.png', + external_link: 'https://giphy.com/gifs/loop-recursion-ting-aaODAv1iuQdgI', + animation_url: 'https://giphy.com/gifs/food-design-donuts-o9ngTPVYW4qo8', + attributes: [ + { + trait_type: 'Eye Color', + value: 'hazel', + display_type: 'string', + max_value: 100, + trait_count: 7, + order: 1, + }, + ], + }, + amount: '1', + name: 'CryptoKitties', + symbol: 'RARI', + token_hash: '502cee781b0fb40ea02508b21d319ced', + last_token_uri_sync: '2021-02-24T00:47:26.647Z', + last_metadata_sync: '2021-02-24T00:47:26.647Z', + }, + ], + }, + }, + { + condition: { + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getWalletTokenBalances.ts b/packages/evmApi/integration/mocks/endpoints/getWalletTokenBalances.ts new file mode 100644 index 0000000000..4e3ab0083d --- /dev/null +++ b/packages/evmApi/integration/mocks/endpoints/getWalletTokenBalances.ts @@ -0,0 +1,40 @@ +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createTokenBalanceResponse } from '../response/tokenBalanceResponse'; + +export const mockGetWalletTokenBalances = MockScenarios.create( + { + method: 'get', + name: 'mockGetWalletTokenBalances', + url: '/:address/erc20', + getParams: (req) => ({ + address: req.params.address, + }), + }, + [ + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + }, + response: [ + createTokenBalanceResponse( + '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', + 'Wrapped Ether', + 'WETH', + 'https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png', + 'https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.png', + 18, + '795917396650797993089', + ), + createTokenBalanceResponse( + '0x514910771af9ca656af840dff83e8264ecf986ca', + 'Chain Link', + 'LINK', + 'https://cdn.moralis.io/eth/0x514910771af9ca656af840dff83e8264ecf986ca.png', + 'https://cdn.moralis.io/eth/0x514910771af9ca656af840dff83e8264ecf986ca_thumb.png', + 18, + '155304334804334409393921', + ), + ], + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/getWalletTokenTransfers.ts b/packages/evmApi/integration/mocks/endpoints/getWalletTokenTransfers.ts index 4e0a35c159..a4429fc44e 100644 --- a/packages/evmApi/integration/mocks/endpoints/getWalletTokenTransfers.ts +++ b/packages/evmApi/integration/mocks/endpoints/getWalletTokenTransfers.ts @@ -1,66 +1,90 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createPaginatedResponse } from '../response/paginatedResponse'; +import { createTransferResponse } from '../response/transferResponse'; -const transfer = { - transaction_hash: '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', - address: '0x4fabb145d64652a948d72533023f6e7a623c7c53', - block_timestamp: '2022-09-01T17:18:14.000Z', - block_number: '15454126', - block_hash: '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', - from_address: '0xd73a9eadfff6a332afda7ddbb18cff84bbf6dd0d', - to_address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', - value: '347995260860000000000', -}; - -export const mockGetWalletTokenTransfers = rest.get(`${EVM_API_ROOT}/:address/erc20/transfers`, (req, res, ctx) => { - const address = req.params.address as string; - const limit = req.url.searchParams.get('limit'); - const cursor = req.url.searchParams.get('cursor'); - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - if (address === '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f') { - if (!limit) { - return res( - ctx.status(200), - ctx.json({ - total: 12, - page: 0, - page_size: 100, - cursor: null, - result: Array(12).fill(transfer), - }), - ); - } - - if (limit === '6' && !cursor) { - return res( - ctx.status(200), - ctx.json({ - total: 12, - page: 0, - page_size: 6, - cursor: 'limit_6_page_0', - result: Array(6).fill(transfer), - }), - ); - } - if (limit === '6' && cursor === 'limit_6_page_0') { - return res( - ctx.status(200), - ctx.json({ - total: 12, - page: 1, - page_size: 6, - cursor: null, - result: Array(6).fill(transfer), - }), - ); - } - } - - throw new Error('getWalletTokenTransfers: Not supported scenario'); -}); +export const mockGetWalletTokenTransfers = MockScenarios.create( + { + method: 'get', + name: 'mockGetWalletTokenTransfers', + url: '/:address/erc20/transfers', + getParams: (req) => ({ + address: req.params.address, + limit: req.url.searchParams.get('limit'), + cursor: req.url.searchParams.get('cursor'), + }), + }, + [ + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + }, + response: createPaginatedResponse( + Array(12).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + '347995260860000000000', + ), + ), + 12, + 0, + 100, + null, + ), + }, + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + limit: '6', + }, + response: createPaginatedResponse( + Array(6).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + '347995260860000000000', + ), + ), + 12, + 0, + 6, + 'limit_6_page_0', + ), + }, + { + condition: { + address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + limit: '6', + cursor: 'limit_6_page_0', + }, + response: createPaginatedResponse( + Array(6).fill( + createTransferResponse( + '0x215f6d1cfe1cb78733f8a8ddff2e7b8c375ce9ce41dd7ec2e73b2404e59dd04d', + '0x4Fabb145d64652a948d72533023f6E7A623C7C53', + '2022-09-01T17:18:14.000Z', + '15454126', + '0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05', + '0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D', + '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + '347995260860000000000', + ), + ), + 12, + 1, + 6, + null, + ), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/resyncMetadata.ts b/packages/evmApi/integration/mocks/endpoints/resyncMetadata.ts new file mode 100644 index 0000000000..032bb0baa7 --- /dev/null +++ b/packages/evmApi/integration/mocks/endpoints/resyncMetadata.ts @@ -0,0 +1,41 @@ +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; + +export const mockResyncMetadata = MockScenarios.create( + { + method: 'get', + name: 'mockResyncMetadata', + url: `/nft/:address/:tokenId/metadata/resync`, + getParams: (req) => ({ + address: req.params.address, + tokenId: req.params.tokenId, + }), + }, + [ + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + tokenId: '15', + }, + response: { + status: 'Resync request executed', + }, + }, + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88', + tokenId: '15', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + tokenId: '000000215', + }, + responseStatus: 400, + response: createErrorResponse('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/searchNFTs.ts b/packages/evmApi/integration/mocks/endpoints/searchNFTs.ts index 7444fa675d..4069df6397 100644 --- a/packages/evmApi/integration/mocks/endpoints/searchNFTs.ts +++ b/packages/evmApi/integration/mocks/endpoints/searchNFTs.ts @@ -1,29 +1,63 @@ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockSearchNFTss: Record = { - Pancake: 5671, -}; - -export const mockSearchNFTs = rest.get(`${EVM_API_ROOT}/nft/search`, (req, res, ctx) => { - const q = req.url.searchParams.get('q') as string; - - const apiKey = req.headers.get('x-api-key'); - - if (apiKey !== MOCK_API_KEY) { - return res(ctx.status(401)); - } - - const value = mockSearchNFTss[q]; - - if (!value) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - total: value, +export const mockSearchNFTs = MockScenarios.create( + { + method: 'get', + name: 'mockSearchNFTs', + url: `/nft/search`, + getParams: (req) => ({ + q: req.url.searchParams.get('q'), }), - ); -}); + }, + [ + { + condition: { + q: '889', + }, + response: { + total: 2000, + page: 2, + page_size: 100, + result: [ + { + token_id: '889', + token_address: '0x8ce66ff0865570d1ff0bb0098fa41b4dc61e02e6', + token_uri: 'https://ipfs.moralis.io:2053/ipfs/QmZZbo8u8zEWg7wtmZhJS2W718WL6FA95T4XdgmCcLp1SJ/889.json', + metadata: + '{"name":"Bape #889","description":"The #1 metavestor clan (NFT/DAO) by a team with multi billion dollar company experience.","image":"https://bapesclan.mypinata.cloud/ipfs/QmTSUD5JA6qHaC5t25mcXySfz19AV9u4Mb6Na7ntQ6tEwf/889.jpg","attributes":[{"trait_type":"Background","value":"Black"},{"trait_type":"Body","value":"Man"},{"trait_type":"Dress","value":"Suit Tie Blue"},{"trait_type":"Face","value":"Pipe"},{"trait_type":"Eye","value":"Eye"}]}', + is_valid: 1, + syncing: 2, + frozen: 0, + resyncing: 0, + contract_type: 'ERC721', + token_hash: 'fffa3102469ce77f569893d16d5884f9', + batch_id: 'fd995c8a-f8b2-40cb-a407-f43e552638b4', + metadata_name: 'Bape #889', + metadata_description: + 'The #1 metavestor clan (NFT/DAO) by a team with multi billion dollar company experience.', + metadata_attributes: + '[{"trait_type":"Background","value":"Black"},{"trait_type":"Body","value":"Man"},{"trait_type":"Dress","value":"Suit Tie Blue"},{"trait_type":"Face","value":"Pipe"},{"trait_type":"Eye","value":"Eye"}]', + block_number_minted: '14265936', + opensea_lookup: null, + minter_address: '0xdcf086e3f7954b38180daae1405569da86588bfe', + transaction_minted: '0x2c8d7ec7a8439b0f67b50e93be63242de52e9b5cdfc7dc0aee80c6a2f104c41a', + frozen_log_index: null, + imported: null, + last_token_uri_sync: '2021-02-24T00:47:26.647Z', + last_metadata_sync: '2021-02-24T00:47:26.647Z', + createdAt: '2022-02-24T00:47:26.647Z', + updatedAt: '2022-04-09T23:56:44.807Z', + }, + ], + }, + }, + { + condition: { + q: 'Pancake', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid search parameter provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/endpoints/syncNFTContract.ts b/packages/evmApi/integration/mocks/endpoints/syncNFTContract.ts index b456560990..36ce4dbb49 100644 --- a/packages/evmApi/integration/mocks/endpoints/syncNFTContract.ts +++ b/packages/evmApi/integration/mocks/endpoints/syncNFTContract.ts @@ -1,25 +1,29 @@ -/* eslint-disable no-console */ -import { rest } from 'msw'; -import { EVM_API_ROOT, MOCK_API_KEY } from '../config'; +import { MockScenarios } from '@moralisweb3/test-utils'; +import { createErrorResponse } from '../response/errorResponse'; -export const mockSyncNFTContractAddresses = ['0x7de3085b3190b3a787822ee16f23be010f5f8686']; - -export const mockSyncNFTContract = rest.put(`${EVM_API_ROOT}/nft/:address/sync`, (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 (!mockSyncNFTContractAddresses.includes(address)) { - return res(ctx.status(404)); - } - - return res( - ctx.status(200), - ctx.json({ - success: true, +export const mockSyncNFTContract = MockScenarios.create( + { + method: 'put', + name: 'mockSyncNFTContract', + url: `/nft/:address/sync`, + getParams: (req) => ({ + address: req.params.address, }), - ); -}); + }, + [ + { + condition: { + address: '0x057ec652a4f150f7ff94f089a38008f49a0df88e', + }, + responseStatus: 200, + response: { success: true }, + }, + { + condition: { + address: '0x7de308', + }, + responseStatus: 400, + response: createErrorResponse('[C0005] Invalid address provided'), + }, + ], +); diff --git a/packages/evmApi/integration/mocks/mockServer.ts b/packages/evmApi/integration/mocks/mockServer.ts index 7b17902b20..0f6ce71003 100644 --- a/packages/evmApi/integration/mocks/mockServer.ts +++ b/packages/evmApi/integration/mocks/mockServer.ts @@ -1,51 +1,73 @@ import { MockServer } from '@moralisweb3/test-utils'; -import { setupServer } from 'msw/node'; import { mockEndpointWeights } from './endpoints/endpointWeights'; import { mockGetBlock } from './endpoints/getBlock'; import { mockGetContractEvents } from './endpoints/getContractEvents'; -import { mockGetContractNFTs } from './endpoints/getContractNFTs'; import { mockGetDateToBlock } from './endpoints/getDateToBlock'; import { mockGetContractLogs } from './endpoints/getContractLogs'; import { mockGetNativeBalance } from './endpoints/getNativeBalance'; -import { mockGetNFTContractMetadata } from './endpoints/getNFTContractMetadata'; -import { mockGetNFTContractTransfers } from './endpoints/getNFTContractTransfers'; -import { mockGetNFTLowestPrice } from './endpoints/getNFTLowestPrice'; -import { mockGetNFTMetadata } from './endpoints/getNFTMetadata'; -import { mockGetNFTOwners } from './endpoints/getNFTOwners'; -import { mockGetNFTTokenIdOwners } from './endpoints/getNFTTokenIdOwners'; -import { mockGetNFTTrades } from './endpoints/getNFTTrades'; -import { mockGetNFTTransfers } from './endpoints/getNFTTransfers'; -import { mockGetNFTTransfersByBlock } from './endpoints/getNFTTransfersByBlock'; -import { mockGetNFTTransfersFromToBlock } from './endpoints/getNFTTransfersFromToBlock'; import { mockGetPairAddress } from './endpoints/getPairAddress'; import { mockGetPairReserves } from './endpoints/getPairReserves'; import { mockGetTokenAllowance } from './endpoints/getTokenAllowance'; import { mockGetTokenMetadata } from './endpoints/getTokenMetadata'; -import { mockGetTokenMetadataBySymbol } from './endpoints/getTokenMetadataBySymbol'; +import { mockGetTokenMetadataBySymbols } from './endpoints/getTokenMetadataBySymbol'; import { mockGetTokenPrice } from './endpoints/getTokenPrice'; import { mockGetTokenTransfers } from './endpoints/getTokenTransfers'; import { mockGetTransaction } from './endpoints/getTransaction'; import { mockGetWalletNFTCollections } from './endpoints/getWalletNFTCollections'; import { mockGetWalletNFTs } from './endpoints/getWalletNFTs'; import { mockGetWalletNFTTransfers } from './endpoints/getWalletNFTTransfers'; +import { mockGetWalletTokenBalances } from './endpoints/getWalletTokenBalances'; import { mockGetWalletTokenTransfers } from './endpoints/getWalletTokenTransfers'; import { mockGetWalletTransactions } from './endpoints/getWalletTransactions'; import { mockResolveAddress } from './endpoints/resolveAddress'; import { mockResolveDomain } from './endpoints/resolveDomain'; import { mockRunContractFunction } from './endpoints/runContractFunction'; -import { mockSearchNFTs } from './endpoints/searchNFTs'; -import { mockSyncNFTContract } from './endpoints/syncNFTContract'; import { mockUploadFolder } from './endpoints/uploadFolder'; import { mockWeb3ApiVersion } from './endpoints/web3ApiVersion'; import { EVM_API_ROOT, MOCK_API_KEY } from './config'; +import { mockGetContractNFTs } from './endpoints/getContractNFTs'; +import { mockGetNFTContractMetadata } from './endpoints/getNFTContractMetadata'; +import { mockGetNFTContractTransfers } from './endpoints/getNFTContractTransfers'; +import { mockGetNFTMetadata } from './endpoints/getNFTMetadata'; +import { mockGetNFTOwners } from './endpoints/getNFTOwners'; +import { mockGetNFTTokenIdOwners } from './endpoints/getNFTTokenIdOwners'; +import { mockGetNFTTrades } from './endpoints/getNFTTrades'; +import { mockGetNFTTransfers } from './endpoints/getNFTTransfers'; +import { mockGetNFTLowestPrice } from './endpoints/getNFTLowestPrice'; +import { mockGetNFTTransfersByBlock } from './endpoints/getNFTTransfersByBlock'; +import { mockGetNFTTransfersFromToBlock } from './endpoints/getNFTTransfersFromToBlock'; +import { mockResyncMetadata } from './endpoints/resyncMetadata'; +import { mockSearchNFTs } from './endpoints/searchNFTs'; +import { mockSyncNFTContract } from './endpoints/syncNFTContract'; -export const handlers = [ +const handler = [ + mockGetDateToBlock, + mockGetNFTContractTransfers, + mockGetTransaction, + mockGetWalletTransactions, + mockGetContractEvents, + mockGetContractLogs, + mockGetBlock, + mockUploadFolder, + mockGetNativeBalance, + mockGetTokenAllowance, + mockGetTokenPrice, + mockGetTokenMetadata, + mockGetTokenMetadataBySymbols, + mockGetTokenTransfers, + mockGetWalletTokenBalances, + mockGetWalletTokenTransfers, + mockResolveAddress, + mockResolveDomain, + mockGetPairAddress, + mockGetPairReserves, + mockRunContractFunction, + mockWeb3ApiVersion, + mockEndpointWeights, mockGetNFTTransfersFromToBlock, mockSearchNFTs, mockGetContractNFTs, - mockGetNativeBalance, mockGetNFTContractMetadata, - mockGetNFTContractTransfers, mockGetNFTLowestPrice, mockGetNFTOwners, mockGetNFTTrades, @@ -53,34 +75,11 @@ export const handlers = [ mockGetNFTTokenIdOwners, mockGetNFTTransfers, mockGetNFTTransfersByBlock, - mockGetTokenAllowance, - mockGetTokenMetadata, - mockGetTokenMetadataBySymbol, - mockGetTokenPrice, - mockGetTokenTransfers, mockGetWalletNFTs, mockGetWalletNFTTransfers, - mockGetWalletTokenTransfers, + mockResyncMetadata, mockSyncNFTContract, mockGetWalletNFTCollections, ]; -const handler2 = [ - mockGetDateToBlock, - mockGetTransaction, - mockGetWalletTransactions, - mockGetContractEvents, - mockGetContractLogs, - mockGetBlock, - mockUploadFolder, - mockResolveAddress, - mockResolveDomain, - mockGetPairAddress, - mockGetPairReserves, - mockRunContractFunction, - mockWeb3ApiVersion, - mockEndpointWeights, -]; - -export const mockServer = setupServer(...handlers); -export const mockServer2 = MockServer.create({ apiKey: MOCK_API_KEY, apiRoot: EVM_API_ROOT }, handler2).start(); +export const mockServer = MockServer.create({ apiKey: MOCK_API_KEY, apiRoot: EVM_API_ROOT }, handler).start(); diff --git a/packages/evmApi/integration/mocks/response/allowanceResponse.ts b/packages/evmApi/integration/mocks/response/allowanceResponse.ts new file mode 100644 index 0000000000..ed21b3c2bd --- /dev/null +++ b/packages/evmApi/integration/mocks/response/allowanceResponse.ts @@ -0,0 +1,3 @@ +export const createAllowanceResponse = (allowance: string) => ({ + allowance, +}); diff --git a/packages/evmApi/integration/mocks/response/balanceResponse.ts b/packages/evmApi/integration/mocks/response/balanceResponse.ts new file mode 100644 index 0000000000..77faa094c7 --- /dev/null +++ b/packages/evmApi/integration/mocks/response/balanceResponse.ts @@ -0,0 +1,3 @@ +export const createBalanceResponse = (balance: string) => ({ + balance, +}); diff --git a/packages/evmApi/integration/mocks/response/paginatedResponse.ts b/packages/evmApi/integration/mocks/response/paginatedResponse.ts new file mode 100644 index 0000000000..9198f9d538 --- /dev/null +++ b/packages/evmApi/integration/mocks/response/paginatedResponse.ts @@ -0,0 +1,13 @@ +export const createPaginatedResponse = ( + result: any[], + total: number, + page: number, + pageSize: number, + cursor?: string | null, +) => ({ + result, + total, + page, + page_size: pageSize, + cursor, +}); diff --git a/packages/evmApi/integration/mocks/response/tokenBalanceResponse.ts b/packages/evmApi/integration/mocks/response/tokenBalanceResponse.ts new file mode 100644 index 0000000000..f6716f52e9 --- /dev/null +++ b/packages/evmApi/integration/mocks/response/tokenBalanceResponse.ts @@ -0,0 +1,17 @@ +export const createTokenBalanceResponse = ( + tokenAddress: string, + name: string, + symbol: string, + logo: string, + thumbnail: string, + decimals: number, + balance: string, +) => ({ + token_address: tokenAddress, + name, + symbol, + logo, + thumbnail, + decimals, + balance, +}); diff --git a/packages/evmApi/integration/mocks/response/tokenMetadataResponse.ts b/packages/evmApi/integration/mocks/response/tokenMetadataResponse.ts new file mode 100644 index 0000000000..e62f8612af --- /dev/null +++ b/packages/evmApi/integration/mocks/response/tokenMetadataResponse.ts @@ -0,0 +1,23 @@ +export const createTokenMetadataResponse = ( + address: string, + name: string, + symbol: string, + decimals: string, + logo: string, + logoHash: string, + thumbnail: string, + blockNumber: string | null, + validated: string | null, + createdAt: string, +) => ({ + address, + name, + symbol, + decimals, + logo, + logo_hash: logoHash, + thumbnail, + block_number: blockNumber, + validated, + created_at: createdAt, +}); diff --git a/packages/evmApi/integration/mocks/response/tokenPriceResponse.ts b/packages/evmApi/integration/mocks/response/tokenPriceResponse.ts new file mode 100644 index 0000000000..8ee3564321 --- /dev/null +++ b/packages/evmApi/integration/mocks/response/tokenPriceResponse.ts @@ -0,0 +1,8 @@ +export const createTokenPriceResponse = (exchangeAddress: string) => { + return { + nativePrice: { value: '8244913831', decimals: 18, name: 'Ether', symbol: 'ETH' }, + usdPrice: 0.000011961341215674, + exchangeAddress, + exchangeName: 'Uniswap v3', + }; +}; diff --git a/packages/evmApi/integration/mocks/response/transferResponse.ts b/packages/evmApi/integration/mocks/response/transferResponse.ts new file mode 100644 index 0000000000..fa15b59c30 --- /dev/null +++ b/packages/evmApi/integration/mocks/response/transferResponse.ts @@ -0,0 +1,19 @@ +export const createTransferResponse = ( + transaction_hash: string, + address: string, + blockTimestamp: string, + blockNumber: string, + blockHash: string, + fromAddress: string, + toAddress: string, + value: string, +) => ({ + transaction_hash, + address, + block_timestamp: blockTimestamp, + block_number: blockNumber, + block_hash: blockHash, + from_address: fromAddress, + to_address: toAddress, + value, +}); diff --git a/packages/evmApi/integration/setup.ts b/packages/evmApi/integration/setup.ts index eb7504fbf8..fe83d53847 100644 --- a/packages/evmApi/integration/setup.ts +++ b/packages/evmApi/integration/setup.ts @@ -3,7 +3,7 @@ import { Core } from '@moralisweb3/common-core'; import { CommonEvmUtils } from '@moralisweb3/common-evm-utils'; import { EvmApi } from '../src/EvmApi'; import { MOCK_API_KEY } from './mocks/config'; -import { mockServer, mockServer2 } from './mocks/mockServer'; +import { mockServer } from './mocks/mockServer'; export function setupEvmApi(): EvmApi { const core = Core.create(); @@ -25,30 +25,6 @@ export function setupEvmApi(): EvmApi { return evmApi; } -export function setupEvmApi2(): EvmApi { - const core = Core.create(); - const apiUtils = ApiUtils.create(core); - const evmUtils = CommonEvmUtils.create(core); - const evmApi = EvmApi.create(core); - - // DO NOT SET `MoralisCoreProvider.setDefault(core)` here! - - core.registerModules([apiUtils, evmUtils, evmApi]); - core.start({ - apiKey: MOCK_API_KEY, - }); - - mockServer2.listen({ - onUnhandledRequest: 'warn', - }); - - return evmApi; -} - export function cleanEvmApi() { mockServer.close(); } - -export function cleanEvmApi2() { - mockServer2.close(); -} diff --git a/packages/evmApi/integration/test/endpointWeights.test.ts b/packages/evmApi/integration/test/endpointWeights.test.ts index c4cd59678d..bbb167bb72 100644 --- a/packages/evmApi/integration/test/endpointWeights.test.ts +++ b/packages/evmApi/integration/test/endpointWeights.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('endpointWeights', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get the endpoint weight ', async () => { diff --git a/packages/evmApi/integration/test/getBlock.test.ts b/packages/evmApi/integration/test/getBlock.test.ts index f413a4a459..08fb311a1d 100644 --- a/packages/evmApi/integration/test/getBlock.test.ts +++ b/packages/evmApi/integration/test/getBlock.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getBlock', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns null when API returns HTTP 404', async () => { diff --git a/packages/evmApi/integration/test/getContractEvents.test.ts b/packages/evmApi/integration/test/getContractEvents.test.ts index 251fdf41c8..3181cde9c9 100644 --- a/packages/evmApi/integration/test/getContractEvents.test.ts +++ b/packages/evmApi/integration/test/getContractEvents.test.ts @@ -1,5 +1,5 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; const ERC721_TRANSFER_ABI = { anonymous: false, @@ -43,11 +43,11 @@ describe('getContractEvents', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns events', async () => { diff --git a/packages/evmApi/integration/test/getContractLogs.test.ts b/packages/evmApi/integration/test/getContractLogs.test.ts index 96087782d6..74b0ce3f7c 100644 --- a/packages/evmApi/integration/test/getContractLogs.test.ts +++ b/packages/evmApi/integration/test/getContractLogs.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getContractLogs', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get logs for an address', async () => { diff --git a/packages/evmApi/integration/test/getContractNFTs.test.ts b/packages/evmApi/integration/test/getContractNFTs.test.ts index 08a1c29043..596f20dd49 100644 --- a/packages/evmApi/integration/test/getContractNFTs.test.ts +++ b/packages/evmApi/integration/test/getContractNFTs.test.ts @@ -2,41 +2,49 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getContractNFTs', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get all token Ids of an account', async () => { - const result = await evmApi.nft.getContractNFTs({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', - }); + describe('Get Contract NFTs', () => { + it('should get NFTs for a given contract address', async () => { + const result = await EvmApi.nft.getContractNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result).toBeDefined(); - expect(result.raw.total).toBe(741); - expect(result).toEqual(expect.objectContaining({})); - }); + const response = result.raw.result?.at(0); - it('should not get all token Ids of an invalid account and throw an error ', async () => { - const failedResult = await evmApi.nft - .getContractNFTs({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); + }); + + it('should not get NFTs for for an invalid contract address and throw an error', async () => { + const failedResult = await EvmApi.nft + .getContractNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getContractNFTs({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getContractNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getDateToBlock.test.ts b/packages/evmApi/integration/test/getDateToBlock.test.ts index bdf02a7b25..52bb098545 100644 --- a/packages/evmApi/integration/test/getDateToBlock.test.ts +++ b/packages/evmApi/integration/test/getDateToBlock.test.ts @@ -1,20 +1,20 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getDateToBlock', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get the closest block of the provided date ', async () => { const result = await evmApi.block.getDateToBlock({ - date: '2021-09-29T13:09:15+00:00', + date: '2021-09-29T13:09:15.000Z', chain: 5, }); diff --git a/packages/evmApi/integration/test/getNFTContractMetadata.test.ts b/packages/evmApi/integration/test/getNFTContractMetadata.test.ts index 246c455ae3..e12c7a89b1 100644 --- a/packages/evmApi/integration/test/getNFTContractMetadata.test.ts +++ b/packages/evmApi/integration/test/getNFTContractMetadata.test.ts @@ -2,33 +2,45 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTContractMetadata', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('returns a data', async () => { - const response = await evmApi.nft.getNFTContractMetadata({ - address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', + describe('Get NFT Contract Metadata', () => { + it('should get the collection metadata for a given contract', async () => { + const result = await EvmApi.nft.getNFTContractMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); + const response = result?.raw!; + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(response.name).toEqual('KryptoKitties'); + expect(response.token_address).toEqual('0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09'); }); - const result = response?.result!; - expect(result).toBeDefined(); - expect(result.result.symbol).toBe('BAYC'); - expect(result.result.name).toBe('BoredApeYachtClub'); - expect(result.result.contractType).toBe('ERC721'); - }); + it('should not get the collection metadata for an invalid contract address and throw a 400 Error', async () => { + const failedResult = await EvmApi.nft + .getNFTContractMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); - it('returns null when API returns HTTP 404', async () => { - const response = await evmApi.nft.getNFTContractMetadata({ - address: '0x4044044044044044044044044044044044044040', + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTContractMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); }); - - expect(response).toBeNull(); }); }); diff --git a/packages/evmApi/integration/test/getNFTContractTransfers.test.ts b/packages/evmApi/integration/test/getNFTContractTransfers.test.ts index bf704b0386..12ee460fc4 100644 --- a/packages/evmApi/integration/test/getNFTContractTransfers.test.ts +++ b/packages/evmApi/integration/test/getNFTContractTransfers.test.ts @@ -2,41 +2,47 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTContractTransfers', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the contract of NFT transfers of an account', async () => { - const result = await evmApi.nft.getNFTContractTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', - }); + describe('Get NFT Contract Transfers', () => { + it('should get the contract transfers of NFTs for a given contract', async () => { + const result = await EvmApi.nft.getNFTContractTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result).toBeDefined(); - expect(result.raw.total).toBe(900); - expect(result).toEqual(expect.objectContaining({})); - }); + const response = result.raw.result?.at(0); - it('should not get the contract of NFT transfers of an invalid account and throw an error ', async () => { - const failedResult = await evmApi.nft - .getNFTContractTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.token_address).toEqual('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + expect(response?.transaction_hash).toEqual('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + }); + + it('should not get the contract of NFT transfers of an invalid address and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTContractTransfers({ + address: '0x7de3085b3190b3a787822ee16f23be010f5f868', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getNFTContractTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTContractTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTLowestPrice.test.ts b/packages/evmApi/integration/test/getNFTLowestPrice.test.ts index 28f078e0de..670b923515 100644 --- a/packages/evmApi/integration/test/getNFTLowestPrice.test.ts +++ b/packages/evmApi/integration/test/getNFTLowestPrice.test.ts @@ -2,40 +2,45 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTLowestPrice', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('returns a data', async () => { - const response = await evmApi.nft.getNFTLowestPrice({ - address: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D', + describe('Get NFT Lowest Price', () => { + it('should get the lowest executed price for an NFT contract', async () => { + const result = await EvmApi.nft.getNFTLowestPrice({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result?.raw.seller_address).toBe('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + expect(result?.raw.transaction_hash).toEqual('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + expect(result?.raw.block_number).toBe('13680123'); }); - const result = response?.result!; - expect(result).toBeDefined(); - expect(result.result.blockNumber.toString()).toBe('15401671'); - expect(result.result.blockHash).toBe('0x69de52caa13ac1c165d6b408a47f7ed79cef12280d1d26099d9c0d2b63b52626'); - expect(result.result.price.wei).toBe('66990000000000000000'); - }); - - it('returns null when API returns HTTP 404', async () => { - const response = await evmApi.nft.getNFTLowestPrice({ - address: '0x4044044044044044044044044044044044044040', - }); - - expect(response).toBeNull(); - }); - - it('returns null when API returns false-positive not found', async () => { - const response = await evmApi.nft.getNFTLowestPrice({ - address: '0x2000000000000000000404404404404404404404', + it('should not get the lowest executed price when an invalid address is provided and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTLowestPrice({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTLowestPrice({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); }); - expect(response).toBeNull(); }); }); diff --git a/packages/evmApi/integration/test/getNFTMetadata.test.ts b/packages/evmApi/integration/test/getNFTMetadata.test.ts new file mode 100644 index 0000000000..47c7a267c4 --- /dev/null +++ b/packages/evmApi/integration/test/getNFTMetadata.test.ts @@ -0,0 +1,74 @@ +import { EvmApi } from '../../src/EvmApi'; +import { cleanEvmApi, setupEvmApi } from '../setup'; + +describe('getNFTMetadata', () => { + let EvmApi: EvmApi; + + beforeAll(() => { + EvmApi = setupEvmApi(); + }); + + afterAll(() => { + cleanEvmApi(); + }); + + describe('Get NFT Metadata', () => { + it('should get NFT metadata for a valid NFT token ID and contract address', async () => { + const result = await EvmApi.nft.getNFTMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '15', + }); + + const response = result?.raw; + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(response?.token_hash).toEqual('502cee781b0fb40ea02508b21d319ced'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); + expect(response?.name).toEqual('CryptoKitties'); + expect(response?.token_address).toEqual('0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'); + }); + + it('should not get NFT metadata for an invalid NFT contract address and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + tokenId: '15', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + tokenId: '15', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); + + it('should not get NFT metadata for an invalid token ID and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '000000215', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTMetadata({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '000000215', + }), + ).rejects.toThrowError('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'); + }); + }); +}); diff --git a/packages/evmApi/integration/test/getNFTOwners.test.ts b/packages/evmApi/integration/test/getNFTOwners.test.ts index a3c30fb606..4dba128ce3 100644 --- a/packages/evmApi/integration/test/getNFTOwners.test.ts +++ b/packages/evmApi/integration/test/getNFTOwners.test.ts @@ -1,44 +1,67 @@ import { EvmApi } from '../../src/EvmApi'; -import { EvmNft } from '@moralisweb3/common-evm-utils'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTOwners', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - function assertOwner(nft: EvmNft) { - expect(nft.blockNumber?.toString()).toEqual('15458263'); - expect(nft.name).toEqual('BoredApeYachtClub'); - expect(nft.contractType).toEqual('ERC721'); - expect(nft.symbol).toEqual('BAYC'); - } + describe('Get NFT Owners', () => { + it('should return a collection of NFT owners when a valid address is provided', async () => { + const result = await EvmApi.nft.getNFTOwners({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - it('returns owners with pagination', async () => { - let response = await evmApi.nft.getNFTOwners({ - address: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D', + const response = result.raw.result?.at(0); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.name).toBe('CryptoKitties'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.symbol).toBe('RARI'); + }); + + it('should not return a collection of NFT owners when an invalid address is provided', async () => { + const failedResult = await EvmApi.nft + .getNFTOwners({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTOwners({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); }); - expect(response.pagination.total).toEqual(150); - expect(response.pagination.page).toEqual(1); - expect(response.pagination.pageSize).toEqual(100); - expect(response.result.length).toEqual(100); - expect(response.hasNext()).toEqual(true); - assertOwner(response.result[0]); - - response = await response.next(); - - expect(response.pagination.total).toEqual(150); - expect(response.pagination.page).toEqual(2); - expect(response.pagination.pageSize).toEqual(100); - expect(response.result.length).toEqual(50); - expect(response.hasNext()).toEqual(false); - assertOwner(response.result[0]); + it('returns owners with pagination', async () => { + let response = await EvmApi.nft.getNFTOwners({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); + + expect(response.pagination.total).toEqual(2000); + expect(response.pagination.pageSize).toEqual(100); + expect(response.hasNext()).toEqual(true); + + response = await response.next(); + + expect(response.pagination.total).toEqual(2000); + expect(response.pagination.page).toEqual(2); + expect(response.pagination.pageSize).toEqual(100); + expect(response.hasNext()).toEqual(true); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTTokenIdOwners.test.ts b/packages/evmApi/integration/test/getNFTTokenIdOwners.test.ts index 70b2de841d..596d530f34 100644 --- a/packages/evmApi/integration/test/getNFTTokenIdOwners.test.ts +++ b/packages/evmApi/integration/test/getNFTTokenIdOwners.test.ts @@ -2,47 +2,72 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTTokenIdOwners', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the token Id owners', async () => { - const result = await evmApi.nft.getNFTTokenIdOwners({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', - format: 'decimal', - tokenId: '1', + describe('Get NFT Token Id Owners', () => { + it('should get owners of a specific NFT given a valid contract address and token ID', async () => { + const result = await EvmApi.nft.getNFTTokenIdOwners({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '15', + }); + + const response = result.raw.result?.at(0); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.name).toEqual('CryptoKitties'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); }); - expect(result).toBeDefined(); - expect(result.raw.total).toBe(10); - expect(result).toEqual(expect.objectContaining({})); - }); + it('should not get owners of a specific NFT given an invalid contract address and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTTokenIdOwners({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88', + tokenId: '15', + }) + .then() + .catch((err) => { + return err; + }); - it('should not get the token Id owners and throw an error ', async () => { - const failedResult = await evmApi.nft - .getNFTTokenIdOwners({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - format: 'decimal', - tokenId: '1', - }) - .then() - .catch((err) => { - return err; - }); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTokenIdOwners({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88', + tokenId: '15', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getNFTTokenIdOwners({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - format: 'decimal', - tokenId: '1', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + it('should not get owners of a specific NFT given an invalid tokenId and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTTokenIdOwners({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '000000215', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTokenIdOwners({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '000000215', + }), + ).rejects.toThrowError('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTTrades.test.ts b/packages/evmApi/integration/test/getNFTTrades.test.ts index 674a0fe598..a4dcd9a44c 100644 --- a/packages/evmApi/integration/test/getNFTTrades.test.ts +++ b/packages/evmApi/integration/test/getNFTTrades.test.ts @@ -2,40 +2,49 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTTrades', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the NFT trades of an account', async () => { - const result = await evmApi.nft.getNFTTrades({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', - }); + describe('Get NFT Trades', () => { + it('should get trades of NFTs for a given contract and marketplace', async () => { + const result = await EvmApi.nft.getNFTTrades({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result.raw.total).toBe(53); - expect(result).toBeDefined(); - }); + const response = result.raw.result?.at(0); - it('should not get the NFT trades and return an error code for an invalid address', async () => { - const failedResult = await evmApi.nft - .getNFTTrades({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.transaction_hash).toBe('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + expect(response?.block_number).toBe('13680123'); + expect(response?.block_hash).toBe('0x4a7c916ca4a970358b9df90051008f729685ff05e9724a9dddba32630c37cb96'); + expect(response?.transaction_hash).toBe('0x057Ec652A4F150f7FF94f089A38008f49a0DF88e'); + }); + + it('should not get the trades of NFTs for an invalid address and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTTrades({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getNFTTrades({ - address: '0x7dE3085b3190B3a787822Ee16F23be010f5F868', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTrades({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTTransfers.test.ts b/packages/evmApi/integration/test/getNFTTransfers.test.ts index 64fdb79f72..345f4690c8 100644 --- a/packages/evmApi/integration/test/getNFTTransfers.test.ts +++ b/packages/evmApi/integration/test/getNFTTransfers.test.ts @@ -2,43 +2,72 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTTransfers', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the transfers of the tokens matching the given parameters', async () => { - const result = await evmApi.nft.getNFTTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', - tokenId: '18', + describe('Get NFT Transfers', () => { + it('should get the transfers of an NFT given a valid contract address and token ID', async () => { + const result = await EvmApi.nft.getNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '15', + }); + + const response = result.raw.result?.at(0); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); }); - expect(result).toBeDefined(); - expect(result).toEqual(expect.objectContaining({})); - expect(result.raw.total).toBe(1); - }); - it('should not get the wallet token Id transfers of an invalid account and throw an error ', async () => { - const failedResult = await evmApi.nft - .getNFTTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - tokenId: '18', - }) - .then() - .catch((err) => { - return err; - }); + it('should not get the transfers of an NFT given an invalid contract address and a valid token ID', async () => { + const failedResult = await EvmApi.nft + .getNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + tokenId: '15', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getNFTTransfers({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f868', - tokenId: '18', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + tokenId: '15', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); + + it('should not get the transfers of an NFT given a valid contract address and invalid token ID', async () => { + const failedResult = await EvmApi.nft + .getNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '000000215', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + tokenId: '000000215', + }), + ).rejects.toThrowError('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTTransfersByBlock.test.ts b/packages/evmApi/integration/test/getNFTTransfersByBlock.test.ts index 6193f22c19..f0070b4ff0 100644 --- a/packages/evmApi/integration/test/getNFTTransfersByBlock.test.ts +++ b/packages/evmApi/integration/test/getNFTTransfersByBlock.test.ts @@ -2,23 +2,49 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTTransfersByBlock', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the NFT transfers by block of a hash', async () => { - const result = await evmApi.nft.getNFTTransfersByBlock({ - blockNumberOrHash: '0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171', + describe('Get NFTs Transfers By Block', () => { + it('should get transfers of NFTs given a block number or block hash', async () => { + const result = await EvmApi.nft.getNFTTransfersByBlock({ + blockNumberOrHash: '0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171', + }); + + const response = result.raw.result?.at(0); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); }); - expect(result).toBeDefined(); - expect(result.raw.total).toBe(3); - expect(result).toEqual(expect.objectContaining({})); + it('should not get transfers of NFTs given an invalid block number or block hash and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTTransfersByBlock({ + blockNumberOrHash: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTransfersByBlock({ + blockNumberOrHash: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid block number or block hash provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getNFTTransfersFromToBlock.test.ts b/packages/evmApi/integration/test/getNFTTransfersFromToBlock.test.ts index ea1359dc7f..1325fd5884 100644 --- a/packages/evmApi/integration/test/getNFTTransfersFromToBlock.test.ts +++ b/packages/evmApi/integration/test/getNFTTransfersFromToBlock.test.ts @@ -2,43 +2,50 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getNFTTransfersFromToBlock', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the NFT transfers from to block of a hash', async () => { - const result = await evmApi.nft.getNFTTransfersFromToBlock({ - fromBlock: 1, + describe('Get NFTs Transfers From block to block', () => { + it('should get transfers of NFTs from a block number to a block number', async () => { + const result = await EvmApi.nft.getNFTTransfersFromToBlock({ + fromBlock: 1, + toBlock: 2, + }); + + const response = result.raw.result?.at(0); + + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); }); - expect(result).toBeDefined(); - expect(result.raw.total).toBe(118072744); - expect(result).toEqual(expect.objectContaining({})); - }); + it('should not get the NFT transfers from to block of an invalid block number and throw an error', async () => { + const failedResult = await EvmApi.nft + .getNFTTransfersFromToBlock({ + fromBlock: 0x7, + }) + .then() + .catch((err) => { + return err; + }); - it('should not get the NFT transfers from to block of an invalid block number and throw an error ', async () => { - const failedResult = await evmApi.nft - .getNFTTransfersFromToBlock({ - fromBlock: 1, - }) - .then() - .catch((err) => { - return err; - }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getNFTTransfersFromToBlock({ - fromBlock: 1, - chain: 'invalid_chain', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot( - `"[C0005] Invalid provided chain, value must be a positive number, or a hex-string starting with '0x'"`, - ); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getNFTTransfersFromToBlock({ + fromBlock: 0x7, + }), + ).rejects.toThrowError('[C0005] Invalid block number provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getPairAddress.test.ts b/packages/evmApi/integration/test/getPairAddress.test.ts index d5a62d8bb8..ad49d4f09a 100644 --- a/packages/evmApi/integration/test/getPairAddress.test.ts +++ b/packages/evmApi/integration/test/getPairAddress.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getPairAddress', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get pair data for a given pair address ', async () => { diff --git a/packages/evmApi/integration/test/getPairReserves.test.ts b/packages/evmApi/integration/test/getPairReserves.test.ts index 3125010140..17ab111cf7 100644 --- a/packages/evmApi/integration/test/getPairReserves.test.ts +++ b/packages/evmApi/integration/test/getPairReserves.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getPairReserves', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get the liquidity reserves for a given pair address ', async () => { diff --git a/packages/evmApi/integration/test/getTokenAddressTransfers.test.ts b/packages/evmApi/integration/test/getTokenAddressTransfers.test.ts deleted file mode 100644 index 2e92cb2be3..0000000000 --- a/packages/evmApi/integration/test/getTokenAddressTransfers.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi, setupEvmApi } from '../setup'; - -describe('getTokenTransfers', () => { - let evmApi: EvmApi; - - beforeAll(() => { - evmApi = setupEvmApi(); - }); - - afterAll(() => { - cleanEvmApi(); - }); - - it('should get the token address transfers of an account', async () => { - const result = await evmApi.token.getTokenTransfers({ - address: '0xa2107fa5b38d9bbd2c461d6edf11b11a50f6b974', - }); - - expect(result).toBeDefined(); - expect(result.raw.total).toBe(807091); - expect(result).toEqual(expect.objectContaining({})); - }); - - it('should not get the token address transfers of an invalid account and throw an error ', async () => { - const failedResult = await evmApi.token - .getTokenTransfers({ - address: '0xa2107fa5b38d9bbd2c461d6edf11b11a50f6b97', - }) - .then() - .catch((err) => { - return err; - }); - - expect(failedResult).toBeDefined(); - expect( - evmApi.token.getTokenTransfers({ - address: '0xa2107fa5b38d9bbd2c461d6edf11b11a50f6b97', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); - }); -}); diff --git a/packages/evmApi/integration/test/getTokenIdMetadata.test.ts b/packages/evmApi/integration/test/getTokenIdMetadata.test.ts deleted file mode 100644 index 909c603515..0000000000 --- a/packages/evmApi/integration/test/getTokenIdMetadata.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi, setupEvmApi } from '../setup'; - -describe('getNFTMetadata', () => { - let evmApi: EvmApi; - - beforeAll(() => { - evmApi = setupEvmApi(); - }); - - afterAll(() => { - cleanEvmApi(); - }); - - it('returns a metadata', async () => { - const response = await evmApi.nft.getNFTMetadata({ - chain: '0x89', - address: '0x2953399124f0cbb46d2cbacd8a89cf0599974963', - tokenId: '113461209507512867518933452141320285231135646094834536306130710983923277496520', - }); - const result = response?.result.result!; - - expect(result).toBeDefined(); - expect(result.name).toEqual('OpenSea Collections'); - expect(result.contractType).toEqual('ERC1155'); - expect(result.symbol).toEqual('OPENSTORE'); - expect(result.ownerOf?.lowercase).toEqual('0x86d2b5f4af69458a22d69a7347b2133854933ba4'); - }); - - it('returns null when API returns HTTP 404', async () => { - const response = await evmApi.nft.getNFTMetadata({ - address: '0x4044044044044044044044044044044044044040', - tokenId: '0', - }); - - expect(response).toBeNull(); - }); -}); diff --git a/packages/evmApi/integration/test/getTokenTransfers.test.ts b/packages/evmApi/integration/test/getTokenTransfers.test.ts new file mode 100644 index 0000000000..4e65be6aff --- /dev/null +++ b/packages/evmApi/integration/test/getTokenTransfers.test.ts @@ -0,0 +1,37 @@ +import { EvmApi } from '../../src/EvmApi'; +import { Erc20Transfer } from '@moralisweb3/common-evm-utils'; +import { cleanEvmApi, setupEvmApi } from '../setup'; + +describe('getTokenTransfers', () => { + let evmApi: EvmApi; + + beforeAll(() => { + evmApi = setupEvmApi(); + }); + + afterAll(() => { + cleanEvmApi(); + }); + + function assertTransfer(transfer: Erc20Transfer) { + expect(transfer).toBeDefined(); + expect(transfer.blockHash).toBe('0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05'); + expect(transfer.blockNumber.toString()).toBe('15454126'); + expect(transfer.value.toString()).toBe('347995260860000000000'); + expect(transfer.fromAddress.checksum).toBe('0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D'); + expect(transfer.toAddress.checksum).toBe('0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F'); + } + + it('returns transfers', async () => { + const response = await evmApi.token.getTokenTransfers({ + address: '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', + }); + + expect(response.pagination.total).toEqual(12); + expect(response.pagination.page).toEqual(0); + expect(response.pagination.pageSize).toEqual(100); + expect(response.result.length).toEqual(12); + + assertTransfer(response.result[0]); + }); +}); diff --git a/packages/evmApi/integration/test/getTransaction.test.ts b/packages/evmApi/integration/test/getTransaction.test.ts index 3762751ac2..c4dbe8af1b 100644 --- a/packages/evmApi/integration/test/getTransaction.test.ts +++ b/packages/evmApi/integration/test/getTransaction.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getTransaction', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns null when API returns HTTP 404', async () => { diff --git a/packages/evmApi/integration/test/getWalletNFTCollections.test.ts b/packages/evmApi/integration/test/getWalletNFTCollections.test.ts index 917440b5e4..dd027cd5b9 100644 --- a/packages/evmApi/integration/test/getWalletNFTCollections.test.ts +++ b/packages/evmApi/integration/test/getWalletNFTCollections.test.ts @@ -2,49 +2,49 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getWalletNFTCollections', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the Nft collections of an account address', async () => { - const result = await evmApi.nft.getWalletNFTCollections({ - address: '0x3514980793dceae1b34d0144e3ae725bee084a70', - }); + describe('Get NFT Collections By Wallet', () => { + it('should get the NFT Collections owned by a given wallet address when a valid address is provided', async () => { + const result = await EvmApi.nft.getWalletNFTCollections({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result).toBeDefined(); - expect(result.pagination.total).toBe(1); - expect(result.pagination.page).toBe(1); - expect(result.toJSON().result![0]).toEqual( - expect.objectContaining({ - contract_type: 'ERC721', - name: 'Test NFT', - symbol: 'TEST', - token_address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', - }), - ); - }); + const response = result.raw.result?.at(0); - it('should not get the Nft collections and return an error code for an invalid address', () => { - const failedResult = evmApi.nft - .getWalletNFTCollections({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.token_address).toEqual('0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'); + expect(response?.name).toBe('CryptoKitties'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.symbol).toBe('RARI'); + }); + + it('should not get the NFT Collections when an invalid address is provided and throw an error', async () => { + const failedResult = await EvmApi.nft + .getWalletNFTCollections({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getWalletNFTCollections({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getWalletNFTCollections({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getWalletNFTTransfers.test.ts b/packages/evmApi/integration/test/getWalletNFTTransfers.test.ts index 497cba11f4..0233833fbb 100644 --- a/packages/evmApi/integration/test/getWalletNFTTransfers.test.ts +++ b/packages/evmApi/integration/test/getWalletNFTTransfers.test.ts @@ -2,41 +2,49 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getWalletNFTTransfers', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the NFTs Transfers of an account address', async () => { - const result = await evmApi.nft.getWalletNFTTransfers({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387a', - }); + describe('Get NFT Transfers By Wallet', () => { + it('should get the Wallet NFT Transfers with a valid address', async () => { + const result = await EvmApi.nft.getWalletNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result).toBeDefined(); - expect(result).toEqual(expect.objectContaining({})); - expect(result.raw.total).toBe(126); - }); + const response = result.raw.result?.at(0); - it('should not get the NFTs Transfers and return an error code for an invalid address', () => { - const failedResult = evmApi.nft - .getWalletNFTTransfers({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.amount).toEqual('1'); + expect(response?.block_number).toBe('88256'); + expect(response?.contract_type).toBe('ERC721'); + expect(response?.token_id).toBe('15'); + }); + + it('should throw a 400 Error on invalid address', async () => { + const failedResult = await EvmApi.nft + .getWalletNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getWalletNFTTransfers({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getWalletNFTTransfers({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getWalletNFTs.test.ts b/packages/evmApi/integration/test/getWalletNFTs.test.ts index a7b9f0521f..4a275f2433 100644 --- a/packages/evmApi/integration/test/getWalletNFTs.test.ts +++ b/packages/evmApi/integration/test/getWalletNFTs.test.ts @@ -2,41 +2,47 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getWalletNFTs', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should get the NFTs of an account address', async () => { - const result = await evmApi.nft.getWalletNFTs({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387a', - }); + describe('Get NFTs By Wallet', () => { + it('should get the NFTs owned by a given valid address', async () => { + const result = await EvmApi.nft.getWalletNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387a', + }); - expect(result).toBeDefined(); - expect(result).toEqual(expect.objectContaining({})); - expect(result.raw.total).toBe(112); - }); + const response = result.raw.result?.at(0); - it('should not get the NFTs and return an error code for an invalid address', () => { - const failedResult = evmApi.nft - .getWalletNFTs({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }) - .then() - .catch((err) => { - return err; - }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + expect(result.raw.total).toBe(2000); + expect(response?.token_address).toBe('0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'); + expect(response?.token_hash).toBe('502cee781b0fb40ea02508b21d319ced'); + }); + + it('should not get the NFTs and throw a 400 Error on invalid address', async () => { + const failedResult = await EvmApi.nft + .getWalletNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }) + .then() + .catch((err) => { + return err; + }); - expect(failedResult).toBeDefined(); - expect( - evmApi.nft.getWalletNFTs({ - address: '0x75e3e9c92162e62000425c98769965a76c2e387', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.getWalletNFTs({ + address: '0x75e3e9c92162e62000425c98769965a76c2e387', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/getWalletTokenBalances.test.ts b/packages/evmApi/integration/test/getWalletTokenBalances.test.ts new file mode 100644 index 0000000000..26e5b17419 --- /dev/null +++ b/packages/evmApi/integration/test/getWalletTokenBalances.test.ts @@ -0,0 +1,36 @@ +import { EvmApi } from '../../src/EvmApi'; +import { cleanEvmApi, setupEvmApi } from '../setup'; + +describe('getWalletTokenBalances', () => { + let evmApi: EvmApi; + + beforeAll(() => { + evmApi = setupEvmApi(); + }); + + afterAll(() => { + cleanEvmApi(); + }); + + it('returns balances', async () => { + const response = await evmApi.token.getWalletTokenBalances({ + address: '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', + }); + + expect(response.result.length).toEqual(2); + + const balance = response.result[0]; + expect(balance.decimals).toBe(18); + expect(balance.amount.toString()).toBe('795917396650797993089'); + + expect(balance.token).toBeDefined(); + expect(balance.token!.contractAddress.checksum).toBe('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'); + expect(balance.token!.name).toBe('Wrapped Ether'); + expect(balance.token!.symbol).toBe('WETH'); + expect(balance.token!.logo).toBe('https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png'); + expect(balance.token!.thumbnail).toBe( + 'https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.png', + ); + expect(balance.token!.decimals).toBe(balance.decimals); + }); +}); diff --git a/packages/evmApi/integration/test/getWalletTokenTransfers.test.ts b/packages/evmApi/integration/test/getWalletTokenTransfers.test.ts index 87de614e25..d410caa299 100644 --- a/packages/evmApi/integration/test/getWalletTokenTransfers.test.ts +++ b/packages/evmApi/integration/test/getWalletTokenTransfers.test.ts @@ -18,13 +18,13 @@ describe('getWalletTokenTransfers', () => { expect(transfer.blockHash).toBe('0x0cd840fb6f116b8dd39ba8c30e3b74d741ecb638c4a8d0704801e7d18baaef05'); expect(transfer.blockNumber.toString()).toBe('15454126'); expect(transfer.value.toString()).toBe('347995260860000000000'); - expect(transfer.fromAddress.lowercase).toBe('0xd73a9eadfff6a332afda7ddbb18cff84bbf6dd0d'); - expect(transfer.toAddress.lowercase).toBe('0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f'); + expect(transfer.fromAddress.checksum).toBe('0xd73a9EAdFff6A332aFDa7dDBB18CFf84bBf6dd0D'); + expect(transfer.toAddress.checksum).toBe('0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F'); } it('returns transfers (no pagination)', async () => { const response = await evmApi.token.getWalletTokenTransfers({ - address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + address: '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', }); expect(response.pagination.total).toEqual(12); @@ -38,7 +38,7 @@ describe('getWalletTokenTransfers', () => { it('returns transfer (with pagination)', async () => { let response = await evmApi.token.getWalletTokenTransfers({ - address: '0x72fdd62fbfa2faa9a8677c58d9992068772e0f7f', + address: '0x72FDD62FbFa2fAa9A8677C58d9992068772e0f7F', limit: 6, }); diff --git a/packages/evmApi/integration/test/getWalletTransactions.test.ts b/packages/evmApi/integration/test/getWalletTransactions.test.ts index 799448ca38..cbce630d6e 100644 --- a/packages/evmApi/integration/test/getWalletTransactions.test.ts +++ b/packages/evmApi/integration/test/getWalletTransactions.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('getWalletTransactions', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should get the transactions of an account', async () => { diff --git a/packages/evmApi/integration/test/resolveAddress.test.ts b/packages/evmApi/integration/test/resolveAddress.test.ts index 4b9459bca6..46d3096cf4 100644 --- a/packages/evmApi/integration/test/resolveAddress.test.ts +++ b/packages/evmApi/integration/test/resolveAddress.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('resolveAddress', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns a name', async () => { diff --git a/packages/evmApi/integration/test/resolveDomain.test.ts b/packages/evmApi/integration/test/resolveDomain.test.ts index edac53fc47..1b962bb9e5 100644 --- a/packages/evmApi/integration/test/resolveDomain.test.ts +++ b/packages/evmApi/integration/test/resolveDomain.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('resolveDomain', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns an address', async () => { diff --git a/packages/evmApi/integration/test/resyncMetadata.test.ts b/packages/evmApi/integration/test/resyncMetadata.test.ts new file mode 100644 index 0000000000..8c06070e8e --- /dev/null +++ b/packages/evmApi/integration/test/resyncMetadata.test.ts @@ -0,0 +1,54 @@ +import { EvmApi } from '../../src/EvmApi'; +import { cleanEvmApi, setupEvmApi } from '../setup'; + +describe('resyncMetadata', () => { + let EvmApi: EvmApi; + + beforeAll(() => { + EvmApi = setupEvmApi(); + }); + + afterAll(() => { + cleanEvmApi(); + }); + + describe('Resync NFT Metadata', () => { + it('should Resync the metadata for an NFT when valid params are provided', async () => { + const result = await EvmApi.nft.reSyncMetadata({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '15', + }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); + }); + + it('should not Resync the metadata for an NFT when an invalid address is provided and throw an error', async () => { + expect( + EvmApi.nft.reSyncMetadata({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88', + tokenId: '15', + }), + ).rejects.toThrowError('[C0005] Invalid address provided'); + }); + + it('should not Resync the metadata for an NFT when an invalid token ID is provided and throw an error', async () => { + const failedResult = await EvmApi.nft + .reSyncMetadata({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '000000215', + }) + .then() + .catch((err) => { + return err; + }); + + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.reSyncMetadata({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + tokenId: '000000215', + }), + ).rejects.toThrowError('[C0006] Request failed, Bad Request(400): [C0005] Invalid TokenId provided'); + }); + }); +}); diff --git a/packages/evmApi/integration/test/runContractFunction.test.ts b/packages/evmApi/integration/test/runContractFunction.test.ts index 8f80ab4673..1f36a7404e 100644 --- a/packages/evmApi/integration/test/runContractFunction.test.ts +++ b/packages/evmApi/integration/test/runContractFunction.test.ts @@ -1,5 +1,5 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; const ABI = [ { @@ -22,11 +22,11 @@ describe('runContractFunction', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should run a contract and return readonly data', async () => { diff --git a/packages/evmApi/integration/test/searchNFTs.test.ts b/packages/evmApi/integration/test/searchNFTs.test.ts index a7f650cdbe..bb91e08acc 100644 --- a/packages/evmApi/integration/test/searchNFTs.test.ts +++ b/packages/evmApi/integration/test/searchNFTs.test.ts @@ -2,23 +2,31 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('searchNFTs', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('returns NFTs', async () => { - const result = await evmApi.nft.searchNFTs({ - q: 'Pancake', - filter: 'name', + describe('Search NFTs', () => { + it('should get NFTs that match a given metadata search query', async () => { + const result = await EvmApi.nft.searchNFTs({ + q: '889', + }); + expect(result).toBeDefined(); + expect(result).toEqual(expect.objectContaining({})); }); - // TODO: need to add the mock responese for above arguments. - expect(result).toBeDefined(); + it('should throw an error when an invalid search parameter is provided', async () => { + expect( + EvmApi.nft.searchNFTs({ + q: 'Pancake', + }), + ).rejects.toThrowError('[C0005] Invalid search parameter provided'); + }); }); }); diff --git a/packages/evmApi/integration/test/syncNFTContract.test.ts b/packages/evmApi/integration/test/syncNFTContract.test.ts index 90b6fd577b..a7c6e27885 100644 --- a/packages/evmApi/integration/test/syncNFTContract.test.ts +++ b/packages/evmApi/integration/test/syncNFTContract.test.ts @@ -2,30 +2,42 @@ import { EvmApi } from '../../src/EvmApi'; import { cleanEvmApi, setupEvmApi } from '../setup'; describe('syncNFTContract', () => { - let evmApi: EvmApi; + let EvmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi(); + EvmApi = setupEvmApi(); }); afterAll(() => { cleanEvmApi(); }); - it('should sync NFT contract of an account', async () => { - const result = await evmApi.nft.syncNFTContract({ - address: '0x7de3085b3190b3a787822ee16f23be010f5f8686', + describe('Resync NFT Contract', () => { + it('should initiate a sync of previously non synced contracts', async () => { + const result = await EvmApi.nft.syncNFTContract({ + address: '0x057Ec652A4F150f7FF94f089A38008f49a0DF88e', + }); + + expect(result.result).toBeDefined(); + expect(result.raw).toStrictEqual({ success: true }); }); - expect(result.result).toBeDefined(); - expect(result.raw).toStrictEqual({ success: true }); - }); + it('should not sync NFT contract of an invalid account and throw an error ', async () => { + const failedResult = await EvmApi.nft + .syncNFTContract({ + address: '0x7de308', + }) + .then() + .catch((err) => { + return err; + }); - it('should not sync NFT contract of an invalid account and throw an error ', async () => { - expect( - evmApi.nft.syncNFTContract({ - address: '0x7de308', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid address provided"`); + expect(failedResult).toBeDefined(); + expect( + EvmApi.nft.syncNFTContract({ + address: '0x7de308', + }), + ).rejects.toThrowError(`[C0005] Invalid address provided`); + }); }); }); diff --git a/packages/evmApi/integration/test/uploadFolder.test.ts b/packages/evmApi/integration/test/uploadFolder.test.ts index aba35d910a..f7a0fed0f1 100644 --- a/packages/evmApi/integration/test/uploadFolder.test.ts +++ b/packages/evmApi/integration/test/uploadFolder.test.ts @@ -1,5 +1,5 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; const ABI = [ { @@ -12,11 +12,11 @@ describe('uploadFolder', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('should Uploads multiple files and place them in a folder directory', async () => { diff --git a/packages/evmApi/integration/test/web3ApiVersion.test.ts b/packages/evmApi/integration/test/web3ApiVersion.test.ts index baa958624c..a0bd510930 100644 --- a/packages/evmApi/integration/test/web3ApiVersion.test.ts +++ b/packages/evmApi/integration/test/web3ApiVersion.test.ts @@ -1,15 +1,15 @@ import { EvmApi } from '../../src/EvmApi'; -import { cleanEvmApi2, setupEvmApi2 } from '../setup'; +import { cleanEvmApi, setupEvmApi } from '../setup'; describe('web3ApiVersion', () => { let evmApi: EvmApi; beforeAll(() => { - evmApi = setupEvmApi2(); + evmApi = setupEvmApi(); }); afterAll(() => { - cleanEvmApi2(); + cleanEvmApi(); }); it('returns version', async () => { diff --git a/packages/streams/integration/mocks/response/statsResponse.ts b/packages/streams/integration/mocks/response/statsResponse.ts index cf91394bb9..814273482e 100644 --- a/packages/streams/integration/mocks/response/statsResponse.ts +++ b/packages/streams/integration/mocks/response/statsResponse.ts @@ -1,4 +1,4 @@ -const defaualtStatsResponse = { +const defaultStatsResponse = { totalWebhooksDelivered: 0, totalWebhooksFailed: 0, totalLogsProcessed: 2, @@ -7,5 +7,5 @@ const defaualtStatsResponse = { }; export const statsResponse = () => ({ - ...defaualtStatsResponse, + ...defaultStatsResponse, }); diff --git a/packages/testUtils/src/MockScenarios.ts b/packages/testUtils/src/MockScenarios.ts index e6f3971c93..7eae468e5f 100644 --- a/packages/testUtils/src/MockScenarios.ts +++ b/packages/testUtils/src/MockScenarios.ts @@ -4,7 +4,8 @@ import omitBy from 'lodash/omitBy'; import { rest, ResponseComposition, RestRequest, PathParams, RestContext } from 'msw'; type Condition = Record; -type Response = Record; +type Request = Record; +type Response = Record | Record[]; type Params = PathParams; const DEFAULT_RESPONSE_STATUS = 200; @@ -20,14 +21,14 @@ export interface MockScenariosOptions { method: 'get' | 'post' | 'put' | 'delete'; // Should return an object with the params that will be used to match the scenario conditions getParams: ( - req: RestRequest, + req: RestRequest, res: ResponseComposition, ctx: RestContext, ) => Record; // Optional hook to run logic after matching scenarios - beforeScenarios?: (req: RestRequest, res: ResponseComposition, ctx: RestContext) => void; + beforeScenarios?: (req: RestRequest, res: ResponseComposition, ctx: RestContext) => void; // Optional hook to run logic before matching scenarios - afterScenarios?: (req: RestRequest, res: ResponseComposition, ctx: RestContext) => void; + afterScenarios?: (req: RestRequest, res: ResponseComposition, ctx: RestContext) => void; // Url of the mock (see https://mswjs.io/docs/api/rest) url: string; // Name of the mock (used in error messages) @@ -77,7 +78,7 @@ export class MockScenarios { }; private mockRequest = async ( - req: RestRequest, + req: RestRequest, res: ResponseComposition, ctx: RestContext, ) => {