This transaction transaction describes Swap event:
amount0_in=14067900000000
amount1_out=393017164305296368
This means that the effective trade rate is:
trade_rate=round(amount1_out/amount0_in)=27,937
Meanwhile, this is the last Sync event before Swap and Sync event after Swap.
Before:
reserve0=95594454080845
reserve1=3070347989941780637
After:
reserve0=109662354080845
reserve1=2677330825636484269
The first one describes rate 32,118
and the second one 24,414
. Neither are equal or close to 27,937
.
Both transactions:
- 0x6e163a94f50cc1c4845880fe0a28cf196446c2fcb64d964441e66a903e2dc51f
- 0x529ba9819eaf1adfff5a4e79c253f537998549a89d0d612ec73fb512e4733a30
What's the relationship between Swap events and reserve values and how was the Swap rate determined?
For what it is worth, I was able to calculate similar output using formula from this answer:
reserve0=95594454080845
reserve1=3070347989941780637
amount0_in=14067900000000
round((reserve1 * amount0_in)/(reserve0 + amount0_in))
This gives 393875809522191052
, which is close to the expected 393017164305296368
, though not exactly the same.
The difference appears to be 0.21%:
a=393,875,809,522,191,052
b=393,017,164,305,296,368
((a-b)/((a+b)/2))*100=0.21
That 0.21% resembles pancakeswap fee. However, I cannot figure out how to incorporate that into the original formula to get the exact same figure.
I even tried to apply the logic from getAmountOut
, but that still gives a different number:
amount_in_with_fee=amount_in*998 = 14,039,764,200,000,000
numerator=amount_in_with_fee*reserve1 = 43,106,961,790,726,571,871,605,795,400,000,000
denominator=reserve0*1000+amount_in_with_fee = 109,634,218,280,845,000
amount_out=round(numerator/denominator) = 393,188,937,420,080,150