import BigNumber from 'bignumber.js' ; function getAmountOut ( amountIn , reservesIn , reservesOut ) { const amountOut = amountIn . eq ( 0 ) ? new BigNumber ( 0 ) : reservesOut . minus ( reservesOut . multipliedBy ( reservesIn ) . dividedBy ( reservesIn . plus ( amountIn . multipliedBy ( 0.997 ) ) ) ) ; return { amountOut , reservesInAfter : reservesIn . plus ( amountIn ) , reservesOutAfter : reservesOut . minus ( amountOut ) } ; } function getAmountIn ( amountOut , reservesIn , reservesOut ) { const amountIn = amountOut . eq ( 0 ) ? new BigNumber ( 0 ) : amountOut . isGreaterThanOrEqualTo ( reservesOut ) ? new BigNumber ( Infinity ) : reservesIn . multipliedBy ( reservesOut ) . dividedBy ( reservesOut . minus ( amountOut ) ) . minus ( reservesIn ) . dividedBy ( 0.997 ) ; return { amountIn , reservesInAfter : reservesIn . plus ( amountIn ) , reservesOutAfter : reservesOut . minus ( amountOut ) } ; } export function computeBidsAsks ( baseReserves , quoteReserves , numSegments = 200 ) { if ( baseReserves . eq ( 0 ) || quoteReserves . eq ( 0 ) ) { return { bids : [ ] , asks : [ ] } ; } const increment = baseReserves . dividedBy ( numSegments + 1 ) ; const baseAmounts = Array . from ( { length : numSegments } , ( x , i ) : BigNumber => increment . multipliedBy ( i ) ) ; const bids = baseAmounts . map ( ( buyBaseAmount ) : [ string , string ] => { const { reservesInAfter : baseReservesBefore , reservesOutAfter : quoteReservesBefore } = getAmountOut ( buyBaseAmount , baseReserves , quoteReserves ) ; const { amountOut } = getAmountOut ( increment , baseReservesBefore , quoteReservesBefore ) ; return [ increment . toString ( ) , amountOut . dividedBy ( increment ) . toString ( ) ] ; } ) ; const asks = baseAmounts . map ( ( sellBaseAmount ) : [ string , string ] => { const { reservesInAfter : baseReservesBefore , reservesOutAfter : quoteReservesBefore } = getAmountIn ( sellBaseAmount , quoteReserves , baseReserves ) ; const { amountIn } = getAmountIn ( increment , baseReservesBefore , quoteReservesBefore ) ; return [ increment . toString ( ) , amountIn . dividedBy ( increment ) . toString ( ) ] ; } ) ; return { bids , asks } ; }