0

I am a complete beginner when it comes to writing javascript so bear with me. I want to write a basic .js file that I can run that will be able to sell something for BNB (in this case a very small amount of USDT)

I am getting the following error:

TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:57:21
    at Generator.next (<anonymous>)
    at C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:24:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:20:12)
    at getTransactionError (C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:33:12)
    at SendTxHelper.<anonymous> (C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\send_tx_helper.js:74:84)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\jewba\Downloads\Running_Smart_Contracts\node_modules\web3-eth\lib\commonjs\utils\send_tx_helper.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  innerError: undefined,
  reason: 'execution reverted',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402
}

The code I am using (with wallet addresses removed is):

var pv = "private key for wallet"
var pu = "public wallet address"
var Tx = require('ethereumjs-tx').Transaction;
var {Web3} = require('web3')
var Common = require('ethereumjs-common').default;

var pancakeSwapAbi = //Removed here 
var pancakeSwapRouterAddress = '0x10ed43c718714eb63d5aa57b78b54704e256024e';
let pancakeSwapContract = pancakeSwapRouterAddress.toLowerCase();

var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed.binance.org/'));
var BSC_FORK = Common.forCustomChain(
    'mainnet',
    {
        name: 'Binance Smart Chain Mainnet',
        networkId: 56,
        chainId: 56,
        url: 'https://bsc-dataseed.binance.org/'
    },
    'istanbul',
);
var BNBTokenAddress = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"; //BNB
var USDTokenAddress = "0x55d398326f99059fF775485246999027B3197955"; //USDT
var gasPrice = 5000000000;

async function sellOneUSDTbuyBNB(targetAccount) {
    var privateKey = Buffer.from(targetAccount.privateKey, 'hex')  ;
    try {
        var contract = new web3.eth.Contract(pancakeSwapAbi, pancakeSwapRouterAddress, {from: targetAccount.address});
        var data = contract.methods.swapExactTokensForETH(
            web3.utils.toHex((Math.round(10**15)).toString()),
            web3.utils.toHex((Math.round(3*10**12)).toString()),
            [USDTokenAddress,BNBTokenAddress],
            targetAccount.address,
            web3.utils.toHex(Math.round(Date.now()/1000)+60*10),
        );

        var count = await web3.eth.getTransactionCount(targetAccount.address);
        var rawTransaction = {
            "from":targetAccount.address,
            "gasPrice":web3.utils.toHex(gasPrice),
            "gasLimit":web3.utils.toHex(2900000),
            "to":pancakeSwapRouterAddress,
            "value":web3.utils.toHex('0'),
            "data":data.encodeABI(),
            "nonce":web3.utils.toHex(count)
        };

        console.log('rawTransaction',rawTransaction);
        var transaction = new Tx(rawTransaction, { 'common': BSC_FORK });
        transaction.sign(privateKey);
        var result = await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'));
        return 1;
    } catch (error) {
        console.log(error)
    }
}

var targetAccount = {"address":pu,"privateKey":pv}
var res = sellOneUSDTbuyBNB(targetAccount);

Can anyone help solve what the issue is here?

Thanks.

1 Answer 1

0

I guess this is only with the issue in approval.

for clarification manually approve 10 usdt to the pan cake contract. once the confirmation is done then try it may not revert if there is no issue with the contract with whom you are interacting with.

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.