goplus_rs 1.0.0

This repository contains a Rust API wrapper for interacting with GoPlusLabs services for risk metrics and analysis on tokens, smart contracts, and wallets across different chain ecosystems.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::time::UNIX_EPOCH;
use reqwest::Client;
use serde_json::{json, Value};
use thiserror::Error;

mod api_structs;
use api_structs::*;

const BASE_URL: &str = "https://api.gopluslabs.io/api/v1";


#[derive(Error, Debug)]
pub enum GpError {
    #[error("Status {0} - {1}")]
    RequestError(u32, String),
    #[error("Parsing failed - {0}")]
    ParseError(String),
}

impl From<reqwest::Error> for GpError {
    fn from(value: reqwest::Error) -> Self {
        match value.to_string().contains("missing field") {
            true => Self::ParseError(value.to_string()),
            false => Self::RequestError(value.status().unwrap().as_u16().into(), value.to_string())
        }
        
    }
}

/// API Driver - handles all interaction with GoPlus endpoints
#[derive(Default)]
pub struct Session {
    inner: Client,
    access_token: Option<String>,
}

/// Used in V2Approval Call
pub enum V2ApprovalERC {
    ERC20,
    ERC721,
    ERC1155
}

impl Session {
    pub fn new() -> Self {
        // If app_key env var set
        let app_key = std::env::var("GP_PUBLIC");
        let secret_key = std::env::var("GP_SECRET");

        if app_key.is_err() || secret_key.is_err(){
            // No access token
            tracing::warn!("Set enviornment variables to get access code");
            tracing::warn!("  `export GP_PUBLIC = $APP_PUBLIC_KEY$`");
            tracing::warn!("  `export GP_PUBLIC = $APP_PRIVATE_KEY$`");
            Self {
                inner: Client::new(),
                access_token: None,
            }
        } 
        else {
            // UNCERTAIN IF WORKS, CAN'T TEST W/OUT KEYS
            use sha1::{Sha1, Digest};
            let mut hasher = Sha1::new();
            let time: u64 = std::time::SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
            let hash_str = format!("{}{}{}", app_key.unwrap(), time, secret_key.unwrap());
            hasher.update(hash_str);
            let f = hasher.finalize();
            let str_hash = format!("{:x}", f);
            
            Self {
                inner: Client::new(),
                access_token: Some(str_hash),
            }
        }

        
    }

    /// Retrieves a list of supported blockchain chains from the API.
    ///
    /// 
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.supported_chains().await?;
    /// let chains: Vec<Chain> = response.result;
    /// ```
    /// Tablular form of return data available [here](https://docs.gopluslabs.io/reference/response-details-9)
    pub async fn supported_chains(&self) -> Result<SupportedChainsResponse, GpError> {
        let url = format!("{BASE_URL}/supported_chains");
        let res = self
            .inner
            .get(url)
            .header("access_token", self.access_token.clone().unwrap_or("None".to_string()))
            .send()
            .await?
            .error_for_status()?
            .json::<SupportedChainsResponse>()
            .await?;

        Ok(res)
    }

    /// Fetches token risk data based on the blockchain chain ID and address.
    ///
    /// # Parameters
    /// - `chain_id`: The blockchain chain ID.
    /// - `addr`: The address to check.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.token_risk("56", "0xEa51801b8F5B88543DdaD3D1727400c15b209D8f").await?;
    /// let risk_data: Hashmap<String, TokenData> = response.result;
    /// ```
    /// Response fields in depth [here](https://docs.gopluslabs.io/reference/response-details)
    pub async fn token_risk(&self, chain_id: &str, addr: &str) -> Result<TokenResponse, anyhow::Error> {
        let url = format!(
            "{}/token_security/{}", BASE_URL, chain_id
        );

        Ok(self.inner.get(url)
            .header("access_token", self.access_token.clone().unwrap_or("None".to_string()))
            .query(&[("contract_addresses", addr)])
            .send()
            .await?
            .error_for_status()?
            .json::<TokenResponse>()
            .await?)
    }

    /// Retrieves risk information about an address, optionally filtered by chain ID.
    ///
    /// If only the address is provided without specifying the chain ID, the `contract_address` 
    /// field in the response may be omitted. This occurs because the same address can represent 
    /// a contract on one blockchain but not on another. Determination of `contract_address` involves
    /// querying a third-party blockchain browser interface, which may delay the response. 
    /// The `contract_address` field may initially be empty due to this delay. A subsequent request 
    /// after about 5 seconds typically returns complete data, including the `contract_address`.
    ///
    /// # Parameters
    /// - `addr`: The address to analyze.
    /// - `chain_id`: Optional blockchain chain ID for filtering.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.address_risk("0xEa51801b8F5B88543DdaD3D1727400c15b209D8f", Some("56")).await;
    /// let risk_data: AccountRisk = response.result;
    /// ```
    /// Response fields in depth [here](https://docs.gopluslabs.io/reference/response-details-1)
    pub async fn address_risk(&self, addr: &str, chain_id: Option<&str>) -> Result<AccountRiskResponse, GpError> {
        let url = format!("{}/address_security/{}", BASE_URL, addr);

        Ok(self.inner.get(url)
            .header("access_token", self.access_token.clone().unwrap_or("None".to_string()))
            .query(&[("chain_id", chain_id.unwrap_or("None"))])
            .send()
            .await?
            .error_for_status()?
            .json::<AccountRiskResponse>()
            .await?)
    }

