Skip to content
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
21 changes: 21 additions & 0 deletions packages/web3-contracts/abis/ERC1155.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"inputs": [
{
"internalType": "bytes4",
"name": "interfaceId",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
]
1 change: 1 addition & 0 deletions packages/web3-shared/evm/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useERC20TokenContract'
export * from './useERC721TokenContract'
export * from './useERC721TokenContracts'
export * from './useERC1155TokenContract'
7 changes: 7 additions & 0 deletions packages/web3-shared/evm/contracts/useERC1155TokenContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ERC1155 from '@masknet/web3-contracts/abis/ERC1155.json'
import type { AbiItem } from 'web3-utils'
import { useContract } from '../hooks'

export function useERC1155TokenContract(address: string) {
return useContract(address, ERC1155 as AbiItem[])
}
2 changes: 2 additions & 0 deletions packages/web3-shared/evm/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ export * from './useImageChecker'
export * from './useTrustedERC20Tokens'
export * from './useTrustERC20TokenCallback'
export * from './useAddERC20TokenCallback'

export * from './useEthereumTokenType'
42 changes: 42 additions & 0 deletions packages/web3-shared/evm/hooks/useEthereumTokenType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useAsyncRetry } from 'react-use'
import { useERC1155TokenContract, useERC721TokenContract } from '../contracts'
import { useWeb3 } from './useWeb3'

import { useERC165 } from '.'
import { EthereumTokenType } from '../types'

const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63'
const ERC1155_ENUMERABLE_INTERFACE_ID = '0xd9b67a26'

function useCheckContract(address: string) {
const web3 = useWeb3()
return useAsyncRetry(async () => {
const result = await web3.eth.getCode(address)
return result !== '0x'
}, [address, web3])
}

function useCheckERC721(address: string) {
const erc721Contract = useERC721TokenContract(address)
return useERC165(erc721Contract, address, ERC721_ENUMERABLE_INTERFACE_ID)
}

function useCheckERC1155(address: string) {
const erc1155Contract = useERC1155TokenContract(address)
return useERC165(erc1155Contract, address, ERC1155_ENUMERABLE_INTERFACE_ID)
}

export function useEthereumTokenType(address: string): EthereumTokenType | undefined {
const { value: isContract, loading: loadingContract } = useCheckContract(address)
const { value: isERC721, loading: loadingERC721 } = useCheckERC721(address)
const { value: isERC1155, loading: loadingERC1155 } = useCheckERC1155(address)

if (loadingERC1155 || loadingERC721 || loadingContract) return
return isERC1155
? EthereumTokenType.ERC1155
: isERC721
? EthereumTokenType.ERC721
: isContract
? EthereumTokenType.ERC20
: undefined
}