Skip to content

Track x402 volume#6181

Open
FelixBruguera wants to merge 1 commit intoDefiLlama:masterfrom
FelixBruguera:x402
Open

Track x402 volume#6181
FelixBruguera wants to merge 1 commit intoDefiLlama:masterfrom
FelixBruguera:x402

Conversation

@FelixBruguera
Copy link
Copy Markdown
Contributor

@FelixBruguera FelixBruguera commented Mar 14, 2026

Closes https://github.com/DefiLlama/internal-docs/issues/15

Tracks the volume of x402 payments processed by facilitators on base, polygon and solana, removing transfers from the facilitators address and payments between the same address

Test results:
Screenshot from 2026-03-14 13-28-26
Screenshot from 2026-03-14 13-26-45
Screenshot from 2026-03-14 13-24-56

Summary by CodeRabbit

Release Notes

  • New Features
    • Added support for a new DEX with daily token volume tracking across BASE, POLYGON, and SOLANA networks. Data aggregation begins from May 15, 2025.

@FelixBruguera FelixBruguera requested a review from g1nt0ki March 14, 2026 18:02
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 14, 2026

📝 Walkthrough

Walkthrough

A new DEX adapter for x402 is introduced across two files. The facilitators module exports chain-to-address mappings for EVM and Solana networks. The main adapter implements a SimpleAdapter that prefetches token transfer volumes from Dune SQL for Base, Polygon, and Solana, then processes results during fetch operations.

Changes

Cohort / File(s) Summary
x402 DEX Adapter
dexs/x402/facilitators.ts, dexs/x402/index.ts
Introduces a new DEX adapter with facilitator address mappings for EVM chains and Solana, alongside a SimpleAdapter that prefetches daily token transfer volumes from Dune queries across Base, Polygon, and Solana, with fetch operations aggregating results by chain.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Adapter as x402 Adapter
    participant Dune as Dune API
    participant EVM as EVM Chains<br/>(Base, Polygon)
    participant Solana
    
    User->>Adapter: prefetch(options)
    activate Adapter
    Adapter->>Dune: Execute SQL Query
    activate Dune
    Dune->>EVM: Fetch token transfers
    EVM-->>Dune: Transfer data (chain, token, volume)
    Dune->>Solana: Fetch token transfers
    Solana-->>Dune: Transfer data (chain, token, volume)
    Dune-->>Adapter: Aggregated volumes per chain
    deactivate Dune
    Adapter-->>User: Cached results
    deactivate Adapter
    
    User->>Adapter: fetch(_, _, options)
    activate Adapter
    Adapter->>Adapter: Filter cached results by chain
    Adapter->>Adapter: Accumulate dailyVolume
    Adapter-->>User: {dailyVolume}
    deactivate Adapter
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hop, hop, the x402 adapter springs to life,
Facilitators mapped through chains so bright,
Dune queries dance through Base and Solana's night,
Volumes prefetched with algorithmic delight,
A new DEX helper, ready for flight! 🚀

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description provides context (linked issue, what is being tracked, and test results) but does not follow the provided template structure for new protocol listings. Clarify whether this PR is for a new protocol listing requiring template completion, or if it's a technical implementation detail where a custom description is appropriate.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Track x402 volume' directly summarizes the main objective of the changeset - implementing volume tracking for the x402 DEX across three chains.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@llamabutler
Copy link
Copy Markdown

The x402 adapter exports:

> adapters@1.0.0 test
> ts-node --transpile-only cli/testAdapter.ts dexs x402

🦙 Running X402 adapter 🦙
---------------------------------------------------
Start Date:	Fri, 13 Mar 2026 00:00:00 GMT
End Date:	Sat, 14 Mar 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:75:33)
DUNE_API_KEYS environment variable is not set

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
dexs/x402/facilitators.ts (1)

3-3: Pin the upstream facilitator snapshot.

