solagent_plugin_cookie/
lib.rs

1use serde_json::Value;
2use solagent_core::SolanaAgentKit;
3use std::error::Error;
4
5/// Retrieve agent details in specified interval by one of its tokens contract address.
6///
7/// # Parameters
8///
9/// - `agent`: An instance of `SolanaAgentKit`.
10/// - `contract_address`: Contract address of one of the tokens contracts (matches case insensitive)
11/// - `interval`: An optional Interval for twitter stats and deltas (_3Days, _7Days). If not provided, returns the _7Days.
12///
13/// # Returns
14///
15/// A `Result` that agent details
16pub async fn get_agent_by_ca(
17    agent: &SolanaAgentKit,
18    contract_address: &str,
19    interval: Option<u32>,
20) -> Result<Value, Box<dyn Error>> {
21    // Get the Cookie API key from the agent's configuration
22    let api_key = match agent.config.cookie_api_key.as_ref() {
23        Some(key) => key,
24        None => return Err("Missing Cookie API key in agent.config.cookie_api_key".into()),
25    };
26
27    let api_url = "https://api.cookie.fun/v2/agents/contractAddress";
28    let url = format!("{}/{}?interval=_{}Days", api_url, contract_address, interval.unwrap_or(7));
29    let client = reqwest::Client::new();
30
31    let response = client.get(&url).header("x-api-key", api_key).send().await?;
32
33    let json: Value = response.json().await?;
34    Ok(json)
35}
36
37/// Retrieve agent details in specified interval by twitter username.
38///
39/// # Parameters
40///
41/// - `agent`: An instance of `SolanaAgentKit`.
42/// - `twitter_name`: Twitter username of agent (matches case insensitive)
43/// - `interval`: An optional Interval for twitter stats and deltas (_3Days, _7Days). If not provided, returns the _7Days.
44///
45/// # Returns
46///
47/// A `Result` that agent details
48pub async fn get_agent_by_name(
49    agent: &SolanaAgentKit,
50    twitter_name: &str,
51    interval: Option<u32>,
52) -> Result<Value, Box<dyn Error>> {
53    // Get the Cookie API key from the agent's configuration
54    let api_key = match agent.config.cookie_api_key.as_ref() {
55        Some(key) => key,
56        None => return Err("Missing Cookie API key in agent.config.cookie_api_key".into()),
57    };
58
59    let api_url = "https://api.cookie.fun/v2/agents/twitterUsername";
60    let url = format!("{}/{}?interval=_{}Days", api_url, twitter_name, interval.unwrap_or(7));
61    let client = reqwest::Client::new();
62
63    let response = client.get(&url).header("x-api-key", api_key).send().await?;
64
65    let json: Value = response.json().await?;
66    Ok(json)
67}
68
69/// Retrieve popular content matching search query, created in time range {from} - {to} (YYYY-MM-DD dates).
70///
71/// # Parameters
72///
73/// - `agent`: An instance of `SolanaAgentKit`.
74/// - `search_query`: Word or phrase to be searched for in text
75/// - `from`: Only consider content created after given date, eg. 2025-01-01
76/// - `to`: Only consider content created before given date, eg. 2025-01-20
77///
78/// # Returns
79///
80/// A `Result` that tweets details
81pub async fn search_tweets(
82    agent: &SolanaAgentKit,
83    tweets: &str,
84    from: &str,
85    to: &str,
86) -> Result<Value, Box<dyn Error>> {
87    // Get the Cookie API key from the agent's configuration
88    let api_key = match agent.config.cookie_api_key.as_ref() {
89        Some(key) => key,
90        None => return Err("Missing Cookie API key in agent.config.cookie_api_key".into()),
91    };
92
93    let api_url = "https://api.cookie.fun/v1/hackathon/search";
94    let url = format!("{}/{}?from={}&to={}", api_url, tweets, from, to);
95    let client = reqwest::Client::new();
96
97    let response = client.get(&url).header("x-api-key", api_key).send().await?;
98
99    let json: Value = response.json().await?;
100    Ok(json)
101}