0

I want to sell token using universal router in base network but transaction revert is coming every time. Can anyone solve this?

CODE

const { BigNumber } = require('bignumber.js');
const routerAbi = require('./abis/routerAbi.json');
const erc20Abi = require('./abis/erc20Abi.json');
require('dotenv').config();

const tokenAddress = '0x717d31A60a9e811469673429c9F8Ea24358990f1';
const routerAddress = '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad';
const pairAddress = '0xb65d78c4d8b987c8c05c85544786a6dd7f6c3421';
const yourWalletAddress = '0xBa1347eAB403FAEd265E906634a6D5962D1E68DD';
const privateKey = process.env.PRIVATE_KEY;
const rpcUrl = 'https://mainnet.base.org';

const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const routerContract = new web3.eth.Contract(routerAbi, routerAddress);
const tokenContract = new web3.eth.Contract(erc20Abi, tokenAddress);

const amountToSell = new BigNumber(10).times(new BigNumber(10).pow(18)); // 10 tokens with 18 decimals
const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes from now
const slippageTolerancePercentage = 2; // 2% slippage tolerance
const liquidityPoolFee = 100; // 1% liquidity pool fee

// Approve Router to spend tokens
async function approveToken() {
    const approvalData = tokenContract.methods.approve(routerAddress, amountToSell.toFixed()).encodeABI();
    const nonce = await web3.eth.getTransactionCount(yourWalletAddress);
    const approvalTxObject = {
        from: yourWalletAddress,
        to: tokenAddress,
        gas: 400000,
        data: approvalData,
        nonce: nonce
    };
    const signedTx = await web3.eth.accounts.signTransaction(approvalTxObject, privateKey);
    const approvalTxReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
    //console.log('Approval transaction receipt:', approvalTxReceipt);
}

// Sell tokens for ETH
async function sellTokensForEth() {
    await approveToken(); // Approve token transfer first

    const commands = '0x3593564c'; // MethodID for execute function
    const inputs = [
        web3.eth.abi.encodeParameter('bytes[]', []), // Empty array for commands
        web3.eth.abi.encodeParameter('address', tokenAddress), // Token address
        web3.eth.abi.encodeParameter('uint256', amountToSell.toFixed()), // Amount of tokens to sell
        web3.eth.abi.encodeParameter('address', pairAddress), // Pair address
        web3.eth.abi.encodeParameter('uint256', 0), // Minimum amount of ETH to receive
        web3.eth.abi.encodeParameter('uint256', deadline), // Deadline
        web3.eth.abi.encodeParameter('uint24', liquidityPoolFee), // Liquidity pool fee (1%)
        web3.utils.toWei((slippageTolerancePercentage / 100).toString(), 'ether') // Slippage tolerance (2%)
    ];

    const data = web3.eth.abi.encodeFunctionCall({
        name: 'execute',
        type: 'function',
        inputs: [
            { type: 'bytes', name: 'commands' },
            { type: 'bytes[]', name: 'inputs' },
            { type: 'uint256', name: 'deadline' }
        ]
    }, [commands, inputs, deadline]);

    const nonce = await web3.eth.getTransactionCount(yourWalletAddress);
    const txObject = {
        from: yourWalletAddress,
        to: routerAddress,
        gas: 400000,
        data: data,
        nonce: nonce
    };
    const signedTx = await web3.eth.accounts.signTransaction(txObject, privateKey);
    const txReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
    console.log('Swap transaction receipt:', txReceipt);
}

sellTokensForEth();

ERROR

        var error = new Error(message);
                    ^

Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0x40b4f01ff8e9e2e1ab974cae15125c15fdedb732744bd7bb97878c13a3d52f84",
  "blockNumber": 13249165,
  "contractAddress": null,
  "cumulativeGasUsed": 4941915,
  "effectiveGasPrice": "0x56883d0",
  "from": "0xba1347eab403faed265e906634a6d5962d1e68dd",
  "gasUsed": 31847,
  "l1Fee": "0x23cdb8bb90",
  "l1GasPrice": "0x4c4a924aa",
  "l1GasUsed": "0x1aa4",
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad",
  "transactionHash": "0x1f31312e9fd952fa17f998eec65ee20c95cbfa1de7e34be76ea8683b51deba9a",
  "transactionIndex": 36,
  "type": "0x0"
}
    at Object.TransactionError (d:\uniswapV3-base-mainnet-\node_modules\web3-core-helpers\lib\errors.js:87:21)
    at Object.TransactionRevertedWithoutReasonError (d:\uniswapV3-base-mainnet-\node_modules\web3-core-helpers\lib\errors.js:98:21)
    at d:\uniswapV3-base-mainnet-\node_modules\web3-core-method\lib\index.js:393:57
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  receipt: {
    blockHash: '0x40b4f01ff8e9e2e1ab974cae15125c15fdedb732744bd7bb97878c13a3d52f84',
    blockNumber: 13249165,
    contractAddress: null,
    cumulativeGasUsed: 4941915,
    effectiveGasPrice: '0x56883d0',
    from: '0xba1347eab403faed265e906634a6d5962d1e68dd',
    gasUsed: 31847,
    l1Fee: '0x23cdb8bb90',
    l1GasPrice: '0x4c4a924aa',
    l1GasUsed: '0x1aa4',
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: false,
    to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad',
    transactionHash: '0x1f31312e9fd952fa17f998eec65ee20c95cbfa1de7e34be76ea8683b51deba9a',
    transactionIndex: 36,
    type: '0x0'
  }
}

Node.js v20.11.1```
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.