# What's the relationship between LP reserves and Swap rate?

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
``````

``````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.