import { formatFixed } from '@ethersproject/bignumber' ; import { BigNumber as OldBigNumber , bnum } from '../../utils/bignumber' ; import { ElementPoolPairData } from './elementPool' ; export function _exactTokenInForTokenOut ( amount , poolPairData ) { if ( amount . isZero ( ) ) return amount ; const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ai = amount . toNumber ( ) ; return bnum ( Bo - ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( 1 / ( 1 - t ) ) - Math . abs ( Ai - Bo + ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( 1 / ( 1 - t ) ) ) * f ) ; } export function _tokenInForExactTokenOut ( amount , poolPairData ) { if ( amount . isZero ( ) ) return amount ; const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ao = amount . toNumber ( ) ; return bnum ( - Bi + ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( 1 / ( 1 - t ) ) + Math . abs ( - Ao - Bi + ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( 1 / ( 1 - t ) ) ) * f ) ; } export function _spotPriceAfterSwapExactTokenInForTokenOut ( amount , poolPairData ) { const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ai = amount . toNumber ( ) ; return bnum ( 1 / ( ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( Ai + Bi ) ** t - Math . abs ( 1 - ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( Ai + Bi ) ** t ) * f ) ) ; } export function _spotPriceAfterSwapTokenInForExactTokenOut ( amount , poolPairData ) { const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ao = amount . toNumber ( ) ; return bnum ( ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( - Ao + Bo ) ** t + Math . abs ( - 1 + ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( - Ao + Bo ) ** t ) * f ) ; } export function _derivativeSpotPriceAfterSwapExactTokenInForTokenOut ( amount , poolPairData ) { const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ai = amount . toNumber ( ) ; return bnum ( - ( ( - ( ( ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 2 + 1 / ( 1 - t ) ) * ( - 1 + 1 / ( 1 - t ) ) * ( 1 - t ) ) / ( Ai + Bi ) ** ( 2 * t ) ) - ( Ai + Bi ) ** ( - 1 - t ) * ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) * t - f * Math . abs ( ( ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 2 + 1 / ( 1 - t ) ) * ( - 1 + 1 / ( 1 - t ) ) * ( 1 - t ) ) / ( Ai + Bi ) ** ( 2 * t ) + ( Ai + Bi ) ** ( - 1 - t ) * ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) * t ) ) / ( ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( Ai + Bi ) ** t - Math . abs ( 1 - ( Bi ** ( 1 - t ) - ( Ai + Bi ) ** ( 1 - t ) + Bo ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) / ( Ai + Bi ) ** t ) * f ) ** 2 ) ) ; } export function _derivativeSpotPriceAfterSwapTokenInForExactTokenOut ( amount , poolPairData ) { const f = parseFloat ( formatFixed ( poolPairData . swapFee , 18 ) ) ; const Bi = parseFloat ( formatFixed ( poolPairData . balanceIn , poolPairData . decimalsIn ) ) ; const Bo = parseFloat ( formatFixed ( poolPairData . balanceOut , poolPairData . decimalsOut ) ) ; const t = getTimeTillExpiry ( poolPairData . expiryTime , poolPairData . currentBlockTimestamp , poolPairData . unitSeconds ) ; const Ao = amount . toNumber ( ) ; return bnum ( ( ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 2 + 1 / ( 1 - t ) ) * ( - 1 + 1 / ( 1 - t ) ) * ( 1 - t ) ) / ( - Ao + Bo ) ** ( 2 * t ) + ( - Ao + Bo ) ** ( - 1 - t ) * ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) * t + f * Math . abs ( ( ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 2 + 1 / ( 1 - t ) ) * ( - 1 + 1 / ( 1 - t ) ) * ( 1 - t ) ) / ( - Ao + Bo ) ** ( 2 * t ) + ( - Ao + Bo ) ** ( - 1 - t ) * ( Bi ** ( 1 - t ) + Bo ** ( 1 - t ) - ( - Ao + Bo ) ** ( 1 - t ) ) ** ( - 1 + 1 / ( 1 - t ) ) * t ) ) ; } export function getTimeTillExpiry ( expiryTime , currentBlockTimestamp , unitSeconds ) { let t = currentBlockTimestamp < expiryTime ? expiryTime - currentBlockTimestamp : 0 ; t = t / unitSeconds ; return t ; }