Skip to content

feat: add --chain filter to protocols top#42

Closed
ggonzalez94 wants to merge 9 commits into
mainfrom
feat/protocols-top-chain-filter
Closed

feat: add --chain filter to protocols top#42
ggonzalez94 wants to merge 9 commits into
mainfrom
feat/protocols-top-chain-filter

Conversation

@ggonzalez94
Copy link
Copy Markdown
Owner

@ggonzalez94 ggonzalez94 commented Mar 17, 2026

Summary

  • Adds --chain flag to protocols top that filters protocols by chain presence and ranks by chain-specific TVL using DefiLlama's chainTvls data
  • When --chain is specified, tvl_usd reflects the protocol's TVL on that specific chain rather than total TVL across all chains
  • Supports combined --category and --chain filtering (e.g. protocols top --chain Arbitrum --category Lending)
  • Output now includes chains count field showing how many chains each protocol is deployed on

Examples

# Top protocols on Ethereum by chain-specific TVL
defi protocols top --chain Ethereum --limit 5

# Top lending protocols on Arbitrum
defi protocols top --chain Arbitrum --category Lending --limit 5

Test plan

  • go test ./... passes
  • go test -race ./... passes
  • go vet ./... passes
  • Smoke tested protocols top --chain Ethereum --limit 5
  • Smoke tested protocols top --chain Arbitrum --category Lending --limit 5
  • Verified chain-specific TVL differs from total TVL (e.g. Binance CEX: $68.5B on Ethereum vs $157B total)
  • Added 5 new httptest-based unit tests covering sorting, chain filtering, combined filters, case insensitivity, and fallback behavior

🤖 Generated with Claude Code


Note

Medium Risk
Adds multiple new market-data commands and expands the DefiLlama adapter plus CLI interfaces; most risk is around new RPC dialing/concurrency in chains gas and potential schema/contract changes (new fields like chains).

Overview
Expands market-discovery command surface. Adds new top-level commands for dexes volume and stablecoins top|chains, plus new protocols fees and protocols revenue rankings sourced from DefiLlama endpoints (no API key).

Enhances chain-related tooling. Introduces chains list (enumerate supported chains/aliases) and chains gas (live EVM gas pricing via RPC, supports multi-chain parallel queries with partial-result warnings), and marks these as metadata-style commands that bypass cache initialization.

Extends existing protocol rankings. protocols top gains a --chain filter that filters by chain presence and ranks by chain-specific TVL (falling back to total TVL when chain TVL is missing), and protocol rows now include a chains count.

Updates provider interfaces/models, adds DefiLlama client support for the new endpoints (including stablecoins API base), and adds extensive unit tests plus docs/README/CHANGELOG updates to reflect the new commands and output fields.

Written by Cursor Bugbot for commit 5dcc08e. This will update automatically on new commits. Configure here.

ggonzalez94 and others added 9 commits March 8, 2026 15:28
Agents and automation tools need to discover available chains before
using other commands. This adds `chains list` which enumerates all
supported chains with names, slugs, CAIP-2 identifiers, namespaces,
and accepted aliases. No API keys required; bypasses cache.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Agents planning execution need gas price context before deciding whether
to proceed with swaps, bridges, or other on-chain operations. This adds
`chains gas --chain <id>` which queries current base fee, priority fee,
and legacy gas price via RPC with EIP-1559 detection.

No API keys required, bypasses cache, supports --rpc-url override.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Allow --chain to accept comma-separated chain identifiers (e.g.
--chain 1,10,137,8453,42161) for parallel multi-chain gas price
queries. Single-chain usage preserves the existing scalar response
for backward compatibility. Multi-chain returns an array with
partial-result support and warnings for failed chains.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `stablecoins top` command using DefiLlama's free stablecoins API to
list stablecoins ranked by circulating market cap. Returns price, chain
count, peg type/mechanism, and day/week/month supply change deltas.
Supports `--peg-type` filter and `--limit`. No API key required.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…cap ranking

Surface stablecoin liquidity distribution across chains using DefiLlama's
stablecoinchains API. Returns chains ranked by total stablecoin supply with
CAIP-2 chain IDs and dominant peg type. No API key required.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `protocols fees` command that ranks protocols by 24h fee revenue
using the DefiLlama fees API. Includes 7d/30d totals, percentage
changes (1d/7d/1m), category filtering, and chain counts. No API key
required.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `dexes volume` command to rank DEXes by 24h trading volume with
7d/30d totals and 1d/7d/1m percentage changes. Uses DefiLlama DEX
volume API (no API key required). Supports `--chain` filter for chain
presence and `--limit`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Revenue (protocol-retained fees) is the most important fundamental
metric for protocol valuation. This adds `protocols revenue` using
DefiLlama's /overview/fees?dataType=dailyRevenue endpoint with the
same --category filter and --limit flags as protocols fees.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds --chain flag to `protocols top` that filters protocols by chain
presence and ranks by chain-specific TVL from DefiLlama's chainTvls data.
Supports combined --category and --chain filtering. Output now includes
chains count field.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.


type peggedAmount struct {
PeggedUSD float64 `json:"peggedUSD"`
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-USD pegged stablecoins report zero circulating supply

High Severity

The peggedAmount struct only deserializes the peggedUSD JSON key. The DefiLlama API returns circulating amounts keyed by peg type — e.g., {"peggedEUR": 100000000} for EUR-pegged stablecoins. Non-USD stablecoins (like EURS with pegType: "peggedEUR") will unmarshal with PeggedUSD = 0, causing zero CirculatingUSD, zero day/week/month deltas, and incorrect sort ordering. The --peg-type peggedEUR filter will return results but all with zero values. The tests mask this by incorrectly using "peggedUSD" as the key for the EURS mock data.

Additional Locations (1)
Fix in Cursor Fix in Web

if tvl <= 0 {
tvl = p.TVL
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Chain TVL fallback inflates multi-chain protocol rankings

Medium Severity

When --chain is specified but chainTVL() returns 0 (no matching key in chainTvls), the code falls back to the protocol's total TVL across all chains. For multi-chain protocols with missing chain-specific data, this inflates their apparent TVL on the queried chain, contradicting the PR's stated behavior that tvl_usd reflects "the protocol's value locked on that chain rather than total TVL."

Fix in Cursor Fix in Web

Copy link
Copy Markdown

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

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: 5dcc08eea0

ℹ️ 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 +224 to +225
if tvl <= 0 {
tvl = p.TVL
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 Avoid falling back to total TVL for chain-filtered results

When a chain filter is set, this branch replaces missing/zero chain TVL with p.TVL, so protocols can be ranked and reported using global TVL even though --chain was requested. This produces inflated tvl_usd and incorrect ordering whenever DefiLlama returns a legitimate 0 for that chain (or no plain chain key but still includes the chain in chains). The fallback should only happen for truly missing chain-specific data, not for valid zero values.

Useful? React with 👍 / 👎.

@ggonzalez94
Copy link
Copy Markdown
Owner Author

Closing: superseded by #43 which contains all commits from this stacked branch chain (this PR's 9 commits are included in #43's 10).

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.

1 participant