美文网首页
ether.js 重载方法

ether.js 重载方法

作者: YANG_ad29 | 来源:发表于2022-05-30 18:11 被阅读0次

用ether.js 调用 safeTransferFrom 方法时总是报 safeTransferFrom is not a function 错误


    function safeTransferFrom(
        address from,
        address to,
        uint256 id
    ) public virtual {
        transferFrom(from, to, id);

        require(
            to.code.length == 0 ||
                ERC721TokenReceiver(to).onERC721Received(
                    msg.sender,
                    from,
                    id,
                    ""
                ) ==
                ERC721TokenReceiver.onERC721Received.selector,
            "UNSAFE_RECIPIENT"
        );
    }

 function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        bytes calldata data
    ) public virtual {
        transferFrom(from, to, id);

        require(
            to.code.length == 0 ||
                ERC721TokenReceiver(to).onERC721Received(
                    msg.sender,
                    from,
                    id,
                    data
                ) ==
                ERC721TokenReceiver.onERC721Received.selector,
            "UNSAFE_RECIPIENT"
        );
    }

在ERC721中 safeTransferFrom 是重载方法 单独根据方法名去找 就会找到两个 。
也就是contract.safeTransferFrom(…) 这种写法是有问题的。
错误写法

contract.safeTransferFrom(addr1, addr2, 1);

正确写法

contract["safeTransferFrom(address,address,uint256)"](addr1, addr2, 1);

参考https://stackoverflow.com/questions/68289806/no-safetransferfrom-function-in-ethers-js-contract-instance

相关文章

网友评论

      本文标题:ether.js 重载方法

      本文链接:https://www.haomeiwen.com/subject/eiooprtx.html