use std::sync::Arc;
use prost::Message;
use tendermint_proto::google::protobuf::Any;
use uniffi::{Object, Record};
use celestia_types::blob::BlobParams;
use celestia_types::block::Block;
use celestia_types::hash::uniffi_types::UniffiHash;
use celestia_types::state::auth::{Account, AuthParams};
use celestia_types::state::{AbciQueryResponse, Coin, TxResponse};
use celestia_types::{AppVersion, Blob};
use celestia_types::{ExtendedHeader, UniffiConversionError};
use crate::grpc::{
BroadcastMode, ConfigResponse, GasEstimate, GasInfo, GetTxResponse, TxPriority,
TxStatusResponse,
};
use crate::tx::TxInfo;
use crate::{IntoProtobufAny, SignDoc, TxConfig};
pub type Result<T, E = GrpcClientError> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum GrpcClientError {
#[error("grpc error: {msg}")]
GrpcError {
msg: String,
},
#[error("uniffi conversion error: {msg}")]
UniffiConversionError {
msg: String,
},
#[error("invalid account public key")]
InvalidAccountPublicKey {
msg: String,
},
#[error("invalid account id: {msg}")]
InvalidAccountId {
msg: String,
},
#[error("error while signing: {msg}")]
SigningError {
msg: String,
},
}
#[derive(Object)]
pub struct GrpcClient {
client: crate::GrpcClient,
}
#[derive(Record)]
pub struct AnyMsg {
pub r#type: String,
pub value: Vec<u8>,
}
#[uniffi::export(async_runtime = "tokio")]
impl GrpcClient {
#[uniffi::method(name = "getAuthParams")]
pub async fn get_auth_params(&self) -> Result<AuthParams> {
Ok(self.client.get_auth_params().await?)
}
#[uniffi::method(name = "getAccount")]
pub async fn get_account(&self, account: &str) -> Result<Account> {
Ok(self.client.get_account(&account.parse()?).await?)
}
#[uniffi::method(name = "getAccounts")]
pub async fn get_accounts(&self) -> Result<Vec<Account>> {
Ok(self.client.get_accounts().await?)
}
#[uniffi::method(name = "getVerifiedBalance")]
pub async fn get_verified_balance(&self, address: &str, header: &str) -> Result<Coin> {
let header: ExtendedHeader = serde_json::from_str(header)
.map_err(|e| GrpcClientError::UniffiConversionError { msg: e.to_string() })?;
Ok(self
.client
.get_verified_balance(&address.parse()?, &header)
.await?)
}
#[uniffi::method(name = "getBalance")]
pub async fn get_balance(&self, address: &str, denom: String) -> Result<Coin> {
Ok(self.client.get_balance(&address.parse()?, denom).await?)
}
#[uniffi::method(name = "getAllBalances")]
pub async fn get_all_balances(&self, address: &str) -> Result<Vec<Coin>> {
Ok(self.client.get_all_balances(&address.parse()?).await?)
}
#[uniffi::method(name = "getSpendableBalances")]
pub async fn get_spendable_balances(&self, address: &str) -> Result<Vec<Coin>> {
Ok(self
.client
.get_spendable_balances(&address.parse()?)
.await?)
}
#[uniffi::method(name = "getTotalSupply")]
pub async fn get_total_supply(&self) -> Result<Vec<Coin>> {
Ok(self.client.get_total_supply().await?)
}
#[uniffi::method(name = "getNodeConfig")]
pub async fn get_node_config(&self) -> Result<ConfigResponse> {
Ok(self.client.get_node_config().await?)
}
#[uniffi::method(name = "getLatestBlock")]
pub async fn get_latest_block(&self) -> Result<Block> {
Ok(self.client.get_latest_block().await?)
}
#[uniffi::method(name = "getBlockByHeight")]
pub async fn get_block_by_height(&self, height: i64) -> Result<Block> {
Ok(self.client.get_block_by_height(height).await?)
}
#[uniffi::method(name = "abciQuery")]
async fn abci_query(
&self,
data: &[u8],
path: &str,
height: u64,
prove: bool,
) -> Result<AbciQueryResponse> {
Ok(self.client.abci_query(data, path, height, prove).await?)
}
#[uniffi::method(name = "broadcastTx")]
pub async fn broadcast_tx(&self, tx_bytes: Vec<u8>, mode: BroadcastMode) -> Result<TxResponse> {
Ok(self.client.broadcast_tx(tx_bytes, mode).await?)
}
#[uniffi::method(name = "getTx")]
pub async fn get_tx(&self, hash: UniffiHash) -> Result<GetTxResponse> {
Ok(self.client.get_tx(hash.try_into()?).await?)
}
pub async fn simulate(&self, tx_bytes: Vec<u8>) -> Result<GasInfo> {
Ok(self.client.simulate(tx_bytes).await?)
}
#[uniffi::method(name = "getBlobParams")]
pub async fn get_blob_params(&self) -> Result<BlobParams> {
Ok(self.client.get_blob_params().await?)
}
#[uniffi::method(name = "txStatus")]
pub async fn tx_status(&self, hash: UniffiHash) -> Result<TxStatusResponse> {
Ok(self.client.tx_status(hash.try_into()?).await?)
}
#[uniffi::method(name = "estimateGasPrice")]
pub async fn estimate_gas_price(&self, priority: TxPriority) -> Result<f64> {
Ok(self.client.estimate_gas_price(priority).await?)
}
#[uniffi::method(name = "estimateGasPriceAndUsage")]
pub async fn estimate_gas_price_and_usage(
&self,
priority: TxPriority,
tx_bytes: Vec<u8>,
) -> Result<GasEstimate> {
Ok(self
.client
.estimate_gas_price_and_usage(priority, tx_bytes)
.await?)
}
#[uniffi::method(name = "appVersion")]
pub async fn app_version(&self) -> Result<AppVersion> {
Ok(self.client.app_version().await?)
}
#[uniffi::method(name = "submitBlobs")]
pub async fn submit_blobs(
&self,
blobs: Vec<Arc<Blob>>,
config: Option<TxConfig>,
) -> Result<TxInfo> {
let blobs = Vec::from_iter(blobs.into_iter().map(Arc::<Blob>::unwrap_or_clone));
let config = config.unwrap_or_default();
Ok(self.client.submit_blobs(&blobs, config).await?)
}
#[uniffi::method(name = "submitMessage")]
pub async fn submit_message(
&self,
message: AnyMsg,
config: Option<TxConfig>,
) -> Result<TxInfo> {
let config = config.unwrap_or_default();
Ok(self.client.submit_message(message, config).await?)
}
}
impl IntoProtobufAny for AnyMsg {
fn into_any(self) -> Any {
Any {
type_url: self.r#type,
value: self.value,
}
}
}
impl From<crate::GrpcClient> for GrpcClient {
fn from(client: crate::GrpcClient) -> GrpcClient {
GrpcClient { client }
}
}
impl From<crate::Error> for GrpcClientError {
fn from(value: crate::Error) -> Self {
GrpcClientError::GrpcError {
msg: value.to_string(),
}
}
}
impl From<UniffiConversionError> for GrpcClientError {
fn from(value: UniffiConversionError) -> Self {
GrpcClientError::GrpcError {
msg: value.to_string(),
}
}
}
#[uniffi::export]
pub fn proto_encode_sign_doc(sign_doc: SignDoc) -> Vec<u8> {
sign_doc.encode_to_vec()
}
impl From<celestia_types::Error> for GrpcClientError {
fn from(value: celestia_types::Error) -> Self {
crate::Error::from(value).into()
}
}