import { Fixed18 } from './fixed-18' ; interface Pool { other : Fixed18 ; base : Fixed18 ; } const ONE = Fixed18 . fromNatural ( 1 ) ; name description param { number } supply param { number } supplyPool param { number } targetPool param { Fixed18 } feeRate param { Fixed18 } slippage returns { number } export function calcSwapTargetAmount ( supply , supplyPool , targetPool , feeRate , slippage = Fixed18 . ZERO ) { const newTargetPool = Fixed18 . fromRational ( supplyPool , supplyPool + supply ) . mul ( Fixed18 . fromNatural ( targetPool ) ) . toNumber ( 0 , 3 ) ; if ( newTargetPool <= 0 ) return 0 ; const receivedAmount = targetPool - newTargetPool ; let fee = feeRate . mul ( Fixed18 . fromNatural ( receivedAmount ) ) . toNumber ( 0 , 3 ) ; fee = fee <= 0 ? 0 : fee ; return ONE . sub ( slippage ) . mul ( Fixed18 . fromNatural ( receivedAmount - fee ) ) . toNumber ( 0 , 3 ) ; } name description param { number } target param { number } supplyPool param { number } targetPool param { Fixed18 } feeRate param { Fixed18 } slippage returns { number } export function calcSwapSupplyAmount ( target , supplyPool , targetPool , feeRate , slippage = Fixed18 . ZERO ) { if ( target === 0 ) return 0 ; target = Fixed18 . fromNatural ( target ) . div ( ONE . sub ( slippage ) ) . toNumber ( 0 , 3 ) ; const rate = ONE . div ( ONE . sub ( feeRate ) ) . decimalPlaces ( 18 , 3 ) ; const newTargetPool = targetPool - rate . mul ( Fixed18 . fromNatural ( target ) ) . toNumber ( 0 , 3 ) ; const result = Fixed18 . fromRational ( supplyPool , newTargetPool ) . add ( Fixed18 . fromParts ( 1 ) ) . mul ( Fixed18 . fromNatural ( targetPool ) ) . toNumber ( 0 , 3 ) - supplyPool ; return result < 0 ? 0 : result ; } name description param { Fixed18 } supply param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcTargetInOtherToBase ( supply , pool , feeRate , slippage = Fixed18 . ZERO ) { const result = calcSwapTargetAmount ( supply . innerToNumber ( ) , pool . other . innerToNumber ( ) , pool . base . innerToNumber ( ) , feeRate , slippage ) ; return Fixed18 . fromParts ( result ) ; } name description param { Fixed18 } supply param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcSupplyInOtherToBase ( target , pool , feeRate , slippage = Fixed18 . ZERO ) { const result = calcSwapSupplyAmount ( target . innerToNumber ( ) , pool . other . innerToNumber ( ) , pool . base . innerToNumber ( ) , feeRate , slippage ) ; return Fixed18 . fromParts ( result ) ; } name description param { Fixed18 } supply param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcTargetInBaseToOther ( supply , pool , feeRate , slippage = Fixed18 . ZERO ) { const result = calcSwapTargetAmount ( supply . innerToNumber ( ) , pool . base . innerToNumber ( ) , pool . other . innerToNumber ( ) , feeRate , slippage ) ; return Fixed18 . fromParts ( result ) ; } name description param { Fixed18 } target param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcSupplyInBaseToOther ( target , pool , feeRate , slippage = Fixed18 . ZERO ) { const result = calcSwapSupplyAmount ( target . innerToNumber ( ) , pool . base . innerToNumber ( ) , pool . other . innerToNumber ( ) , feeRate , slippage ) ; return Fixed18 . fromParts ( result ) ; } name description param { Fixed18 } supply param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcTargetInOtherToOther ( supply , supplyPool , targetPool , feeRate , slippage = Fixed18 . ZERO ) { const intermediateBaseCurrencyAmount = calcSwapTargetAmount ( supply . innerToNumber ( ) , supplyPool . other . innerToNumber ( ) , supplyPool . base . innerToNumber ( ) , feeRate ) ; const targetCurrencyAmount = calcSwapTargetAmount ( intermediateBaseCurrencyAmount , targetPool . base . innerToNumber ( ) , targetPool . other . innerToNumber ( ) , feeRate ) ; return ONE . sub ( slippage ) . mul ( Fixed18 . fromParts ( targetCurrencyAmount ) ) ; } name description param { Fixed18 } target param { Fixed18 } pool . other param { Fixed18 } pool . base param { Fixed18 } feeRate param { Fixed18 } slippage returns { Fixed } export function calcSupplyInOtherToOther ( target , supplyPool , targetPool , feeRate , slippage = Fixed18 . ZERO ) { const _target = target . div ( ONE . sub ( slippage ) ) ; const intermediateBaseCurrencyAmount = calcSwapSupplyAmount ( _target . innerToNumber ( ) , targetPool . base . innerToNumber ( ) , targetPool . other . innerToNumber ( ) , feeRate ) ; const targetCurrencyAmount = calcSwapSupplyAmount ( intermediateBaseCurrencyAmount , supplyPool . other . innerToNumber ( ) , supplyPool . base . innerToNumber ( ) , feeRate ) ; return ONE . sub ( slippage ) . mul ( Fixed18 . fromParts ( targetCurrencyAmount ) ) ; }