'Member "sub" not found or not visible after argument-dependent lookup in tuple()
I'm trying to convert a function to solidity 0.8.0 but keep getting a type error any help hold be appreciated
TypeError: Member "sub" not found or not visible after argument-dependent lookup in tuple().
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;
}
Solution 1:[1]
.sub() in this context is almost certainly a function of the SafeMath library.
In Solidity 0.8+ you don't need to use SafeMath anymore, because the integer underflow/overflow check is performed on a lower level.
So you can safely replace
uint256 newViperId = vipers.push(newViper).sub(1);
with
vipers.push(newViper);
uint256 newViperId = vipers.length - 1;
Solution 2:[2]
In cases where you are dealing with potentially larger numbers with less certain values, and you actually should take advantage of the SafeMath checks, you can import it directly:
import './libraries/SafeMath.sol'; // check correct path
...
uint256 newViperId = SafeMath.sub(vipers.length, 1);
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Fred |
