use {
super::{Error, configuration},
crate::{
apis::{ContentType, ResponseContent},
models,
},
async_trait::async_trait,
reqwest,
serde::{Deserialize, Serialize, de::Error as _},
std::sync::Arc,
};
#[async_trait]
pub trait ContractInteractionsApi: Send + Sync {
async fn get_deployed_contract_abi(
&self,
params: GetDeployedContractAbiParams,
) -> Result<models::ContractAbiResponseDto, Error<GetDeployedContractAbiError>>;
async fn get_transaction_receipt(
&self,
params: GetTransactionReceiptParams,
) -> Result<models::TransactionReceiptResponse, Error<GetTransactionReceiptError>>;
async fn read_call_function(
&self,
params: ReadCallFunctionParams,
) -> Result<Vec<models::ParameterWithValue>, Error<ReadCallFunctionError>>;
async fn write_call_function(
&self,
params: WriteCallFunctionParams,
) -> Result<models::WriteCallFunctionResponseDto, Error<WriteCallFunctionError>>;
}
pub struct ContractInteractionsApiClient {
configuration: Arc<configuration::Configuration>,
}
impl ContractInteractionsApiClient {
pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
Self { configuration }
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct GetDeployedContractAbiParams {
pub contract_address: String,
pub base_asset_id: String,
pub idempotency_key: Option<String>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct GetTransactionReceiptParams {
pub base_asset_id: String,
pub tx_hash: String,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct ReadCallFunctionParams {
pub contract_address: String,
pub base_asset_id: String,
pub read_call_function_dto: models::ReadCallFunctionDto,
pub idempotency_key: Option<String>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct WriteCallFunctionParams {
pub contract_address: String,
pub base_asset_id: String,
pub write_call_function_dto: models::WriteCallFunctionDto,
pub idempotency_key: Option<String>,
}
#[async_trait]
impl ContractInteractionsApi for ContractInteractionsApiClient {
async fn get_deployed_contract_abi(
&self,
params: GetDeployedContractAbiParams,
) -> Result<models::ContractAbiResponseDto, Error<GetDeployedContractAbiError>> {
let GetDeployedContractAbiParams {
contract_address,
base_asset_id,
idempotency_key,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!(
"{}/contract_interactions/base_asset_id/{baseAssetId}/contract_address/\
{contractAddress}/functions",
local_var_configuration.base_path,
contractAddress = crate::apis::urlencode(contract_address),
baseAssetId = crate::apis::urlencode(base_asset_id)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(local_var_param_value) = idempotency_key {
local_var_req_builder =
local_var_req_builder.header("Idempotency-Key", local_var_param_value.to_string());
}
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_content).map_err(Error::from)
}
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to \
`models::ContractAbiResponseDto`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::ContractAbiResponseDto`"
))));
}
}
} else {
let local_var_entity: Option<GetDeployedContractAbiError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn get_transaction_receipt(
&self,
params: GetTransactionReceiptParams,
) -> Result<models::TransactionReceiptResponse, Error<GetTransactionReceiptError>> {
let GetTransactionReceiptParams {
base_asset_id,
tx_hash,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!(
"{}/contract_interactions/base_asset_id/{baseAssetId}/tx_hash/{txHash}/receipt",
local_var_configuration.base_path,
baseAssetId = crate::apis::urlencode(base_asset_id),
txHash = crate::apis::urlencode(tx_hash)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_content).map_err(Error::from)
}
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to \
`models::TransactionReceiptResponse`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::TransactionReceiptResponse`"
))));
}
}
} else {
let local_var_entity: Option<GetTransactionReceiptError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn read_call_function(
&self,
params: ReadCallFunctionParams,
) -> Result<Vec<models::ParameterWithValue>, Error<ReadCallFunctionError>> {
let ReadCallFunctionParams {
contract_address,
base_asset_id,
read_call_function_dto,
idempotency_key,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!(
"{}/contract_interactions/base_asset_id/{baseAssetId}/contract_address/\
{contractAddress}/functions/read",
local_var_configuration.base_path,
contractAddress = crate::apis::urlencode(contract_address),
baseAssetId = crate::apis::urlencode(base_asset_id)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(local_var_param_value) = idempotency_key {
local_var_req_builder =
local_var_req_builder.header("Idempotency-Key", local_var_param_value.to_string());
}
local_var_req_builder = local_var_req_builder.json(&read_call_function_dto);
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_content).map_err(Error::from)
}
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to \
`Vec<models::ParameterWithValue>`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `Vec<models::ParameterWithValue>`"
))));
}
}
} else {
let local_var_entity: Option<ReadCallFunctionError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn write_call_function(
&self,
params: WriteCallFunctionParams,
) -> Result<models::WriteCallFunctionResponseDto, Error<WriteCallFunctionError>> {
let WriteCallFunctionParams {
contract_address,
base_asset_id,
write_call_function_dto,
idempotency_key,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!(
"{}/contract_interactions/base_asset_id/{baseAssetId}/contract_address/\
{contractAddress}/functions/write",
local_var_configuration.base_path,
contractAddress = crate::apis::urlencode(contract_address),
baseAssetId = crate::apis::urlencode(base_asset_id)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(local_var_param_value) = idempotency_key {
local_var_req_builder =
local_var_req_builder.header("Idempotency-Key", local_var_param_value.to_string());
}
local_var_req_builder = local_var_req_builder.json(&write_call_function_dto);
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_content).map_err(Error::from)
}
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to \
`models::WriteCallFunctionResponseDto`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::WriteCallFunctionResponseDto`"
))));
}
}
} else {
let local_var_entity: Option<WriteCallFunctionError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetDeployedContractAbiError {
DefaultResponse(models::ErrorSchema),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetTransactionReceiptError {
DefaultResponse(models::ErrorSchema),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReadCallFunctionError {
DefaultResponse(models::ErrorSchema),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum WriteCallFunctionError {
DefaultResponse(models::ErrorSchema),
UnknownValue(serde_json::Value),
}