Line 3 points at a moving main branch. Since this list defines the historical accounting scope, please record the exact upstream commit or release used for this snapshot so later updates stay auditable.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@dexs/x402/facilitators.ts` at line 3, The upstream facilitators source URL
currently references a moving branch ("main"); replace that with a pinned
snapshot by recording the exact commit SHA or release tag used (e.g., change the
URL at the top of dexs/x402/facilitators.ts from ".../tree/main/..." to
".../tree/<COMMIT_SHA_OR_TAG>/..."), and also add a nearby single-line comment
or constant (e.g., FACILITATOR_SNAPSHOT_COMMIT or a short comment) stating the
full commit SHA, author/date, and a brief rationale so the snapshot is auditable
and reproducible.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@dexs/x402/index.ts`:
- Around line 19-20: The WHERE clause only excludes rows when t.tx_from equals
the facilitator signer (tx_from) but still allows transfers originated by other
facilitator-owned addresses; update the predicate to exclude any sender or token
account that belongs to the facilitator set (use facilitators.EVM) by checking
both t.tx_from and t."from" (and any other sender fields used later such as
t."to" where relevant) against the facilitators list; modify the expressions
around t.tx_from, t."from", and t."to" (and replicate the same change at the
similar block referenced at lines 29-30) so all facilitator-owned addresses are
filtered out rather than only the tx signer.
- Line 53: The adapter configuration's start field currently reads start:
"2025-05-15" which mismatches the PR screenshots showing Start Time = 5/3/2025;
update the start value to "2025-05-03" in the adapter config (the object
containing the start property) so the x402 adapter begins on May 3, 2025 and
does not skip May 3–14, 2025; after changing, run any existing date-range tests
or validation code that references the start property to ensure no other offsets
or timezone conversions are needed.

---

Nitpick comments:
In `@dexs/x402/facilitators.ts`:
- Line 3: The upstream facilitators source URL currently references a moving
branch ("main"); replace that with a pinned snapshot by recording the exact
commit SHA or release tag used (e.g., change the URL at the top of
dexs/x402/facilitators.ts from ".../tree/main/..." to
".../tree/<COMMIT_SHA_OR_TAG>/..."), and also add a nearby single-line comment
or constant (e.g., FACILITATOR_SNAPSHOT_COMMIT or a short comment) stating the
full commit SHA, author/date, and a brief rationale so the snapshot is auditable
and reproducible.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dee2d729-0eff-4527-83eb-a0fcd52463b5

📥 Commits

Reviewing files that changed from the base of the PR and between fe0dafb and b9b3a0f.

📒 Files selected for processing (2)
  • dexs/x402/facilitators.ts
  • dexs/x402/index.ts

Comment on lines +19 to +20
where t.tx_from IN ( ${facilitators.EVM} )
and t."from" != tx_from and t."from" != t."to" and t.blockchain IN ('base', 'polygon')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Exclude facilitator-owned senders, not just the signer.

These predicates only drop rows where the transfer sender matches tx_from / tx_signer. They still count payouts from another facilitator-owned address or token account, which does not match the PR goal of removing transfers from facilitator addresses.

Suggested change
+const evmFacilitatorsSql = facilitators.EVM.join(", ")
+const solanaFacilitatorsSql = facilitators[CHAIN.SOLANA]
+  .map((address) => `'${address}'`)
+  .join(", ")
+
 const prefetch = async (options: FetchOptions) => {
     return queryDuneSql(options, `
         select
             blockchain as chain,
             CAST(contract_address as varchar) as token,
             SUM(amount_raw) as volume
         from tokens.transfers t
-        where t.tx_from IN ( ${facilitators.EVM} )
-        and t."from" != tx_from and t."from" != t."to" and t.blockchain IN ('base', 'polygon')
+        where t.tx_from IN ( ${evmFacilitatorsSql} )
+        and t."from" NOT IN ( ${evmFacilitatorsSql} )
+        and t."from" != t."to" and t.blockchain IN ('base', 'polygon')
         and TIME_RANGE
         group by blockchain, contract_address
         union all
         select
             'solana' as chain,
             token_mint_address as token,
             SUM(amount) as volume
         from tokens_solana.transfers
-        where tx_signer IN ( ${facilitators.solana.map(address => `'${address}'`)} )
-        and tx_signer != from_owner and from_owner != to_owner
+        where tx_signer IN ( ${solanaFacilitatorsSql} )
+        and from_owner NOT IN ( ${solanaFacilitatorsSql} )
+        and from_owner != to_owner
         and TIME_RANGE
         group by token_mint_address
         `
     );
 };

Also applies to: 29-30

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@dexs/x402/index.ts` around lines 19 - 20, The WHERE clause only excludes rows
when t.tx_from equals the facilitator signer (tx_from) but still allows
transfers originated by other facilitator-owned addresses; update the predicate
to exclude any sender or token account that belongs to the facilitator set (use
facilitators.EVM) by checking both t.tx_from and t."from" (and any other sender
fields used later such as t."to" where relevant) against the facilitators list;
modify the expressions around t.tx_from, t."from", and t."to" (and replicate the
same change at the similar block referenced at lines 29-30) so all
facilitator-owned addresses are filtered out rather than only the tx signer.

@noateden noateden self-assigned this Apr 11, 2026
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.

3 participants