    pub async fn approval_security_v1(&self, chain_id: &str, contract_addr: &str) -> Result<V1ApprovalResponse, GpError> {
        let url = format!("{}/approval_security/{}", BASE_URL, chain_id);
        Ok(self.inner.get(url)
            .header("access_token", self.access_token.clone().unwrap_or("None".to_string()))
            .query(&[("contract_addresses", contract_addr)])
            .send()
            .await?
            .error_for_status()?
            .json::<V1ApprovalResponse>()
            .await?)
    }

    
    pub async fn approval_security_v2(&self, erc: V2ApprovalERC, chain_id: &str, address: &str) -> Result<V2ApprovalResponse, GpError> {
        let base_url = "https://api.gopluslabs.io/api/v2";
        let url = match erc {
            V2ApprovalERC::ERC20 => format!("{}/token_approval_security/{}", base_url, chain_id),
            V2ApprovalERC::ERC721 => format!("{}/nft721_approval_security/{}", base_url, chain_id),
            V2ApprovalERC::ERC1155 => format!("{}/nft1155_approval_security/{}", base_url, chain_id),
        };
        
        Ok(self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .query(&[("addresses", address)])
            .send()
            .await?
            .error_for_status()?
            .json::<V2ApprovalResponse>()
            .await?)

        
    }

