religion-counter / onlyone

DeFi future without taxes
78 stars 44 forks source link

About Sell #5

Closed cem-altuner closed 2 years ago

cem-altuner commented 3 years ago

Hi bro,

I fixed all my mistakes for buying a token but, can't sell 🤒

Could you help ?

Failed Transaction

// Sells ONLYONE for BNB from pancakeswap for address ${targetAccounts[targetIndex].address}

var fs = require('fs')
var Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var Common = require('ethereumjs-common').default;

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',
);

let rawData = fs.readFileSync('data.json');
let progData = JSON.parse(rawData);

sellMulty();

async function sellMulty() {
    var onyloneAmount = 1;
    var res = sellOnlyone(progData.mainAccount, onyloneAmount*1e18)
    .catch(e => {
        console.error("Error in sell:", e);
        process.exit(1);
    });
    console.log(res);

}

async function sellOnlyone(targetAccount, amount) {

    var amountToSell = web3.utils.toHex(amount);
    var privateKeyBuffer =  progData.privateKey;
    var privateKey = Buffer.from(privateKeyBuffer,'hex');
    var abiArray = JSON.parse(JSON.parse(fs.readFileSync('onlyone-abi.json','utf-8')));

    var tokenAddress = '0x370AbbD30774E52C47C596807a288cec48f42319'; // CAKEWHALE contract address
    var WBNBAddress = '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c'; // WBNB token address
    var pancakeSwapRouterAddress = '0x10ed43c718714eb63d5aa57b78b54704e256024e';

    // Approve ONLYONE spend
    var onlyoneContract = new web3.eth.Contract(abiArray, tokenAddress, {from: targetAccount});
    var approveOnlyoneSpendData = onlyoneContract.methods.approve(pancakeSwapRouterAddress, web3.utils.toWei('1', 'ether'));
    var count = await web3.eth.getTransactionCount(targetAccount);
    var rawTransactionApprove = {
        "from":targetAccount,
        "gasPrice":web3.utils.toHex(5000000000),
        "gasLimit":web3.utils.toHex(210000),
        "to":tokenAddress,
        "value":"0x0",
        "data":approveOnlyoneSpendData.encodeABI(),
        "nonce":web3.utils.toHex(count)
    };
    var transactionApprove = new Tx(rawTransactionApprove, {'common':BSC_FORK});
    transactionApprove.sign(privateKey)

    var resultApprove = await web3.eth.sendSignedTransaction('0x' + transactionApprove.serialize().toString('hex'));
    console.log("Approved" + resultApprove);

    // var onlyOneWbnbCakePairAddress = '0xd22fa770dad9520924217b51bf7433c4a26067c2';
    // var pairAbi = JSON.parse(fs.readFileSync('cake-pair-onlyone-bnb-abi.json', 'utf-8'));
    // var pairContract = new web3.eth.Contract(pairAbi, onlyOneWbnbCakePairAddress/*, {from: targetAccount.address}*/);
    var amountOutMin = web3.utils.toHex(1); // 540BNB is the price of one onlyone

    var routerAbi = JSON.parse(fs.readFileSync('pancake-router-abi.json', 'utf-8'));
    var contract = new web3.eth.Contract(routerAbi, pancakeSwapRouterAddress, {from: targetAccount});
    var data = contract.methods.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amountToSell,
        amountOutMin,
        [tokenAddress,
        //  '0xe9e7cea3dedca5984780bafc599bd69add087d56' /* BUSD address */, // Add this if you want to go through the onlyone-busd pair
         WBNBAddress],
        targetAccount,
        web3.utils.toHex(Math.round(Date.now()/1000)+60*20),
    );

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

    var transaction = new Tx(rawTransaction, { 'common': BSC_FORK });
    transaction.sign(privateKey);

    var result = await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'));
    console.log(result)
    return result;
}

function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
} 
religion-counter commented 3 years ago

Hi @cem-altuner ,

I saw that the gas used by the transaction was 98%. Try increasing the gas limit from 460000 to 700000 for example. The BSC network is more busy in the last days. Also first try to sell manually on the site and then try to simulate the same transaction with code.