【问题标题】:Why does this Solidity function return a 0 after assert?为什么这个 Solidity 函数在断言后返回 0?
【发布时间】:2018-09-24 21:58:09
【问题描述】:

我写了这个函数:

// Function to get an owned token's id by referencing the index of the user's owned tokens.
// ex: user has 5 tokens, tokenOfOwnerByIndex(owner,3) will give the id of the 4th token.
function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint _tokenId) {
    // TODO: Make sure this works. Does not appear to throw when _index<balanceOf(_owner), which violates
    //       ERC721 compatibility.
    assert(_index<balanceOf(_owner)); // throw if outside range
    return ownedTokenIds[_owner][_index];
}

当 _index 为 2 且 _owner 使 balanceOf(_owner) 为 0 时,该函数在 Remix IDE 中返回 0。我的假设是它不会返回任何东西。我的问题是:

A) 为什么断言失败后返回 0?

B) 当我使用上述参数运行它时,如何让它不返回 0?

谢谢, 沃恩

【问题讨论】:

    标签: ethereum solidity remix


    【解决方案1】:

    删除我的另一个答案,因为它不正确。

    view 函数中的错误处理条件不会冒泡到客户端。它们仅用于恢复区块链上的状态更改交易。对于 view 函数,处理将停止并返回指定返回类型的初始 0 值(uint 为 0,bool 为 false 等)。

    因此,view 函数的错误处理必须在客户端进行。如果您需要能够区分有效的 0 返回值和错误,您可以执行以下操作:

    function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint, bool) {
        bool success = _index < balanceOf(_owner);
        return (ownedTokenIds[_owner][_index], success);
    }
    

    【讨论】:

    • 如果您使用 tokenOfOwnerByIndex() 的自定义定义,那么它将与已发布的 ERC-721 标准不兼容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2018-08-27
    • 2015-09-12
    • 2016-03-21
    • 2018-01-28
    • 1970-01-01
    相关资源
    最近更新 更多