Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniswap V2 invalid BigNumber value for JSBI negative value

Hi I was following uniswapV2 document to perform a trade transaction and I encounter error as follow invalid bignumber value

I got my input amount as 2941991120 and in JSBI form it is -1352976176, which gave me invalid bignumber value bug. Here is my code code screenshot. But I was following exactly what the tutorial says https://uniswap.org/docs/v2/javascript-SDK/trading/

Can anyone tell me where I did wrong ?

like image 203
Poutine Avatar asked Sep 07 '25 23:09

Poutine


2 Answers

The example tells you the value should be converted to hex:

const value = trade.inputAmount.raw // // needs to be converted to e.g. hex

Same for one of the other values. Have you tried this?

If you use a (signed) integer, its sign can be positive/negative (+/-). Whatever value you're sending is deemed to be a negative one, which is unexpected and so the response is telling you.

This exmaple seems to suggest you can do: https://ethereum.stackexchange.com/questions/87983/failed-transaction-error-encountered-during-contract-execution-on-uniswap-rout

...
const amountOutMinHex = ethers.BigNumber.from(amountOutMin.toString()).toHexString();
...
like image 152
Rob Evans Avatar answered Sep 10 '25 07:09

Rob Evans


The accepted answer is unnecessarily complex. The JSBI::toString() method takes a radix parameter, so your linked example would simply look like:

trade.minimumAmountOut(slippageTolerance).raw.toString(16);
like image 31
Joe Coder Avatar answered Sep 10 '25 09:09

Joe Coder