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?