Skip to content

Commit

Permalink
Merge pull request #31 from gevulotnetwork/fix/default-gas-price
Browse files Browse the repository at this point in the history
Fix default gas price and rounding issues
  • Loading branch information
trusch authored Dec 12, 2024
2 parents 7c0ab55 + 148af71 commit 2a800a9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/base_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct BaseClient {
// Message client
pub tx_client: TxServiceClient<Channel>,

gas_price: u128,
gas_price: f64,
denom: String,
gas_multiplier: f64,

Expand All @@ -56,7 +56,7 @@ impl BaseClient {
/// # Returns
///
/// A Result containing the new instance of BaseClient or an error.
pub async fn new(endpoint: &str, gas_price: u128, gas_multiplier: f64) -> Result<Self> {
pub async fn new(endpoint: &str, gas_price: f64, gas_multiplier: f64) -> Result<Self> {
use rand::Rng;
use tokio::time::{sleep, Duration};

Expand Down Expand Up @@ -256,10 +256,11 @@ impl BaseClient {
.map_err(|_| Error::Parse("fail".to_string()))?;
let tx_body = cosmrs::tx::BodyBuilder::new().msg(msg).memo(memo).finish();
let signer_info = cosmrs::tx::SignerInfo::single_direct(self.pub_key, sequence);
let gas_per_ucredit = (1.0 / self.gas_price).floor() as u128;
let fee = cosmrs::tx::Fee::from_amount_and_gas(
Coin {
denom: self.denom.parse()?,
amount: self.gas_price,
amount: (gas as u128) / gas_per_ucredit + 1,
},
gas,
);
Expand Down Expand Up @@ -299,11 +300,12 @@ impl BaseClient {
.await?;
log::debug!("simulate_response: {:#?}", simulate_response);
let gas_info = simulate_response.gas_info.ok_or("Failed to get gas info")?;
let gas_limit = (gas_info.gas_used * (100. * self.gas_multiplier) as u64) / 100; // Adjust gas limit based on simulation
let gas_limit = (gas_info.gas_used * ((self.gas_multiplier * 10000.0) as u64)) / 10000; // Adjust gas limit based on simulation
let gas_per_ucredit = (1.0 / self.gas_price).floor() as u128;
let fee = cosmrs::tx::Fee::from_amount_and_gas(
Coin {
denom: self.denom.parse()?,
amount: self.gas_price * gas_limit as u128,
amount: (gas_limit as u128 / gas_per_ucredit) + 1,
},
gas_limit,
);
Expand Down
6 changes: 3 additions & 3 deletions src/gevulot_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GevulotClient {
/// Builder for GevulotClient
pub struct GevulotClientBuilder {
endpoint: String,
gas_price: u128,
gas_price: f64,
gas_multiplier: f64,
mnemonic: Option<String>,
password: Option<String>,
Expand All @@ -40,7 +40,7 @@ impl Default for GevulotClientBuilder {
fn default() -> Self {
Self {
endpoint: "http://127.0.0.1:9090".to_string(),
gas_price: 1000,
gas_price: 0.025,
gas_multiplier: 1.2,
mnemonic: None,
password: None,
Expand All @@ -61,7 +61,7 @@ impl GevulotClientBuilder {
}

/// Sets the gas price for the GevulotClient
pub fn gas_price(mut self, gas_price: u128) -> Self {
pub fn gas_price(mut self, gas_price: f64) -> Self {
self.gas_price = gas_price;
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ mod tests {

let mut cli = GevulotClientBuilder::new()
.endpoint("http://127.0.0.1:9090") // default endpoint
.gas_price(1000) // default gas price
.gas_price(0.025) // default gas price
.gas_multiplier(1.2) // default gas multiplier
.mnemonic(mnemonic.as_str())
.build()
Expand Down

0 comments on commit 2a800a9

Please sign in to comment.