0

I am trying to call a simple approve() to allow my contract to spend my tokens. Trying to make the amount argument = (2**256 - 1) however I am getting the below error message every time.

Unhandled Rejection (Error): invalid number value (arg="amount", coderType="uint256", value="1.157920892373162e+77") ▶ 11 stack frames were collapsed.

I have tried putting the number in hex, I have tried toString() also, but I get the same error every time.

The below code is called when a button in my front-end is pressed.

approveSpend = async() => {
    const maxSpend = (2**256-1);
    const forwardAuctionAddress = ForwardAuction.networks[this.networkId].address;
    await this.tokenInstance.methods.approve(forwardAuctionAddress, maxSpend.toString()).send({from: this.accounts[0]});
    
  }

Can anyone please help

Thanks!

1

JavaScript can't handle big numbers natively. That's where various BigNumber libraries come to play.

To be consistent, it's best to always use BigNumber when dealing with integers in web3 projects. There are tons of different libraries out there, but I recommend using the one which your web3 library uses under the hood - you'll probably get utils for it directly from your web3 library.

3
  • Hi, would you please be able to help me with how to do that? I have tried so many different things, different packages, everything and I just cant get it to work. Sep 13 at 10:33
  • I figured it out. I was trying to have const maxInt = new BN(2^256 - 1); You need to make each number a new BN and perform the arithemetic to make it work. Also the numbers have to be in quotations I think. const maxInt= new BN("2").pow(new BN("256").sub(new BN("1"))); // Will pass into solidity as uint 2**256 - 1 Sep 13 at 10:56
0

I figured it out. I was trying to have

const maxInt = new BN(2^256 - 1);

You need to make each number a new BN and perform the arithemetic to make it work. Also the numbers have to be in quotations I think.

const maxInt= new BN("2").pow(new BN("256").sub(new BN("1"))); // Will pass into solidity as uint 2**256 - 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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