0

This is my code.

const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin
const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]);

const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS
console.log(`
~~~~~~~~~~~~~~~~~~~~
Buying new token
~~~~~~~~~~~~~~~~~~~~
buyToken: ${amountIn.toString()} ${buyToken} (WBNB)
sellToken: ${amountOutMin.toString()} ${sellToken}
`);
const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [buyToken, sellToken],
    addresses.me,
    Date.now() + 1000 * 60 * 5 //5 minutes
);
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);

But there's nowhere that gas is added to this. I only knew how to send signed transactions but now I find the swapExactTokensForTokens function and I really can't work out how to send gas when using it.

1 Answer 1

0

I guess you're using Ethers.js, so you can use overrides, contract.METHOD_NAME( ...args [ , overrides ] );

in your case:

const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [buyToken, sellToken],
    addresses.me,
    Date.now() + 1000 * 60 * 5, //5 minutes
    {
       gasPrice: 1 //(wei)
    }
);

You can use it for:

  • overrides.from - the msg.sender (or CALLER) to use during the execution of the code
  • overrides.value - the msg.value (or CALLVALUE) to use during the execution of the code overrides.
  • overrides.gasPrice , etc...

More info here https://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall

Your Answer

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

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