synnax 0.2.0

Cosmos-SDK multichain client
Documentation
use crate::cosmos::types::{Coin, Pagination};
use crate::lcd::Lcd;
use serde::{Deserialize, Serialize};
use serde_aux::prelude::*;

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct TxBody {
    pub messages: serde_json::Value,
    pub memo: String,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub timeout_height: u64,
    pub extension_options: serde_json::Value,
    pub non_critical_extension_options: serde_json::Value,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Fee {
    pub amount: Vec<Coin>,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub gas_limit: u64,
    pub payer: String,
    pub granter: String,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct AuthInfo {
    pub signer_infos: serde_json::Value,
    pub fee: Fee,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Txs {
    pub body: TxBody,
    pub auth_info: AuthInfo,
    pub signatures: serde_json::Value,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Attributes {
    pub key: String,
    pub value: String,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct StringEvent {
    #[serde(rename(deserialize = "type"))]
    pub event_type: String,
    pub attributes: Vec<Attributes>,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Logs {
    pub msg_index: u32,
    pub log: String,
    pub events: Vec<StringEvent>,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct EventAttribute {
    pub key: String,
    pub value: String,
    pub index: bool,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Events {
    #[serde(rename(deserialize = "type"))]
    pub event_type: String,
    pub attributes: Vec<EventAttribute>,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct TxResponseResponse {
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub height: u64,
    pub txhash: String,
    pub codespace: String,
    pub code: u32,
    pub data: String,
    pub raw_log: String,
    pub logs: Vec<Logs>,
    pub info: String,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub gas_wanted: u64,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub gas_used: u64,
    pub tx: serde_json::Value,
    pub timestamp: String,
    pub events: Vec<Events>,
}

#[derive(Serialize, Clone, Debug, Eq, PartialEq)]
pub struct SimulateRequest {
    pub tx_bytes: Vec<u8>,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct GasInfo {
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub gas_wanted: u64,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub gas_used: u64,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct AbciResult {
    pub data: String,
    pub log: String,
    pub events: Vec<Events>,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct SimulateResponse {
    pub gas_info: GasInfo,
    pub result: AbciResult,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct TxResponse {
    pub tx: Txs,
    pub tx_response: TxResponseResponse,
}

#[derive(Serialize, Clone, Debug, Eq, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum BroadcastMode {
    BroadcastModeUnspecified = 0,
    BroadcastModeBlock = 1,
    BroadcastModeSync = 2,
    BroadcastModeAsync = 3,
}

#[derive(Serialize, Clone, Debug, Eq, PartialEq)]
pub struct BroadcastTxRequest {
    pub tx_bytes: Vec<u8>,
    pub mode: BroadcastMode,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct BroadcastTxResponse {
    pub tx_response: TxResponseResponse,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct PartSetHeader {
    pub total: u32,
    pub hash: String,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct BlockID {
    pub hash: String,
    pub part_set_header: PartSetHeader,
}

#[derive(Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct GetBlockWithTxsResponse {
    pub txs: Vec<Txs>,

    pub block_id: BlockID,

    pub pagination: Pagination,
}

pub struct Tx<'a> {
    lcd: &'a Lcd,
}

impl<'a> Tx<'a> {
    pub fn new(lcd: &'a Lcd) -> Self {
        Tx { lcd }
    }

    pub fn tx(&self, hash: String) -> Result<TxResponse, anyhow::Error> {
        self.lcd.get(format!("/cosmos/tx/v1beta1/txs/{}", hash))
    }

    pub fn simulate(&self, tx: SimulateRequest) -> Result<SimulateResponse, anyhow::Error> {
        self.lcd
            .post("/cosmos/tx/v1beta1/simulate".to_string(), &tx)
    }

    pub fn broadcast_tx(
        &self,
        tx: BroadcastTxRequest,
    ) -> Result<BroadcastTxResponse, anyhow::Error> {
        self.lcd.post("/cosmos/tx/v1beta1/txs".to_string(), &tx)
    }

    pub fn get_block_with_txs(
        &self,
        height: u64,
    ) -> Result<GetBlockWithTxsResponse, anyhow::Error> {
        self.lcd
            .get(format!("/cosmos/tx/v1beta1/txs/block/{}", height))
    }
}