diff --git a/.changeset/young-crews-dream.md b/.changeset/young-crews-dream.md new file mode 100644 index 0000000000..6d015cc1b1 --- /dev/null +++ b/.changeset/young-crews-dream.md @@ -0,0 +1,6 @@ +--- +'@moralisweb3/evm-api': patch +'@moralisweb3/evm-utils': patch +--- + +Support not defined `contractType` for NFT types. diff --git a/packages/evmApi/src/resolvers/nft/getNFTContractMetadata.ts b/packages/evmApi/src/resolvers/nft/getNFTContractMetadata.ts index 273d48298a..e6d61953a3 100644 --- a/packages/evmApi/src/resolvers/nft/getNFTContractMetadata.ts +++ b/packages/evmApi/src/resolvers/nft/getNFTContractMetadata.ts @@ -6,7 +6,7 @@ import { EvmChain, EvmChainish, EvmNftMetadata, - validateValidEvmContractType, + noramlizeEvmNftContractType, } from '@moralisweb3/evm-utils'; import { operations } from '../../generated/types'; import { EvmChainResolver } from '../EvmChainResolver'; @@ -36,7 +36,7 @@ export const getNFTContractMetadata = createEndpointFactory((core) => chain: EvmChainResolver.resolve(params.chain, core), tokenAddress: EvmAddress.create(data.token_address, core), syncedAt: data.synced_at ? new Date(data.synced_at) : null, - contractType: data.contract_type ? validateValidEvmContractType(data.contract_type) : null, + contractType: noramlizeEvmNftContractType(data.contract_type), }), resultToJson: (data) => data.toJSON(), parseParams: (params: Params): ApiParams => ({ diff --git a/packages/evmUtils/src/dataTypes/EvmNft/EvmNft.ts b/packages/evmUtils/src/dataTypes/EvmNft/EvmNft.ts index 869e0d8a45..416a8c14f5 100644 --- a/packages/evmUtils/src/dataTypes/EvmNft/EvmNft.ts +++ b/packages/evmUtils/src/dataTypes/EvmNft/EvmNft.ts @@ -10,7 +10,7 @@ import MoralisCore, { } from '@moralisweb3/core'; import { EvmAddress } from '../EvmAddress'; import { EvmChain } from '../EvmChain'; -import { validateValidEvmContractType } from '../EvmNftContractType'; +import { noramlizeEvmNftContractType } from '../EvmNftContractType'; import { EvmNftData, EvmNftInput } from './types'; /** @@ -53,7 +53,7 @@ export class EvmNft implements MoralisDataObject { static parse = (data: EvmNftInput, core: MoralisCore): EvmNftData => ({ ...data, chain: EvmChain.create(data.chain, core), - contractType: validateValidEvmContractType(data.contractType), + contractType: noramlizeEvmNftContractType(data.contractType), tokenAddress: EvmAddress.create(data.tokenAddress, core), metadata: maybe(data.metadata, this.validateMetadata), tokenUri: maybe(data.tokenUri), diff --git a/packages/evmUtils/src/dataTypes/EvmNft/types.ts b/packages/evmUtils/src/dataTypes/EvmNft/types.ts index 69b39d77eb..41e68b5d1d 100644 --- a/packages/evmUtils/src/dataTypes/EvmNft/types.ts +++ b/packages/evmUtils/src/dataTypes/EvmNft/types.ts @@ -51,7 +51,7 @@ export interface EvmNftInput { */ export interface EvmNftData { tokenId: number | string; - contractType: EvmNftContractType; + contractType?: EvmNftContractType; chain: EvmChain; tokenUri?: string; tokenAddress: EvmAddress; diff --git a/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.test.ts b/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.test.ts index feb74b7c2c..19fe695253 100644 --- a/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.test.ts +++ b/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.test.ts @@ -38,7 +38,7 @@ describe('EvmNftCollection', () => { it('should throw an error when creating with an invalid contractType', () => { expect(() => EvmNftCollection.create({ ...exampleInput, contractType: 'ERC100' }), - ).toThrowErrorMatchingInlineSnapshot(`"[C0005] Invalid NFT contract type provided"`); + ).toThrowError('[C0005] Invalid NFT contract type provided'); }); /** diff --git a/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.ts b/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.ts index 6be2b99b58..1b93db0d22 100644 --- a/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.ts +++ b/packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.ts @@ -1,7 +1,7 @@ import MoralisCore, { MoralisDataObject, MoralisCoreProvider } from '@moralisweb3/core'; import { EvmAddress } from '../EvmAddress'; import { EvmChain } from '../EvmChain'; -import { validateValidEvmContractType } from '../EvmNftContractType'; +import { noramlizeEvmNftContractType } from '../EvmNftContractType'; import { EvmNftCollectionData, EvmNftCollectionInput } from './types'; /** @@ -39,7 +39,7 @@ export class EvmNftCollection implements MoralisDataObject { ...data, tokenAddress: EvmAddress.create(data.tokenAddress, core), chain: EvmChain.create(data.chain, core), - contractType: validateValidEvmContractType(data.contractType), + contractType: noramlizeEvmNftContractType(data.contractType), }); /** diff --git a/packages/evmUtils/src/dataTypes/EvmNftCollection/types.ts b/packages/evmUtils/src/dataTypes/EvmNftCollection/types.ts index 4107c05b22..077d152d7e 100644 --- a/packages/evmUtils/src/dataTypes/EvmNftCollection/types.ts +++ b/packages/evmUtils/src/dataTypes/EvmNftCollection/types.ts @@ -28,7 +28,7 @@ export interface EvmNftCollectionInput { */ export interface EvmNftCollectionData { chain: EvmChain; - contractType: EvmNftContractType; + contractType?: EvmNftContractType; name: string; symbol: string; tokenAddress: EvmAddress; diff --git a/packages/evmUtils/src/dataTypes/EvmNftContractType.test.ts b/packages/evmUtils/src/dataTypes/EvmNftContractType.test.ts new file mode 100644 index 0000000000..f1b761d03b --- /dev/null +++ b/packages/evmUtils/src/dataTypes/EvmNftContractType.test.ts @@ -0,0 +1,23 @@ +import { noramlizeEvmNftContractType, EvmNftContractType } from './EvmNftContractType'; + +describe('noramlizeEvmNftContractType()', () => { + it('returns ERC721', () => { + expect(noramlizeEvmNftContractType('erc721')).toEqual(EvmNftContractType.ERC721); + expect(noramlizeEvmNftContractType('ERC721')).toEqual(EvmNftContractType.ERC721); + }); + + it('returns ERC1155', () => { + expect(noramlizeEvmNftContractType('erc1155')).toEqual(EvmNftContractType.ERC1155); + expect(noramlizeEvmNftContractType('ERC1155')).toEqual(EvmNftContractType.ERC1155); + }); + + it('returns undefined', () => { + expect(noramlizeEvmNftContractType('')).toEqual(undefined); + expect(noramlizeEvmNftContractType(null as any)).toEqual(undefined); + expect(noramlizeEvmNftContractType(undefined as any)).toEqual(undefined); + }); + + it('throws error if value is undefined', () => { + expect(() => noramlizeEvmNftContractType('wrongErc')).toThrowError('Invalid NFT contract type provided'); + }); +}); diff --git a/packages/evmUtils/src/dataTypes/EvmNftContractType.ts b/packages/evmUtils/src/dataTypes/EvmNftContractType.ts index e4585cb3d6..04b4de1788 100644 --- a/packages/evmUtils/src/dataTypes/EvmNftContractType.ts +++ b/packages/evmUtils/src/dataTypes/EvmNftContractType.ts @@ -5,24 +5,20 @@ export enum EvmNftContractType { ERC1155 = 'ERC1155', } -export const isValidEvmContractType = (value: string): value is EvmNftContractType => { +export function noramlizeEvmNftContractType(value: string): EvmNftContractType | undefined { + if (!value) { + return undefined; + } + switch (value.toUpperCase()) { case EvmNftContractType.ERC1155: - return true; + return EvmNftContractType.ERC1155; case EvmNftContractType.ERC721: - return true; - default: - return false; + return EvmNftContractType.ERC721; } -}; -export const validateValidEvmContractType = (value: string) => { - if (!isValidEvmContractType(value)) { - throw new MoralisCoreError({ - code: CoreErrorCode.INVALID_ARGUMENT, - message: 'Invalid NFT contract type provided', - }); - } - - return value; -}; + throw new MoralisCoreError({ + code: CoreErrorCode.INVALID_ARGUMENT, + message: `Invalid NFT contract type provided: ${value}`, + }); +} diff --git a/packages/evmUtils/src/dataTypes/EvmNftMetadata/types.ts b/packages/evmUtils/src/dataTypes/EvmNftMetadata/types.ts index 50a21ea66a..c86506b792 100644 --- a/packages/evmUtils/src/dataTypes/EvmNftMetadata/types.ts +++ b/packages/evmUtils/src/dataTypes/EvmNftMetadata/types.ts @@ -22,7 +22,7 @@ export interface EvmNftMetadataInput { tokenAddress: EvmAddressish; name: string; symbol: string; - contractType: EvmNftContractType | null; + contractType?: EvmNftContractType; syncedAt: DateInput | null; } @@ -34,6 +34,6 @@ export interface EvmNftMetadataData { tokenAddress: EvmAddress; name: string; symbol: string; - contractType: EvmNftContractType | null; + contractType?: EvmNftContractType; syncedAt?: Date; }