solagent_plugin_helius/
create_webhook.rs

1// Copyright 2025 zTgx
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16use solagent_core::SolanaAgentKit;
17
18#[derive(Deserialize, Serialize)]
19pub struct HeliusWebhookResponse {
20    pub webhook_url: String,
21    pub webhook_id: String,
22}
23
24pub async fn create_webhook(
25    agent: &SolanaAgentKit,
26    account_addresses: Vec<String>,
27    webhook_url: String,
28) -> Result<HeliusWebhookResponse, Box<dyn std::error::Error>> {
29    // Get the Helius API key from the agent's configuration
30    let api_key = match agent.config.helius_api_key.as_ref() {
31        Some(key) => key,
32        None => return Err("Missing Helius API key in agent.config.HELIUS_API_KEY".into()),
33    };
34
35    let url = format!("https://api.helius.xyz/v0/webhooks?api-key={}", api_key);
36
37    let body = serde_json::json!({
38        "webhookURL": webhook_url,
39        "transactionTypes": ["Any"],
40        "accountAddresses": account_addresses,
41        "webhookType": "enhanced",
42        "txnStatus": "all",
43    });
44
45    let client = reqwest::Client::new();
46    let response = client.post(url).header("Content-Type", "application/json").json(&body).send().await?;
47
48    let data = response.json::<serde_json::Value>().await?;
49    let webhook_url = data.get("webhookURL").expect("webhookURL field").as_str().expect("webhookURL text");
50    let webhook_id = data.get("webhookID").expect("webhookID field").as_str().expect("webhookID text");
51
52    Ok(HeliusWebhookResponse { webhook_url: webhook_url.to_string(), webhook_id: webhook_id.to_string() })
53}