    /// Decodes ABI input data for interacting with smart contracts.
    ///
    /// # Parameters
    /// - `chain_id`: Blockchain chain ID.
    /// - `data`: ABI data to decode.
    /// - `contract_addr`: Optional contract address.
    /// - `signer`: Optional signer.
    /// - `txn_type`: Optional transaction type.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.abi_decode(
    ///     "56", 
    ///     "0xa9059cbb00000000000000000000000055d398326f99059ff775485246999027b319795500000000000000000000000000000000000000000000000acc749097d9d00000", 
    ///     Some("0x55d398326f99059ff775485246999027b3197955"),
    ///     // None,
    ///     None, 
    ///     None
    /// ).await?;
    /// ```
    /// Parameters and response fields in depth [here](https://docs.gopluslabs.io/reference/response-details-4)
    pub async fn abi_decode(&self, 
        chain_id: &str, 
        data: &str,
        contract_addr: Option<&str>,
        signer: Option<&str>,
        txn_type: Option<&str>
    ) -> Result<AbiDecodeResponse, anyhow::Error> {
        
        let url = format!("{}/abi/input_decode", BASE_URL);

        let params = json!({
            "chain_id": chain_id,
            "data": data,
            "contract_address": contract_addr,
            "signer": signer,
            "transaction_type": txn_type
        });

        Ok(self.inner.post(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .json(&params)
            .send()
            .await?
            .json::<AbiDecodeResponse>()
            .await?)
    }

    /// Evaluates NFT risk for a specific contract address on a blockchain.
    ///
    /// # Parameters
    /// - `chain_id`: Blockchain chain ID.
    /// - `contract_addr`: Contract address.
    /// - `token_id`: Optional token ID.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.nft_risk("1", "0x...", Some("123")).await?;
    /// let nft_risk: NftRisk = response.result;
    /// ```
    /// 
    /// Response fields explained in depth [here](https://docs.gopluslabs.io/reference/response-details-5)
    pub async fn nft_risk(&self, chain_id: &str, contract_addr: &str, token_id: Option<&str>) -> Result<NftRiskResponse, GpError> {
        let url = format!("{}/nft_security/{}",BASE_URL, chain_id);

        Ok(self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .query(&[("contract_addresses", contract_addr), ("token_id", token_id.unwrap_or("None"))])
            .send()
            .await?
            .json::<NftRiskResponse>()
            .await?)
    }

    // TODO: No successfully found url
    pub async fn dapp_risk_by_url(&self, dapp_url: &str) -> Result<Value, anyhow::Error> {
        tracing::warn!("The only response I've been able to get is 'DAPP NOT FOUND'");
        let url = format!("{}/dapp_security", BASE_URL);
        
        Ok(self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .query(&[("url", dapp_url)])
            .send()
            .await?
            .error_for_status()?
            .json::<Value>()
            .await?)
    }

    /// Analyzes phishing risks for a given site URL.
    ///
    /// # Parameters
    /// - `site_url`: URL of the site to check.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.phishing_site_risk("go-ethdenver.com").await?;
    /// ```
    /// Response fields in depth [here](https://docs.gopluslabs.io/reference/phishingsiteusingget)
    pub async fn phishing_site_risk(&self, site_url: &str) -> Result<PhishingSiteResponse, GpError> {
        let url = format!("{}/phishing_site", BASE_URL);

        Ok(self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .query(&[("url", site_url)])
            .send()
            .await?
            .error_for_status()?
            .json::<PhishingSiteResponse>()
            .await?)
    }
    
    /// Assesses the risk of a rug pull for a contract on a specific blockchain.
    ///
    /// # Parameters
    /// - `chain_id`: Blockchain chain ID.
    /// - `contract_addr`: Contract address.
    ///
    /// # Example Usage
    /// ```ignore
    /// let session = Session::new();
    /// let response = session.rug_pull_risk("1", "0x6B175474E89094C44Da98b954EedeAC495271d0F").await?;
    /// ```
    /// Response fields in depth [here](https://docs.gopluslabs.io/reference/response-details-7)
    pub async fn rug_pull_risk(&self, chain_id: &str, contract_addr: &str) -> Result<RugPullRiskResponse, GpError> {
        let url = format!("{}/rugpull_detecting/{}", BASE_URL, chain_id);

        Ok(self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .query(&[("contract_addresses", contract_addr)])
            .send()
            .await?
            .error_for_status()?
            .json::<RugPullRiskResponse>()
            .await?)
    }

    #[deprecated = "Token retrieved on initialization when keys are env variables. 
    Can be used if you compute signature (method in new()/docs)."]
    /// Obtains an access token using SHA-1 signature method.
    ///
    /// # Sign Method
    /// Concatenate `app_key`, `time`, and `app_secret` in turn, and apply SHA-1 hashing.
    /// 
    /// `time` should be +- 1000s around the current timestamp
    /// 
    /// # Example
    /// ```ignore
    /// let app_key = "mBOMg20QW11BbtyH4Zh0";
    /// let time = 1647847498;
    /// let app_secret = "V6aRfxlPJwN3ViJSIFSCdxPvneajuJsh";
    /// let sign = "sha1(mBOMg20QW11BbtyH4Zh01647847498V6aRfxlPJwN3ViJSIFSCdxPvneajuJsh)"; // This results in `7293d385b9225b3c3f232b76ba97255d0e21063e`
    /// ```
    ///
    /// # Parameters
    /// - `app_key`: Application key provided by the service.
    /// - `signature`: Computed SHA-1 hash as a string.
    /// - `time`: Current time as a UNIX timestamp.
    ///
    /// # Example Usage
    /// ```ignore
    /// let mut instance = Session::new(None);
    /// instance.get_access_token("mBOMg20QW11BbtyH4Zh0", "7293d385b9225b3c3f232b76ba97255d0e21063e", 1647847498).await?;
    /// ```
    pub async fn get_access_token(&mut self, app_key: &str, signature: &str, time: u64) -> Result<(), GpError> {
        let url = format!("{}/token", BASE_URL);

        let params = json!({
            "app_key": app_key,
            "sign": signature,
            "time": time,
        });

        let access_code_res = self.inner.get(url)
            .header("access_token", self.access_token.as_ref().unwrap_or(&"None".to_string()))
            .json(&params)
            .send()
            .await?
            .error_for_status()?
            .json::<AccessCodeResponse>()
            .await?;

        if access_code_res.code == 1 {
            tracing::trace!("New access token expires in {} minutes", (access_code_res.result.as_ref().unwrap().expires_in)/60);
            self.access_token = Some(access_code_res.result.unwrap().access_token);
            Ok(())
            
        } else {
            tracing::error!("Error getting access token\nCode: {}", access_code_res.code);
            Err(GpError::RequestError(access_code_res.code, access_code_res.message))
        }
        
        

    }
    
}




pub fn interpret_gp_status_code(code: u32) -> &'static str {
    match code {
        1 => "Complete data prepared",
        2 => "Partial data obtained. The complete data can be requested again in about 15 seconds.",
        2004 => "Contract address format error!",
        2018 => "ChainID not supported",
        2020 => "Non-contract address",
        2021 => "No info for this contract",
        2022 => "Non-supported chainId",
        2026 => "dApp not found",
        2027 => "ABI not found",
        2028 => "The ABI not support parsing",
        4010 => "App_key not exist",
        4011 => "Signature expiration (the same request parameters cannot be requested more than once)",
        4012 => "Wrong Signature",
        4023 => "Access token not found",
        4029 => "Request limit reached",
        5000 => "System error",
        5006 => "Param error!",
        _ => "Unknown status code",
    }
}