1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Strategy-related GraphQL queries
use crate::client::Client;
use crate::error::Result;
use crate::types::Strategy;
use serde::Deserialize;
/// Strategy query builder for filtering strategies
#[derive(Debug, Default, Clone)]
pub struct StrategyFilter {
chain_id: Option<u64>,
vault: Option<String>,
v3: Option<bool>,
addresses: Option<Vec<String>>,
}
impl StrategyFilter {
/// Create a new filter
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Filter by chain ID
#[must_use]
pub fn chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = Some(chain_id);
self
}
/// Filter by vault address
#[must_use]
pub fn vault(mut self, vault: impl Into<String>) -> Self {
self.vault = Some(vault.into());
self
}
/// Filter v3 strategies only
#[must_use]
pub fn v3(mut self, v3: bool) -> Self {
self.v3 = Some(v3);
self
}
/// Filter by specific addresses
#[must_use]
pub fn addresses(mut self, addresses: Vec<String>) -> Self {
self.addresses = Some(addresses);
self
}
/// Build the GraphQL arguments string
fn build_args(&self) -> String {
let mut args = Vec::new();
if let Some(chain_id) = self.chain_id {
args.push(format!("chainId: {chain_id}"));
}
if let Some(ref vault) = self.vault {
args.push(format!("vault: \"{vault}\""));
}
if let Some(v3) = self.v3 {
args.push(format!("v3: {v3}"));
}
if let Some(ref addresses) = self.addresses {
let addr_str: Vec<String> = addresses.iter().map(|a| format!("\"{a}\"")).collect();
args.push(format!("addresses: [{}]", addr_str.join(", ")));
}
if args.is_empty() {
String::new()
} else {
format!("({})", args.join(", "))
}
}
}
/// Strategies API
pub struct StrategiesApi<'a> {
client: &'a Client,
}
impl<'a> StrategiesApi<'a> {
/// Create a new strategies API instance
#[must_use]
pub fn new(client: &'a Client) -> Self {
Self { client }
}
/// Get all strategies (with optional filter)
///
/// # Example
///
/// ```no_run
/// # async fn example() -> ykong::error::Result<()> {
/// use ykong::Client;
///
/// let client = Client::new()?;
/// let strategies = client.strategies().list(None).await?;
/// println!("Found {} strategies", strategies.len());
/// # Ok(())
/// # }
/// ```
pub async fn list(&self, filter: Option<StrategyFilter>) -> Result<Vec<Strategy>> {
let args = filter.unwrap_or_default().build_args();
let query = format!(
r"{{
strategies{args} {{
address
name
chainId
apiVersion
vault
v3
activation
inceptTime
inceptBlock
lastReport
totalDebt
totalGain
totalLoss
performanceFee
debtRatio
estimatedTotalAssets
isActive
isShutdown
keeper
strategist
risk {{ riskLevel riskGroup }}
apy {{ net weeklyNet monthlyNet }}
tvl {{ close blockNumber blockTime }}
}}
}}"
);
#[derive(Deserialize)]
struct Response {
strategies: Vec<Strategy>,
}
let response: Response = self.client.query(&query).await?;
Ok(response.strategies)
}
/// Get strategies for a specific chain
pub async fn by_chain(&self, chain_id: u64) -> Result<Vec<Strategy>> {
self.list(Some(StrategyFilter::new().chain_id(chain_id)))
.await
}
/// Get strategies for a specific vault
pub async fn by_vault(&self, chain_id: u64, vault: &str) -> Result<Vec<Strategy>> {
self.list(Some(StrategyFilter::new().chain_id(chain_id).vault(vault)))
.await
}
/// Get a single strategy by address and chain
pub async fn get(&self, chain_id: u64, address: &str) -> Result<Option<Strategy>> {
let query = format!(
r#"{{
strategy(chainId: {chain_id}, address: "{address}") {{
address
name
chainId
apiVersion
vault
v3
activation
inceptTime
inceptBlock
lastReport
totalDebt
totalGain
totalLoss
performanceFee
debtRatio
estimatedTotalAssets
isActive
isShutdown
keeper
strategist
risk {{ riskLevel riskGroup tvlImpact auditScore codeReviewScore complexityScore longevityImpact protocolSafetyScore teamKnowledgeScore testingScore }}
apy {{ net weeklyNet monthlyNet inceptionNet grossApr }}
tvl {{ close blockNumber blockTime }}
}}
}}"#
);
#[derive(Deserialize)]
struct Response {
strategy: Option<Strategy>,
}
let response: Response = self.client.query(&query).await?;
Ok(response.strategy)
}
}