solagent_plugin_birdeye/
lib.rs

1use anyhow::Result;
2use solagent_core::{IWallet, SolanaAgentKit};
3
4mod primitive;
5pub use primitive::*;
6
7/// Get overview of a token.
8///
9/// # Parameters
10///
11/// * `agent` - An instance of SolanaAgentKit (with .config.HELIUS_API_KEY)
12///
13/// - `address`: Address of a token
14///
15/// # Returns
16///
17/// A `Result` TokenOverviewResponse
18pub async fn get_token_overview<W: IWallet>(agent: &SolanaAgentKit<W>, address: &str) -> Result<TokenOverviewResponse> {
19    let api_key = agent
20        .config
21        .birdeye_api_key
22        .as_ref()
23        .ok_or_else(|| anyhow::anyhow!("Missing Birdeye API key in agent.config.birdeye_api_key"))?;
24
25    let client = reqwest::Client::new();
26    let url = format!("{}/defi/token_overview", BIRDEYE_URL);
27
28    let resp = client
29        .get(url)
30        .query(&[("address", address)])
31        .header("X-API-KEY", api_key)
32        .header("accept", "application/json")
33        .header("x-chain", "solana")
34        .send()
35        .await?
36        .json::<TokenOverviewResponse>()
37        .await?;
38
39    Ok(resp)
40}
41
42/// Get market data of single token
43///
44/// # Parameters
45///
46/// * `agent` - An instance of SolanaAgentKit
47///
48/// - `address`: Address of a token
49///
50/// # Returns
51///
52/// A `Result`
53pub async fn get_market_data<W: IWallet>(agent: &SolanaAgentKit<W>, address: &str) -> Result<MarketDataResponse> {
54    let api_key = agent
55        .config
56        .birdeye_api_key
57        .as_ref()
58        .ok_or_else(|| anyhow::anyhow!("Missing Birdeye API key in agent.config.birdeye_api_key"))?;
59
60    let client = reqwest::Client::new();
61    let url = format!("{}/defi/v3/token/market-data", BIRDEYE_URL);
62
63    let resp = client
64        .get(url)
65        .query(&[("address", address)])
66        .header("X-API-KEY", api_key)
67        .header("accept", "application/json")
68        .header("x-chain", "solana")
69        .send()
70        .await?
71        .json::<MarketDataResponse>()
72        .await?;
73
74    Ok(resp)
75}
76
77/// Get Wallet Portfolio
78///
79/// # Parameters
80///
81/// * `agent` - An instance of SolanaAgentKit
82///
83/// - `address`: Address of a wallet
84///
85/// # Returns
86///
87/// A `WalletPortfolioResponse`
88pub async fn get_wallet_portfolio<W: IWallet>(
89    agent: &SolanaAgentKit<W>,
90    wallet_address: &str,
91) -> Result<WalletPortfolioResponse> {
92    let api_key = agent
93        .config
94        .birdeye_api_key
95        .as_ref()
96        .ok_or_else(|| anyhow::anyhow!("Missing Birdeye API key in agent.config.birdeye_api_key"))?;
97
98    let client = reqwest::Client::new();
99    let url = format!("{}/v1/wallet/token_list", BIRDEYE_URL);
100
101    let response = client
102        .get(&url)
103        .query(&[("wallet", wallet_address)])
104        .header("accept", "application/json")
105        .header("X-API-KEY", api_key)
106        .header("x-chain", "solana")
107        .send()
108        .await?
109        .json::<WalletPortfolioResponse>()
110        .await?;
111
112    Ok(response)
113}