Skip to content

Commit

Permalink
fix lognormal fee accounting and update optimal arb logic (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC authored Feb 22, 2024
1 parent 8ee49e0 commit 3b05853
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
56 changes: 27 additions & 29 deletions src/LogNormal/LogNormalExtendedLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,35 +315,33 @@ function diffLower(
int256 iV = int256(v);
int256 gamma = I_ONE - swapFee;

int256 ierfcNum = I_TWO.wadMul(iX).wadMul(iV + iX);
int256 ierfcDen = iL.wadMul(iV + iX - iV.wadMul(gamma));
int256 ierfcNum = I_TWO.wadMul(iV + iX);
int256 ierfcDen = iL + iV - iV.wadMul(gamma);
int256 ierfcRes = Gaussian.ierfc(ierfcNum.wadDiv(ierfcDen));

int256 firstFrac;
int256 a;
{
int256 firstExp = -(sigma.powWad(I_TWO).wadMul(tau).wadDiv(I_TWO));
int256 firstExp = -(sigma.wadMul(sigma).wadMul(tau).wadDiv(I_TWO));
int256 secondExp =
sqrtTwo.wadMul(sigma).wadMul(sqrtTau).wadMul(ierfcRes);

int256 first = FixedPointMathLib.expWad(firstExp + secondExp);
int256 second = strike.wadMul(iX).wadMul(gamma);
int256 second = strike.wadMul(iL + iX.wadMul(-I_ONE + gamma));

int256 firstNum = first.wadMul(second);
int256 firstDen = iV + iX - iV.wadMul(gamma);
firstFrac = firstNum.wadDiv(firstDen);
int256 firstDen = iL + iV - iV.wadMul(gamma);
a = firstNum.wadDiv(firstDen);
}

int256 secondFrac;
int256 b;
{
int256 first = strike.wadMul(iL).wadMul(-I_ONE + gamma);
int256 first = I_HALF.wadMul(strike).wadMul(-I_ONE + gamma);
int256 erfcFirst = sigma.wadMul(sqrtTau).wadDiv(sqrtTwo);
int256 erfcSecond = ierfcRes;
int256 num = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
int256 den = I_TWO.wadMul(iX);
secondFrac = num.wadDiv(den);
b = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
}

return -iS + firstFrac + secondFrac;
return -iS + a + b;
}

function diffRaise(
Expand All @@ -363,40 +361,40 @@ function diffRaise(
int256 sqrtTwo = int256(FixedPointMathLib.sqrt(TWO) * 1e9);
int256 sqrtTau = int256(FixedPointMathLib.sqrt(params.tau) * 1e9);
int256 iS = int256(S);
int256 iX = int256(rX);
int256 iY = int256(rY);
int256 iL = int256(L);
int256 iV = int256(v);
int256 gamma = I_ONE - swapFee;

int256 ierfcNum = I_TWO.wadMul(iY).wadMul(iV + iY);
int256 ierfcDen = -strike.wadMul(iL).wadMul(iV + iY)
+ strike.wadMul(iL).wadMul(iV).wadMul(gamma);
int256 ierfcRes = Gaussian.ierfc(-ierfcNum.wadDiv(ierfcDen));
int256 ierfcNum = I_TWO.wadMul(iV + iY);
int256 ierfcDen = strike.wadMul(iL) + iV - iV.wadMul(gamma);
int256 ierfcRes = Gaussian.ierfc(ierfcNum.wadDiv(ierfcDen));

int256 firstFrac;
int256 a;
{
int256 firstExp = -(sigma.powWad(I_TWO).wadMul(tau).wadDiv(I_TWO));
int256 firstExp = -(sigma.wadMul(sigma).wadMul(tau).wadDiv(I_TWO));
int256 secondExp =
sqrtTwo.wadMul(sigma).wadMul(sqrtTau).wadMul(ierfcRes);
int256 first = FixedPointMathLib.expWad(firstExp + secondExp);
int256 second = iS.wadMul(iY).wadMul(gamma);
int256 firstNum = first.wadMul(second);
int256 firstDen = strike.wadMul(iV + iY - iV.wadMul(gamma));
firstFrac = firstNum.wadDiv(firstDen);
int256 second = iS.wadMul(strike.wadMul(iL) + iY.wadMul(-I_ONE + gamma));

int256 num = first.wadMul(second);
int256 den = strike.wadMul(strike.wadMul(iL) + iV - iV.wadMul(gamma));
a = num.wadDiv(den);
}

int256 secondFrac;
int256 b;
{
int256 first = iL.wadMul(iS).wadMul(-I_ONE + gamma);
int256 first = iS.wadMul(-I_ONE + gamma);
int256 erfcFirst = sigma.wadMul(sqrtTau).wadDiv(sqrtTwo);
int256 erfcSecond = ierfcRes;
int256 num = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
int256 den = I_TWO.wadMul(iY);
secondFrac = num.wadDiv(den);
int256 den = I_TWO.wadMul(strike);

b = num.wadDiv(den);
}

return -I_ONE + firstFrac + secondFrac;
return -I_ONE + a + b;
}

function computeOptimalLower(
Expand Down
12 changes: 4 additions & 8 deletions src/LogNormal/LogNormalSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ contract LogNormalSolver {
);

if (swapXIn) {
uint256 fees = amountIn.mulWadUp(poolParams.swapFee);
uint256 deltaL =
fees.mulWadUp(startComputedL).divWadUp(startReserves.rx);
deltaL += 1;
uint256 deltaL = amountIn.mulWadUp(poolParams.swapFee);

endReserves.rx = startReserves.rx + amountIn;
endReserves.L = startComputedL + deltaL;
Expand All @@ -224,10 +221,9 @@ contract LogNormalSolver {
);
amountOut = startReserves.ry - endReserves.ry;
} else {
uint256 fees = amountIn.mulWadUp(poolParams.swapFee);
uint256 deltaL =
fees.mulWadUp(startComputedL).divWadUp(startReserves.ry);
deltaL += 1;
uint256 deltaL = amountIn.mulWadUp(poolParams.swapFee).divWadUp(
poolParams.strike
);

endReserves.ry = startReserves.ry + amountIn;
endReserves.L = startComputedL + deltaL;
Expand Down
1 change: 1 addition & 0 deletions src/lib/StrategyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uint256 constant INFINITY_IS_NOT_REAL = type(uint256).max;
uint256 constant ZERO = 0;
int256 constant I_ONE = int256(ONE);
int256 constant I_TWO = int256(TWO);
int256 constant I_HALF = int256(HALF);

/// @dev the swap constant should never fall outside of range [-EPSILON, EPSILON]
int256 constant EPSILON = 20;
Expand Down
4 changes: 2 additions & 2 deletions src/test/LogNormal/LogNormalTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ contract LogNormalTest is Test {
function test_ln_diff_lower() public basic {
uint256 poolId = dfmm.nonce() - 1;
int256 diffLowered = solver.calculateDiffLower(
poolId, 1.9 ether, 160_249_195_342_896_967
poolId, 1.9 ether, 171_917_954_632_821_596
);

console2.log(diffLowered);
Expand All @@ -156,7 +156,7 @@ contract LogNormalTest is Test {
function test_ln_diff_raise() public basic {
uint256 poolId = dfmm.nonce() - 1;
int256 diffLowered = solver.calculateDiffRaise(
poolId, 2.1 ether, 302_756_023_375_108_995
poolId, 2.1 ether, 326_118_838_819_816_034
);

console2.log(diffLowered);
Expand Down

0 comments on commit 3b05853

Please sign in to comment.