在 tuple() 中进行参数相关查找后,成员 "sub" 未找到或不可见
Member "sub" not found or not visible after argument-dependent lookup in tuple()
我正在尝试将函数转换为 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;
}
.sub()
在这种情况下几乎可以肯定是 SafeMath 库的一个函数。
在 Solidity 0.8+ 中,您不再需要使用 SafeMath,因为整数 underflow/overflow 检查是在较低级别执行的。
所以你可以安全地替换
uint256 newViperId = vipers.push(newViper).sub(1);
和
vipers.push(newViper);
uint256 newViperId = vipers.length - 1;
如果您正在处理具有不确定值的可能更大的数字,并且您实际上应该利用 SafeMath 检查,
可以直接导入:
import './libraries/SafeMath.sol'; // check correct path
...
uint256 newViperId = SafeMath.sub(vipers.length, 1);
...
我正在尝试将函数转换为 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;
}
.sub()
在这种情况下几乎可以肯定是 SafeMath 库的一个函数。
在 Solidity 0.8+ 中,您不再需要使用 SafeMath,因为整数 underflow/overflow 检查是在较低级别执行的。
所以你可以安全地替换
uint256 newViperId = vipers.push(newViper).sub(1);
和
vipers.push(newViper);
uint256 newViperId = vipers.length - 1;
如果您正在处理具有不确定值的可能更大的数字,并且您实际上应该利用 SafeMath 检查, 可以直接导入:
import './libraries/SafeMath.sol'; // check correct path
...
uint256 newViperId = SafeMath.sub(vipers.length, 1);
...