dexpaprika-cli 0.3.0

DexPaprika CLI — free DEX data from the terminal. Pools, tokens, on-chain trades across 33+ chains.
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::client::ApiClient;
use crate::output::OutputFormat;

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenDetail {
    pub id: Option<String>,
    pub name: Option<String>,
    pub symbol: Option<String>,
    pub chain: Option<String>,
    pub decimals: Option<i64>,
    pub total_supply: Option<f64>,
    pub description: Option<String>,
    pub website: Option<String>,
    pub telegram: Option<String>,
    pub twitter: Option<String>,
    pub has_image: Option<bool>,
    pub added_at: Option<String>,
    pub price_stats: Option<TokenPriceStats>,
    pub summary: Option<TokenSummary>,
    pub last_updated: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPriceStats {
    pub high_24h: Option<f64>,
    pub low_24h: Option<f64>,
    pub ath: Option<f64>,
    pub ath_date: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenSummary {
    pub chain: Option<String>,
    pub id: Option<String>,
    pub price_usd: Option<f64>,
    pub fdv: Option<f64>,
    pub liquidity_usd: Option<f64>,
    pub pools: Option<i64>,
    #[serde(rename = "24h")]
    pub h24: Option<TokenPeriodStats>,
    #[serde(rename = "6h")]
    pub h6: Option<TokenPeriodStats>,
    #[serde(rename = "1h")]
    pub h1: Option<TokenPeriodStats>,
    #[serde(rename = "30m")]
    pub m30: Option<TokenPeriodStats>,
    #[serde(rename = "15m")]
    pub m15: Option<TokenPeriodStats>,
    #[serde(rename = "5m")]
    pub m5: Option<TokenPeriodStats>,
    #[serde(rename = "1m")]
    pub m1: Option<TokenPeriodStats>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPeriodStats {
    pub volume: Option<f64>,
    pub volume_usd: Option<f64>,
    pub sells: Option<i64>,
    pub buys: Option<i64>,
    pub txns: Option<i64>,
    pub buy_usd: Option<f64>,
    pub sell_usd: Option<f64>,
    pub last_price_usd_change: Option<f64>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPoolItem {
    pub id: Option<String>,
    pub dex_id: Option<String>,
    pub dex_name: Option<String>,
    pub tokens: Option<Vec<TokenPoolToken>>,
    pub price_usd: Option<f64>,
    pub volume_usd: Option<f64>,
    pub liquidity_usd: Option<f64>,
    pub last_price_change_usd_24h: Option<f64>,
    pub created_at: Option<String>,
}

/// Wrapper for paginated token-pools responses
#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPoolsResponse {
    pub pools: Vec<TokenPoolItem>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPoolToken {
    pub id: Option<String>,
    pub name: Option<String>,
    pub symbol: Option<String>,
    #[serde(flatten)]
    pub extra: Option<std::collections::HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenPrice {
    pub id: Option<String>,
    pub chain: Option<String>,
    pub price_usd: Option<f64>,
}

// --- Token filter types (for GET /networks/{network}/tokens/filter) ---

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenFilterResponse {
    pub results: Vec<TokenFilterResult>,
    pub page_info: Option<PageInfo>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TokenFilterResult {
    pub chain: Option<String>,
    pub address: Option<String>,
    pub price_usd: Option<f64>,
    pub volume_usd_24h: Option<f64>,
    pub volume_usd_7d: Option<f64>,
    pub liquidity_usd: Option<f64>,
    pub fdv_usd: Option<f64>,
    pub txns_24h: Option<i64>,
    pub created_at: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct PageInfo {
    pub limit: Option<i64>,
    pub page: Option<i64>,
    pub total_items: Option<i64>,
    pub total_pages: Option<i64>,
}

// --- Top tokens API types (for GET /networks/{network}/tokens/top) ---

#[derive(Debug, Deserialize, Serialize)]
pub struct TopTokensResponse {
    pub tokens: Vec<TopTokenApiItem>,
    pub page_info: Option<PageInfo>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TopTokenApiItem {
    pub address: Option<String>,
    pub name: Option<String>,
    pub symbol: Option<String>,
    pub chain: Option<String>,
    pub decimals: Option<i64>,
    pub has_image: Option<bool>,
    pub price_usd: Option<f64>,
    pub fdv: Option<f64>,
    pub liquidity_usd: Option<f64>,
    pub pools: Option<i64>,
    #[serde(rename = "24h")]
    pub h24: Option<TopTokenTimeData>,
    #[serde(rename = "1h")]
    pub h1: Option<TopTokenTimeData>,
    #[serde(rename = "5m")]
    pub m5: Option<TopTokenTimeData>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TopTokenTimeData {
    pub volume_usd: Option<f64>,
    pub buys: Option<i64>,
    pub sells: Option<i64>,
    pub txns: Option<i64>,
    pub last_price_usd_change: Option<f64>,
}

/// Summary of a token for the top-tokens ranking (derived from full TokenDetail)
#[derive(Debug, Serialize)]
pub struct TopTokenEntry {
    pub address: String,
    pub name: String,
    pub symbol: String,
    pub price_usd: Option<f64>,
    pub volume_usd_24h: Option<f64>,
    pub change_24h: Option<f64>,
    pub liquidity_usd: Option<f64>,
    pub buys_24h: Option<i64>,
    pub sells_24h: Option<i64>,
    pub txns_24h: Option<i64>,
    pub fdv: Option<f64>,
    pub pools: Option<i64>,
}

pub async fn execute_top_tokens(
    client: &ApiClient,
    network: &str,
    limit: usize,
    page: usize,
    order_by: &str,
    sort: &str,
    output: OutputFormat,
    raw: bool,
) -> Result<()> {
    let limit_str = limit.to_string();
    let page_str = page.to_string();
    let resp: TopTokensResponse = client
        .dexpaprika_get(
            &format!("/networks/{network}/tokens/top"),
            &[
                ("limit", &limit_str),
                ("page", &page_str),
                ("order_by", order_by),
                ("sort", sort),
            ],
        )
        .await?;

    let entries: Vec<TopTokenEntry> = resp
        .tokens
        .iter()
        .map(|t| {
            let (vol, change, buys, sells, txns) = match &t.h24 {
                Some(h) => (
                    h.volume_usd,
                    h.last_price_usd_change,
                    h.buys,
                    h.sells,
                    h.txns,
                ),
                None => (None, None, None, None, None),
            };
            TopTokenEntry {
                address: t.address.clone().unwrap_or_default(),
                name: t.name.clone().unwrap_or_default(),
                symbol: t.symbol.clone().unwrap_or_default(),
                price_usd: t.price_usd,
                volume_usd_24h: vol,
                change_24h: change,
                liquidity_usd: t.liquidity_usd,
                buys_24h: buys,
                sells_24h: sells,
                txns_24h: txns,
                fdv: t.fdv,
                pools: t.pools,
            }
        })
        .collect();

    match output {
        OutputFormat::Table => {
            crate::output::tokens::print_top_tokens_table(&entries);
            if let Some(pi) = &resp.page_info {
                println!(
                    "  Page {}/{} ({} tokens total)",
                    pi.page.unwrap_or(0),
                    pi.total_pages.unwrap_or(0),
                    pi.total_items.unwrap_or(0)
                );
            }
        }
        OutputFormat::Json => {
            crate::output::print_json_wrapped(
                &resp,
                crate::output::ResponseMeta::dexpaprika(&format!("/networks/{network}/tokens/top")),
                raw,
            )?;
        }
    }
    Ok(())
}

pub async fn execute_token(
    client: &ApiClient,
    network: &str,
    token_address: &str,
    output: OutputFormat,
    raw: bool,
) -> Result<()> {
    let token: TokenDetail = client
        .dexpaprika_get(&format!("/networks/{network}/tokens/{token_address}"), &[])
        .await?;
    match output {
        OutputFormat::Table => crate::output::tokens::print_token_detail(&token),
        OutputFormat::Json => {
            crate::output::print_json_wrapped(
                &token,
                crate::output::ResponseMeta::dexpaprika(&format!(
                    "/token/{network}/{token_address}"
                )),
                raw,
            )?;
        }
    }
    Ok(())
}

pub async fn execute_token_pools(
    client: &ApiClient,
    network: &str,
    token_address: &str,
    limit: usize,
    page: usize,
    order_by: &str,
    sort: &str,
    output: OutputFormat,
    raw: bool,
) -> Result<()> {
    let limit_str = limit.to_string();
    let page_str = page.to_string();
    let resp: TokenPoolsResponse = client
        .dexpaprika_get(
            &format!("/networks/{network}/tokens/{token_address}/pools"),
            &[
                ("limit", &limit_str),
                ("page", &page_str),
                ("order_by", order_by),
                ("sort", sort),
            ],
        )
        .await?;
    let pools = resp.pools;
    match output {
        OutputFormat::Table => crate::output::tokens::print_token_pools_table(&pools),
        OutputFormat::Json => {
            crate::output::print_json_wrapped(
                &pools,
                crate::output::ResponseMeta::dexpaprika(&format!(
                    "/token/{network}/{token_address}/pools"
                )),
                raw,
            )?;
        }
    }
    Ok(())
}

pub async fn execute_prices(
    client: &ApiClient,
    network: &str,
    tokens: &str,
    output: OutputFormat,
    raw: bool,
) -> Result<()> {
    let prices: Vec<TokenPrice> = client
        .dexpaprika_get(
            &format!("/networks/{network}/multi/prices"),
            &[("tokens", tokens)],
        )
        .await?;

    if prices.is_empty() {
        anyhow::bail!(
            "No price data found. Check that the token addresses are valid on {network}."
        );
    }

    match output {
        OutputFormat::Table => crate::output::tokens::print_prices_table(&prices),
        OutputFormat::Json => {
            crate::output::print_json_wrapped(
                &prices,
                crate::output::ResponseMeta::dexpaprika(&format!("/network/{network}/prices")),
                raw,
            )?;
        }
    }
    Ok(())
}

pub async fn execute_filter_tokens(
    client: &ApiClient,
    network: &str,
    limit: usize,
    page: usize,
    sort_by: &str,
    sort_dir: &str,
    volume_24h_min: Option<f64>,
    volume_24h_max: Option<f64>,
    liquidity_usd_min: Option<f64>,
    fdv_min: Option<f64>,
    fdv_max: Option<f64>,
    txns_24h_min: Option<u64>,
    created_after: Option<u64>,
    created_before: Option<u64>,
    output: OutputFormat,
    raw: bool,
) -> Result<()> {
    let limit_str = limit.to_string();
    let page_str = page.to_string();

    let mut params: Vec<(&str, String)> = vec![
        ("limit", limit_str),
        ("page", page_str),
        ("sort_by", sort_by.to_string()),
        ("sort_dir", sort_dir.to_string()),
    ];
    if let Some(v) = volume_24h_min {
        params.push(("volume_24h_min", v.to_string()));
    }
    if let Some(v) = volume_24h_max {
        params.push(("volume_24h_max", v.to_string()));
    }
    if let Some(v) = liquidity_usd_min {
        params.push(("liquidity_usd_min", v.to_string()));
    }
    if let Some(v) = fdv_min {
        params.push(("fdv_min", v.to_string()));
    }
    if let Some(v) = fdv_max {
        params.push(("fdv_max", v.to_string()));
    }
    if let Some(v) = txns_24h_min {
        params.push(("txns_24h_min", v.to_string()));
    }
    if let Some(v) = created_after {
        params.push(("created_after", v.to_string()));
    }
    if let Some(v) = created_before {
        params.push(("created_before", v.to_string()));
    }

    let param_refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();

    let resp: TokenFilterResponse = client
        .dexpaprika_get(&format!("/networks/{network}/tokens/filter"), &param_refs)
        .await?;

    match output {
        OutputFormat::Table => {
            crate::output::tokens::print_token_filter_table(&resp.results);
            if let Some(pi) = &resp.page_info {
                println!(
                    "  Page {}/{} ({} tokens total)",
                    pi.page.unwrap_or(0),
                    pi.total_pages.unwrap_or(0),
                    pi.total_items.unwrap_or(0)
                );
            }
        }
        OutputFormat::Json => {
            crate::output::print_json_wrapped(
                &resp,
                crate::output::ResponseMeta::dexpaprika(&format!(
                    "/networks/{network}/tokens/filter"
                )),
                raw,
            )?;
        }
    }
    Ok(())
}