0

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);
     .....
}
5
  • You can just import MaxUint256 from @ethersproject/constants Commented Sep 10, 2022 at 20:53
  • OK I looked into this - and I'm not sure I follow. (I've made a small update to my original question - please have a look at the bottom.) But this @ethersproject/constants seems like a node module for Solidity, 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 this MaxUint256 in my JS code. Please look at the update. Thank you.
    – Mark55
    Commented Sep 10, 2022 at 22:46
  • @PaulRazvanBerg Can you please elaborate on your previous comment? I have no idea what I'm supposed to be doing with this @ethersproject/constants or MaxUint256 - and how to use them in my javascript code to pass the value of 2^256-1 into my smart-contract
    – Mark55
    Commented Sep 10, 2022 at 23:21
  • can you try it with smaller values. That might be a bug if your are using v5
    – Yilmaz
    Commented Sep 11, 2022 at 3:24
  • @Mark55 you might need to take a Node.js development course. Commented Sep 11, 2022 at 8:47

1 Answer 1

1

If you are using ethers.js, there is a constants module you can use directly: here

const maxNumber = ethers.constants.MaxUint256

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.