[Ethereum] How to calculate the price after trade on Uniswap

blockchainuniswapweb3js

I would like to get the price of a token after a swap. For example the price is now $1, and I would like to see how much will be the price after my swap. Is the execution price what I need in this case?

Best Answer

Let's use this transaction as an example:

https://bscscan.com/tx/0xdd38b435d9840062db9106e23712873fa2da44c366fa737b17f7fc6d111ef076#eventlog

Here 172054109197 Zombie tokens were transferred to Zombie / BUSD LP in exchange for 6357581284196624466 BUSD tokens.

Here is the Swap event:

amount0In: 172054109197
amount1In: 0
amount0Out: 0
amount1Out: 6357581284196624466

If we look at both (Zombie, BUSD) contracts, they both use 18 decimals, i.e. we can think of these values as:

0.000000172 Zombie traded for 6.3575 BUSD

To understand the price impact, we need to look at the Zombie / BUSD LP reserves before and after the transaction.

Before:

reserve0: 1203765031
reserve1: 6402195517789394391

After:

reserve0: 173257874228
reserve1: 44614233592769925

We know (by looking at the LP contract) that reserve0 represents Zombie token and reserve1 represents BUSD token.

As we traded Zombie for BUSD, we have to add the amount being traded in to the first reserve and deduct the amount being traded for from the second reserve, i.e.

reserve0: 1203765031+172054109197=173257874228
reserve1: 6402195517789394391+6357581284196624466=44614233592769925

This is how the Swap affected the reserves and therefore the price ratio.

Related Topic