solagent_plugin_rugcheck/
token_report_summary.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 crate::RUGCHECK_URL;
16use serde::{Deserialize, Serialize};
17
18#[derive(Debug, Serialize, Deserialize, Default)]
19pub struct Risk {
20    pub name: String,
21    pub level: String,
22    pub description: String,
23    pub score: f64,
24}
25
26#[derive(Debug, Serialize, Deserialize, Default)]
27pub struct TokenCheck {
28    pub token_program: String,
29    pub token_type: String,
30    pub risks: Vec<Risk>,
31}
32
33/// Fetches a summary report for a specific token.
34///
35/// # Parameters
36///
37/// - `mint` - The mint address of the token.
38///
39/// # Returns
40/// Token summary report.
41///
42/// # Errors
43/// Throws an error if the API call fails.
44pub async fn fetch_summary_report(mint: String) -> Result<TokenCheck, Box<dyn std::error::Error>> {
45    let url = format!("{}/tokens/{}/report/summary", RUGCHECK_URL, mint);
46
47    let response = reqwest::get(&url).await?;
48    if !response.status().is_success() {
49        return Err(format!("HTTP error! status: {}", response.status()).into());
50    }
51
52    let data: serde_json::Value = response.json().await?;
53
54    let mut token_check = TokenCheck::default();
55    let token_program = data.get("tokenProgram").and_then(|p| p.as_str()).expect("tokenProgram field");
56    token_check.token_program = token_program.into();
57
58    let token_type = data.get("tokenType").and_then(|p| p.as_str()).expect("tokenType field");
59    token_check.token_type = token_type.into();
60
61    let mut risks: Vec<Risk> = vec![];
62    let risks_data = data.get("risks").and_then(|p| p.as_array()).expect("risks field");
63    for risk in risks_data {
64        let mut r = Risk::default();
65        let name = risk.get("name").and_then(|p| p.as_str()).expect("name field");
66        let description = risk.get("description").and_then(|p| p.as_str()).expect("description field");
67        let score = risk.get("score").and_then(|p| p.as_f64()).expect("score field");
68        let level = risk.get("level").and_then(|p| p.as_str()).expect("level field");
69
70        r.name = name.into();
71        r.description = description.into();
72        r.score = score;
73        r.level = level.into();
74
75        risks.push(r);
76    }
77    token_check.risks = risks;
78
79    Ok(token_check)
80}