mirror of
https://github.com/zoo-labs/zoo.git
synced 2026-11-10 22:32:34 +00:00
83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
import {
|
|
ALLOWED_PRICE_IMPACT_HIGH,
|
|
ALLOWED_PRICE_IMPACT_LOW,
|
|
ALLOWED_PRICE_IMPACT_MEDIUM,
|
|
BLOCKED_PRICE_IMPACT_NON_EXPERT,
|
|
} from '../constants'
|
|
import { ChainId, Currency, CurrencyAmount, Fraction, JSBI, Percent, Trade, TradeType } from '@zoolabs/zdk'
|
|
|
|
import { Field } from '../state/swap/actions'
|
|
import { basisPointsToPercent } from './convert'
|
|
|
|
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 = ONE_HUNDRED_PERCENT.subtract(THIRTY_BIPS_FEE)
|
|
|
|
const TWENTY_FIVE_BIPS_FEE = new Percent(JSBI.BigInt(25), JSBI.BigInt(10000))
|
|
const FIVE_BIPS_FEE = new Percent(JSBI.BigInt(5), JSBI.BigInt(10000))
|
|
|
|
export function formatExecutionPrice(
|
|
trade: Trade<Currency, Currency, TradeType>,
|
|
inverted?: boolean,
|
|
chainId?: ChainId
|
|
): string {
|
|
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
|
|
}`
|
|
}
|
|
|
|
// computes realized lp fee as a percent
|
|
export function computeRealizedLPFeePercent(trade: Trade<Currency, Currency, TradeType>): Percent {
|
|
if (trade instanceof Trade) {
|
|
// 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<Percent>(
|
|
(currentFee: Percent): Percent => currentFee.multiply(INPUT_FRACTION_AFTER_FEE),
|
|
ONE_HUNDRED_PERCENT
|
|
)
|
|
)
|
|
return new Percent(percent.numerator, percent.denominator)
|
|
}
|
|
|
|
return new Percent(0, 0);
|
|
}
|
|
|
|
// computes price breakdown for the trade
|
|
export function computeRealizedLPFeeAmount(
|
|
trade?: Trade<Currency, Currency, TradeType> | null
|
|
): CurrencyAmount<Currency> | 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
|
|
}
|