I am using web3.js trying to make a simple transaction on UniswapV3 Rinkeby using the function exactInputSingle:
import Web3 from 'web3';
import * as fs from 'fs';
import { exit } from 'process';
const urlNode = "wss://rinkeby.infura.io/ws/v3/<key>"
const uniswapV3RouterAddress = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45";
const privateKey = <private_key>;
const token0ERC20Address = "0xc778417E063141139Fce010982780140Aa0cD5Ab";
const token0Decimals = 18;
const token1ERC20Address = "0x4DBCdF9B62e891a7cec5A2568C3F4FAF9E8Abe2b";
const token1Decimals = 6;
const web3 = new Web3(urlNode);
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const uniswapV3RouterABI = JSON.parse(fs.readFileSync('ABIs/UniswapV3Router.abi'))
const uniswapV3RouterContract = new web3.eth.Contract(uniswapV3RouterABI,uniswapV3RouterAddress);
swap(token0ERC20Address,token0Decimals,token1ERC20Address,account.address,0.0005).then((sucess)=>{
console.log(sucess);
exit();
});
async function propagateTX(encodedTx,to){
const block = await web3.eth.getBlock("latest");
const gasLimit = Math.round(block.gasLimit / block.transactions.length);
const tx = {
gas: gasLimit,
to: to,
data: encodedTx
}
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const sendedTx = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
return sendedTx;
}
async function swap(tokenFrom,tokenFromDecimals,tokenTo,recipient,amount){
const payload = {
"tokenIn": tokenFrom,
"tokenOut": tokenTo,
"fee": BigInt(3000),
"recipient": recipient,
"deadline": BigInt(Math.floor(Date.now() / 1000) + 900),
"amountIn": BigInt(amount*10**tokenFromDecimals),
"amountOutMinimum": BigInt(0),
"sqrtPriceLimitX96": BigInt(0)
};
const encoded = await uniswapV3RouterContract.methods.exactInputSingle(payload).encodeABI();
const sendedTx = await propagateTX(encoded,uniswapV3RouterAddress);
return sendedTx;
}
I am receiving the following error:
Error: Transaction has been reverted by the EVM:
etherscan.io show my transaction with error "Fail with error 'LOK'": (https://rinkeby.etherscan.io/tx/0x5973df2bb1ca984c5093143004354c9e6919783d156d82d02e960f2464762424)
I am not sure why the problem and how to solve, any help is welcome, thanks.