-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Centrifuge adaptor #2666
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
0x4Graham
wants to merge
5
commits into
DefiLlama:master
Choose a base branch
from
0x4Graham:centrifuge-adaptor
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
Centrifuge adaptor #2666
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a81b86
add centrifuge APY feeds
0x4Graham cd0a86b
revert package-lock.json changes
0x4Graham 35e51c7
address PR review: pagination, timeouts, null APY fallback
0x4Graham 6f45d8d
rename adaptor to centrifuge-protocol to match DefiLlama slug
0x4Graham b548378
add GraphQL response validation and skip vaults with missing decimals
0x4Graham 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| const sdk = require('@defillama/sdk'); | ||
| const axios = require('axios'); | ||
| const utils = require('../utils'); | ||
|
|
||
| const API_URL = 'https://api.centrifuge.io/graphql'; | ||
| const HTTP_TIMEOUT_MS = 15000; | ||
| const http = axios.create({ timeout: HTTP_TIMEOUT_MS }); | ||
| const SHARE_UNIT = '1000000000000000000'; // 1e18 | ||
| const DAY = 86400; | ||
|
|
||
| // Centrifuge API chain IDs → DefiLlama SDK chain names | ||
| // Token symbols to exclude (equity products, not yield) | ||
| const EXCLUDED_SYMBOLS = new Set(['deSPXA', 'SPXA']); | ||
|
|
||
| const CHAIN_MAP = { | ||
| 1: 'ethereum', | ||
| 2: 'base', | ||
| 3: 'arbitrum', | ||
| 4: 'plume_mainnet', | ||
| 5: 'avax', | ||
| 6: 'bsc', | ||
| 9: 'hyperliquid', | ||
| 10: 'optimism', | ||
| 11: 'monad', | ||
| 12: 'pharos', | ||
| }; | ||
|
|
||
| const VAULTS_QUERY = `query($cursor: String) { | ||
| vaults(where: { isActive: true }, limit: 200, after: $cursor) { | ||
| items { | ||
| id | ||
| centrifugeId | ||
| assetAddress | ||
| tokenId | ||
| token { | ||
| name | ||
| symbol | ||
| decimals | ||
| pool { name } | ||
| } | ||
| asset { | ||
| symbol | ||
| decimals | ||
| } | ||
| } | ||
| pageInfo { hasNextPage endCursor } | ||
| } | ||
| }`; | ||
|
|
||
| const ABIS = { | ||
| totalAssets: 'function totalAssets() external view returns (uint256)', | ||
| convertToAssets: | ||
| 'function convertToAssets(uint256 shares) external view returns (uint256)', | ||
| }; | ||
|
|
||
| async function getHistoricalBlock(chain, timestamp) { | ||
| const { data } = await http.get( | ||
| `https://coins.llama.fi/block/${chain}/${timestamp}` | ||
| ); | ||
| return data.height; | ||
| } | ||
|
|
||
| async function fetchVaults() { | ||
| const items = []; | ||
| let cursor = null; | ||
| while (true) { | ||
| const { data } = await http.post(API_URL, { | ||
| query: VAULTS_QUERY, | ||
| variables: { cursor }, | ||
| }); | ||
| const page = data?.data?.vaults; | ||
| if (!page?.items) throw new Error('Unexpected GraphQL response'); | ||
| items.push(...page.items); | ||
| if (!page.pageInfo.hasNextPage) break; | ||
| cursor = page.pageInfo.endCursor; | ||
| } | ||
| return items; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| async function processChain(chain, vaults) { | ||
| const vaultAddresses = vaults.map((v) => v.id); | ||
| const uniqueAssets = [...new Set(vaults.map((v) => v.assetAddress))]; | ||
|
|
||
| // Historical blocks for APY (1d and 7d) | ||
| const now = Math.floor(Date.now() / 1000); | ||
| let block1d = null; | ||
| let block7d = null; | ||
| try { | ||
| [block1d, block7d] = await Promise.all([ | ||
| getHistoricalBlock(chain, now - DAY), | ||
| getHistoricalBlock(chain, now - 7 * DAY), | ||
| ]); | ||
| } catch (e) { | ||
| console.error( | ||
| `centrifuge: cannot get historical blocks for ${chain}, skipping APY:`, | ||
| e.message | ||
| ); | ||
| } | ||
|
|
||
| // Batch on-chain reads for TVL and share prices | ||
| const vaultCalls = vaultAddresses.map((v) => ({ target: v })); | ||
| const sharePriceCalls = vaultAddresses.map((v) => ({ | ||
| target: v, | ||
| params: [SHARE_UNIT], | ||
| })); | ||
|
|
||
| const [totalAssetsRes, priceNowRes, price1dRes, price7dRes] = | ||
| await Promise.all([ | ||
| sdk.api.abi.multiCall({ | ||
| calls: vaultCalls, | ||
| abi: ABIS.totalAssets, | ||
| chain, | ||
| permitFailure: true, | ||
| }), | ||
| sdk.api.abi.multiCall({ | ||
| calls: sharePriceCalls, | ||
| abi: ABIS.convertToAssets, | ||
| chain, | ||
| permitFailure: true, | ||
| }), | ||
| block1d | ||
| ? sdk.api.abi.multiCall({ | ||
| calls: sharePriceCalls, | ||
| abi: ABIS.convertToAssets, | ||
| chain, | ||
| block: block1d, | ||
| permitFailure: true, | ||
| }) | ||
| : { output: vaultAddresses.map(() => ({ success: false })) }, | ||
| block7d | ||
| ? sdk.api.abi.multiCall({ | ||
| calls: sharePriceCalls, | ||
| abi: ABIS.convertToAssets, | ||
| chain, | ||
| block: block7d, | ||
| permitFailure: true, | ||
| }) | ||
| : { output: vaultAddresses.map(() => ({ success: false })) }, | ||
| ]); | ||
|
|
||
| // Get USD prices for underlying assets | ||
| const { pricesByAddress } = await utils.getPrices(uniqueAssets, chain); | ||
|
|
||
| // Build pool objects | ||
| const pools = []; | ||
| for (let i = 0; i < vaults.length; i++) { | ||
| try { | ||
| const v = vaults[i]; | ||
| const ta = totalAssetsRes.output[i]; | ||
| const pNow = priceNowRes.output[i]; | ||
| const p1d = (price1dRes.output || [])[i] || {}; | ||
| const p7d = (price7dRes.output || [])[i] || {}; | ||
|
|
||
| if (!ta || !ta.success || !pNow || !pNow.success) continue; | ||
|
|
||
| const decimals = v.asset?.decimals; | ||
| if (decimals == null) continue; | ||
| const assetSymbol = v.asset?.symbol ?? 'UNKNOWN'; | ||
| const usdPrice = pricesByAddress[v.assetAddress.toLowerCase()]; | ||
| if (!usdPrice) continue; | ||
|
|
||
| const tvlUsd = (Number(ta.output) / 10 ** decimals) * usdPrice; | ||
|
|
||
| // Geometric annualization of share price growth | ||
| let apyBase = null; | ||
| if (p1d.success && Number(p1d.output) > 0) { | ||
| apyBase = | ||
| (Math.pow(Number(pNow.output) / Number(p1d.output), 365) - 1) * 100; | ||
| } | ||
|
|
||
| let apyBase7d = null; | ||
| if (p7d.success && Number(p7d.output) > 0) { | ||
| apyBase7d = | ||
| (Math.pow(Number(pNow.output) / Number(p7d.output), 365 / 7) - 1) * | ||
| 100; | ||
| } | ||
|
|
||
| const tokenName = v.token?.name ?? v.token?.symbol ?? 'Unknown'; | ||
|
|
||
| pools.push({ | ||
| pool: `${v.id}-${chain}`.toLowerCase(), | ||
| chain: utils.formatChain(chain), | ||
| project: 'centrifuge-protocol', | ||
| symbol: assetSymbol, | ||
| tvlUsd, | ||
| apyBase: apyBase ?? apyBase7d ?? null, | ||
| apyBase7d, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| underlyingTokens: [v.assetAddress], | ||
| poolMeta: tokenName, | ||
| url: 'https://app.centrifuge.io', | ||
| }); | ||
| } catch (e) { | ||
| console.error( | ||
| `centrifuge: error processing vault ${vaults[i]?.id} on ${chain}:`, | ||
| e.message | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return pools; | ||
| } | ||
|
|
||
| const apy = async () => { | ||
| const allVaults = await fetchVaults(); | ||
|
|
||
| // Group by chain, dedup by token per chain (one vault per share class) | ||
| const byChain = {}; | ||
| const seenTokens = new Set(); | ||
|
|
||
| for (const v of allVaults) { | ||
| const chain = CHAIN_MAP[v.centrifugeId]; | ||
| if (!chain) continue; | ||
| if (EXCLUDED_SYMBOLS.has(v.token?.symbol)) continue; | ||
|
|
||
| // Dedup: one vault per (chain, tokenId) to avoid double-counting TVL | ||
| const key = `${chain}-${v.tokenId}`; | ||
| if (seenTokens.has(key)) continue; | ||
| seenTokens.add(key); | ||
|
|
||
| if (!byChain[chain]) byChain[chain] = []; | ||
| byChain[chain].push(v); | ||
| } | ||
|
|
||
| const pools = []; | ||
| for (const [chain, vaults] of Object.entries(byChain)) { | ||
| try { | ||
| const chainPools = await processChain(chain, vaults); | ||
| pools.push(...chainPools); | ||
| } catch (e) { | ||
| console.error(`centrifuge: error processing ${chain}:`, e.message); | ||
| } | ||
| } | ||
|
|
||
| return pools.filter((p) => utils.keepFinite(p)); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| timetravel: false, | ||
| apy, | ||
| url: 'https://app.centrifuge.io', | ||
| }; | ||
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.