import { CurrencyAmount , Fraction , JSBI , Percent , TokenAmount , Trade } from '@pancakeswap-libs/sdk' ; import { BLOCKED_PRICE_IMPACT_NON_EXPERT , ALLOWED_PRICE_IMPACT_HIGH , ALLOWED_PRICE_IMPACT_LOW , ALLOWED_PRICE_IMPACT_MEDIUM , } from '../constants' ; import { Field } from '../state/swap/actions' ; import { basisPointsToPercent } from './index' ; const BASE_FEE = new Percent ( JSBI . BigInt ( 20 ) , JSBI . BigInt ( 10000 ) ) ; const ONE_HUNDRED_PERCENT = new Percent ( JSBI . BigInt ( 10000 ) , JSBI . BigInt ( 10000 ) ) ; const INPUT_FRACTION_AFTER_FEE = ONE_HUNDRED_PERCENT . subtract ( BASE_FEE ) ; export function computeTradePriceBreakdown ( trade ? ) { const realizedLPFee = ! trade ? undefined : ONE_HUNDRED_PERCENT . subtract ( trade . route . pairs . reduce < Fraction > ( ( currentFee ) : Fraction => currentFee . multiply ( INPUT_FRACTION_AFTER_FEE ) , ONE_HUNDRED_PERCENT ) ) ; const priceImpactWithoutFeeFraction = trade && realizedLPFee ? trade . priceImpact . subtract ( realizedLPFee ) : undefined ; const priceImpactWithoutFeePercent = priceImpactWithoutFeeFraction ? new Percent ( priceImpactWithoutFeeFraction ?. numerator , priceImpactWithoutFeeFraction ?. denominator ) : undefined ; const realizedLPFeeAmount = realizedLPFee && trade && ( trade . inputAmount instanceof TokenAmount ? new TokenAmount ( trade . inputAmount . token , realizedLPFee . multiply ( trade . inputAmount . raw ) . quotient ) : CurrencyAmount . ether ( realizedLPFee . multiply ( trade . inputAmount . raw ) . quotient ) ) ; return { priceImpactWithoutFee : priceImpactWithoutFeePercent , realizedLPFee : realizedLPFeeAmount } ; } export function computeSlippageAdjustedAmounts ( trade , allowedSlippage ) { const pct = basisPointsToPercent ( allowedSlippage ) ; return { [ Field . INPUT ] : trade ?. maximumAmountIn ( pct ) , [ Field . OUTPUT ] : trade ?. minimumAmountOut ( pct ) , } ; } export function warningSeverity ( priceImpact ) { if ( ! priceImpact ?. lessThan ( BLOCKED_PRICE_IMPACT_NON_EXPERT ) ) return 4 ; if ( ! priceImpact ?. lessThan ( ALLOWED_PRICE_IMPACT_HIGH ) ) return 3 ; if ( ! priceImpact ?. lessThan ( ALLOWED_PRICE_IMPACT_MEDIUM ) ) return 2 ; if ( ! priceImpact ?. lessThan ( ALLOWED_PRICE_IMPACT_LOW ) ) return 1 ; return 0 ; } export function formatExecutionPrice ( trade ? , inverted ? ) { if ( ! trade ) { return '' ; } return inverted ? `${ trade . executionPrice . invert ( ) . toSignificant ( 6 ) } ${ trade . inputAmount . currency . symbol } / ${ trade . outputAmount . currency . symbol }` : `${ trade . executionPrice . toSignificant ( 6 ) } ${ trade . outputAmount . currency . symbol } / ${ trade . inputAmount . currency . symbol }` ; }