import JSBI from "jsbi"; import { Currency, CurrencyAmount, Percent, TradeType, } from "@uniswap/sdk-core"; import { Trade as V2Trade } from "@uniswap/v2-sdk"; import { ALLOWED_PRICE_IMPACT_HIGH, ALLOWED_PRICE_IMPACT_LOW, ALLOWED_PRICE_IMPACT_MEDIUM, BLOCKED_PRICE_IMPACT_NON_EXPERT, } from "../constants/misc"; const THIRTY_BIPS_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 = (bpsFee?: Percent) => ONE_HUNDRED_PERCENT.subtract(bpsFee ?? THIRTY_BIPS_FEE); // computes realized lp fee as a percent export function computeRealizedLPFeePercent( trade: V2Trade, bpsLpFee?: number | string ): Percent { // for each hop in our trade, take away the x*y=k price impact from 0.3% fees // e.g. for 3 tokens/2 hops: 1 - ((1 - .03) * (1-.03)) const percent = ONE_HUNDRED_PERCENT.subtract( trade.route.pairs.reduce( (currentFee: Percent): Percent => currentFee.multiply( INPUT_FRACTION_AFTER_FEE( bpsLpFee ? new Percent(JSBI.BigInt(bpsLpFee), JSBI.BigInt(10000)) : undefined ) ), ONE_HUNDRED_PERCENT ) ); return new Percent(percent.numerator, percent.denominator); } // computes price breakdown for the trade export function computeRealizedLPFeeAmount( trade?: V2Trade | null ): CurrencyAmount | undefined { if (trade) { const realizedLPFee = computeRealizedLPFeePercent(trade); // the amount of the input that accrues to LPs return CurrencyAmount.fromRawAmount( trade.inputAmount.currency, trade.inputAmount.multiply(realizedLPFee).quotient ); } return undefined; } const IMPACT_TIERS = [ BLOCKED_PRICE_IMPACT_NON_EXPERT, ALLOWED_PRICE_IMPACT_HIGH, ALLOWED_PRICE_IMPACT_MEDIUM, ALLOWED_PRICE_IMPACT_LOW, ]; type WarningSeverity = 0 | 1 | 2 | 3 | 4; export function warningSeverity( priceImpact: Percent | undefined ): WarningSeverity { if (!priceImpact) return 4; let impact: WarningSeverity = IMPACT_TIERS.length as WarningSeverity; for (const impactLevel of IMPACT_TIERS) { if (impactLevel.lessThan(priceImpact)) return impact; impact--; } return 0; }