#![allow(clippy::too_many_arguments)]
use crate::{GearApi, api::Result};
use gear_core::{
ids::{ActorId, CodeId, MessageId},
rpc::ReplyInfo,
};
use gsdk::{GasInfo, ext::subxt::utils::H256};
use parity_scale_codec::Decode;
impl GearApi {
pub async fn calculate_create_gas(
&self,
origin: Option<H256>,
code_id: CodeId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
) -> Result<GasInfo> {
self.calculate_create_gas_at(origin, code_id, payload, value, allow_other_panics, None)
.await
}
pub async fn calculate_create_gas_at(
&self,
origin: Option<H256>,
code_id: CodeId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc()
.calculate_create_gas(origin, code_id, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
}
pub async fn calculate_upload_gas(
&self,
origin: Option<H256>,
code: Vec<u8>,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
) -> Result<GasInfo> {
self.calculate_upload_gas_at(origin, code, payload, value, allow_other_panics, None)
.await
}
pub async fn calculate_upload_gas_at(
&self,
origin: Option<H256>,
code: Vec<u8>,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc()
.calculate_upload_gas(origin, code, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
}
pub async fn calculate_handle_gas(
&self,
origin: Option<H256>,
destination: ActorId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
) -> Result<GasInfo> {
self.calculate_handle_gas_at(
origin,
destination,
payload,
value,
allow_other_panics,
None,
)
.await
}
pub async fn calculate_handle_gas_at(
&self,
origin: Option<H256>,
destination: ActorId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc()
.calculate_handle_gas(origin, destination, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
}
pub async fn calculate_reply_gas(
&self,
origin: Option<H256>,
message_id: MessageId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
) -> Result<GasInfo> {
self.calculate_reply_gas_at(origin, message_id, payload, value, allow_other_panics, None)
.await
}
pub async fn calculate_reply_gas_at(
&self,
origin: Option<H256>,
message_id: MessageId,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc()
.calculate_reply_gas(origin, message_id, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
}
pub async fn read_state_bytes(&self, program_id: ActorId, payload: Vec<u8>) -> Result<Vec<u8>> {
self.read_state_bytes_at(program_id, payload, None).await
}
pub async fn read_state_bytes_at(
&self,
program_id: ActorId,
payload: Vec<u8>,
at: Option<H256>,
) -> Result<Vec<u8>> {
let response: String = self
.0
.api()
.read_state(H256(program_id.into()), payload, at)
.await?;
crate::utils::hex_to_vec(response)
}
pub async fn read_state<D: Decode>(&self, program_id: ActorId, payload: Vec<u8>) -> Result<D> {
self.read_state_at(program_id, payload, None).await
}
pub async fn read_state_at<D: Decode>(
&self,
program_id: ActorId,
payload: Vec<u8>,
at: Option<H256>,
) -> Result<D> {
let bytes = self.read_state_bytes_at(program_id, payload, at).await?;
D::decode(&mut bytes.as_ref()).map_err(Into::into)
}
pub async fn read_metahash(&self, program_id: ActorId) -> Result<H256> {
self.read_metahash_at(program_id, None).await
}
pub async fn read_metahash_at(&self, program_id: ActorId, at: Option<H256>) -> Result<H256> {
self.0
.api()
.read_meta_hash(H256(program_id.into()), at)
.await
.map_err(Into::into)
}
#[cfg(test)]
#[allow(unused)]
async fn rpc_request<T: gsdk::ext::sp_runtime::DeserializeOwned>(
&self,
method: &str,
params: gsdk::ext::subxt_rpcs::client::RpcParams,
) -> Result<T> {
Ok(self
.0
.api()
.rpc()
.request(method, params)
.await
.map_err(gsdk::Error::from)?)
}
pub async fn calculate_reply_for_handle(
&self,
origin: Option<H256>,
destination: ActorId,
payload: Vec<u8>,
gas_limit: u64,
value: u128,
) -> Result<ReplyInfo> {
self.calculate_reply_for_handle_at(origin, destination, payload, gas_limit, value, None)
.await
}
pub async fn calculate_reply_for_handle_at(
&self,
origin: Option<H256>,
destination: ActorId,
payload: Vec<u8>,
gas_limit: u64,
value: u128,
at: Option<H256>,
) -> Result<ReplyInfo> {
self.0
.rpc()
.calculate_reply_for_handle(origin, destination, payload, gas_limit, value, at)
.await
.map_err(Into::into)
}
}