Hello everyone,
I want to test some swap functions (swap my token for ETH). In order to do this, I want to test it using Hardhat. For swapping, it needs to have some liquidity so I have to provide liquidity and there is my problem.
I was able to transfer WETH to the owner of MyTokens in order to add liquidity but I am unable to do so.
There is the function addLiquidity :
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external payable returns (uint, uint, uint) {
IERC20(tokenA).transferFrom(msg.sender, address(this), amountADesired);
IERC20(tokenB).transferFrom(msg.sender, address(this), amountBDesired);
IERC20(tokenA).approve(UNISWAP_V2_ROUTER, amountADesired);
IERC20(tokenB).approve(UNISWAP_V2_ROUTER, amountBDesired);
(uint amountToken, uint amountETH, uint liquidity) = IUniswapV2Router02(UNISWAP_V2_ROUTER).addLiquidity(
tokenA,
tokenB,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
to,
deadline
);
return (amountToken, amountETH, liquidity);
}
This is the test file :
//...
TestRewardERC20 = await ethers.getContractFactory("TestRewardERC20", owner);
mt = await TestRewardERC20.deploy();
const amountADesired = 50000;
const amountAMin = 5000;
const amountBDesired = 100;
const amountBMin = 1;
await mt.approve(mt.address, amountADesired);
await WETHContract.approve(mt.address, amountBDesired);
const latestBlock = await ethers.provider.getBlockNumber();
const timestamp = (await ethers.provider.getBlock(latestBlock)).timestamp;
await mt.addLiquidity(
mt.address,
WETHAddress,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
owner.address,
timestamp2 + 1000
);
This is the hardhat .config file :
//...
defaultNetwork: "hardhat",
networks: {
hardhat: {
forking: {
url: `https://eth-mainnet.alchemyapi.io/v2/CwCV16rJMJOAXnbqJxj2esvQIYLTCKph`,
blockNumber: 14638929,
},
},
When I run "npx hardhat test" I have either "TRANSFER_FROM_FAILED" either "Error: Transaction reverted without a reason string." depending on what I change.
Thank you very much for any help, have a great day