6eer / uniswap-sushiswap-arbitrage-bot

Two bots written in JS that uses flashswaps and normal swaps to arbitrage Uniswap. Includes an automated demostration.
MIT License
586 stars 264 forks source link

Question about sAmountIn #10

Open rkedia145 opened 3 years ago

rkedia145 commented 3 years ago

I'm a bit confused about the calculation of sAmountIn:

const sAmountIn = await sRouter.methods.getAmountIn(**amountIn**,sReserve1 ,sReserve0).call()

Output log:

Watermelon (WTM) {T0} | Pineapple (PNA) {T1} reserves

On Uniswap
WTM: 1000 | PNA: 500

On Sushiswap
WTM: 10000 | PNA: 100000

Swap's direction
PNA -> WTM

Uniswap's pool state
WTM excess/PNA shortage

On Uniswap
Mid price before swap: 2.00 WTM/PNA
Mid price after swap: 0.10 WTM/PNA
Swap 1738 PNA for 776 WTM
Trade price: 0.45 WTM/PNA (sell price)

Sushiswap price: 0.10 WTM/PNA (buy price)
Difference: 0.34 WTM/PNA
Total difference: 56.49445 ETH or 598.68 WTM

Gas needed: 0.36 M
Gas price: 20 gwei
Gas cost: 0.00720 ETH

Profit: 56.48725 ETH or 113855.24 DAI

My understanding is that the bot discovers and arbitrage opportunity where WTM is underpriced relative to PNA on Uniswap than it is on sushiswap (since on Uniswap, WTM is in excess and PNA is in shortage). So in order to exploit this, we would want to purchase WTM on Uniswap and sell PNA on Uniswap, hence we would be swapping amountIn PNA for amountOut WTM on uniswap.

Now that we have WTM, we would look to sell it on Sushiswap, so we would be sending in WTM and getting back PNA.

If so, why is it that sAmountIn is calculated from amountIn (this PNA amount going into Uniswap), and not amountOut (the WTM amount coming out of uniswap)?

6eer commented 3 years ago

hey rob,

My understanding is that the bot discovers and arbitrage opportunity where WTM is underpriced relative to PNA on Uniswap than it is on sushiswap (since on Uniswap, WTM is in excess and PNA is in shortage). So in order to exploit this, we would want to purchase WTM on Uniswap and sell PNA on Uniswap, hence we would be swapping amountIn PNA for amountOut WTM on uniswap. Now that we have WTM, we would look to sell it on Sushiswap, so we would be sending in WTM and getting back PNA.

It's all right.

If so, why is it that sAmountIn is calculated from amountIn (this PNA amount going into Uniswap), and not amountOut (the WTM amount coming out of uniswap)?

Its because you wanna know how much WTM u will need to payback on sushiswap given u borrow (amountIn) PNA on sushi, so once u know how much u need to repay to sushi, your profit will be the (amountOut) WTMs that u got from uniswap less the sAmountIn of WTM u need to payback the flashloan on sushi (payback for the PNA u borrowed)

I hope have been clear! (otherwise, let me know but i think u got it, after all u understand all the shitty computations i made haha, cheers!)

rkedia145 commented 3 years ago

Hi @6eer ,

Thanks for your comment. I am still a bit confused though. Let me try to break this down into steps and maybe you can identify where exactly I am misunderstanding?

Steps:

  1. To start, we don't have any WTM or PNA, we only have ETH
  2. Arbitrage Opportunity Detected:
  1. We determine that it is optimal to swap 'amountIn' PNA on Uniswap in return for 'amountOut' WTM from Uniswap
  2. Since we don't own any PNA, we must first borrow 'amountIn' PNA on Sushiswap, meaning we are taking PNA out of Sushiswap, so we must be sending 'sAmountIn' WTM on Sushiswap.

This is where i am getting confused, because we dont currently own any WTM, so how can we send in 'sAmountIn' WTM into Sushiswap? Shouldn't there be a step somewhere to borrow WTM on Sushiswap for the ETH that we currently do own? (note that here i am referring to the normal bot, so there should not be any flash loans involved).

6eer commented 3 years ago

Ah, ok! (I saw the edit). Yeah u are right with the normal bot u need to be holding the WTM, u only use sushiswap as oracle, as if the sushiprice was the market price (a VERY BAD assumption, here i suppose using a binance API to get the price reference would be a much better idea). So instead of

4) Since we don't own any PNA, we must first borrow 'amountIn' PNA on Sushiswap, meaning we are taking PNA out of Sushiswap, so we must be sending 'sAmountIn' WTM on Sushiswap.

U use the PNA tokens that u owns (the account u use to send & sign the transaction must have enough PNA). I wrote it in the first paragraph of the What its included in this repo? section

[...]The other uses normal swaps, this bot require you to be holding the tokens needed to do the trade but it contrast it cost less gas to execute.[...]

U dont borrow nothing!! u just make the arbitrage with your funds