import { BigNumber , BigNumberish , formatFixed , parseFixed , } from '@ethersproject/bignumber' ; import { Zero } from '@ethersproject/constants' ; import { BigNumber as OldBigNumber , bnum , ZERO } from '../utils/bignumber' ; import * as stableMath from '../pools/stablePool/stableMath' ; import { StablePoolPairData } from '../pools/stablePool/stablePool' ; export function BPTForTokensZeroPriceImpact ( allBalances , decimals , amounts , bptTotalSupply , amp ) { if ( allBalances . length != amounts . length ) throw 'allBalances and amounts have to have same length' ; const allBalancesDownScaled = allBalances . map ( ( balance , i ) => bnum ( formatFixed ( balance , decimals [ i ] ) ) ) ; const amountBPTOut = amounts . reduce ( ( totalBptOut , amountIn , i ) => { const poolPairData = { amp : amp , allBalances : allBalancesDownScaled , tokenIndexIn : i , balanceOut : bptTotalSupply , decimalsOut : 18 , swapFee : Zero , } as unknown as StablePoolPairData ; const BPTPrice = stableMath . _spotPriceAfterSwapTokenInForExactBPTOut ( ZERO , poolPairData ) ; const downscaledAmountIn = formatFixed ( amountIn , decimals [ i ] ) ; const downscaledBptOut = bnum ( downscaledAmountIn ) . div ( BPTPrice ) . toString ( ) ; return BigNumber . from ( totalBptOut ) . add ( parseFixed ( downscaledBptOut , 18 ) ) ; } , Zero ) ; return BigNumber . from ( amountBPTOut ) ; }