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: fix getting nft tokenURI #4136

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,15 @@ describe('AssetsContractController', () => {
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

it('should throw an error when address given is not an ERC-721 NFT', async () => {
it('should not throw an error when address given does not support NFT Metadata interface', async () => {
const { assetsContract, messenger, provider, networkClientConfiguration } =
await setupAssetContractControllers();
assetsContract.configure({ provider });
const errorLogSpy = jest
.spyOn(console, 'error')
.mockImplementationOnce(() => {
/**/
});
mockNetworkWithDefaultChainId({
networkClientConfiguration,
mocks: [
Expand All @@ -630,17 +635,34 @@ describe('AssetsContractController', () => {
result: '0x',
},
},
{
request: {
method: 'eth_call',
params: [
{
to: '0x0000000000000000000000000000000000000000',
data: '0xc87b56dd0000000000000000000000000000000000000000000000000000000000000000',
},
'latest',
],
},
response: {
result:
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002468747470733a2f2f6170692e676f6473756e636861696e65642e636f6d2f636172642f3000000000000000000000000000000000000000000000000000000000',
},
},
],
});
const result = async () => {
await assetsContract.getERC721TokenURI(
'0x0000000000000000000000000000000000000000',
'0',
);
};
const uri = await assetsContract.getERC721TokenURI(
'0x0000000000000000000000000000000000000000',
'0',
);
expect(uri).toBe('https://api.godsunchained.com/card/0');
expect(errorLogSpy).toHaveBeenCalledTimes(1);
expect(errorLogSpy.mock.calls).toContainEqual([
'Contract does not support ERC721 metadata interface.',
]);

sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
const error = 'Contract does not support ERC721 metadata interface.';
await expect(result).rejects.toThrow(error);
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('AssetsContractController with NetworkClientId', () => {
messenger.clearEventSubscriptions('NetworkController:stateChange');
});

it('should throw an error when address given is not an ERC-721 NFT', async () => {
it('should not throw an error when address given is does not support NFT Metadata interface', async () => {
const { assetsContract, messenger, networkClientConfiguration } =
await setupAssetContractControllers();
mockNetworkWithDefaultChainId({
Expand All @@ -441,18 +441,39 @@ describe('AssetsContractController with NetworkClientId', () => {
result: '0x',
},
},
{
request: {
method: 'eth_call',
params: [
{
to: '0x0000000000000000000000000000000000000000',
data: '0xc87b56dd0000000000000000000000000000000000000000000000000000000000000000',
},
'latest',
],
},
response: {
result:
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002468747470733a2f2f6170692e676f6473756e636861696e65642e636f6d2f636172642f3000000000000000000000000000000000000000000000000000000000',
},
},
],
});
const result = async () => {
await assetsContract.getERC721TokenURI(
'0x0000000000000000000000000000000000000000',
'0',
'mainnet',
);
};

const error = 'Contract does not support ERC721 metadata interface.';
await expect(result).rejects.toThrow(error);
const errorLogSpy = jest
.spyOn(console, 'error')
.mockImplementationOnce(() => {
/**/
});
const uri = await assetsContract.getERC721TokenURI(
'0x0000000000000000000000000000000000000000',
'0',
'mainnet',
);
expect(uri).toBe('https://api.godsunchained.com/card/0');
expect(errorLogSpy).toHaveBeenCalledTimes(1);
expect(errorLogSpy.mock.calls).toContainEqual([
'Contract does not support ERC721 metadata interface.',
]);
messenger.clearEventSubscriptions('NetworkController:stateChange');
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export class ERC721Standard {
address,
);
if (!supportsMetadata) {
throw new Error('Contract does not support ERC721 metadata interface.');
// Do not throw error here, supporting Metadata interface is optional even though majority of ERC721 nfts do support it.
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
// This change is made because of instances of NFTs that are ERC404( mixed ERC20 / ERC721 implementation).
// As of today, ERC404 is unofficial but some people use it, the contract does not support Metadata interface, but it has the tokenURI() fct.
console.error('Contract does not support ERC721 metadata interface.');
}
return contract.tokenURI(tokenId);
Copy link
Contributor Author

@sahar-fehri sahar-fehri Apr 8, 2024

Choose a reason for hiding this comment

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

In cases where contract is ERC721 and does not have fct tokenURI, we are already catching the error here

, the user will be able to add the nft, but the image wont be displayed.

};
Expand Down
Loading