I'm trying to pass the following number to the approve()
function of a standard ERC20
Token contract:
2^256-1
I read that this should be done using BigNumber
, using the following code:
const maxInt = new BN("2").pow(new BN("256").sub(new BN("1")));
// Will pass into solidity as uint 2**256 - 1
Well I've gotten ALL SORTS of errors using that code - for starters it looks like the syntax and commands for BigNumber
are quite different depending on the version of ethers
you happen to be using - and I happen to be on V. 4.0 - and would very much prefer to stay on it right now to avoid other potential headaches.
So I've tried all sorts of variations on the above code - here's one where I broke down each component of that formula into it's own individual piece - just to see what's up:
// ==>Using 'bigNumberify'
let bigNum2 = ethers.utils.bigNumberify("2");
console.log("'bigNum2' -> '2' = ", bigNum2);
let bigNum256 = ethers.utils.bigNumberify("256");
console.log("'bigNum256' -> '256' = ", bigNum256);
let bigNum2power256 = ethers.utils.bigNumberify(bigNum2.pow(bigNum256));
console.log("'bigNum2power256' -> 2^256= ", bigNum2power256);
let bigNum1 = ethers.utils.bigNumberify("1");
console.log("'bigNum1' -> '1' = ", bigNum1);
let finalNumber = ethers.utils.bigNumberify(bigNum2power256 - bigNum1);
console.log("'finalNumber' = ", finalNumber);
Well when I pass this finalNumber
into approve()
I get this error:
Uncaught (in promise) Error: underflow (operation="setValue", fault="underflow", value=1.157920892373162e+77, outputValue=1, version=4.0.45)
I've tried all sorts of variations on this code - like replacing this line:
let bigNum2power256 = ethers.utils.bigNumberify(bigNum2.pow(bigNum256));
with:
let bigNum2power256 = bigNum1.pow(bigNum2);
but that doesn't work either.
Every variation I tried - and there've been lots - seems to give me yet another sort of error - why is this is difficult? What's the right way to do this?
=========================
UPDATE:
To be clear, the value of 2^256-1
- which I'm storing in my bigNum2power256
variable, ultimately needs to be passed-in as the 2nd argument of the following contract function call:
tokenContractInstance.approve(contractAddress, bigNum2power256, { gasLimit: 500_000 }).then((txResult) => {
console.log("Back from 'tokenContractInstance' 'approve'");
console.log(">Here's the 'approve' txResult = \n", txResult);
.....
}
MaxUint256
from@ethersproject/constants
@ethersproject/constants
seems like a node module forSolidity
, not for Javascript. (I think.) So I don't understand how exactly I'm supposed to import it into my JS code, or how I'm supposed to then use thisMaxUint256
in my JS code. Please look at the update. Thank you.@ethersproject/constants
orMaxUint256
- and how to use them in my javascript code to pass the value of 2^256-1 into my smart-contract