检查合约地址是否为代币(获取符号等代币信息)

时间:2021-05-08 17:56:18

标签: ethereum web3

在提供合约地址时,是否可以通过 web3 获取诸如符号和总供应量之类的令牌信息,例如 etherscan api pro 端点令牌信息?

我想检查我收集的地址是代币还是普通合约。

问候

1 个答案:

答案 0 :(得分:0)

您可以在加载合约数据时将通用令牌(ERC-20、ERC-721、...)ABI JSON 接口传递到您的 web3 实例中。然后你可以尝试调用这些函数,看看它们是否返回值或失败。

https://web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html

const contract = new Contract(jsonInterface, address);

try {
    const symbol = await contract.methods.symbol().call();
} catch (e) {
    // does not have the `symbol` function or public property
}

请注意,某些代币标准(例如 ERC-721)不要求合约包含 namesymbol 字段。