solana_tx_decoding/utilities/
fetch_token_metadata_from_uri.rs

1use reqwest::Client;
2use serde_json::Value;
3
4/// Fetch additional token metadata from off-chain URI. Token metadata URIs point to JSON APIs that
5/// return additional metadata. This function extracts description, twitter, and website fields
6/// from the JSON response, returning empty strings if fields are missing or not strings.
7pub async fn fetch_token_metadata_from_uri(
8  client: &Client,
9  uri: &str,
10) -> (String, String, String) {
11  // Make the async GET request and panic if it fails
12  let response = client
13    .get(uri)
14    .send()
15    .await
16    .expect("Failed to make GET request");
17
18  // Parse the response as JSON and panic if it fails
19  let data: Value = response.json().await.expect("Failed to parse JSON response");
20
21  // Extract fields, using empty string if field doesn't exist or isn't a string
22  let description = data
23    .get("description")
24    .and_then(|v| v.as_str())
25    .unwrap_or("")
26    .to_string();
27
28  let twitter = data
29    .get("twitter")
30    .and_then(|v| v.as_str())
31    .unwrap_or("")
32    .to_string();
33
34  let website = data
35    .get("website")
36    .and_then(|v| v.as_str())
37    .unwrap_or("")
38    .to_string();
39
40  (description, twitter, website)
41}