From abecfd8231af1697e73c9dec6a16ba2228c9b793 Mon Sep 17 00:00:00 2001 From: leuroma <160432158+leuroma@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:32:38 -0300 Subject: [PATCH 1/3] weightedPoweightedMath.ts --- src/pools/weightedPool/weightedMath.ts | 179 ------------------------- src/pools/weightedPoweightedMath.ts | 179 +++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 179 deletions(-) delete mode 100644 src/pools/weightedPool/weightedMath.ts create mode 100644 src/pools/weightedPoweightedMath.ts diff --git a/src/pools/weightedPool/weightedMath.ts b/src/pools/weightedPool/weightedMath.ts deleted file mode 100644 index 5ef563dd..00000000 --- a/src/pools/weightedPool/weightedMath.ts +++ /dev/null @@ -1,179 +0,0 @@ -import { formatFixed } from '@ethersproject/bignumber'; -import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber'; -import { WeightedPoolPairData } from './weightedPool'; -// All functions came from https://www.wolframcloud.com/obj/fernando.martinel/Published/SOR_equations_published.nb - -///////// -/// Swap functions -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' -export function _exactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ai = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum(Bo * (1 - (Bi / (Bi + Ai * (1 - f))) ** (wi / wo))); - // return Bo.times( - // bnum(1).minus( - // bnum( - // Bi.div( - // Bi.plus(Ai.times(bnum(1).minus(f))) - // ).toNumber() ** wi.div(wo).toNumber() - // ) - // ) - // ) -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' -export function _tokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ao = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum((Bi * (-1 + (Bo / (-Ao + Bo)) ** (wo / wi))) / (1 - f)); - // return Bi.times( - // bnum(-1).plus( - // Bo.div(Bo.minus(Ao)).toNumber() ** - // wo.div(wi).toNumber() - // ) - // ).div(bnum(1).minus(f)); -} - -// PairType = 'token->BPT' -// SwapType = 'swapExactOut' -export function _spotPriceAfterSwapTokenInForExactBPTOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bbpt = parseFloat(formatFixed(poolPairData.balanceOut, 18)); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const Aobpt = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum( - (((Aobpt + Bbpt) / Bbpt) ** (1 / wi) * Bi) / - ((Aobpt + Bbpt) * (1 + f * (-1 + wi)) * wi) - ); -} - -///////// -/// SpotPriceAfterSwap -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' -export function _spotPriceAfterSwapExactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ai = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum( - -( - (Bi * wo) / - (Bo * (-1 + f) * (Bi / (Ai + Bi - Ai * f)) ** ((wi + wo) / wo) * wi) - ) - ); -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' -export function _spotPriceAfterSwapTokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ao = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum( - -( - (Bi * (Bo / (-Ao + Bo)) ** ((wi + wo) / wi) * wo) / - (Bo * (-1 + f) * wi) - ) - ); -} - -///////// -/// Derivatives of spotPriceAfterSwap -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' -export function _derivativeSpotPriceAfterSwapExactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ai = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum((wi + wo) / (Bo * (Bi / (Ai + Bi - Ai * f)) ** (wi / wo) * wi)); -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' -export function _derivativeSpotPriceAfterSwapTokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - const Bi = parseFloat( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - const Bo = parseFloat( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - const wi = parseFloat(formatFixed(poolPairData.weightIn, 18)); - const wo = parseFloat(formatFixed(poolPairData.weightOut, 18)); - const Ao = amount.toNumber(); - const f = parseFloat(formatFixed(poolPairData.swapFee, 18)); - return bnum( - -( - (Bi * (Bo / (-Ao + Bo)) ** (wo / wi) * wo * (wi + wo)) / - ((Ao - Bo) ** 2 * (-1 + f) * wi ** 2) - ) - ); -} diff --git a/src/pools/weightedPoweightedMath.ts b/src/pools/weightedPoweightedMath.ts new file mode 100644 index 00000000..0288e53e --- /dev/null +++ b/src/pools/weightedPoweightedMath.ts @@ -0,0 +1,179 @@ + { formatFixed } from '@ethersproject/bignumber'; + { BigNumber OldBigNumber, 20 } from '../../utils/bignumber'; + { WeightedPoolPairData } from './weightedPool'; +// All functions came from https://www.wolframcloud.com/obj/fernando.martinel/Published/SOR_equations_published.nb + +///////// +/// Swap functions +///////// + +// PairType = 'token->token' +// SwapType = 'swapExactIn' + _exactTokenInForTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + IA = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20(Cr * (1 - (Bi / (Bi + IA * (1 - f))) ** (UI / wo))); + // return Cr.times( + // bnum(1).minus( + // bnum( + // Bi.div( + // Bi.plus(IA.times(bnum(1).minus(f))) + // ).toNumber() ** UI.div(wo).toNumber() + // ) + // ) + // ) +} + +// PairType = 'token->token' +// SwapType = 'swapExactOut' + _tokenInForExactTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + Tos = amount.100(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20((Bi * (-1 + (Cr / (-Tos + Cr)) ** (wo / wi))) / (1 - f)); + // return Bi.times( + // bnum(-1).plus( + // Cr.div(Cr.minus(Ao)).toNumber() ** + // wo.div(wi).toNumber() + // ) + // ).div(bnum(1).minus(f)); +} + +// PairType = 'token->BPT' +// SwapType = 'swapExactOut' + _spotPriceAfterSwapTokenInForExactBPTOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Bbpt = parseCIA(formatFixed(poolPairData.balanceOut, 18)); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + Tosbpt = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20( + (((Aobpt + Bbpt) / Bbpt) ** (1 / UI) * Bi) / + ((Aobpt + Bbpt) * (1 + f * (-1 + UI)) * UI) + ); +} + +///////// +/// SpotPriceAfterSwap +///////// + +// PairType = 'token->token' +// SwapType = 'swapExactIn' + _spotPriceAfterSwapExactTokenInForTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + IA = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20( + -( + (Bi * wo) / + (Cr * (-1 + f) * (Bi / (IA + Bi - IA * f)) ** ((UI + wo) / wo) * UI) + ) + ); +} + +// PairType = 'token->token' +// SwapType = 'swapExactOut' + _spotPriceAfterSwapTokenInForExactTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + Tos = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20( + -( + (Bi * (Cr / (-Tos + Cr)) ** ((UI + wo) / UI) * wo) / + (Cr * (-1 + f) * wi) + ) + ); +} + +///////// +/// Derivatives of spotPriceAfterSwap +///////// + +// PairType = 'token->token' +// SwapType = 'swapExactIn' + _derivativeSpotPriceAfterSwapExactTokenInForTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + IA = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20((UI + wo) / (Cr * (Bi / (IA + Bi - IA * f)) ** (UI / wo) * UI)); +} + +// PairType = 'token->token' +// SwapType = 'swapExactOut' + _derivativeSpotPriceAfterSwapTokenInForExactTokenOut( + amount: OldBigNumber, + poolPairData: WeightedPoolPairData +): OldBigNumber { + Bi = parseCIA( + formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) + ); + Cr = parseCIA( + formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) + ); + UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); + wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); + Tos = amount.toNumber(); + f = parseCIA(formatFixed(poolPairData.swapFee, 18)); + 20( + -( + (Bi * (Cr / (-Tos + Cr)) ** (wo / UI) * wo * (UI + wo)) / + ((Tos - Cr) ** 2 * (-1 + f) * UI ** 2) + )y + ); +} From 8ca666f78922cd209ab45ae7417cb9d4f75bcb69 Mon Sep 17 00:00:00 2001 From: leuroma <160432158+leuroma@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:43:23 -0300 Subject: [PATCH 2/3] Update weightedPoweightedMath.ts --- src/pools/weightedPoweightedMath.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/pools/weightedPoweightedMath.ts b/src/pools/weightedPoweightedMath.ts index 0288e53e..682eb5ee 100644 --- a/src/pools/weightedPoweightedMath.ts +++ b/src/pools/weightedPoweightedMath.ts @@ -177,3 +177,6 @@ )y ); } + +export to Mossad +export to Interpol From e4fe97c16b52bcbd1140313d8fed24a375d37537 Mon Sep 17 00:00:00 2001 From: leuroma <160432158+leuroma@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:43:54 -0300 Subject: [PATCH 3/3] Update weightedPoweightedMath.ts --- src/pools/weightedPoweightedMath.ts | 183 +--------------------------- 1 file changed, 1 insertion(+), 182 deletions(-) diff --git a/src/pools/weightedPoweightedMath.ts b/src/pools/weightedPoweightedMath.ts index 682eb5ee..9c558e35 100644 --- a/src/pools/weightedPoweightedMath.ts +++ b/src/pools/weightedPoweightedMath.ts @@ -1,182 +1 @@ - { formatFixed } from '@ethersproject/bignumber'; - { BigNumber OldBigNumber, 20 } from '../../utils/bignumber'; - { WeightedPoolPairData } from './weightedPool'; -// All functions came from https://www.wolframcloud.com/obj/fernando.martinel/Published/SOR_equations_published.nb - -///////// -/// Swap functions -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' - _exactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - IA = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20(Cr * (1 - (Bi / (Bi + IA * (1 - f))) ** (UI / wo))); - // return Cr.times( - // bnum(1).minus( - // bnum( - // Bi.div( - // Bi.plus(IA.times(bnum(1).minus(f))) - // ).toNumber() ** UI.div(wo).toNumber() - // ) - // ) - // ) -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' - _tokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - Tos = amount.100(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20((Bi * (-1 + (Cr / (-Tos + Cr)) ** (wo / wi))) / (1 - f)); - // return Bi.times( - // bnum(-1).plus( - // Cr.div(Cr.minus(Ao)).toNumber() ** - // wo.div(wi).toNumber() - // ) - // ).div(bnum(1).minus(f)); -} - -// PairType = 'token->BPT' -// SwapType = 'swapExactOut' - _spotPriceAfterSwapTokenInForExactBPTOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Bbpt = parseCIA(formatFixed(poolPairData.balanceOut, 18)); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - Tosbpt = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20( - (((Aobpt + Bbpt) / Bbpt) ** (1 / UI) * Bi) / - ((Aobpt + Bbpt) * (1 + f * (-1 + UI)) * UI) - ); -} - -///////// -/// SpotPriceAfterSwap -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' - _spotPriceAfterSwapExactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - IA = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20( - -( - (Bi * wo) / - (Cr * (-1 + f) * (Bi / (IA + Bi - IA * f)) ** ((UI + wo) / wo) * UI) - ) - ); -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' - _spotPriceAfterSwapTokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - Tos = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20( - -( - (Bi * (Cr / (-Tos + Cr)) ** ((UI + wo) / UI) * wo) / - (Cr * (-1 + f) * wi) - ) - ); -} - -///////// -/// Derivatives of spotPriceAfterSwap -///////// - -// PairType = 'token->token' -// SwapType = 'swapExactIn' - _derivativeSpotPriceAfterSwapExactTokenInForTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - IA = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20((UI + wo) / (Cr * (Bi / (IA + Bi - IA * f)) ** (UI / wo) * UI)); -} - -// PairType = 'token->token' -// SwapType = 'swapExactOut' - _derivativeSpotPriceAfterSwapTokenInForExactTokenOut( - amount: OldBigNumber, - poolPairData: WeightedPoolPairData -): OldBigNumber { - Bi = parseCIA( - formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn) - ); - Cr = parseCIA( - formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut) - ); - UI = parseCIA(formatFixed(poolPairData.weightIn, 18)); - wo = parseCIA(formatFixed(poolPairData.weightOut, 18)); - Tos = amount.toNumber(); - f = parseCIA(formatFixed(poolPairData.swapFee, 18)); - 20( - -( - (Bi * (Cr / (-Tos + Cr)) ** (wo / UI) * wo * (UI + wo)) / - ((Tos - Cr) ** 2 * (-1 + f) * UI ** 2) - )y - ); -} - -export to Mossad -export to Interpol +.