Closed esteblock closed 5 months ago
The main code we can find it on v2-sdk
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount)
Where midprice is :
public get midPrice(): Price<TInput, TOutput> {
if (this._midPrice !== null) return this._midPrice
const prices: Price<Currency, Currency>[] = []
for (const [i, pair] of this.pairs.entries()) {
prices.push(
this.path[i].equals(pair.token0)
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient)
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient)
)
}
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0])
return (this._midPrice = new Price(this.input, this.output, reduced.denominator, reduced.numerator))
}
The computePriceImpact
function is defined on sdk-core
as follows:
/**
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
* @param midPrice mid price before the trade
* @param inputAmount the input amount of the trade
* @param outputAmount the output amount of the trade
*/
export function computePriceImpact<TBase extends Currency, TQuote extends Currency>(
midPrice: Price<TBase, TQuote>,
inputAmount: CurrencyAmount<TBase>,
outputAmount: CurrencyAmount<TQuote>
): Percent {
const quotedOutputAmount = midPrice.quote(inputAmount)
// calculate price impact := (exactQuote - outputAmount) / exactQuote
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount)
return new Percent(priceImpact.numerator, priceImpact.denominator)
}
Also they substract the fees
//src/utils/prices.ts
export function computeRealizedPriceImpact(trade: Trade<Currency, Currency, TradeType>): Percent {
const realizedLpFeePercent = computeRealizedLPFeePercent(trade)
return trade.priceImpact.subtract(realizedLpFeePercent)
}
//...
where computeRealizedLPFeePercent
is defined as follows:
// computes realized lp fee as a percent
function computeRealizedLPFeePercent(trade: Trade<Currency, Currency, TradeType>): Percent {
let percent: Percent
// Since routes are either all v2 or all v3 right now, calculate separately
if (trade.swaps[0].route.pools instanceof Pair) {
// 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))
percent = ONE_HUNDRED_PERCENT.subtract(
trade.swaps.reduce<Percent>(
(currentFee: Percent): Percent => currentFee.multiply(INPUT_FRACTION_AFTER_FEE),
ONE_HUNDRED_PERCENT
)
)
} else {
percent = ZERO_PERCENT
for (const swap of trade.swaps) {
const { numerator, denominator } = swap.inputAmount.divide(trade.inputAmount)
const overallPercent = new Percent(numerator, denominator)
const routeRealizedLPFeePercent = overallPercent.multiply(
ONE_HUNDRED_PERCENT.subtract(
swap.route.pools.reduce<Percent>((currentFee: Percent, pool): Percent => {
const fee =
pool instanceof Pair
? // not currently possible given protocol check above, but not fatal
FeeAmount.MEDIUM
: pool.fee
return currentFee.multiply(ONE_HUNDRED_PERCENT.subtract(new Fraction(fee, 1_000_000)))
}, ONE_HUNDRED_PERCENT)
)
)
percent = percent.add(routeRealizedLPFeePercent)
}
}
return new Percent(percent.numerator, percent.denominator)
}