Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support not defined contractType for NFT types. #673

Merged
merged 5 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not just something like:

    contractType: maybe(data.contractType, validateValidEvmContractType),

Copy link
Collaborator Author

@b4rtaz b4rtaz Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've standardized the code.

contractType: maybe(data.contractType, normalizeEvmNftContractType),

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 @@ -36,9 +36,9 @@ 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"`);
expect(() => EvmNftCollection.create({ ...exampleInput, contractType: 'ERC100' })).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
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;
}