1

I am using ethers.js, hardhat, and pancakeswaps router to run getAmountsOut(), but I am getting an error.

The error I get in the terminal is...

Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="getAmountsOut(uint256,address[])", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.7.0)

The error I get in the hardhat node is...

Contract call: <UnrecognizedContract>
From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
To: 0xeff92a263d31888d860bd50809a8d171709b7b1c

Error: Transaction reverted without a reason string
at <UnrecognizedContract>.<unknown> (0xeff92a263d31888d860bd50809a8d171709b7b1c)

This is the code I am currently working with...

const fetch = require('node-fetch');
const hre = require('hardhat');
const { ethers } = require("ethers");
const { ChainId, Fetcher} = require('@uniswap/sdk');


const WETH =  "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";     //weth address
const router =  "0xEfF92A263d31888d860bD50809A8D171709b7b1c";   //uniswap v2 router address


const provider = new hre.ethers.providers.WebSocketProvider("ws://127.0.0.1:8545/");


const ME = "";    //pulls wallet from private key
const wallet = new hre.ethers.Wallet("");
const signer = wallet.connect(provider);

const routerContract = new hre.ethers.Contract(
    router,
    [
        'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
        'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
        'function swapExactTokensForETHSupportingFeeOnTransferTokens(uint amountIn,uint amountOutMin,address[] calldata path,address to,uint deadline) external'
     ],
    signer
);



//inputs: token address as a string and buy amount in ETH(1 = 1 ETH)
 async function purchaseToken(token_address,buyAmount) {


    const ETHAmountIn = hre.ethers.utils.parseUnits(buyAmount.toString(), 'ether');


    let amounts = await routerContract.getAmountsOut(ETHAmountIn, [WETH, token_address]);


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

    
    const swapTx = await routerContract.swapExactETHForTokens(
        amountOutMin,
        [WETH, token_address],
        ME,
        Date.now() + 1000 * 60 * 10,
        {'value': ETHAmountIn, 'gasLimit': 900000, 'gasPrice': '0x22ECB25C00'}
    )


    let receipt = await swapTx.wait();
    console.log(receipt);
    
    return true;
}

I have tried the same code with uniswap v2 router but when I swap it with pancakes router I get these errors. Does anyone know what im doing wrong?

2
  • Do not share your Private key! I suggest you to move your funds to another wallet
    – Adam Boudj
    Commented Apr 7, 2023 at 13:32
  • The Private key is one of hardhat's test net wallet Commented Apr 7, 2023 at 15:59

1 Answer 1

0

The issue you're facing might be due to two reasons:

  • Your provider is pointing to a local network:

const provider = new hre.ethers.providers.WebSocketProvider("ws://127.0.0.1:8545/");

Make sure you connect to the correct network.

By addressing these issues, your code should work correctly with PancakeSwap Router.

1
  • I want my provider pointing to hardhats test net, so that I can import a wallet from the testnet right, so inset it supposed to pointing to localhost? and the import statements from Uniswap SDK are not being used for the purchaseToken function. Commented Apr 7, 2023 at 22:19

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.