helius_rust_client/client/
tokens.rs

1use crate::models::{
2    enriched_transaction::RequestConfig,
3    nft::{
4        ActiveListingsRequestConfig, ActiveListingsResponse, MintListRequestConfig,
5        MintListResponse, NftEvent, NftMetadata, NftResponse, TokenBalancesResponse,
6    },
7    structs::TokenMetadata,
8};
9
10use super::{
11    init::{HeliusClient, API_URL_V0},
12    parse_response,
13};
14use solana_client::client_error::{Result as ClientResult};
15
16use std::collections::HashMap;
17
18use super::init::API_URL_V1;
19
20impl HeliusClient {
21    /// Returns the native balance and token balances for a given address. GET request to `https://api.helius.xyz/v0/addresses/{address}/balances`.
22    /// * `address` - The address that you want token balances for.
23    pub async fn get_token_balances(&self, address: String) -> ClientResult<TokenBalancesResponse> {
24        let request_url = format!(
25            "{}/addresses/{}/balances?api-key={}",
26            API_URL_V0, address, self.api_key
27        );
28
29        let response = self
30            .http_client
31            .get(request_url)
32            .header("accept", "application/json")
33            .header("Content-Type", "application/json")
34            .send()
35            .await;
36
37        parse_response(response).await
38    }
39
40    /// Returns the NFTs held for a given address. GET request to `https://api.helius.xyz/v0/addresses/{address}/nfts`.
41    /// * `address` - The address that you want nfts for.
42    pub async fn get_nfts(
43        &self,
44        address: String,
45        page_number: Option<usize>,
46    ) -> ClientResult<NftResponse> {
47        let mut request_url = format!(
48            "{}/addresses/{}/nfts?api-key={}",
49            API_URL_V0, address, self.api_key
50        );
51
52        if page_number.is_some() {
53            request_url = format!("{}&pageNumber={}", request_url, page_number.unwrap());
54        }
55
56        let response = self
57            .http_client
58            .get(request_url)
59            .header("accept", "application/json")
60            .header("Content-Type", "application/json")
61            .send()
62            .await;
63
64        parse_response(response).await
65    }
66
67    /// Returns NFT metadata for the given token mint addresses. POST request to `https://api.helius.xyz/v1/nfts`.
68    /// * `token_mints` - The nft mint addresses that you want metadata for.
69    pub async fn get_nfts_metadata(
70        &self,
71        token_mints: Vec<String>,
72    ) -> ClientResult<Vec<NftMetadata>> {
73        let request_url = format!("{}/nfts?api-key={}", API_URL_V1, self.api_key);
74        let mut body = HashMap::new();
75        body.insert("mints", token_mints);
76
77        let response = self
78            .http_client
79            .post(request_url)
80            .header("accept", "application/json")
81            .header("Content-Type", "application/json")
82            .json(&body)
83            .send()
84            .await;
85
86        parse_response(response).await
87    }
88
89    /// Returns all NFT related events associated with the given address. POST request to `https://api.helius.xyz/v1/nft-events`.
90    /// * `config` - The [`RequestConfig`](crate::models::enriched_transaction::RequestConfig).
91    pub async fn get_nft_events_for_address(&self, config: RequestConfig) -> ClientResult<Vec<NftEvent>> {
92        let query = config.generate_query_parameters(self.api_key.clone())?;
93        let request_url = format!(
94            "{}/addresses/{}/nft-events?",
95            API_URL_V0,
96            config.address.to_string(),
97        );
98
99        let response = self
100            .http_client
101            .get(request_url)
102            .query(&query)
103            .send()
104            .await;
105
106        parse_response(response).await
107    }
108
109    /// Returns all NFT related events associated with the given address. GET request to `https://api.helius.xyz/v1/addresses/{address}/nft-events`.
110    /// * `config` - The [`RequestConfig`](crate::models::enriched_transaction::RequestConfig).
111    pub async fn get_nft_events(&self, config: RequestConfig) -> ClientResult<Vec<NftEvent>> {
112        let query = config.generate_query_parameters(self.api_key.clone())?;
113        let request_url = format!(
114            "{}/addresses/{}/nft-events?",
115            API_URL_V0,
116            config.address.to_string(),
117        );
118
119        let response = self.http_client.get(request_url).query(&query).send().await;
120
121        parse_response(response).await
122    }
123
124    /// Query for active NFT listings. POST request to `https://api.helius.xyz/v1/active-listings`.
125    /// * `config` - The [`ActiveListingsRequestConfig`](crate::models::nft::ActiveListingsRequestConfig).
126    pub async fn get_active_nft_listings(
127        &self,
128        config: ActiveListingsRequestConfig,
129    ) -> ClientResult<ActiveListingsResponse> {
130        let body = config.generate_request_body()?;
131        let request_url = format!("{}/active-listings?api-key={}", API_URL_V1, self.api_key);
132
133        let response = self
134            .http_client
135            .post(request_url)
136            .header("accept", "application/json")
137            .header("Content-Type", "application/json")
138            .json(&body)
139            .send()
140            .await;
141
142        parse_response(response).await
143    }
144
145    /// Returns a list of mint accounts for a given NFT collection. POST request to `https://api.helius.xyz/v1/mintlist`.
146    /// * `config` - The [`MintListRequestConfig`](crate::models::nft::MintListRequestConfig).
147    pub async fn get_mint_list(
148        &self,
149        config: MintListRequestConfig,
150    ) -> ClientResult<MintListResponse> {
151        let request_url = format!("{}/mintlist?api-key={}", API_URL_V1, self.api_key);
152
153        let body = config.generate_request_body()?;
154        let response = self
155            .http_client
156            .post(request_url)
157            .header("accept", "application/json")
158            .header("Content-Type", "application/json")
159            .json(&body)
160            .send()
161            .await;
162
163        parse_response(response).await
164    }
165
166    /// Returns token metadata (whether NFT or Fungible) for the given token mint addresses. POST request to `https://api.helius.xyz/v0/tokens/metadata`.
167    /// * `token_mints` - The token mint addresses that you want metadata for.
168    pub async fn get_tokens_metadata(
169        &self,
170        token_mints: Vec<String>,
171    ) -> ClientResult<Vec<TokenMetadata>> {
172        let request_url = format!("{}/tokens/metadata?api-key={}", API_URL_V0, self.api_key);
173        let mut body = HashMap::new();
174        body.insert("mintAccounts", token_mints);
175
176        let response = self
177            .http_client
178            .post(request_url)
179            .header("accept", "application/json")
180            .header("Content-Type", "application/json")
181            .json(&body)
182            .send()
183            .await;
184
185        parse_response(response).await
186    }
187}