bolt-cw-sdk 1.0.0

SDK for the BOLT protocol, providing utilities for interacting with the BOLT contracts.
Documentation
use cosmrs::proto::cosmos::tx::v1beta1::{SimulateRequest, SimulateResponse};
use cosmrs::tendermint::abci::Code;
use prost::Message;
use tendermint_rpc::Client;
use tracing::{error, instrument};

use super::error::CosmosClientError;
use super::{CosmosClient, QueryResponse};

impl CosmosClient {
    #[instrument(name = "cosmos_client.simulate", skip_all, fields(tx.gas_wanted, tx.gas_used))]
    pub async fn simulate(&self, tx_bytes: Vec<u8>) -> Result<SimulateResponse, CosmosClientError> {
        let res: QueryResponse<SimulateResponse> = self
            .client
            .abci_query(
                Some("/cosmos.tx.v1beta1.Service/Simulate".to_string()),
                // We NEED to use the 'tx' tag to define the structure
                #[allow(deprecated)]
                SimulateRequest { tx: None, tx_bytes }.encode_to_vec(),
                None,
                false,
            )
            .await?
            .into();

        if let Some(gas) = &res.value.gas_info {
            tracing::Span::current().record("tx.gas_wanted", gas.gas_wanted);
            tracing::Span::current().record("tx.gas_used", gas.gas_used);
        }

        match res.code {
            Code::Ok => Ok(res.value),
            Code::Err(err_code) => {
                error!("Gas simulation error: {:?}", res.log);
                Err(CosmosClientError::GasSimulationError {
                    code: err_code,
                    log: res.log,
                })
            }
        }
    }
}