-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add retention adapter type backed by a stateful indexing service #9006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tinkererlife
wants to merge
5
commits into
DefiLlama:master
Choose a base branch
from
tinkererlife:feat/stateful-retention-adapters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1fbd1a8
feat: add stateful retention adapters
tinkererlife b5f446a
feat: declare source data availability lag
tinkererlife 088e845
chore: remove unrelated workflow diff
tinkererlife c273ff9
fix: isolate retention adapter for CI
tinkererlife 89c8713
docs: document retention manifest validation
tinkererlife File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| import type { FetchOptions, FetchResultRetention, SimpleAdapter } from "../adapters/types"; | ||
|
|
||
| const DAY = 86400; | ||
| const FIRST_HORIZON_WEEKS = 4; | ||
|
|
||
| export interface RetentionDuneSqlSource { | ||
| id: string; | ||
| type: "duneSql"; | ||
| /** Raw read-only SQL. The backend replaces the two required day tokens. */ | ||
| sql: string; | ||
| output: { | ||
| day: string; | ||
| wallet: string; | ||
| volumeUsd: string; | ||
| }; | ||
| } | ||
|
|
||
| export interface RetentionEvmStaticTargets { | ||
| type: "static"; | ||
| addresses: string[]; | ||
| } | ||
|
|
||
| export interface RetentionAccessControlRoleMember { | ||
| role: string; | ||
| member: string; | ||
| } | ||
|
|
||
| export interface RetentionAccessControlRoleChange | ||
| extends RetentionAccessControlRoleMember { | ||
| blockNumber: number; | ||
| logIndex: number; | ||
| isGrant: boolean; | ||
| } | ||
|
|
||
| export interface RetentionAccessControlHistory { | ||
| /** Day whose start corresponds to activeAtStart. */ | ||
| startDay: string; | ||
| /** First day whose registry events must be queried live by the backend. */ | ||
| liveFromDay: string; | ||
| activeAtStart: RetentionAccessControlRoleMember[]; | ||
| changesBeforeLive: RetentionAccessControlRoleChange[]; | ||
| } | ||
|
|
||
| export interface RetentionEvmAccessControlTargets { | ||
| type: "accessControlRegistry"; | ||
| address: string; | ||
| roles: string[]; | ||
| grantedTopic0: string; | ||
| revokedTopic0: string; | ||
| history: RetentionAccessControlHistory; | ||
| } | ||
|
|
||
| export type RetentionEvmTargets = | ||
| | RetentionEvmStaticTargets | ||
| | RetentionEvmAccessControlTargets; | ||
|
|
||
| export interface RetentionEvmEventField { | ||
| type: "address" | "bytes32" | "uint256"; | ||
| topic?: number; | ||
| dataWord?: number; | ||
| } | ||
|
|
||
| export interface RetentionEvmEventSource { | ||
| id: string; | ||
| type: "evmEvents"; | ||
| targets: RetentionEvmTargets; | ||
| event: { | ||
| /** Human-readable ABI for reviewers; topic0 is the RPC filter. */ | ||
| abi: string; | ||
| topic0: string; | ||
| fields: Record<string, RetentionEvmEventField>; | ||
| }; | ||
| where?: Array<{ field: string; equals: string }>; | ||
| output: { | ||
| wallet: string; | ||
| volumeUsd: { field: string; decimals: number }; | ||
| }; | ||
| } | ||
|
|
||
| export type RetentionSource = RetentionDuneSqlSource | RetentionEvmEventSource; | ||
|
|
||
| export interface RetentionManifest { | ||
| project: string; | ||
| chain: string; | ||
| stateVersion: number; | ||
| observationStart: string; | ||
| firstCohortStart: string; | ||
| /** Delay after a UTC day ends before its source data is safe to index. */ | ||
| dataAvailabilityLagHours: number; | ||
| maxQueryDays?: number; | ||
| methodology: string; | ||
| sources: RetentionSource[]; | ||
| } | ||
|
|
||
| /** | ||
| * Validates manifest dates, numeric bounds, and source IDs. | ||
| * @returns The validated manifest unchanged. | ||
| */ | ||
| export function defineRetentionManifest( | ||
| manifest: RetentionManifest, | ||
| ): RetentionManifest { | ||
| validateDate(manifest.project, "observationStart", manifest.observationStart); | ||
| validateDate(manifest.project, "firstCohortStart", manifest.firstCohortStart); | ||
| if (manifest.firstCohortStart < manifest.observationStart) { | ||
| throw new Error(`${manifest.project}: firstCohortStart precedes observationStart`); | ||
| } | ||
| if ( | ||
| !Number.isFinite(manifest.dataAvailabilityLagHours) || | ||
| manifest.dataAvailabilityLagHours < 0 | ||
| ) { | ||
| throw new Error( | ||
| `${manifest.project}: dataAvailabilityLagHours must be a non-negative number`, | ||
| ); | ||
| } | ||
| if (!Number.isInteger(manifest.stateVersion) || manifest.stateVersion < 1) { | ||
| throw new Error(`${manifest.project}: stateVersion must be a positive integer`); | ||
| } | ||
| if ( | ||
| manifest.maxQueryDays !== undefined && | ||
| (!Number.isInteger(manifest.maxQueryDays) || manifest.maxQueryDays < 1) | ||
| ) { | ||
| throw new Error(`${manifest.project}: maxQueryDays must be a positive integer`); | ||
| } | ||
| if (!Array.isArray(manifest.sources) || manifest.sources.length === 0) { | ||
| throw new Error(`${manifest.project}: sources must be a non-empty array`); | ||
| } | ||
| const sourceIds = new Set<string>(); | ||
| for (const source of manifest.sources) { | ||
| if (!source.id || sourceIds.has(source.id)) { | ||
| throw new Error(`${manifest.project}: source ids must be non-empty and unique`); | ||
| } | ||
| sourceIds.add(source.id); | ||
| } | ||
| return manifest; | ||
| } | ||
|
|
||
| /** Creates the normal DefiLlama adapter. Its fetch is a read-only state-service call. */ | ||
| export function createRetentionFetchAdapter( | ||
| manifest: RetentionManifest, | ||
| ): SimpleAdapter { | ||
| const start = addDays( | ||
| manifest.firstCohortStart, | ||
| FIRST_HORIZON_WEEKS * 7 + 6, | ||
| ); | ||
|
|
||
| return { | ||
| version: 1, | ||
| chains: [manifest.chain], | ||
| start, | ||
| methodology: manifest.methodology, | ||
| fetch: (options: FetchOptions) => fetchRetentionMetrics(manifest, options), | ||
| }; | ||
| } | ||
|
|
||
| async function fetchRetentionMetrics( | ||
| manifest: RetentionManifest, | ||
| options: FetchOptions, | ||
| ): Promise<FetchResultRetention> { | ||
| const baseUrl = process.env.RETENTION_API_URL?.replace(/\/$/, ""); | ||
| if (!baseUrl) { | ||
| throw new Error("RETENTION_API_URL is required to fetch retention metrics"); | ||
| } | ||
|
|
||
| const url = new URL( | ||
| `${baseUrl}/v1/retention/${encodeURIComponent(manifest.project)}/${options.dateString}`, | ||
| ); | ||
| url.searchParams.set("stateVersion", String(manifest.stateVersion)); | ||
| const response = await fetch(url); | ||
| const body = await response.text(); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `${manifest.project}: retention backend returned ${response.status}: ${body.slice(0, 300)}`, | ||
| ); | ||
| } | ||
|
|
||
| const result = JSON.parse(body) as FetchResultRetention; | ||
| validateMetrics(manifest.project, result); | ||
| return result; | ||
| } | ||
|
|
||
| function validateMetrics(project: string, result: FetchResultRetention): void { | ||
| if (!result || typeof result !== "object") { | ||
| throw new Error(`${project}: retention backend returned a non-object result`); | ||
| } | ||
| const w4Keys = [ | ||
| "dailyRetentionW4CohortWallets", | ||
| "dailyRetentionW4ReturnedWallets", | ||
| "dailyRetentionW4CohortVolume", | ||
| "dailyRetentionW4ReturnedVolume", | ||
| ] as const; | ||
| const w12Keys = [ | ||
| "dailyRetentionW12CohortWallets", | ||
| "dailyRetentionW12ReturnedWallets", | ||
| "dailyRetentionW12CohortVolume", | ||
| "dailyRetentionW12ReturnedVolume", | ||
| ] as const; | ||
| for (const key of w4Keys) { | ||
| const value = result[key]; | ||
| if (typeof value !== "number" || !Number.isFinite(value) || value < 0) { | ||
| throw new Error(`${project}: retention backend returned invalid ${key}`); | ||
| } | ||
| } | ||
| const presentW12Keys = w12Keys.filter((key) => result[key] !== undefined); | ||
| if (presentW12Keys.length !== 0 && presentW12Keys.length !== w12Keys.length) { | ||
| throw new Error(`${project}: retention backend returned a partial W12 result`); | ||
| } | ||
| for (const key of presentW12Keys) { | ||
| const value = result[key]; | ||
| if (typeof value !== "number" || !Number.isFinite(value) || value < 0) { | ||
| throw new Error(`${project}: retention backend returned invalid ${key}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function validateDate(project: string, field: string, value: string): void { | ||
| const parsed = Date.parse(`${value}T00:00:00Z`); | ||
| if (!Number.isFinite(parsed) || new Date(parsed).toISOString().slice(0, 10) !== value) { | ||
| throw new Error(`${project}: invalid ${field} ${value}`); | ||
| } | ||
| } | ||
|
|
||
| function addDays(value: string, days: number): string { | ||
| return new Date(Date.parse(`${value}T00:00:00Z`) + days * DAY * 1000) | ||
| .toISOString() | ||
| .slice(0, 10); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { CHAIN } from "../../helpers/chains"; | ||
| import { | ||
| createRetentionFetchAdapter, | ||
| defineRetentionManifest, | ||
| } from "../../helpers/retention"; | ||
|
|
||
| // Wallet and volume retention (W4/W12) for Collector Crypt on Solana. | ||
| // | ||
| // A pack purchase is a USDC transfer into one of the on-chain gacha sinks. Each | ||
| // query scans only the requested date range of tokens_solana.transfers. | ||
| // Both sinks are observed: the current one went live on 2025-12-07, while its | ||
| // predecessor carries the history before that. Without it every buyer who | ||
| // migrated across would look like a brand new wallet. | ||
| // - Sink labels and transfers: https://solscan.io/account/GachaNgyXTU3zFogQ8Z5jR2BLXs8215X2AtEH18VxJq3 | ||
| // - First transaction on the current sink (2025-12-07): https://solscan.io/tx/2iSpTcqEc85tjD6VJ4i9Q9NCvEdf8pZ3axSw287FSmpgVahpErv5911ntuALUGxvzYCBTNZ28vhHrXBsJeatXjq6 | ||
| // - Solana USDC mint: https://solscan.io/token/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v | ||
| // | ||
| // The fiat/credit-card rail (96DULv…, part of TEAM_ADDRESSES) is deliberately not | ||
| // a sink: those top-ups settle off-chain in bundles and carry no per-buyer | ||
| // identity, so card purchases are out of scope - a narrower perimeter than | ||
| // fees/collector-crypt. | ||
| const USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; | ||
|
|
||
| // Kept local so loading this manifest does not execute the existing Allium fee adapter. | ||
| const GACHA_ONCHAIN_ADDRESSES = [ | ||
| "GachazZscHZ5bn3vnq1yEC4zpYdhAYJBzuKJwSJksc9z", // decommissioned pre-Dec-2025, kept for history | ||
| "GachaNgyXTU3zFogQ8Z5jR2BLXs8215X2AtEH18VxJq3", // primary on-chain sink | ||
| ]; | ||
|
|
||
| // Protocol-controlled wallets excluded from buyer cohorts. This is the same | ||
| // public exclusion set used by fees/collector-crypt. | ||
| const TEAM_ADDRESSES = [ | ||
| "BAxTk97HsaJqbnbFmTiQTaL4KSRvJ8Y65ArZCsP6vA5M", | ||
| "21KhtC7y2JGYvwc8dcGqTdbrudbM8fgMPJsVwxRQqdY8", | ||
| "DFEstpYN3fsz93AC9v2ujzPPngPgodqH2xxopuyfSsAE", | ||
| "HW2HRqN1pXQGH9GfP9xet4XwqtLqFyYGDNRKjUAVgh9u", | ||
| "HighJBfnAaqH9cKkeMErQFJZ4ATxQJwxqFupX6zaKTns", | ||
| "LGNDXqcm6U57QQ6Ad7icZ6oizkAVKRWrw97KwZy5nVf", | ||
| "EpicWWZspT1trKndbDDr29ULViN56rN5vofWSKZp8ePF", | ||
| "Mid9NeCpPNxP59fAdsLgMLy7BYexxXFw52ZP58Jrney", | ||
| "Lowq9dkpY43VpjfYeRjtKfGA6JtB7HaMmwQgXkjHLvN", | ||
| "Low6UekJP3QrFVMfNRTL8CPK2SiGFhvp57sgF2pkmVu", | ||
| "miDtj3vgdxVykHzRyFwyG8MXpvK8eQqamSLVdBr7WPt", | ||
| "HiGHqwYddP5N2waqUmXPdaASpMpUEvfqPr2fSawctEb", | ||
| "epiC3zkqa1RfcPMMM1Kc8m3GZGDwF2RmjbfA3g1BBjn", | ||
| "LGNDfXQFMiRMz3qqTNAREmRFQutMvazqqRrzn5i98uj", | ||
| "SPrT7eFrCM9UJ4j7Xf9iktKCoBwJjfykFbiNbRsKQm8", | ||
| "Cc4pHGnoaRWL1WnHsV517T3YvQn5gLDBMiuVXkF9rZhK", | ||
| "8373hLiAEXxaJ3oV7SRzx4KHwurEg9rEG98tUPj1sdtX", | ||
| "onePMfirJs2Rx3eixoPnjY6NHiaC74pkQ2k313K2Lxs", | ||
| "SportGmqffp9zC3VZV7Wwz6s2nCkEB5Q3nVwKGU4esD", | ||
| "DQPERZ9e86pNJ4mhUnCEP8V75yxZofsipoVrRWT5Wdxd", | ||
| "cc3novbXuNSe292qKH2gGhxToaWjuBvJbA7zQf8NVxi", | ||
| "GachaNgyXTU3zFogQ8Z5jR2BLXs8215X2AtEH18VxJq3", | ||
| "GachazZscHZ5bn3vnq1yEC4zpYdhAYJBzuKJwSJksc9z", | ||
| "96DULv1BqYfe5wyMr6pVUNC6Uyrtj6yr3tNi6VtfwW9s", | ||
| "jrS7Pbn38wKiPsXbyNhGCr3icfXuJxdytZr1N4TwdFu", | ||
| ]; | ||
|
|
||
| const sinks = GACHA_ONCHAIN_ADDRESSES.map((address) => `'${address}'`).join(", "); | ||
| const excluded = TEAM_ADDRESSES.map((address) => `'${address}'`).join(", "); | ||
|
|
||
| const purchases = { | ||
| id: "pack-purchases", | ||
| type: "duneSql" as const, | ||
| sql: ` | ||
| SELECT cast(date_trunc('day', t.block_time) AS date) AS day, | ||
| t.from_owner AS wallet, | ||
| sum(t.amount_display) AS volume_usd | ||
| FROM tokens_solana.transfers t | ||
| WHERE t.block_date >= date '{{fromDay}}' AND t.block_date < date '{{toDayExclusive}}' | ||
| AND t.token_mint_address = '${USDC_MINT}' | ||
| AND t.to_owner IN (${sinks}) | ||
| AND t.from_owner IS NOT NULL | ||
| AND t.from_owner NOT IN (${excluded}) | ||
| AND t.amount_display > 0 | ||
| GROUP BY 1, 2 | ||
| `, | ||
| output: { | ||
| day: "day", | ||
| wallet: "wallet", | ||
| volumeUsd: "volume_usd", | ||
| }, | ||
| }; | ||
|
|
||
| export const retentionManifest = defineRetentionManifest({ | ||
| project: "collector-crypt", | ||
| chain: CHAIN.SOLANA, | ||
| stateVersion: 1, | ||
| observationStart: "2025-01-01", | ||
| // The current sink went live on Sunday 2025-12-07; cohorts start with the next | ||
| // full UTC week, backed by eleven months of predecessor history. | ||
| firstCohortStart: "2025-12-08", | ||
| // Dune's indexed Solana tables can trail the completed UTC day. | ||
| dataAvailabilityLagHours: 10, | ||
| sources: [purchases], | ||
| methodology: | ||
| "Daily rolling weekly cohort retention for Collector Crypt on Solana. Each daily row ends a complete seven-day return window; W4 and W12 compare it with the same seven-day window shifted 4 or 12 weeks earlier. The cohort contains wallets whose first observed USDC pack purchase into one of the on-chain gacha sinks occurred in that earlier window, with team and treasury wallets excluded. Purchases paid by card settle off-chain in bundled top-ups without a per-buyer identity and are not counted. Activity is observed from 2025-01-01 across both the current sink and its predecessor, so buyers who migrated to the 2025-12-07 sink are not counted as new; cohorts start on 2025-12-08.", | ||
| }); | ||
|
|
||
| export default createRetentionFetchAdapter(retentionManifest); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.