Skip to content

Commit

Permalink
feat: fix stack too deep errors
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlak committed Feb 23, 2024
1 parent 78a4392 commit e63cea8
Showing 1 changed file with 97 additions and 48 deletions.
145 changes: 97 additions & 48 deletions src/LogNormal/LogNormalExtendedLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ struct DiffLowerStruct {
uint256 v;
int256 sqrtTwo;
int256 gamma;
uint256 L;
}

function computeDiffLowerFirstFrac(DiffLowerStruct memory params)
Expand All @@ -331,6 +332,21 @@ function computeDiffLowerFirstFrac(DiffLowerStruct memory params)
return firstNum.wadDiv(firstDen);
}

function computeDiffLowerSecondFrac(DiffLowerStruct memory params)
pure
returns (int256)
{
int256 first = int256(params.strike).wadMul(int256(params.L)).wadMul(
-I_ONE + params.gamma
);
int256 erfcFirst =
int256(params.sigma).wadMul(params.sqrtTau).wadDiv(params.sqrtTwo);
int256 erfcSecond = params.ierfcRes;
int256 num = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
int256 den = I_TWO.wadMul(int256(params.rX));
return num.wadDiv(den);
}

function diffLower(
uint256 S,
uint256 rX,
Expand Down Expand Up @@ -373,28 +389,77 @@ function diffLower(
sqrtTau: sqrtTau,
rX: rX,
strike: params.strike,
L: L,
v: v,
sqrtTwo: sqrtTwo,
gamma: I_ONE - int256(params.swapFee)
});

int256 firstFrac = computeDiffLowerFirstFrac(parameters);
int256 secondFrac = computeDiffLowerSecondFrac(parameters);
return computeDiffLowerResult(S, firstFrac, secondFrac);
}

int256 secondFrac;
{
int256 first = int256(params.strike).wadMul(int256(L)).wadMul(
-I_ONE + I_ONE - int256(params.swapFee)
);
int256 erfcFirst = int256(params.sigma).wadMul(sqrtTau).wadDiv(sqrtTwo);
int256 erfcSecond = ierfcRes;
int256 num = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
int256 den = I_TWO.wadMul(int256(rX));
secondFrac = num.wadDiv(den);
}

function computeDiffLowerResult(
uint256 S,
int256 firstFrac,
int256 secondFrac
) pure returns (int256) {
return -int256(S) + firstFrac + secondFrac;
}

struct DiffRaiseStruct {
int256 ierfcNum;
uint256 sigma;
uint256 tau;
int256 gamma;
int256 sqrtTwo;
int256 sqrtTau;
int256 ierfcRes;
uint256 rY;
uint256 S;
uint256 v;
uint256 strike;
uint256 L;
}

function computeDiffRaiseFirstFrac(DiffRaiseStruct memory params)
pure
returns (int256)
{
int256 firstExp = -(
int256(params.sigma).powWad(I_TWO).wadMul(int256(params.tau)).wadDiv(
I_TWO
)
);
int256 secondExp = params.sqrtTwo.wadMul(int256(params.sigma)).wadMul(
params.sqrtTau
).wadMul(params.ierfcRes);
int256 first = FixedPointMathLib.expWad(firstExp + secondExp);
int256 second =
int256(params.S).wadMul(int256(params.rY)).wadMul(params.gamma);
int256 firstNum = first.wadMul(second);
int256 firstDen = int256(params.strike).wadMul(
int256(params.v) + int256(params.rY)
- int256(params.v).wadMul(params.gamma)
);
return firstNum.wadDiv(firstDen);
}

function computeDiffRaiseSecondFrac(DiffRaiseStruct memory params)
pure
returns (int256)
{
int256 first =
int256(params.L).wadMul(int256(params.S)).wadMul(-I_ONE + params.gamma);
int256 erfcFirst =
int256(params.sigma).wadMul(params.sqrtTau).wadDiv(params.sqrtTwo);
int256 erfcSecond = params.ierfcRes;
int256 num = first.wadMul(Gaussian.erfc(erfcFirst - erfcSecond));
int256 den = I_TWO.wadMul(int256(params.rY));
return num.wadDiv(den);
}

function diffRaise(
uint256 S,
uint256 rX,
Expand All @@ -403,47 +468,31 @@ function diffRaise(
uint256 v,
LogNormal.LogNormalParams memory params
) pure returns (int256) {
(int256 strike, int256 sigma, int256 tau, int256 swapFee) = (
int256(params.strike),
int256(params.sigma),
int256(params.tau),
int256(params.swapFee)
);
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 gamma = I_ONE - int256(params.swapFee);
int256 ierfcNum = I_TWO.wadMul(int256(rY)).wadMul(int256(v) + int256(rY));
int256 ierfcDen = -int256(params.strike).wadMul(int256(L)).wadMul(int256(v) + int256(rY))
+ int256(params.strike).wadMul(int256(L)).wadMul(int256(v)).wadMul(gamma);
int256 ierfcRes = Gaussian.ierfc(-ierfcNum.wadDiv(ierfcDen));

int256 firstFrac;
{
int256 firstExp = -(sigma.powWad(I_TWO).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);
}
DiffRaiseStruct memory parameters = DiffRaiseStruct({
ierfcNum: ierfcNum,
sigma: params.sigma,
tau: params.tau,
gamma: gamma,
sqrtTwo: sqrtTwo,
sqrtTau: sqrtTau,
ierfcRes: ierfcRes,
S: S,
rY: rY,
v: v,
strike: params.strike,
L: L
});

int256 secondFrac;
{
int256 first = iL.wadMul(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 firstFrac = computeDiffRaiseFirstFrac(parameters);
int256 secondFrac = computeDiffRaiseSecondFrac(parameters);

return -I_ONE + firstFrac + secondFrac;
}
Expand Down

0 comments on commit e63cea8

Please sign in to comment.