【问题标题】:Member "sub" not found or not visible after argument-dependent lookup in tuple()在 tuple() 中进行参数相关查找后,成员“sub”未找到或不可见
【发布时间】:2021-06-11 12:46:15
【问题描述】:

我正在尝试将函数转换为solidity 0.8.0,但不断收到类型错误,感谢任何帮助

TypeError:在 tuple() 中进行参数相关查找后,成员“sub”未找到或不可见。

 function createViper(
    uint256 matron,
    uint256 sire,
    address viperOwner
)
    internal
    returns (uint)
{
    require(viperOwner != address(0));
    uint8 newGenes = generateViperGenes(matron, sire);
    Viper memory newViper = Viper({
        genes: newGenes,
        matronId: matron,
        sireId: sire
    });
    uint256 newViperId = vipers.push(newViper).sub(1);
    super._mint(viperOwner, newViperId);
    emit Birth(
        viperOwner,
        newViperId,
        newViper.matronId,
        newViper.sireId,
        newViper.genes
    );
    return newViperId;
}

【问题讨论】:

    标签: types solidity


    【解决方案1】:

    .sub() 在这种情况下几乎肯定是SafeMath 库的一个函数。

    在 Solidity 0.8+ 中,您不再需要使用 SafeMath,因为整数下溢/上溢检查是在较低级别执行的。

    所以你可以放心更换

    uint256 newViperId = vipers.push(newViper).sub(1);
    

    vipers.push(newViper);
    uint256 newViperId = vipers.length - 1;
    

    【讨论】:

    • 嗨,我已经尝试过了,但出现了 2 个错误 - TypeError: Operator - 与类型 tuple() 和 int_const 1 和 TypeError 不兼容:左侧 (1) 上的组件数量不同右手边 (0)
    • @konnection 我现在看到push() 不会在新版本中返回值,这是我的错误。更新了答案以反映这一事实。
    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2021-06-22
    • 2019-04-29
    • 2021-12-29
    • 2021-08-18
    • 2021-02-18
    相关资源
    最近更新 更多