DFMM icon indicating copy to clipboard operation
DFMM copied to clipboard

bug(high-severity): fee calculation for `LogNormal`

Open Autoparallel opened this issue 1 year ago • 0 comments

Description

The LogNormal strategy uses an incorrect fee calculation for the increase in L that will result in fees that are dependent on the current price of the pool.

Details

The fee calculation roughly looks like

fees = amountIn * swapFee;
deltaL = fees * L / R

where R is either one of the two reserves, and specifically is the reserve that is being tendered in for a swap.

In the case where the pool is priced such that R is much less than L``, can see that we then require a substantial growth in L` for the swap to be accepted. This can lead to worse execution prices and could lead to a pool becoming stale/bricked if the reported price falls too far from the "strike" price of the pool (also depends on the sigma parameter).

Fix

To fix the issue, we just calculate the fees and deltaL slightly differently.

If X is being tendered:

deltaL = amountIn * swapFee

else if Y is being tendered:

deltaL = amountIn * swapFee / params.strikePrice

Testing in Mathematica notebooks seems to show this change fixes the fee problem and as the pool depth increases, this choice of fee calculation leads to what we expect to see from a ConstantSum CFMM.

Autoparallel avatar Feb 20 '24 22:02 Autoparallel