2

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:

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 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
Gajus
  • 713
  • 4
  • 21

1 Answers1

2
amount_in=14067900000000
amount_out=393017164305296368

reserve0=95594454080845
reserve1=3070347989941780637

amount_in_with_fee=amount_in*998-14067900000000*0.5
numerator=amount_in_with_fee*reserve1
denominator=reserve0*1000+amount_in_with_fee
amount_out=round(numerator/denominator) = 393,017,164,305,296,368

I figured out the how, but not the why.

I will leave this unanswered until someone proposes the why it works this way.

Gajus
  • 713
  • 4
  • 21
  • 1
    where did you get the 998 for working out the amount_in_with_fee? I can see it in the code for getAmountIn but wondering is this because the pancake fee is 0.02%, but then why we mulitpling it by half (*0.5). https://github.com/pancakeswap/pancake-swap-periphery/blob/d769a6d136b74fde82502ec2f9334acc1afc0732/contracts/libraries/PancakeLibrary.sol#L53 – Daithí Feb 05 '22 at 15:46