Skip to content

feat(dex): integrate Agra DEX (ethereum, hyperevm) - #9839

Closed
cxheng315 wants to merge 9 commits into
duneanalytics:mainfrom
cxheng315:agra-dex-integration
Closed

feat(dex): integrate Agra DEX (ethereum, hyperevm)#9839
cxheng315 wants to merge 9 commits into
duneanalytics:mainfrom
cxheng315:agra-dex-integration

Conversation

@cxheng315

Copy link
Copy Markdown
Contributor

Thank you for contributing to Spellbook 🪄

Description

Integrates Agra, a Seaport-fork order-settlement (direct) DEX, into the DEX sector for ethereum and hyperevm.

What's included

  • New source agra_multichain (sources/_sector/dex/trades/ethereum/_sources.yml), with settlement_evt_orderfulfilled and settlement_evt_ordersmatched. A single multichain source serves both chains (the decoded table is multichain).
  • Platform base trades for both chains, generated from a shared agra_base_trades(blockchain, start_date) macro — each chain model is config + a one-line macro call.
  • Registered agra in dex_ethereum_base_trades / dex_hyperevm_base_trades and in dex_info.
  • Schema tests (unique_combination_of_columns, check_dex_base_trades_seed) + seeds per chain.
  • Branded agra.trades project view via dex_project_trades('agra') — inherits amount_usd from the sector enrichment.

Notes

  • Decodes Seaport OrderFulfilled (offer → token_bought, consideration → token_sold) and dedupes the aggregate taker leg of matchOrders settlements.
  • volume_usd comes from the standard sector enrichment in dex.trades; the project view just filters the enriched per-chain trades.
  • Start date: 2026-03-01.

🤖 Generated with Claude Code

cxheng315 and others added 8 commits June 27, 2026 16:17
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Drop the duplicated taker aggregate leg of Seaport matchOrders settlements
using settlement_evt_ordersmatched; set agra_start_date to 2026-03-01.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collapse the near-identical agra_ethereum/agra_hyperevm base_trades models
(105 lines each, differing only by chain) into a parameterized
agra_base_trades(blockchain, start_date) macro. Each chain model is now
config + a one-line macro call. Compiled SQL is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a branded agra.trades view via dex_project_trades('agra'), unioning the
enriched dex_<chain>_trades filtered to project = 'agra'. Inherits amount_usd
from the sector enrichment; currently covers ethereum and hyperevm. Register
the view in dex_trades_views_schema.yml.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions github-actions Bot added WIP work in progress dbt: dex covers the DEX dbt subproject labels Jun 27, 2026
@cxheng315
cxheng315 marked this pull request as ready for review June 27, 2026 14:13
@github-actions github-actions Bot added ready-for-review this PR development is complete, please review and removed WIP work in progress labels Jun 27, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bbfd7a2670

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +90 to +92
inner join consideration_side con
on con.tx_hash = o.tx_hash
and con.evt_index = o.evt_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve item identity when joining Seaport legs

When an Agra OrderFulfilled contains more than one ERC20 offer or consideration item, offer_side and consideration_side each produce one row per token, but this join only uses tx_hash and evt_index. A single event then expands into every offer-token × consideration-token combination while the model is keyed and tested only by tx_hash, evt_index, so multi-asset fills will either fail the unique test/merge or be arbitrarily de-duped downstream in dex_base_trades_macro.

Useful? React with 👍 / 👎.

@cxheng315 cxheng315 Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't happen for Agra. It's a CLOB — every order matches exactly one ERC20 against the quote token (a/b or c/b). A single order paying two tokens for one (a + c / b) isn't a valid trade; it could only come from a contract failure. Confirmed across all production data so far (247 events: 235 ethereum, 12 hyperevm) — every OrderFulfilled has exactly one distinct ERC20 per side.

And even if it did occur, a multi-asset fill isn't representable in dex.trades anyway (one token_bought_address / one token_sold_address per row, and no prices at the base layer to split a leg), so it's intentionally out of scope — consistent with the rest of the base-trades sector.

That said, I added a deterministic guard so a malformed order can never silently break the (tx_hash, evt_index) unique key or the incremental merge: each side collapses to one row per event via row_number() (QUALIFY isn't supported in DuneSQL/Trino). It's a verified no-op on all current data — old-vs-new output is byte-identical (209 = 209 rows, empty symmetric difference) — and the unique key stays the tripwire if an anomalous order ever appears.

Collapse offer/consideration to one deterministic row per (tx_hash,
evt_index) via row_number, so a malformed multi-token Agra order cannot
break the unique key or the incremental merge. Agra is a CLOB (1:1
settlement), so this is a no-op on all real data; verified output is
byte-identical to before. Uses a row_number subquery rather than QUALIFY,
which DuneSQL/Trino does not support.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@cxheng315

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #9881, which was merged in 62ee319.

#9881 carries everything from this PR plus the work that came out of review:

  • monad supportagra_monad_base_trades + seed, registered in dex_monad_base_trades
  • payment-leg selection — picks the consideration item by recipient = offerer instead of by raw amount, which fixes multi-token consideration orders
  • self-matched fills filteredofferer <> recipient (the Agra team confirmed these are test fills); that filter strictly subsumes the old matchOrders dedup, so the matched_hashes CTE and the settlement_evt_ordersmatched source were removed
  • test/seed hardeningnot_null + descriptions on tx_hash/evt_index, refreshed seeds on all three chains

The 14 files touched here are a subset of the 18 in #9881, so nothing from this PR is left behind.

@cxheng315 cxheng315 closed this Aug 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 19, 2026
@cxheng315
cxheng315 deleted the agra-dex-integration branch August 19, 2026 08:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

dbt: dex covers the DEX dbt subproject ready-for-review this PR development is complete, please review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant