Skip to main content

hanzo_client/apis/
web3_api.rs

1/*
2 * Hanzo Cloud API
3 *
4 * The Hanzo Cloud API as a customer calls it: every operation under /v1/ except the operator's admin product, relay routes, legacy spellings and capabilities still reached by flag. Tagged by product: the first path segment after /v1/.
5 *
6 * The version of the OpenAPI document: v1
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`get_web3_chains`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetWeb3ChainsError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_web3_chains_by_chain`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetWeb3ChainsByChainError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`get_web3_tokens_by_chain_by_address`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum GetWeb3TokensByChainByAddressError {
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`post_web3_rpc_by_chain`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum PostWeb3RpcByChainError {
43    UnknownValue(serde_json::Value),
44}
45
46
47/// Reports the chains this deployment can reach. The list is the declared registry, so it is exactly what /v1/web3/rpc will accept — a chain that appears here is one this deployment actually has an upstream for.
48pub async fn get_web3_chains(configuration: &configuration::Configuration, ) -> Result<models::ChainList, Error<GetWeb3ChainsError>> {
49
50    let uri_str = format!("{}/v1/web3/chains", configuration.base_path);
51    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
52
53    if let Some(ref user_agent) = configuration.user_agent {
54        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
55    }
56    if let Some(ref token) = configuration.bearer_access_token {
57        req_builder = req_builder.bearer_auth(token.to_owned());
58    };
59
60    let req = req_builder.build()?;
61    let resp = configuration.client.execute(req).await?;
62
63    let status = resp.status();
64    let content_type = resp
65        .headers()
66        .get("content-type")
67        .and_then(|v| v.to_str().ok())
68        .unwrap_or("application/octet-stream");
69    let content_type = super::ContentType::from(content_type);
70
71    if !status.is_client_error() && !status.is_server_error() {
72        let content = resp.text().await?;
73        match content_type {
74            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
75            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ChainList`"))),
76            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ChainList`")))),
77        }
78    } else {
79        let content = resp.text().await?;
80        let entity: Option<GetWeb3ChainsError> = serde_json::from_str(&content).ok();
81        Err(Error::ResponseError(ResponseContent { status, content, entity }))
82    }
83}
84
85/// Reports one chain and whether its upstream is answering. An unreachable chain is still a 200 with live:false — the chain is configured, which is a different fact from the chain being up, and a 502 here would make a console page error rather than show the outage.
86pub async fn get_web3_chains_by_chain(configuration: &configuration::Configuration, chain: &str) -> Result<models::ChainStatus, Error<GetWeb3ChainsByChainError>> {
87    // add a prefix to parameters to efficiently prevent name collisions
88    let p_chain = chain;
89
90    let uri_str = format!("{}/v1/web3/chains/{chain}", configuration.base_path, chain=crate::apis::urlencode(p_chain));
91    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
92
93    if let Some(ref user_agent) = configuration.user_agent {
94        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
95    }
96    if let Some(ref token) = configuration.bearer_access_token {
97        req_builder = req_builder.bearer_auth(token.to_owned());
98    };
99
100    let req = req_builder.build()?;
101    let resp = configuration.client.execute(req).await?;
102
103    let status = resp.status();
104    let content_type = resp
105        .headers()
106        .get("content-type")
107        .and_then(|v| v.to_str().ok())
108        .unwrap_or("application/octet-stream");
109    let content_type = super::ContentType::from(content_type);
110
111    if !status.is_client_error() && !status.is_server_error() {
112        let content = resp.text().await?;
113        match content_type {
114            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
115            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ChainStatus`"))),
116            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ChainStatus`")))),
117        }
118    } else {
119        let content = resp.text().await?;
120        let entity: Option<GetWeb3ChainsByChainError> = serde_json::from_str(&content).ok();
121        Err(Error::ResponseError(ResponseContent { status, content, entity }))
122    }
123}
124
125/// Reads an address's native balance on a chain.  ERC-20 positions are NOT enumerated here: eth_getBalance answers the native one, but \"every token this address holds\" is an indexer question — there is no RPC call that answers it, and walking a token list would return a number that silently omits whatever the list missed. explorer owns the indexer relationship; this returns the balance the chain itself can prove.
126pub async fn get_web3_tokens_by_chain_by_address(configuration: &configuration::Configuration, chain: &str, address: &str) -> Result<models::Balances, Error<GetWeb3TokensByChainByAddressError>> {
127    // add a prefix to parameters to efficiently prevent name collisions
128    let p_chain = chain;
129    let p_address = address;
130
131    let uri_str = format!("{}/v1/web3/tokens/{chain}/{address}", configuration.base_path, chain=crate::apis::urlencode(p_chain), address=crate::apis::urlencode(p_address));
132    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
133
134    if let Some(ref user_agent) = configuration.user_agent {
135        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
136    }
137    if let Some(ref token) = configuration.bearer_access_token {
138        req_builder = req_builder.bearer_auth(token.to_owned());
139    };
140
141    let req = req_builder.build()?;
142    let resp = configuration.client.execute(req).await?;
143
144    let status = resp.status();
145    let content_type = resp
146        .headers()
147        .get("content-type")
148        .and_then(|v| v.to_str().ok())
149        .unwrap_or("application/octet-stream");
150    let content_type = super::ContentType::from(content_type);
151
152    if !status.is_client_error() && !status.is_server_error() {
153        let content = resp.text().await?;
154        match content_type {
155            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
156            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Balances`"))),
157            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Balances`")))),
158        }
159    } else {
160        let content = resp.text().await?;
161        let entity: Option<GetWeb3TokensByChainByAddressError> = serde_json::from_str(&content).ok();
162        Err(Error::ResponseError(ResponseContent { status, content, entity }))
163    }
164}
165
166/// Forwards a JSON-RPC call to the named chain and returns its answer unchanged. Only declared chains are reachable, and only to a caller with a validated principal — this is the deployment's upstream, not an open relay.
167pub async fn post_web3_rpc_by_chain(configuration: &configuration::Configuration, chain: &str, rpc_in: models::RpcIn) -> Result<models::RpcOut, Error<PostWeb3RpcByChainError>> {
168    // add a prefix to parameters to efficiently prevent name collisions
169    let p_chain = chain;
170    let p_rpc_in = rpc_in;
171
172    let uri_str = format!("{}/v1/web3/rpc/{chain}", configuration.base_path, chain=crate::apis::urlencode(p_chain));
173    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
174
175    if let Some(ref user_agent) = configuration.user_agent {
176        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
177    }
178    if let Some(ref token) = configuration.bearer_access_token {
179        req_builder = req_builder.bearer_auth(token.to_owned());
180    };
181    req_builder = req_builder.json(&p_rpc_in);
182
183    let req = req_builder.build()?;
184    let resp = configuration.client.execute(req).await?;
185
186    let status = resp.status();
187    let content_type = resp
188        .headers()
189        .get("content-type")
190        .and_then(|v| v.to_str().ok())
191        .unwrap_or("application/octet-stream");
192    let content_type = super::ContentType::from(content_type);
193
194    if !status.is_client_error() && !status.is_server_error() {
195        let content = resp.text().await?;
196        match content_type {
197            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
198            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RpcOut`"))),
199            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RpcOut`")))),
200        }
201    } else {
202        let content = resp.text().await?;
203        let entity: Option<PostWeb3RpcByChainError> = serde_json::from_str(&content).ok();
204        Err(Error::ResponseError(ResponseContent { status, content, entity }))
205    }
206}
207