`computeRealizedLPFeePercent` may be calculating LP fee take for a route incorrectly
Open
Nobody has claimed this yet.
bug
- Dominant language
- TypeScript
- Stars
- 5.5k
- Forks
- 5.4k
- PR merge metrics
- No merged PRs in 30d
Description
The current logic calculates fees as though the whole route were v2 or v3. Since the router recently updated to support interleaving routes this is no longer a safe assumption.
see:
// Since routes are either all v2 or all v3 right now, calculate separately
and
if (trade.swaps[0].route.pools instanceof (v2)Pair) {
// computes realized lp fee as a percent
export 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)
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.