solagent_plugin_helius/
transaction_parsing.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 serde_json::json;
17use solagent_core::SolanaAgentKit;
18
19#[derive(Debug, Serialize, Deserialize)]
20pub struct HeliusWebhookIdResponse {
21    pub wallet: String,
22    pub webhook_url: String,
23    pub transaction_types: Vec<String>,
24    pub account_addresses: Vec<String>,
25    pub webhook_type: String,
26}
27
28/// Parse a Solana transaction using the Helius Enhanced Transactions API
29///
30/// # Arguments
31/// * `agent` - An instance of SolanaAgentKit (with .config.HELIUS_API_KEY)
32/// * `transaction_id` - The transaction ID to parse
33///
34/// # Returns
35/// Parsed transaction data
36pub async fn transaction_parse(
37    agent: &SolanaAgentKit,
38    transaction_id: &str,
39) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
40    // Get the Helius API key from the agent's configuration
41    let api_key = match agent.config.helius_api_key.as_ref() {
42        Some(key) => key,
43        None => return Err("Missing Helius API key in agent.config.HELIUS_API_KEY".into()),
44    };
45
46    let client = reqwest::Client::new();
47    let url = format!("https://api.helius.xyz/v0/transactions/?api-key={}", api_key);
48
49    let body = json!( {
50        "transactions": vec![transaction_id.to_string()],
51    });
52
53    let response = client.post(url).header("Content-Type", "application/json").json(&body).send().await?;
54
55    let data = response.json().await?;
56    Ok(data)
57}