Skip to content

Commit

Permalink
fix(bridge-ui): fix ERC721 and ERC1155 detection in NFT bridge (#16680)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricecodekhmer authored Apr 9, 2024
1 parent 2c63cb0 commit ca45aa6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
23 changes: 5 additions & 18 deletions packages/bridge-ui/src/libs/token/detectContractType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,7 @@ describe('detectContractType', () => {
// Given
const contractAddress = zeroAddress;
const chainId = 1;
vi.mocked(readContract).mockImplementationOnce(() => Promise.resolve());

// When
const result = await detectContractType(contractAddress, chainId);

// Then
expect(result).toBe(TokenType.ERC721);
});

it('should return ERC721 for a valid ERC721 contract with invalid Token ID', async () => {
// Given
const contractAddress = zeroAddress;
const chainId = 1;
vi.mocked(readContract).mockImplementationOnce(() => Promise.reject(new Error('ERC721: invalid token ID')));
vi.mocked(readContract).mockImplementationOnce(() => Promise.resolve(true));

// When
const result = await detectContractType(contractAddress, chainId);
Expand All @@ -40,8 +27,8 @@ describe('detectContractType', () => {
const contractAddress = zeroAddress;
const chainId = 1;
vi.mocked(readContract)
.mockImplementationOnce(() => Promise.reject())
.mockImplementationOnce(() => Promise.resolve());
.mockImplementationOnce(() => Promise.reject(false))
.mockImplementationOnce(() => Promise.resolve(true));

// When
const result = await detectContractType(contractAddress, chainId);
Expand All @@ -55,8 +42,8 @@ describe('detectContractType', () => {
const contractAddress = zeroAddress;
const chainId = 1;
vi.mocked(readContract)
.mockImplementationOnce(() => Promise.reject())
.mockImplementationOnce(() => Promise.reject())
.mockImplementationOnce(() => Promise.reject(new Error()))
.mockImplementationOnce(() => Promise.reject(new Error()))
.mockImplementationOnce(() => Promise.resolve());

// When
Expand Down
20 changes: 9 additions & 11 deletions packages/bridge-ui/src/libs/token/detectContractType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,28 @@ const log = getLogger('detectContractType');

async function isERC721(address: Address, chainId: number): Promise<boolean> {
try {
await readContract(config, {
return await readContract(config, {
address,
abi: erc721Abi,
functionName: 'ownerOf',
args: [0n],
functionName: 'supportsInterface',
args: ['0x80ac58cd'], // Identifier for ERC-721
chainId,
});
return true;
} catch (err) {
// we expect this error to be thrown if the token is a ERC721 and the tokenId is invalid
return (err as Error)?.message?.includes('ERC721: invalid token ID') ?? false;
} catch {
return false;
}
}
// return err instanceof ContractFunctionExecutionError &&
// err.cause.message.includes('ERC721: invalid token ID');
async function isERC1155(address: Address, chainId: number): Promise<boolean> {
try {
await readContract(config, {
return await readContract(config, {
address,
abi: erc1155Abi,
functionName: 'isApprovedForAll',
args: ['0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000'],
functionName: 'supportsInterface',
args: ['0xd9b67a26'], // Identifier for ERC-1155
chainId,
});
return true;
} catch {
return false;
}
Expand All @@ -52,6 +49,7 @@ async function isERC20(address: Address, chainId: number): Promise<boolean> {
args: ['0x0000000000000000000000000000000000000000'],
chainId,
});

return true;
} catch {
return false;
Expand Down

0 comments on commit ca45aa6

Please sign in to comment.