2

I am attempting to create a test arbitrage bot using Uniswap & SushiSwap.

I can successfully get the amount of X tokens I would get for 1 WETH on both dexes. For example:

Uniswap: WETH / USDT 1704.93153

Sushi Swap: WETH / USDT 1701.745783

To do this I am using the `getAmountsIn` method and passing in 1 WETH.

async getBuyPrice() {
    const pathToBuy = await this.routerContract.methods
    // getAmountsIn already includes Uniswap & SushiSwap fees in the calculation
    .getAmountsIn(this.swapAmount, [this.swapTo, this.swapFrom])
        .call();
    // TODO: modify the / 1e12 on a per token basis
    return Math.floor(ethers.utils.parseUnits(pathToBuy[0], this.swapToDecimal) * 100) / 100 / 1e12;
}

I then do a simple profit calculation of

if (uniswapPrice > sushiswapPrice) {
return Math.abs(sushiswapPrice - uniswapPrice); 
}

With these basic calculations I get outputs like:

estimated profit on trade WETH/ USDT 3.185746999999992

My issue comes when I want to then take away the TX costs from the profit including gas fees.

I get the gas price back with:

let gasPrice;
   await web3.eth.getGasPrice().then((result) => {
    gasPrice = web3.utils.fromWei(result, 'ether');
});

gas price in Eth 0.000000013596475603

However, this gives me the gas price in Eth whereas `getAmountsIn` allows me to calculate the profit in the token I am attempting to swap, for example 3.1 USDT.

Would I then need to convert `WETH/ USDT 3.185746999999992` into Eth and minus TX fees to work out the true profit? I'm quite new to all of this so am unsure.

Thanks!

1 Answer 1

0
const finalProfit = estimatedProfit - gasPrice

Yes you need to do this math.

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.