bolt-cw-sdk 1.0.0

SDK for the BOLT protocol, providing utilities for interacting with the BOLT contracts.
Documentation
use cosmrs::tendermint::abci::Code;
use cosmrs::tendermint::Hash;
use tendermint_rpc::endpoint::broadcast::tx_commit;
use tendermint_rpc::endpoint::tx;
use tendermint_rpc::Client;
use tracing::instrument;

use super::error::CosmosClientError;
use super::CosmosClient;

impl CosmosClient {
    #[instrument(name = "cosmos_client.broadcast_tx", skip_all, fields(tx.height, tx.hash, tx.response_code, tx.gas_used, tx.gas_wanted))]
    pub async fn broadcast_tx(
        &self,
        tx_bytes: Vec<u8>,
    ) -> Result<tx_commit::Response, CosmosClientError> {
        let response = self
            .client
            .broadcast_tx_commit(tx_bytes)
            .await
            .map_err(CosmosClientError::Tendermint)?;
        if response.check_tx.code != Code::Ok {
            return match response.check_tx.code.value() {
                11 => Err(CosmosClientError::InsufficientGas(
                    response.check_tx.gas_wanted,
                    response.check_tx.gas_used,
                )),
                13 => Err(CosmosClientError::InsufficientFee(response.check_tx.log)),
                _ => Err(CosmosClientError::BroadcastTx(response.check_tx.log)),
            };
        };
        if response.tx_result.code != Code::Ok {
            return match response.tx_result.code.value() {
                11 => Err(CosmosClientError::InsufficientGas(
                    response.tx_result.gas_wanted,
                    response.tx_result.gas_used,
                )),
                13 => Err(CosmosClientError::InsufficientFee(response.tx_result.log)),
                _ => Err(CosmosClientError::BroadcastTx(response.tx_result.log)),
            };
        }

        tracing::Span::current().record("tx.height", response.height.value());
        tracing::Span::current().record("tx.hash", response.hash.to_string());
        tracing::Span::current().record("tx.gas_used", response.tx_result.gas_used);
        tracing::Span::current().record("tx.gas_wanted", response.tx_result.gas_wanted);
        tracing::Span::current().record("tx.response_code", response.check_tx.code.value());

        Ok(response)
    }

    pub async fn get_tx(&self, hash: Hash) -> Result<tx::Response, CosmosClientError> {
        self.client
            .tx(hash, false)
            .await
            .map_err(CosmosClientError::Tendermint)
    }
}