import { formatFixed } from '@ethersproject/bignumber' ; import { BigNumber as OldBigNumber , bnum } from '../../utils/bignumber' ; import { WeightedPoolPairData } from './weightedPool' ; export function _exactTokenInForTokenOut ( amount , poolPairData ) { 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 ) ) ) ; } export function _tokenInForExactTokenOut ( amount , poolPairData ) { 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 ) ) ; } export function _spotPriceAfterSwapTokenInForExactBPTOut ( amount , poolPairData ) { 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 ) ) ; } export function _spotPriceAfterSwapExactTokenInForTokenOut ( amount , poolPairData ) { 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 ) ) ) ; } export function _spotPriceAfterSwapTokenInForExactTokenOut ( amount , poolPairData ) { 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 ) ) ) ; } export function _derivativeSpotPriceAfterSwapExactTokenInForTokenOut ( amount , poolPairData ) { 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 ) ) ; } export function _derivativeSpotPriceAfterSwapTokenInForExactTokenOut ( amount , poolPairData ) { 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 ) ) ) ; }