ferrox-actions 0.1.0

Action system for the Ferrox AI framework
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use reqwest::{
    header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE},
    Client,
};
use solana_sdk::pubkey::Pubkey;

const BASE_URL: &str = "https://public-api.birdeye.so";

#[derive(Debug, Clone)]
pub struct BirdeyeClient {
    api_key: String,
    client: Client,
}

impl BirdeyeClient {
    pub fn new(api_key: String) -> Self {
        Self {
            api_key,
            client: Client::new(),
        }
    }

    fn get_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert("X-API-KEY", HeaderValue::from_str(&self.api_key).unwrap());
        headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers
    }

    async fn make_request(&self, endpoint: &str) -> Result<String, String> {
        let url = format!("{}{}", BASE_URL, endpoint);
        println!("Making request to {}", url);
        let response = self
            .client
            .get(&url)
            .headers(self.get_headers())
            .send()
            .await
            .map_err(|e| e.to_string())?;

        if response.status().is_success() {
            response.text().await.map_err(|e| e.to_string())
        } else {
            Err(format!("Request failed with status: {}", response.status()))
        }
    }

    fn format_resolution(resolution: String) -> String {
        // If resolution is just a number, append "M"
        if resolution.chars().all(|c| c.is_numeric()) {
            format!("{}m", resolution)
        } else {
            resolution
        }
    }

    fn validate_solana_address(address: &str) -> Result<Pubkey, String> {
        address
            .parse::<Pubkey>()
            .map_err(|e| format!("Invalid Solana address: {}", e))
    }

    pub async fn get_token_price(&self, address: String) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        self.make_request(&format!("/defi/price?address={}", pubkey.to_string()))
            .await
    }

    pub async fn get_token_price_history(
        &self,
        address: String,
        resolution: String,
        time_from: Option<i64>,
        time_to: Option<i64>,
        limit: Option<i32>,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        let formatted_resolution = Self::format_resolution(resolution);
        let mut endpoint = format!(
            "/defi/history_price?address={}&address_type=token&type={}",
            pubkey.to_string(),
            formatted_resolution
        );

        if let Some(from) = time_from {
            endpoint.push_str(&format!("&time_from={}", from));
        }
        if let Some(to) = time_to {
            endpoint.push_str(&format!("&time_to={}", to));
        }
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_multi_token_price(&self, addresses: String) -> Result<String, String> {
        let pubkeys: Result<Vec<Pubkey>, String> = addresses
            .split(',')
            .map(|addr| Self::validate_solana_address(addr.trim()))
            .collect();
        let pubkeys = pubkeys?;

        let formatted_addresses = pubkeys
            .iter()
            .map(|pubkey| pubkey.to_string())
            .collect::<Vec<String>>()
            .join(",");

        self.make_request(&format!(
            "/defi/multi_price?list_address={}",
            formatted_addresses
        ))
        .await
    }

    pub async fn get_token_trending(&self, limit: Option<i32>) -> Result<String, String> {
        let mut endpoint = "/defi/token_trending".to_string();
        if let Some(limit) = limit {
            endpoint.push_str(&format!("?limit={}", limit));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_token_ohlcv(
        &self,
        address: String,
        resolution: String,
        time_from: i64,
        time_to: i64,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        let formatted_resolution = Self::format_resolution(resolution);
        self.make_request(&format!(
            "/defi/ohlcv?address={}&type={}&time_from={}&time_to={}",
            pubkey.to_string(),
            formatted_resolution,
            time_from,
            time_to
        ))
        .await
    }

    pub async fn get_pair_ohlcv(
        &self,
        pair_address: String,
        resolution: String,
        time_from: i64,
        time_to: i64,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&pair_address)?;
        let formatted_resolution = Self::format_resolution(resolution);
        self.make_request(&format!(
            "/defi/ohlcv/pair?address={}&type={}&time_from={}&time_to={}",
            pubkey.to_string(),
            formatted_resolution,
            time_from,
            time_to
        ))
        .await
    }

    pub async fn get_token_trades(
        &self,
        address: String,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        println!("Pubkey: {:?}", pubkey);
        let mut endpoint = format!(
            "/defi/txs/token?address={}&sort_type=desc",
            pubkey.to_string()
        );
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!("&offset={}", offset));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_pair_trades(
        &self,
        pair_address: String,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&pair_address)?;
        println!("Pubkey: {:?}", pubkey);
        let mut endpoint = format!(
            "/defi/txs/pair?address={}&tx_type=swap&sort_type=desc",
            pubkey.to_string()
        );
        if let Some(limit) = limit {
            if limit >= 50 {
                endpoint.push_str("&limit=50");
            } else {
                endpoint.push_str(&format!("&limit={}", limit));
            }
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!("&offset={}", offset));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_token_overview(&self, address: String) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        self.make_request(&format!(
            "/defi/token_overview?address={}",
            pubkey.to_string()
        ))
        .await
    }

    pub async fn get_token_list(
        &self,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let mut endpoint = "/defi/tokenList".to_string();
        let mut has_param = false;
        if let Some(limit) = limit {
            endpoint.push_str(&format!("?limit={}", limit));
            has_param = true;
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!(
                "{}offset={}",
                if has_param { "&" } else { "?" },
                offset
            ));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_token_security(&self, address: String) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        self.make_request(&format!(
            "/defi/token_security?address={}",
            pubkey.to_string()
        ))
        .await
    }

    pub async fn get_token_market_list(&self, address: String) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        self.make_request(&format!("/defi/v2/markets?address={}", pubkey.to_string()))
            .await
    }

    pub async fn get_token_new_listing(
        &self,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let mut endpoint = "/defi/v2/tokens/new_listing".to_string();
        let mut has_param = false;
        if let Some(limit) = limit {
            endpoint.push_str(&format!("?limit={}", limit));
            has_param = true;
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!(
                "{}offset={}",
                if has_param { "&" } else { "?" },
                offset
            ));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_token_top_traders(
        &self,
        address: String,
        limit: Option<i32>,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        let mut endpoint = format!("/defi/v2/tokens/top_traders?address={}", pubkey.to_string());
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        self.make_request(&endpoint).await
    }

    // Trader endpoints
    pub async fn get_gainers_losers(&self) -> Result<String, String> {
        self.make_request("/trader/gainers-losers").await
    }

    pub async fn get_trader_txs_by_time(
        &self,
        address: String,
        time_from: i64,
        time_to: i64,
        limit: Option<i32>,
    ) -> Result<String, String> {
        let pubkey = Self::validate_solana_address(&address)?;
        let mut endpoint = format!(
            "/trader/txs/seek_by_time?address={}&from={}&to={}",
            pubkey.to_string(),
            time_from,
            time_to
        );
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        self.make_request(&endpoint).await
    }

    // Wallet endpoints
    pub async fn list_supported_chains(&self) -> Result<String, String> {
        self.make_request("/v1/wallet/list_supported_chain").await
    }

    pub async fn get_wallet_portfolio(
        &self,
        wallet_address: String,
        chain_id: String,
    ) -> Result<String, String> {
        self.make_request(&format!(
            "/v1/wallet/token_list?wallet={}&chain_id={}",
            wallet_address, chain_id
        ))
        .await
    }

    pub async fn get_wallet_portfolio_multichain(
        &self,
        wallet_address: String,
    ) -> Result<String, String> {
        self.make_request(&format!(
            "/v1/wallet/multichain_token_list?wallet={}",
            wallet_address
        ))
        .await
    }

    // pub async fn get_wallet_token_balance(
    //     &self,
    //     wallet_address: String,
    //     token_address: String,
    //     chain_id: String,
    // ) -> Result<String, String> {
    //     self.make_request(&format!(
    //         "/v1/wallet/token_balance?wallet={}&token_address={:?}&chain_id={}",
    //         wallet_address, token_address, chain_id
    //     ))
    //     .await
    // }

    pub async fn get_wallet_transaction_history(
        &self,
        wallet_address: String,
        chain_id: String,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let mut endpoint = format!(
            "/v1/wallet/tx_list?wallet={}&chain_id={}",
            wallet_address, chain_id
        );
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!("&offset={}", offset));
        }
        self.make_request(&endpoint).await
    }

    pub async fn get_wallet_transaction_history_multichain(
        &self,
        wallet_address: String,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> Result<String, String> {
        let mut endpoint = format!("/v1/wallet/multichain_tx_list?wallet={}", wallet_address);
        if let Some(limit) = limit {
            endpoint.push_str(&format!("&limit={}", limit));
        }
        if let Some(offset) = offset {
            endpoint.push_str(&format!("&offset={}", offset));
        }
        self.make_request(&endpoint).await
    }

    pub async fn simulate_transaction(
        &self,
        chain_id: String,
        tx_data: String,
    ) -> Result<String, String> {
        self.make_request(&format!(
            "/v1/wallet/simulate?chain_id={}&tx_data={}",
            chain_id, tx_data
        ))
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn setup_client() -> BirdeyeClient {
        let api_key = std::env::var("BIRDEYE_API_KEY")
            .expect("BIRDEYE_API_KEY must be set in .env for tests");
        BirdeyeClient::new(api_key)
    }

    const SOL_ADDRESS: &str = "So11111111111111111111111111111111111111112";
    const USDC_ADDRESS: &str = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
    const TEST_WALLET: &str = "9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM"; // Example Solana wallet
    const TEST_CHAIN_ID: &str = "solana";

    #[tokio::test]
    async fn test_get_token_price() {
        let client = setup_client();
        let result = client.get_token_price(SOL_ADDRESS.to_string()).await;
        println!("Token price result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_price_history() {
        let client = setup_client();
        let result = client
            .get_token_price_history(
                SOL_ADDRESS.to_string(),
                "15m".to_string(),
                Some(1677652288),
                Some(1677738688),
                Some(100),
            )
            .await;
        println!("Price history result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_multi_token_price() {
        let client = setup_client();
        let addresses = format!("{},{}", SOL_ADDRESS, USDC_ADDRESS);
        let result = client.get_multi_token_price(addresses).await;
        println!("Multi token price result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_ohlcv() {
        let client = setup_client();
        let result = client
            .get_token_ohlcv(
                SOL_ADDRESS.to_string(),
                "1D".to_string(),
                1677652288,
                1677738688,
            )
            .await;
        println!("OHLCV result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_pair_ohlcv() {
        let client = setup_client();
        let result = client
            .get_pair_ohlcv(
                "8HoQnePLqPj4M7PUDzfw8e3Ymdwgc7NLGnaTUapubyvu".to_string(), // SOL/USDC pair
                "1D".to_string(),
                1677652288,
                1677738688,
            )
            .await;
        println!("Pair OHLCV result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_trades() {
        let client = setup_client();
        let result = client
            .get_token_trades(SOL_ADDRESS.to_string(), Some(10), Some(0))
            .await;
        println!("Token trades result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_pair_trades() {
        let client = setup_client();
        let result = client
            .get_pair_trades(
                "8HoQnePLqPj4M7PUDzfw8e3Ymdwgc7NLGnaTUapubyvu".to_string(),
                Some(10),
                Some(0),
            )
            .await;
        println!("Pair trades result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_overview() {
        let client = setup_client();
        let result = client.get_token_overview(SOL_ADDRESS.to_string()).await;
        println!("Token overview result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_list() {
        let client = setup_client();
        let result = client.get_token_list(Some(10), Some(0)).await;
        println!("Token list result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_security() {
        let client = setup_client();
        let result = client.get_token_security(SOL_ADDRESS.to_string()).await;
        println!("Token security result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_market_list() {
        let client = setup_client();
        let result = client.get_token_market_list(SOL_ADDRESS.to_string()).await;
        println!("Market list result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_new_listing() {
        let client = setup_client();
        let result = client.get_token_new_listing(Some(10), Some(0)).await;
        println!("New listing result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_top_traders() {
        let client = setup_client();
        let result = client
            .get_token_top_traders(SOL_ADDRESS.to_string(), Some(10))
            .await;
        println!("Top traders result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_token_trending() {
        let client = setup_client();
        let result = client.get_token_trending(Some(10)).await;
        println!("Trending result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_gainers_losers() {
        let client = setup_client();
        let result = client.get_gainers_losers().await;
        println!("Gainers/Losers result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_trader_txs_by_time() {
        let client = setup_client();
        let result = client
            .get_trader_txs_by_time(SOL_ADDRESS.to_string(), 1677652288, 1677738688, Some(10))
            .await;
        println!("Trader txs result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_list_supported_chains() {
        let client = setup_client();
        let result = client.list_supported_chains().await;
        println!("Supported chains result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_wallet_portfolio() {
        let client = setup_client();
        let result = client
            .get_wallet_portfolio(TEST_WALLET.to_string(), TEST_CHAIN_ID.to_string())
            .await;
        println!("Wallet portfolio result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_wallet_portfolio_multichain() {
        let client = setup_client();
        let result = client
            .get_wallet_portfolio_multichain(TEST_WALLET.to_string())
            .await;
        println!("Multichain portfolio result: {:?}", result);
        assert!(result.is_ok());
    }

    // #[tokio::test]
    // async fn test_get_wallet_token_balance() {
    //     let client = setup_client();
    //     let result = client
    //         .get_wallet_token_balance(
    //             TEST_WALLET.to_string(),
    //             SOL_ADDRESS.to_string(),
    //             TEST_CHAIN_ID.to_string(),
    //         )
    //         .await;
    //     println!("Token balance result: {:?}", result);
    //     assert!(result.is_ok());
    // }

    #[tokio::test]
    async fn test_get_wallet_transaction_history() {
        let client = setup_client();
        let result = client
            .get_wallet_transaction_history(
                TEST_WALLET.to_string(),
                TEST_CHAIN_ID.to_string(),
                Some(10),
                Some(0),
            )
            .await;
        println!("Transaction history result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_wallet_transaction_history_multichain() {
        let client = setup_client();
        let result = client
            .get_wallet_transaction_history_multichain(TEST_WALLET.to_string(), Some(10), Some(0))
            .await;
        println!("Multichain transaction history result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_simulate_transaction() {
        let client = setup_client();
        let result = client
            .simulate_transaction(
                TEST_CHAIN_ID.to_string(),
                "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDBXsgXgYAAAAAAAA".to_string(),
            )
            .await;
        println!("Transaction simulation result: {:?}", result);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_error_handling() {
        let client = BirdeyeClient::new("invalid-api-key".to_string());
        let result = client.get_token_price(SOL_ADDRESS.to_string()).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_format_resolution() {
        assert_eq!(BirdeyeClient::format_resolution("1".to_string()), "1m");
        assert_eq!(BirdeyeClient::format_resolution("15".to_string()), "15m");
        assert_eq!(BirdeyeClient::format_resolution("1D".to_string()), "1D");
        assert_eq!(BirdeyeClient::format_resolution("1W".to_string()), "1W");
    }
}