I have this function.I want to perform swap 3 times with the interval of 30 second using swapExactTokenForTokens.But only only one transaction get success.Others failing. Here is my Code.
const erc20ABI = ["function decimals() public view returns (uint8)",
"function approve(address spender, uint rawAmount) external returns (bool)",
"function name() view returns (string)",
"function symbol() view returns (string)",
"function balanceOf(address) view returns (uint)"];
const routerABI = ["function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)",
"function swapExactTokensForTokens(uint amountIn, uint amountInMin,address[] calldata path,address to,uint deadline) external returns (uint[] memory amounts)",
"function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts)"];
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";//uniswap router address
const WETHAddress = "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"; //WETH
const myTokenAddress = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";//UNI
const providerAddress = "-------------";
const privateKey = "--------------------------";
const provider = new ethers.providers.JsonRpcProvider(providerAddress);
wallet = new ethers.Wallet(privateKey, provider)
const signer = wallet.connect(provider);
const routerInstance = new ethers.Contract(routerAddress, routerABI, signer);
const token1 = new ethers.Contract(myTokenAddress, erc20ABI, signer);
const token2 = new ethers.Contract(WETHAddress, erc20ABI, signer);
const TokenSwap = async(amount, myTokenAddress, WETHAddress) => {
const decimal1 = await token1.decimals()
const decimal2 = await token2.decimals()
const amountIn = ethers.utils.parseUnits(amount, decimal1).toString();
const amountInMin = [0,0];
const amountOutMin = ethers.utils.parseUnits(Number((ethers.utils.formatUnits(amountInMin[1],18)) - 0.1).toString(),18);
const tx = await routerInstance.swapExactTokensForTokens(
amountIn,
amountInMin[0].toString(),
[myTokenAddress, WETHAddress],
wallet.address,
Date.now() + 1000 * 60 * 5,
)
tx.wait();//
console.log(tx.hash)
}
for (i=0; i<3;i++){
amount = '0.1'
TokenSwap(amount, myTokenAddress, WETHAddress)
}
One of them is in success state and other 2 getting failed.Can you help me to solve this issue
0x1191f08fdb2b66008c64d9a8b93fead591085dd5a7e4e8f40f05353a04bca535 0xe9e2161eeffc33ae55021ede873dbadd14b28deafbe3c4bae5076a76f4d18991 0xfcc8952d8ef55b62700e6f0ca8c18882f154e89ecc596009babe8b91b285d7f7
Also I have done approve correctly.