【问题标题】:How to check if transaction is NFT using Web3j?如何使用 Web3j 检查交易是否为 NFT?
【发布时间】:2021-11-30 14:16:43
【问题描述】:

我正在使用 Web3j 库来处理区块链。我想解决在特定交易期间转移了哪些代币。我已经尝试过的:

  • 使用名称supportsInterface拨打Function,查看是否支持NFT标准(ERC721、ERC1155等)。没有成功。
  • 尝试解码Transaction Logs,了解如何检索Token ID,但我无法处理此信息。

对此有何建议?

【问题讨论】:

    标签: java ethereum nft web3-java erc721


    【解决方案1】:

    您无法从事务哈希中获取使用的令牌:这是我的答案,看看如果您解码它可以从 trnasaction 哈希中获取哪些信息:How to know the cryptocurrency used in a transaction through the transaction hash?

    但是你可以知道使用的是哪个接口。

    supportsInterface 用于此目的:

    // I did not use web3j. I am not sure how you call methods in web3j
    const is721 = await contract.methods.supportsInterface('0x80ac58cd').call();
    if(is721) {
        return "ERC721";
    }
    

    为了调用它,你的合约必须继承自 ERC165。如果您使用的是 openzeppelin 的合同,它已经继承并且应该可以工作:

    contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {}
    

    但如果您要实现自己的 ERC721,则必须确保继承自 ERC165。 ERC165 用于检测智能合约实现了哪些接口。 ERC165只有一个函数用于检查合约签名是否满足指定的接口签名。

    interface ERC165 {
    /// @notice Query whether a contract implements some interface or
    not
    /// @param interfaceID Interface ID specified by ERC-165 standard
    /// @dev Interface ID is defined in ERC-165 standard.
    /// This function use GAS less than 30,000 gas.
    /// @return If contract implements the specified interface, then
    return
    /// `true`
    /// and `interfaceID` is not 0xffffffff, or return `false`
    function supportsInterface(bytes4 interfaceID) external view
    returns (bool);
    }
    

    【讨论】:

    • 谢谢 (+1)。比方说,我有一个交易对象或交易收据,我如何加载它的合约来检查它是否是 ERC165?对不起,如果这很明显,我是 web3 的新手。
    • @SashaShpota 看看这个:stackoverflow.com/questions/70935158/…
    • 谢谢,之前知道怎么做,但是忘记写答案了。基本上,检查它是否是 NFT。调用supportsInterface方法检查是否支持0x80ac58cd(ERC721)和0xd9b67a26(ERC1155)
    • @Yilmaz 感谢您指出链接。但是,我仍然不知道如何从交易哈希到获得合同。你能澄清一下吗?这可能很明显,但我是 web3 的新手,只是看不到任何链接。
    • @wollenS 你能发布你的解决方案吗?
    猜你喜欢
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2021-11-23
    • 2022-09-22
    • 2018-10-22
    • 2021-07-15
    相关资源
    最近更新 更多