Skip to content

fix(futarchy-amm): correct fee accounting for both swap legs#2

Open
meta-reid wants to merge 2 commits into
masterfrom
fix/futarchy-amm-fee-accounting
Open

fix(futarchy-amm): correct fee accounting for both swap legs#2
meta-reid wants to merge 2 commits into
masterfrom
fix/futarchy-amm-fee-accounting

Conversation

@meta-reid
Copy link
Copy Markdown

@meta-reid meta-reid commented Jun 1, 2026

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), where AVG(price) was a single unweighted average blended across all DAOs and PASS/FAIL conditional tokens (the aggregate CTE has no GROUP 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_amount for buys (USDC in), output_amount for sells (USDC out). For any single swap tokens × 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)

  • Revenue = Fees = 0.5% is correct: MetaDAO is the sole LP of the spot pools, so both the protocol fee and the LP fee accrue to the protocol.
  • Saved Dune query 6556188 has a minor residual version of Bug 1 in its token_fees_usdc column (reconstructs via per-token average_price instead of SUM(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)

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>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

The futarchy-amm adapter exports:

> adapters@1.0.0 test
> ts-node --transpile-only cli/testAdapter.ts fees futarchy-amm

🦙 Running FUTARCHY-AMM adapter 🦙
---------------------------------------------------
Start Date:	Sun, 31 May 2026 00:00:00 GMT
End Date:	Mon, 01 Jun 2026 00:00:00 GMT
---------------------------------------------------

------ ERROR ------
Error: DUNE_API_KEYS environment variable is not set
    at getAxiosDune (/home/runner/work/dimension-adapters/dimension-adapters/helpers/dune.ts:17:11)
    at submitQuery (/home/runner/work/dimension-adapters/dimension-adapters/helpers/dune.ts:81:33)
DUNE_API_KEYS environment variable is not set

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

The futarchy-amm adapter exports:

> adapters@1.0.0 test
> ts-node --transpile-only cli/testAdapter.ts fees futarchy-amm

🦙 Running FUTARCHY-AMM adapter 🦙
---------------------------------------------------
Start Date:	Sun, 31 May 2026 00:00:00 GMT
End Date:	Mon, 01 Jun 2026 00:00:00 GMT
---------------------------------------------------

------ ERROR ------
Error: DUNE_API_KEYS environment variable is not set
    at getAxiosDune (/home/runner/work/dimension-adapters/dimension-adapters/helpers/dune.ts:17:11)
    at submitQuery (/home/runner/work/dimension-adapters/dimension-adapters/helpers/dune.ts:81:33)
DUNE_API_KEYS environment variable is not set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants