Skip to content

Commit

Permalink
feat: use configured cosmos gas price
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-savu committed Dec 11, 2023
1 parent 5a0944b commit 5ad57e3
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 16 deletions.
3 changes: 3 additions & 0 deletions rust/chains/hyperlane-cosmos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum HyperlaneCosmosError {
/// protobuf error
#[error("{0}")]
Protobuf(#[from] prost::DecodeError),
/// The string is not a valid number
#[error("Failed to parse number from string")]
NumStrParse,
}

impl From<HyperlaneCosmosError> for ChainCommunicationError {
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/hyperlane-cosmos/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Mailbox for CosmosMailbox {

let result = TxCostEstimate {
gas_limit: gas_limit.into(),
gas_price: U256::from(2500),
gas_price: self.provider.gas_price(),
l2_gas_limit: None,
};

Expand Down
10 changes: 9 additions & 1 deletion rust/chains/hyperlane-cosmos/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hyperlane_core::{
};
use tendermint_rpc::{client::CompatMode, HttpClient};

use crate::{ConnectionConf, HyperlaneCosmosError, Signer};
use crate::{ConnectionConf, CosmosAmount, HyperlaneCosmosError, Signer};

use self::grpc::WasmGrpcProvider;

Expand All @@ -21,6 +21,7 @@ pub struct CosmosProvider {
canonical_asset: String,
grpc_client: WasmGrpcProvider,
rpc_client: HttpClient,
gas_price: CosmosAmount,
}

impl CosmosProvider {
Expand All @@ -41,12 +42,14 @@ impl CosmosProvider {
.compat_mode(CompatMode::latest())
.build()
.map_err(Into::<HyperlaneCosmosError>::into)?;
let gas_price = CosmosAmount::try_from(conf.get_minimum_gas_price().clone())?;

Ok(Self {
domain,
rpc_client,
grpc_client,
canonical_asset: conf.get_canonical_asset(),
gas_price,
})
}

Expand All @@ -59,6 +62,11 @@ impl CosmosProvider {
pub fn rpc(&self) -> &HttpClient {
&self.rpc_client
}

/// Get the gas price
pub fn gas_price(&self) -> U256 {
self.gas_price.amount
}
}

impl HyperlaneChain for CosmosProvider {
Expand Down
46 changes: 43 additions & 3 deletions rust/chains/hyperlane-cosmos/src/trait_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use derive_new::new;
use hyperlane_core::{ChainCommunicationError, U256};

use crate::HyperlaneCosmosError;

/// Cosmos connection configuration
#[derive(Debug, Clone)]
Expand All @@ -14,15 +17,47 @@ pub struct ConnectionConf {
/// Canoncial Assets Denom
canonical_asset: String,
/// The minimum gas price set by the cosmos-sdk validator
minimum_gas_price: CosmosBalance,
minimum_gas_price: RawCosmosAmount,
}

/// Untyped cosmos amount
#[derive(serde::Serialize, serde::Deserialize, new, Clone, Debug)]
pub struct CosmosBalance {
pub struct RawCosmosAmount {
/// Coin denom (e.g. `untrn`)
pub denom: String,
/// Amount in the given denom
pub amount: String,
}

/// Typed cosmos amount
#[derive(Clone, Debug)]
pub struct CosmosAmount {
/// Coin denom (e.g. `untrn`)
pub denom: String,
/// Amount in the given denom
pub amount: U256,
}

impl TryFrom<RawCosmosAmount> for CosmosAmount {
type Error = ChainCommunicationError;
fn try_from(raw: RawCosmosAmount) -> Result<Self, ChainCommunicationError> {
// Converts to U256 by always rounding up.

// Remove the decimal part
let integer = raw
.amount
.split('.')
.next()
.ok_or(HyperlaneCosmosError::NumStrParse)?;
let amount = U256::from_dec_str(integer)?;
Ok(Self {
denom: raw.denom,
// Add one to conservatively estimate the gas cost in case there was a decimal part
amount: amount + 1,
})
}
}

/// An error type when parsing a connection configuration.
#[derive(thiserror::Error, Debug)]
pub enum ConnectionConfError {
Expand Down Expand Up @@ -69,14 +104,19 @@ impl ConnectionConf {
self.canonical_asset.clone()
}

/// Get the minimum gas price
pub fn get_minimum_gas_price(&self) -> RawCosmosAmount {
self.minimum_gas_price.clone()
}

/// Create a new connection configuration
pub fn new(
grpc_url: String,
rpc_url: String,
chain_id: String,
prefix: String,
canonical_asset: String,
minimum_gas_price: CosmosBalance,
minimum_gas_price: RawCosmosAmount,
) -> Self {
Self {
grpc_url,
Expand Down
4 changes: 4 additions & 0 deletions rust/config/mainnet3_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@
"grpcUrl": "https://grpc-kralum.neutron-1.neutron.org:80",
"canonicalAsset": "untrn",
"prefix": "neutron",
"minimumGasPrice": {
"amount": "1",
"denom": "untrn"
},
"index": {
"from": 4000000,
"chunk": 100000
Expand Down
4 changes: 4 additions & 0 deletions rust/config/testnet4_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,10 @@
"grpcUrl": "http://52.43.22.152:9090",
"canonicalAsset": "token",
"prefix": "dual",
"minimumGasPrice": {
"amount": "1",
"denom": "udual"
},
"index": {
"from": 1,
"chunk": 100000
Expand Down
6 changes: 3 additions & 3 deletions rust/hyperlane-base/src/settings/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use convert_case::{Case, Casing};
use eyre::{eyre, Context};
use h_cosmos::CosmosBalance;
use h_cosmos::RawCosmosAmount;
use hyperlane_core::{
cfg_unwrap_all, config::*, HyperlaneDomain, HyperlaneDomainProtocol, IndexMode,
};
Expand Down Expand Up @@ -401,7 +401,7 @@ pub fn recase_json_value(mut val: Value, case: Case) -> Value {
}

/// Expects AgentSigner.
fn parse_cosmos_gas_price(gas_price: ValueParser) -> ConfigResult<CosmosBalance> {
fn parse_cosmos_gas_price(gas_price: ValueParser) -> ConfigResult<RawCosmosAmount> {
let mut err = ConfigParsingError::default();

let amount = gas_price
Expand All @@ -416,5 +416,5 @@ fn parse_cosmos_gas_price(gas_price: ValueParser) -> ConfigResult<CosmosBalance>
.parse_string()
.end();
cfg_unwrap_all!(&gas_price.cwp, err: [denom, amount]);
err.into_result(CosmosBalance::new(denom.to_owned(), amount.to_owned()))
err.into_result(RawCosmosAmount::new(denom.to_owned(), amount.to_owned()))
}
4 changes: 2 additions & 2 deletions rust/utils/run-locally/src/cosmos/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, io::Write, path::PathBuf, process::Stdio};

use hyperlane_cosmos::CosmosBalance;
use hyperlane_cosmos::RawCosmosAmount;
use k256::ecdsa::SigningKey;

use crate::{
Expand Down Expand Up @@ -254,7 +254,7 @@ impl OsmosisCLI {
sender: &str,
contract: &str,
execute_msg: T,
funds: Vec<CosmosBalance>,
funds: Vec<RawCosmosAmount>,
) -> TxResponse {
let mut cmd = self
.cli()
Expand Down
4 changes: 2 additions & 2 deletions rust/utils/run-locally/src/cosmos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{env, fs};

use cosmwasm_schema::cw_serde;
use hpl_interface::types::bech32_decode;
use hyperlane_cosmos::CosmosBalance;
use hyperlane_cosmos::RawCosmosAmount;
use macro_rules_attribute::apply;
use maplit::hashmap;
use tempfile::tempdir;
Expand Down Expand Up @@ -507,7 +507,7 @@ fn run_locally() {
metadata: "".to_string(),
},
},
vec![CosmosBalance {
vec![RawCosmosAmount {
denom: "uosmo".to_string(),
amount: 25_000_000.to_string(),
}],
Expand Down
8 changes: 4 additions & 4 deletions rust/utils/run-locally/src/cosmos/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::BTreeMap, path::PathBuf};

use hpl_interface::types::bech32_decode;
use hyperlane_cosmos::CosmosBalance;
use hyperlane_cosmos::RawCosmosAmount;

use super::{cli::OsmosisCLI, CosmosNetwork};

Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Deployments {

#[derive(serde::Serialize, serde::Deserialize)]
pub struct BalanceResponse {
pub balances: Vec<CosmosBalance>,
pub balances: Vec<RawCosmosAmount>,
}

#[derive(serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -120,7 +120,7 @@ pub struct AgentConfig {
pub prefix: String,
pub signer: AgentConfigSigner,
pub index: AgentConfigIndex,
pub minimum_gas_price: CosmosBalance,
pub minimum_gas_price: RawCosmosAmount,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -160,7 +160,7 @@ impl AgentConfig {
key: format!("0x{}", hex::encode(validator.priv_key.to_bytes())),
prefix: "osmo".to_string(),
},
minimum_gas_price: CosmosBalance {
minimum_gas_price: RawCosmosAmount {
denom: "uosmo".to_string(),
amount: "0.02".to_string(),
},
Expand Down

0 comments on commit 5ad57e3

Please sign in to comment.