I'm trying to swap some wftm tokens for boo tokens on spookyswap. I have 1.11 worth of wftms. I've done it manually with 0.01 worth on the spookyswap website so i don't understand why this code won't work. I've tried varying the gas and tokenamountout value from 0 to 10000000000 with no success? The transaction always comes back as callback transaction fail and the value in the error stack is usually something like value: bignumber (0x00) no matter what value i enter to buy, however i can always see the gas price changing in the error stack.

const ethers = require('ethers');
const Web3 = require('web3');
const Web3HttpProvider = require('web3-providers-http');
const http = require('http');

const prompt = require("prompt-sync")();

const recipient = prompt("Enter your wallet address: ");
const private = prompt("Enter your wallet private key: ");
const buy = prompt("Enter the token address to buy: ");
var amount_in = prompt("Enter the amount to buy in ftm: ");

const addresses = {
 Wftm: '0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83',
 factory: '0x152eE697f2E276fA89E96742e9bB9aB1F2E61bE3',
  router: '0xF491e7B69E4244ad4002BC14e878a34207E38c29',
  //recipient: 'recipient of the profit here'
 const provider = new ethers.providers.WebSocketProvider('');////
 const wallet = new ethers.Wallet(private, provider);
 const factory = new ethers.Contract(
  ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
  const router = new ethers.Contract(
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)',
'function swapExactTokensForETH(uint256 amountIn,uint256 amountOutMin,address[] path,address to,uint256 deadline,bool ifmint )',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'

    const wftm = new ethers.Contract(
   'function approve(address spender, uint amount) public returns(bool)',
   'function deposit() external payable'
const init = async () => {
const tx = await wftm.approve(
  const receipt = await tx.wait(); 
   console.log('Approve Transaction receipt');

 //buy function
async function main() {
tokenAmountOut = ethers.utils.parseUnits('0.01', 'ether');
const amounts = await router.getAmountsOut(tokenAmountOut, [addresses.Wftm, buy]);
const amountOutMin = amounts[1].sub(amounts[1].div(10));
const amount_out_min_hex = ethers.BigNumber.from(amountOutMin.toString()).toHexString();
console.log('This is amounts',amounts);
console.log('This is amountoutmin',amount_out_min_hex);
const price = ethers.utils.formatUnits(await provider.getGasPrice(), 'gwei')
const pgwei = ethers.utils.parseUnits(price, 'gwei')

const swapTx = await router.swapExactETHForTokens(
    [addresses.Wftm, buy],
    Math.round(Date.now() / 1000) + 60 * 10,
    {gasLimit: 100000, gasPrice: 10000000000}

receipt = await swapTx.wait();
console.log('Buy Transaction receipt');

  main().then().finally(() => {});enter code here

This is the most recent error stack

enter image description here enter image description here

1 Answer 1


I've done it. I changed the swapExactETHForTokens to swapExactTokenssForTokens along with replacing this:

const amountOutMin = amounts[1].sub(amounts[1].div(10));

to this:

 const amountOutMin = ethers.utils.parseUnits(Number((ethers.utils.formatUnits(amounts[1],18)) - 0.1).toString(),18);

Now i wonder how to wrap ftm, so that i don't have to do it manually?

  • wrappFTM contract should have a functions wrap/unwrap
    – gazoblock
    Commented Jul 11, 2022 at 13:19
  • @gazoblock i tried implementing this, but the deposit function has no arguments, so you can't tell it how much to deposit. Then i also don't know how to call it from ethers.js. I've tried with await wrapper.connect(wallet).deposit(); but i'm not sure its correct. Wrapper is the contract instance i created from the wftm address.
    – pkjag
    Commented Jul 11, 2022 at 13:25
  • 1
    Shure From WFTM contract code: deposit wraps received FTM tokens as wFTM in 1:1 ratio by minting. the received amount of FTMs in wFTM on the sender's address. You need call function Deposit without arguments, but with value property with desired amount of FTM, and you will receive 1:1 WFTM. await WFTM.deposit({value: ethers.utils.parseEther("1.0")})
    – gazoblock
    Commented Jul 12, 2022 at 8:00

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.