Skip to main content

finance_query/models/crypto/
defi.rs

1//! DeFi / on-chain models.
2//!
3//! Populated by the DefiLlama adapter. These describe *protocols and chains*
4//! rather than tradable instruments, so they live beside — not inside —
5//! [`CryptoQuote`](super::CryptoQuote).
6
7use serde::{Deserialize, Serialize};
8
9/// Total value locked in a DeFi protocol, with the metadata needed to
10/// identify it.
11///
12/// Obtain via [`CryptoCoin::tvl`](crate::CryptoCoin::tvl).
13#[derive(Debug, Clone, Serialize, Deserialize)]
14#[non_exhaustive]
15pub struct ProtocolTvl {
16    /// DefiLlama protocol slug, as queried (e.g. `"aave"`).
17    pub slug: String,
18    /// Display name (e.g. `"AAVE"`).
19    pub name: Option<String>,
20    /// Governance/token symbol, when the protocol has one.
21    pub symbol: Option<String>,
22    /// Project homepage.
23    pub url: Option<String>,
24    /// Chains the protocol is deployed on.
25    pub chains: Vec<String>,
26    /// Latest total value locked, in USD.
27    pub tvl: Option<f64>,
28    /// TVL broken down by chain, in USD.
29    pub tvl_by_chain: Vec<ChainAllocation>,
30    /// Change in TVL over the last day, as a percentage.
31    pub change_1d_percent: Option<f64>,
32    /// Change in TVL over the last seven days, as a percentage.
33    pub change_7d_percent: Option<f64>,
34    /// Market capitalisation of the protocol's token, in USD.
35    pub market_cap: Option<f64>,
36}
37
38/// One chain's share of a protocol's TVL.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[non_exhaustive]
41pub struct ChainAllocation {
42    /// Chain name (e.g. `"Ethereum"`).
43    pub chain: String,
44    /// Value locked on that chain, in USD.
45    pub tvl: f64,
46}
47
48/// One point of a protocol's TVL history.
49///
50/// Obtain via [`CryptoCoin::tvl_history`](crate::CryptoCoin::tvl_history).
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[non_exhaustive]
53pub struct TvlPoint {
54    /// Unix timestamp (seconds) of the snapshot.
55    pub timestamp: i64,
56    /// Total value locked at that moment, in USD.
57    pub tvl: f64,
58}
59
60/// Aggregate value locked on one blockchain.
61///
62/// Obtain via [`defi::chains`](crate::defi::chains).
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[non_exhaustive]
65pub struct ChainTvl {
66    /// Chain name (e.g. `"Ethereum"`).
67    pub name: String,
68    /// Native token symbol, when the chain has one.
69    pub token_symbol: Option<String>,
70    /// CoinGecko id of the native token, for cross-referencing a price.
71    pub gecko_id: Option<String>,
72    /// EVM chain id, where one exists.
73    pub chain_id: Option<i64>,
74    /// Total value locked across every protocol on the chain, in USD.
75    pub tvl: Option<f64>,
76}
77
78/// Circulating supply of one stablecoin.
79///
80/// Obtain via [`defi::stablecoins`](crate::defi::stablecoins).
81#[derive(Debug, Clone, Serialize, Deserialize)]
82#[non_exhaustive]
83pub struct StablecoinSupply {
84    /// Stablecoin name (e.g. `"Tether"`).
85    pub name: String,
86    /// Ticker symbol (e.g. `"USDT"`).
87    pub symbol: Option<String>,
88    /// CoinGecko id, for cross-referencing a price.
89    pub gecko_id: Option<String>,
90    /// What the coin is pegged to, e.g. `"peggedUSD"`.
91    pub peg_type: Option<String>,
92    /// How the peg is maintained, e.g. `"fiat-backed"`, `"crypto-backed"`.
93    pub peg_mechanism: Option<String>,
94    /// Current circulating supply, denominated in the pegged asset.
95    pub circulating: Option<f64>,
96    /// Circulating supply one day ago, for change calculations.
97    pub circulating_prev_day: Option<f64>,
98    /// Circulating supply one week ago.
99    pub circulating_prev_week: Option<f64>,
100    /// Circulating supply one month ago.
101    pub circulating_prev_month: Option<f64>,
102}