Skip to content

Commit

Permalink
support not defined contractType for NFT types.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Sep 15, 2022
1 parent 7a8d21d commit 1ec4c2f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changeset/young-crews-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moralisweb3/evm-api': patch
'@moralisweb3/evm-utils': patch
---

Support not defined `contractType` for NFT types.
4 changes: 2 additions & 2 deletions packages/evmApi/src/resolvers/nft/getNFTContractMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EvmChain,
EvmChainish,
EvmNftMetadata,
validateValidEvmContractType,
noramlizeEvmNftContractType,
} from '@moralisweb3/evm-utils';
import { operations } from '../../generated/types';
import { EvmChainResolver } from '../EvmChainResolver';
Expand Down Expand Up @@ -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 => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/evmUtils/src/dataTypes/EvmNft/EvmNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/evmUtils/src/dataTypes/EvmNft/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface EvmNftInput {
*/
export interface EvmNftData {
tokenId: number | string;
contractType: EvmNftContractType;
contractType?: EvmNftContractType;
chain: EvmChain;
tokenUri?: string;
tokenAddress: EvmAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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),
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/evmUtils/src/dataTypes/EvmNftCollection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface EvmNftCollectionInput {
*/
export interface EvmNftCollectionData {
chain: EvmChain;
contractType: EvmNftContractType;
contractType?: EvmNftContractType;
name: string;
symbol: string;
tokenAddress: EvmAddress;
Expand Down
23 changes: 23 additions & 0 deletions packages/evmUtils/src/dataTypes/EvmNftContractType.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
28 changes: 12 additions & 16 deletions packages/evmUtils/src/dataTypes/EvmNftContractType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
});
}
4 changes: 2 additions & 2 deletions packages/evmUtils/src/dataTypes/EvmNftMetadata/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface EvmNftMetadataInput {
tokenAddress: EvmAddressish;
name: string;
symbol: string;
contractType: EvmNftContractType | null;
contractType?: EvmNftContractType;
syncedAt: DateInput | null;
}

Expand All @@ -34,6 +34,6 @@ export interface EvmNftMetadataData {
tokenAddress: EvmAddress;
name: string;
symbol: string;
contractType: EvmNftContractType | null;
contractType?: EvmNftContractType;
syncedAt?: Date;
}

0 comments on commit 1ec4c2f

Please sign in to comment.