import { FixedPointNumber } from '@acala-network/sdk-core' ; import { Fee } from './types' ; export function getTargetAmount ( supplyPool , targetPool , supplyAmount , exchangeFee ) { if ( supplyAmount . isZero ( ) || supplyPool . isZero ( ) || targetPool . isZero ( ) ) return FixedPointNumber . ZERO ; const supplyAmountWithFee = supplyAmount . times ( exchangeFee . denominator . minus ( exchangeFee . numerator ) ) ; const numerator = supplyAmountWithFee . times ( targetPool ) ; const denominator = supplyPool . times ( exchangeFee . denominator ) . plus ( supplyAmountWithFee ) ; if ( denominator . isZero ( ) ) return FixedPointNumber . ZERO ; return numerator . div ( denominator ) ; } export function getSupplyAmount ( supplyPool , targetPool , targetAmount , exchangeFee ) { if ( targetAmount . isZero ( ) || supplyPool . isZero ( ) || targetPool . isZero ( ) ) return FixedPointNumber . ZERO ; const { numerator : feeNumerator , denominator : feeDenominator } = exchangeFee ; const numerator = supplyPool . times ( targetAmount ) . times ( feeDenominator ) ; const denominator = targetPool . minus ( targetAmount ) . times ( feeDenominator . minus ( feeNumerator ) ) . max ( FixedPointNumber . ZERO ) ; if ( denominator . isZero ( ) ) return FixedPointNumber . ZERO ; return FixedPointNumber . fromInner ( numerator . div ( denominator ) . _getInner ( ) . toNumber ( ) + 1 , numerator . getPrecision ( ) ) ; }