import { useMemo } from 'react' ; import JSBI from 'jsbi' ; import { Trade as V2Trade } from '@uniswap/v2-sdk' ; import { Percent , Fraction , CurrencyAmount , Currency } from '@uniswap/sdk-core' ; import type { Trade } from '../../types' ; const BASE_FEE = new Percent ( JSBI . BigInt ( 30 ) , 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 ) ; function computeRealizedLPFeePercent ( trade ) { if ( trade instanceof V2Trade ) { const percent = ONE_HUNDRED_PERCENT . subtract ( trade . route . pairs . reduce < Percent > ( ( currentFee ) : Percent => currentFee . multiply ( INPUT_FRACTION_AFTER_FEE ) , ONE_HUNDRED_PERCENT ) ) ; return new Percent ( percent . numerator , percent . denominator ) ; } else { const percent = ONE_HUNDRED_PERCENT . subtract ( trade . route . pools . reduce < Percent > ( ( currentFee , pool ) : Percent => currentFee . multiply ( ONE_HUNDRED_PERCENT . subtract ( new Fraction ( pool . fee , 1000000 ) ) ) , ONE_HUNDRED_PERCENT ) ) ; return new Percent ( percent . numerator , percent . denominator ) ; } } function computeRealizedLPFeeAmount ( trade ? ) { if ( trade ) { const realizedLPFee = computeRealizedLPFeePercent ( trade ) ; return CurrencyAmount . fromRawAmount ( trade . inputAmount . currency , trade . inputAmount . multiply ( realizedLPFee ) . quotient ) ; } return undefined ; } export function useTradeBreakdown ( trade ) { return useMemo ( ( ) => { if ( ! trade ) return null ; const realizedLPFeePercent = computeRealizedLPFeePercent ( trade ) ; const realizedLPFeeAmount = computeRealizedLPFeeAmount ( trade ) ; return { realizedLPFeePercent , realizedLPFeeAmount , realizedLPFee : trade . inputAmount . multiply ( realizedLPFeePercent ) as CurrencyAmount < Currency > , priceImpact : trade . priceImpact . subtract ( realizedLPFeePercent ) as Percent , } ; } , [ trade ] ) ; }