1
wallet = ethers.Wallet.fromMnemonic(mnemonic)
// console.log(wallet.address)
const signer = wallet.connect(provider);
// console.log(signer)

const routerInstance = new ethers.Contract(routerAddress, routerABI, signer);
const token1 = new ethers.Contract(fromAddress, erc20ABI, provider);
const token2 = new ethers.Contract(toAddress, erc20ABI, provider);





//--------------------------Input variabals--------------------------//




const gasLimit = 20000




//--------------------------Token Swap Function--------------------------//




const TokenSwap = async(amount, fromAddress, toAddress) => {

    const decimal1 = await token1.decimals()
    const decimal2 = await token2.decimals()

    // const amountIn = ethers.utils.parseUnits(amount, decimal1).toString();
    // const amountIn = ethers.BigNumber.from(amount, decimal1).toString();
    const amountIn = ethers.utils.parseEther(amount).toString();
    const amountOut = await routerInstance.getAmountsOut(amountIn, [fromAddress, toAddress])
    console.log(amountIn)

    const output = ethers.utils.formatUnits(amountOut[1].toString(), decimal2)
    console.log(output)
    console.log(Date.now() + 1000 * 60 * 10)
    const tx = await routerInstance.swapTokensForExactTokens(
        amountIn,
        amountOut,
        [fromAddress, toAddress],
        wallet.address,
        Date.now() + 1000 * 60 * 5,
    )
}

amount = "0.000000000000002";
TokenSwap(amount, fromAddress, toAddress);


                ^

Here is my error:

D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\logger\lib\index.js:238
        var error = new Error(message);
    Error: invalid BigNumber value (argument="value", value=[{"type":"BigNumber","hex":"0x07d0"},{"type":"BigNumber","hex":"0x00"}], code=INVALID_ARGUMENT, version=bignumber/5.7.0)
        at Logger.makeError (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\logger\lib\index.js:238:21)
        at Logger.throwError (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\logger\lib\index.js:247:20)
        at Logger.throwArgumentError (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\logger\lib\index.js:250:21)
        at BigNumber.from (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\bignumber\lib\bignumber.js:239:23)
        at NumberCoder.encode (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\abi\lib\coders\number.js:36:39)
        at D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\abi\lib\coders\array.js:74:19
        at Array.forEach (<anonymous>)
        at pack (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\abi\lib\coders\array.js:60:12)
        at TupleCoder.encode (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\abi\lib\coders\tuple.js:71:33)
        at AbiCoder.encode (D:\blockchain\Volume Bot -ruv\node_modules\@ethersproject\abi\lib\abi-coder.js:91:15) {   reason: 'invalid BigNumber value',   code: 'INVALID_ARGUMENT',   argument: 'value',   value: [
        BigNumber { _hex: '0x07d0', _isBigNumber: true },
        BigNumber { _hex: '0x00', _isBigNumber: true }   ] }
    
    Node.js v20.9.0
1
  • Please click the Accept tick ✅ if my answer helped you.
    – Mila A
    Commented Feb 13 at 13:07

1 Answer 1

1

Decoding your error shows that 0x07d0 corresponds to decimal 2000.

And 0x00 corresponds to decimal 0.

Which means that the problem is with one of your amountIn or amountOut parameters.

Considering amountIn is converted to string on this line (const amountIn = ethers.utils.parseEther(amount).toString();), you should look into how you're pass amountOut in:

const amountOut = await routerInstance.getAmountsOut(amountIn, [fromAddress, toAddress])

amountOut is actually an array here, you can look that up in the UniswapRouter02.sol ABI, or the function here:

function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts);

So in your ethers.js project you should pick a specific slot from the return value and then convert it to string, because all BigNumber values should be converted to string before used in ethers.js contracts.

Like this:

const tx = await routerInstance.swapTokensForExactTokens(
        amountIn,
        amountOut[0].toString(),
        [fromAddress, toAddress],
        wallet.address,
        Date.now() + 1000 * 60 * 5,
    );
6
  • 1
    Yay! It solved my problem. I got this error reverting my transaction "Fail with error 'ds-math-sub-underflow'" Commented Feb 11 at 11:26
  • 1
    Great that this solved the problem! Regarding the new error, please see this for details about the ds-math-sub-underflow exception: ethereum.stackexchange.com/questions/97032/…. Answer by Turbut Alin: "This also happens if the Uniswap token related to the pool has not been approved for the wallet sending the transaction. Solution: Call the approve method of the new Pair contract with spender as the Uniswap router address" That is likely the culprit in your case
    – Mila A
    Commented Feb 11 at 12:39
  • You should give the routerInstance the approval to take your tokens.
    – Mila A
    Commented Feb 11 at 12:40
  • Please accept my answer if it helped you. :)
    – Mila A
    Commented Feb 11 at 12:40
  • 1
    Thank You it has helped me :) Commented Feb 11 at 14:11

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.