fix(futarchy-amm): correct fee accounting for both swap legs#2
Open
meta-reid wants to merge 2 commits into
Open
fix(futarchy-amm): correct fee accounting for both swap legs#2meta-reid wants to merge 2 commits into
meta-reid wants to merge 2 commits into
Conversation
The futarchy AMM charges a 0.5% taker fee on the INPUT of every swap (0.25% protocol + 0.25% LP), routed to the quote balance for buys and the base balance for sells. Two fee bugs distorted reported fees: 1. Live path (helpers/queries/futarchy.sql, dates after 2026-04-07): the sell leg was valued as SUM(sell tokens) * AVG(price), where AVG(price) was a single unweighted average blended across ALL DAOs and PASS/FAIL conditional tokens (no GROUP BY token). With token prices differing by orders of magnitude across DAOs, this grossly overstated sell fees. Now values each leg by its real USDC notional: input_amount for buys (USDC in), output_amount for sells (USDC out). 2. Historical path (fees/futarchy-amm/index.ts, dates on/before 2026-04-07): only the sell leg (token_fees_usdc) was added; the buy leg (usdc_fees) was dropped, under-counting fees. Since every SpotSwap pays the fee regardless of direction, both legs now count. Revenue treatment (Fees = Revenue = 0.5%) is unchanged and correct: MetaDAO is the sole LP of the spot pools, so both the protocol and LP portions accrue to the protocol. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The futarchy-amm adapter exports: |
|
The futarchy-amm adapter exports: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The futarchy AMM charges a 0.5% taker fee on the input of every swap (0.25% protocol + 0.25% LP), routed to the quote balance for buys and the base balance for sells — confirmed against the deployed program source (
programs/futarchy/src/state/futarchy_amm.rs,Pool::swap). Two bugs distorted the fees we report to DefiLlama.Bug 1 — live path (
helpers/queries/futarchy.sql, dates after 2026-04-07)The sell leg was valued as
SUM(sell tokens) * AVG(price), whereAVG(price)was a single unweighted average blended across all DAOs and PASS/FAIL conditional tokens (the aggregate CTE has noGROUP BY token). Because token prices differ by orders of magnitude across DAOs, multiplying a token-count sum (dominated by the cheapest token) by an average price (lifted by the most expensive) produced a USDC figure tied to no real trade — grossly overstating sell fees on any multi-DAO day.Fix: value each leg by its real USDC notional —
input_amountfor buys (USDC in),output_amountfor sells (USDC out). For any single swaptokens × realized_price == output_amount, so this is exact, not an approximation.Bug 2 — historical path (
fees/futarchy-amm/index.ts, dates on/before 2026-04-07)Only the sell leg (
token_fees_usdc) was added; the buy leg (usdc_fees) was silently dropped, under-counting fees. Since every SpotSwap pays the fee regardless of direction, both legs now count.Not changed (intentional)
6556188has a minor residual version of Bug 1 in itstoken_fees_usdccolumn (reconstructs via per-tokenaverage_priceinstead ofSUM(sell output_amount)). It is bounded because it groups by token, so it's a sub-percent precision issue, not the gross distortion. Tightening it requires editing the query in Dune (not in this repo) — tracked as a follow-up.Verification
pnpm test fees futarchy-amm <date after 2026-04-07>(exercises the SQL path)pnpm test fees futarchy-amm <date on/before 2026-04-07>(exercises the historical path)