如何测试合约接收 ERC721 代币的能力?

时间:2021-07-07 16:21:23

标签: javascript mocha.js ethereum solidity truffle

我正在 Truffle 中测试我的合同。我启用了合约以接收 ERC721 代币:

chrome is not defined

有没有办法使用 Mocha 和 Chai 模拟将令牌发送到此合约?

1 个答案:

答案 0 :(得分:1)

在 EVM 之外(例如在 JS 测试中),无法检查交易的返回值。只有它的状态(成功/恢复)、发出的事件(在你的情况下是非)和其他一些元数据。您还可以检查调用的返回值,如 assert.equal 语句。

contract('MyContract', () => {
    it('receives a token', async () => {
        const tx = await myContract.onERC721Received(
            '0x123',      // address
            '0x456',      // address _from
            1,            // uint256 _tokenId
            [0x01, 0x02]  // bytes calldata
        );

        assert.equal(tx.receipt.status, true); // tx succeeded

        assert.equal(await contract.nftContract, '0x123');
        assert.equal((await contract.tokenId).toNumber(), 1);
        assert.equal(await contract.tokenAdded, true);
    });
});

文档:

  • 松露 contract 而不是摩卡 describe - docs
  • 收据状态 - Truffle docs、Web3 docs(Truffle 文档中指向 Web3 的链接已过时)
  • 不必在 Truffle 中使用 .send().call(),因为它会自动从合约 ABI 中选择 tx 或调用 - docs