use super::error::CosmosClientError;
use crate::cosmos_client::CosmosClient;
use cosmrs::proto::cosmos::tx::v1beta1::SimulateResponse;
use cosmrs::tx::Fee;
use cosmrs::Coin;
#[derive(Debug, Clone)]
pub struct ClientGasInfo {
pub gas_price: u128,
pub gas_adjustment: f64,
pub denom: String,
}
impl ClientGasInfo {
pub fn new(gas_price: u128, denom: String) -> Self {
Self {
gas_price,
denom,
gas_adjustment: 1.0,
}
}
pub fn with_adjustment(mut self, adjustment: f64) -> Self {
self.gas_adjustment = adjustment;
self
}
pub fn fee(&self, simulation: SimulateResponse) -> Result<Fee, CosmosClientError> {
let gas_limit = ((simulation.gas_info.unwrap_or_default().gas_used as f64)
* self.gas_adjustment) as u64;
let fees = self.gas_price * gas_limit as u128;
let amount = Coin::new(fees, &self.denom)?;
Ok(Fee::from_amount_and_gas(amount, gas_limit))
}
}
impl CosmosClient {
pub fn with_gas_info(mut self, gas_info: ClientGasInfo) -> Self {
self.gas_info = Some(gas_info);
self
}
pub fn gas_info(&self) -> Result<&ClientGasInfo, CosmosClientError> {
self.gas_info.as_ref().ok_or(CosmosClientError::NoGasInfo)
}
}