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?