0

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) enter image description here

I am not sure why the problem and how to solve, any help is welcome, thanks.

2 Answers 2

1

I can't speak to why you get the LOK error - in Uniswap, that error (see here) means there was an attempt at reentrancy to a pool. I don't really see how that could be the case for this tx

However, I would still expect this transaction to fail. You are using the Ethereum mainnet addresses for WETH and USDC, however neither of those addresses are contracts on Rinkeby. Generally speaking the addresses for assets on testnets wont be the same as their mainnet addresses

2
0

I solved with a workaround, i did:

  1. Changed to Goerli, Rinkeby is deprecated.
  2. Not sure why, but the UI of UniswapV3 is using routerV2 instead of router V3, i assumed this is the right one, the contract is: https://goerli.etherscan.io/address/0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
  3. Instead of exactInputSingle, i am using the functions swapTokensForExactTokens and swapExactTokensForTokens with some adaptations in the calls.

And now i can swap.

1
  • On your #2 point, I believe the UniswapV3 UI will route to v2 or v3, whichever has better pricing
    – natewelch_
    Jul 31, 2022 at 1:03

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.