ether.js 重载方法

2022-05-30  本文已影响0人  YANG_ad29

用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

上一篇 下一篇

猜你喜欢

热点阅读