Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: func styling for maxBaseIn and maxFYTokenOut #8

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions packages/math/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,13 @@ export function buyFYToken(
*
* @returns { BigNumber } max amount of shares that can be bought from the pool
*
* y = fyToken
* z = vyToken
* x = Δy
* y = maxSharesIn
* Y = fyTokenReserves (virtual)
* Z = sharesReserves
*
* 1/μ * ( ( sum )^( invA ) - z
* 1/μ * ( ( ( cua ) * Za + Ya ) / (c/μ + 1) )^( invA ) - z
* Δz = 1/μ * ( ( ( cμ^a * z^a + μy^a) / (c + μ) )^(1 / (1 - t)) - z
* y = 1/μ ( ( numerator ) / ( denominator ) )^invA - Z
* y = 1/μ ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA - Z
* y = 1/μ ( ( c/μ * (μZ)^a + Y^a ) / ( c/u + 1 ) )^(1/a) - Z
*
*/
export function maxBaseIn(
Expand All @@ -636,14 +636,12 @@ export function maxBaseIn(
const c_ = _getC(c);
const mu_ = _getMu(mu);

const cua = c_.mul(mu_.pow(a));
const Za = sharesReserves_.pow(a);
const Ya = mu_.mul(fyTokenReserves_.pow(a));
const top = cua.mul(Za).add(Ya);
const bottom = c_.add(mu_);
const sum = top.div(bottom);
const Za = c_.div(mu_).mul(mu_.mul(sharesReserves_).pow(a));
const Ya = fyTokenReserves_.pow(a);
const numerator = Za.add(Ya);
const denominator = c_.div(mu_).add(ONE);

const res = ONE.div(mu_).mul(sum.pow(invA)).sub(sharesReserves_);
const res = ONE.div(mu_).mul(numerator.div(denominator).pow(invA)).sub(sharesReserves_);

/* Handle precision variations */
const safeRes = res.gt(MAX.sub(precisionFee)) ? MAX : res.add(precisionFee);
Expand Down Expand Up @@ -727,11 +725,10 @@ export function maxFyTokenIn(
* y = maxFyTokenOut
* Y = fyTokenReserves (virtual)
* Z = sharesReserves
* cmu = cμ^a
*
* ( ( sum ) / ( denominator ) )^invA
* ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA
* y = Y - ( ( ( cμ^a * Z^a ) + ( μY^a ) ) / ( c + μ ) )^(1/a)
* y = Y - ( ( numerator ) / ( denominator ) )^invA
* y = Y - ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA
* y = Y - ( ( c/μ * (μZ)^a + Y^a ) / ( c + 1 ) )^(1/a)
*
* @param { BigNumber | string } sharesReserves
* @param { BigNumber | string } fyTokenReserves
Expand Down Expand Up @@ -766,14 +763,12 @@ export function maxFyTokenOut(

const [a, invA] = _computeA(timeTillMaturity, ts, g1);

const cmu = c_.mul(mu_.pow(a));

const Za = cmu.mul(sharesReserves_.pow(a));
const Ya = mu_.mul(fyTokenReserves_.pow(a));
const sum = Za.add(Ya);
const denominator = c_.add(mu_);
const Za = c_.div(mu_).mul(mu_.mul(sharesReserves_).pow(a));
const Ya = fyTokenReserves_.pow(a);
const numerator = Za.add(Ya);
const denominator = c_.div(mu_).add(ONE);

const res = fyTokenReserves_.sub(sum.div(denominator).pow(invA));
const res = fyTokenReserves_.sub(numerator.div(denominator).pow(invA));

/* Handle precision variations */
const safeRes = res.gt(MAX.sub(precisionFee)) ? MAX : res.add(precisionFee);
Expand Down