0

I'm developing a crypto bot with node and ethers. Had faced a lot of erros but was able to overcome till this one. I'm not a huge experienced dev and don't feel confortable with this architeture yet don't know how to improve.


const main = async () => {
    const tokens = '0xC762043E211571eB34f1ef377e5e8e76914962f9'

    try {

    // SOME LOGIC

        if(apePrice > cakePrice){
            console.log(
                apePrice - cakePrice, 
                "ape oportunity",
                (apePrice - cakePrice) / apePrice * 100
            )
            const amount = await wallet.getBalance()
            const tokenAmount = await token.balanceOf(wallet.address)
            const tokensForBuy = amount * 0.5
            const tokensForSell = Number(tokenAmount._hex)
            const slipage = tokensForSell * ( 5 / 10)
            const stringBuy = String(tokensForBuy)
            const stringSell = String(tokensForSell)
            const stringSlipage = String(slipage)
            console.log(tokensForBuy, tokensForSell, slipage, amount._hex)
            const formatedBuy  = ethers.utils.parseUnits(stringBuy, 18)
            const formatedSell  = ethers.utils.parseUnits(stringSell, 18)
            const formatedSlipage = ethers.utils.parseEther(stringSlipage)
            console.log(formatedBuy)
            console.log(wallet.estimateGas)
            let approveApe = token.approve(apeRouter.address, formatedBuy._hex)            
            let approveCake = token.approve(apeRouter.address, formatedBuy._hex)            
            let approveApeBNB = wBNB.approve(apeRouter.address, formatedBuy._hex)            
            let approveCakeBNB = wBNB.approve(apeRouter.address, formatedBuy._hex)
            let tx = pancakeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(formatedBuy._hex, formatedSlipage._hex, ['0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', tokens], wallet.address, 30000000, {gasLimit: 90000});
            let tx2 = apeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(formatedSell, formatedSlipage._hex, [tokens, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'], wallet.address, 30000000, {gasLimit: 90000})

            await approveApe.wait()
            await approveCake.wait()
            await approveApeBNB.wait()
            await approveCakeBNB.wait()
            await tx.wait();
            await tx2.wait()
            console.log("cake to ape")
        }

        if(cakePrice > apePrice){
            console.log(
                cakePrice - apePrice, 
                "Cake oportunity",
                (cakePrice - apePrice) / cakePrice * 100
                )
                const amount = await wallet.getBalance()
                const tokenAmount = await token.balanceOf(wallet.address)
                const tokensForBuy = amount * 0.5
                const tokensForSell = Number(tokenAmount._hex)
                const slipage = tokensForSell * ( 5 / 10)
                const stringBuy = String(tokensForBuy)
                const stringSell = String(tokensForSell)
                const stringSlipage = String(slipage)
                console.log(tokensForBuy, tokensForSell, slipage, amount._hex)
                const formatedBuy  = ethers.utils.parseUnits(stringBuy, 18)
                const formatedSell  = ethers.utils.parseUnits(stringSell, 18)
                const formatedSlipage = ethers.utils.parseEther(stringSlipage)
                console.log(formatedBuy)
                console.log(wallet.estimateGas)
                let approveApe = token.approve(apeRouter.address, formatedBuy._hex)
                console.log("Ape approved for appe route")
                let approveCake = token.approve(apeRouter.address, formatedBuy._hex)                
                console.log("Ape approved for cake route")
                let approveApeBNB = wBNB.approve(apeRouter.address, formatedBuy._hex)                
                console.log("BNB approved for appe route")
                let approveCakeBNB = wBNB.approve(apeRouter.address, formatedBuy._hex)
                console.log("BNB approved for cake route")                
                let tx = apeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(formatedBuy._hex, formatedSlipage._hex, ['0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', tokens], wallet.address, 3000000, {gasLimit: ethers.utils.parseUnits('5', 'gwei')});
                console.log("Ape brought on appe route")
                let tx2 = pancakeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(formatedSell, formatedSlipage._hex, [tokens, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'], wallet.address, 3000000, {gasLimit: ethers.utils.parseUnits('5', 'gwei')})
                await approveApe.wait()
                await approveCake.wait()
                await approveApeBNB.wait()
                await approveCakeBNB.wait()
                await tx.wait();
                await tx2.wait()
                console.log("cake to ape") 
        }
    
        console.log(apePrice, cakePrice)

    } catch (error) {
        console.log(error)
    }
}


The error that i'm facing is this one:

'{"jsonrpc":"2.0","id":79,"error":{"code":-32000,"message":"replacement transaction underpriced"}}\n',

I researched and find out that this says something about transacation nounce but i thought that since i didn't determined the nonce ethers should calculate it automatically. Am i right? Someone can give me a hint?

1 Answer 1

0

I'm not sure, but it seems like you have stuck transaction, and you need to overwrite it. Check this to get an idea how to do that with metamask wallet. You can also check this overwriting method on the net.

In your code you use wait() methods on transactions. From ethers documentation u can see, that wait() is used to make sure transaction has been mined. So if you want these transactions go one by one, you need to use wait() between transactions and probably add more try-catch blocks inside this fucntion for error handling and to check if transaction will fail.

Also, you can create separate function to approve tokens with different addresses as func parameters... (for example), but this is at your discretion.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.