solagent_plugin_birdeye/
lib.rs

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
use anyhow::Result;
use solagent_core::{IWallet, SolanaAgentKit};

mod primitive;
pub use primitive::*;

/// Get overview of a token.
///
/// # Parameters
///
/// * `agent` - An instance of SolanaAgentKit (with .config.HELIUS_API_KEY)
///
/// - `address`: Address of a token
///
/// # Returns
///
/// A `Result` TokenOverviewResponse
pub async fn get_token_overview<W: IWallet>(agent: &SolanaAgentKit<W>, address: &str) -> Result<TokenOverviewResponse> {
    let api_key = agent
        .config
        .birdeye_api_key
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Missing Birdeye API key in agent.config.birdeye_api_key"))?;

    let client = reqwest::Client::new();
    let url = format!("{}/defi/token_overview", BIRDEYE_URL);

    let resp = client
        .get(url)
        .query(&[("address", address)])
        .header("X-API-KEY", api_key)
        .header("accept", "application/json")
        .header("x-chain", "solana")
        .send()
        .await?
        .json::<TokenOverviewResponse>()
        .await?;

    Ok(resp)
}

/// Get market data of single token
///
/// # Parameters
///
/// * `agent` - An instance of SolanaAgentKit
///
/// - `address`: Address of a token
///
/// # Returns
///
/// A `Result`
pub async fn get_market_data<W: IWallet>(agent: &SolanaAgentKit<W>, address: &str) -> Result<MarketDataResponse> {
    let api_key = agent
        .config
        .birdeye_api_key
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Missing Birdeye API key in agent.config.birdeye_api_key"))?;

    let client = reqwest::Client::new();
    let url = format!("{}/defi/v3/token/market-data", BIRDEYE_URL);

    let resp = client
        .get(url)
        .query(&[("address", address)])
        .header("X-API-KEY", api_key)
        .header("accept", "application/json")
        .header("x-chain", "solana")
        .send()
        .await?
        .json::<MarketDataResponse>()
        .await?;

    Ok(resp)
}