Skip to content

Commit ca45aa6

Browse files
fix(bridge-ui): fix ERC721 and ERC1155 detection in NFT bridge (#16680)
1 parent 2c63cb0 commit ca45aa6

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

packages/bridge-ui/src/libs/token/detectContractType.test.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@ describe('detectContractType', () => {
1313
// Given
1414
const contractAddress = zeroAddress;
1515
const chainId = 1;
16-
vi.mocked(readContract).mockImplementationOnce(() => Promise.resolve());
17-
18-
// When
19-
const result = await detectContractType(contractAddress, chainId);
20-
21-
// Then
22-
expect(result).toBe(TokenType.ERC721);
23-
});
24-
25-
it('should return ERC721 for a valid ERC721 contract with invalid Token ID', async () => {
26-
// Given
27-
const contractAddress = zeroAddress;
28-
const chainId = 1;
29-
vi.mocked(readContract).mockImplementationOnce(() => Promise.reject(new Error('ERC721: invalid token ID')));
16+
vi.mocked(readContract).mockImplementationOnce(() => Promise.resolve(true));
3017

3118
// When
3219
const result = await detectContractType(contractAddress, chainId);
@@ -40,8 +27,8 @@ describe('detectContractType', () => {
4027
const contractAddress = zeroAddress;
4128
const chainId = 1;
4229
vi.mocked(readContract)
43-
.mockImplementationOnce(() => Promise.reject())
44-
.mockImplementationOnce(() => Promise.resolve());
30+
.mockImplementationOnce(() => Promise.reject(false))
31+
.mockImplementationOnce(() => Promise.resolve(true));
4532

4633
// When
4734
const result = await detectContractType(contractAddress, chainId);
@@ -55,8 +42,8 @@ describe('detectContractType', () => {
5542
const contractAddress = zeroAddress;
5643
const chainId = 1;
5744
vi.mocked(readContract)
58-
.mockImplementationOnce(() => Promise.reject())
59-
.mockImplementationOnce(() => Promise.reject())
45+
.mockImplementationOnce(() => Promise.reject(new Error()))
46+
.mockImplementationOnce(() => Promise.reject(new Error()))
6047
.mockImplementationOnce(() => Promise.resolve());
6148

6249
// When

packages/bridge-ui/src/libs/token/detectContractType.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,28 @@ const log = getLogger('detectContractType');
1313

1414
async function isERC721(address: Address, chainId: number): Promise<boolean> {
1515
try {
16-
await readContract(config, {
16+
return await readContract(config, {
1717
address,
1818
abi: erc721Abi,
19-
functionName: 'ownerOf',
20-
args: [0n],
19+
functionName: 'supportsInterface',
20+
args: ['0x80ac58cd'], // Identifier for ERC-721
2121
chainId,
2222
});
23-
return true;
24-
} catch (err) {
25-
// we expect this error to be thrown if the token is a ERC721 and the tokenId is invalid
26-
return (err as Error)?.message?.includes('ERC721: invalid token ID') ?? false;
23+
} catch {
24+
return false;
2725
}
2826
}
2927
// return err instanceof ContractFunctionExecutionError &&
3028
// err.cause.message.includes('ERC721: invalid token ID');
3129
async function isERC1155(address: Address, chainId: number): Promise<boolean> {
3230
try {
33-
await readContract(config, {
31+
return await readContract(config, {
3432
address,
3533
abi: erc1155Abi,
36-
functionName: 'isApprovedForAll',
37-
args: ['0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000'],
34+
functionName: 'supportsInterface',
35+
args: ['0xd9b67a26'], // Identifier for ERC-1155
3836
chainId,
3937
});
40-
return true;
4138
} catch {
4239
return false;
4340
}
@@ -52,6 +49,7 @@ async function isERC20(address: Address, chainId: number): Promise<boolean> {
5249
args: ['0x0000000000000000000000000000000000000000'],
5350
chainId,
5451
});
52+
5553
return true;
5654
} catch {
5755
return false;

0 commit comments

Comments
 (0)