Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos gas price config #3042

Merged
merged 9 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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,
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
}

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 {
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
self.gas_price.amount
}
}

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

use crate::HyperlaneCosmosError;

/// Cosmos connection configuration
#[derive(Debug, Clone)]
pub struct ConnectionConf {
Expand All @@ -11,6 +16,46 @@ pub struct ConnectionConf {
prefix: String,
/// Canoncial Assets Denom
canonical_asset: String,
/// The minimum gas price set by the cosmos-sdk validator
minimum_gas_price: RawCosmosAmount,
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
}

/// Untyped cosmos amount
#[derive(serde::Serialize, serde::Deserialize, new, Clone, Debug)]
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
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
.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.
Expand Down Expand Up @@ -59,20 +104,27 @@ 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: RawCosmosAmount,
) -> Self {
Self {
grpc_url,
rpc_url,
chain_id,
prefix,
canonical_asset,
minimum_gas_price,
}
}
}
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",
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
"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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use url::Url;
use crate::settings::envs::*;
use crate::settings::ChainConnectionConf;

use super::ValueParser;
use super::{parse_cosmos_gas_price, ValueParser};

pub fn build_ethereum_connection_conf(
rpcs: &[Url],
Expand Down Expand Up @@ -94,6 +94,12 @@ pub fn build_cosmos_connection_conf(
None
};

let minimum_gas_price = chain
.chain(err)
.get_opt_key("minimumGasPrice")
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
.and_then(parse_cosmos_gas_price)
.end();

if !local_err.is_ok() {
err.merge(local_err);
None
Expand All @@ -104,6 +110,7 @@ pub fn build_cosmos_connection_conf(
chain_id.unwrap().to_string(),
prefix.unwrap().to_string(),
canonical_asset.unwrap(),
minimum_gas_price.unwrap(),
)))
}
}
Expand Down
20 changes: 20 additions & 0 deletions rust/hyperlane-base/src/settings/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{

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

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

let amount = gas_price
.chain(&mut err)
.get_opt_key("amount")
.parse_string()
.end();

let denom = gas_price
.chain(&mut err)
.get_opt_key("denom")
.parse_string()
.end();
cfg_unwrap_all!(&gas_price.cwp, err: [denom, amount]);
err.into_result(RawCosmosAmount::new(denom.to_owned(), amount.to_owned()))
}
1 change: 1 addition & 0 deletions rust/utils/run-locally/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version.workspace = true

[dependencies]
hyperlane-core = { path = "../../hyperlane-core", features = ["float"]}
hyperlane-cosmos = { path = "../../chains/hyperlane-cosmos"}
toml_edit.workspace = true
k256.workspace = true
ripemd.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions rust/utils/run-locally/src/cosmos/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::BTreeMap, io::Write, path::PathBuf, process::Stdio};

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

use crate::{
Expand All @@ -9,7 +10,7 @@ use crate::{

use super::{
crypto::KeyPair, default_keys, modify_toml, sed, types::BalanceResponse, wait_for_node, Codes,
Coin, TxResponse,
TxResponse,
};

const GENESIS_FUND: u128 = 1000000000000;
Expand Down Expand Up @@ -253,7 +254,7 @@ impl OsmosisCLI {
sender: &str,
contract: &str,
execute_msg: T,
funds: Vec<Coin>,
funds: Vec<RawCosmosAmount>,
) -> TxResponse {
let mut cmd = self
.cli()
Expand Down
4 changes: 1 addition & 3 deletions rust/utils/run-locally/src/cosmos/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// TODO: this file can be removed if `CosmosAddress` can be imported from `hyperlane-cosmos`.
// However, adding a hyperlane-cosmos dep creates a dep cycle.
// Look into how this can be fixed.
// TODO: this file can be removed by replacing `KeyPair` uses with `CosmosAddress`

use k256::ecdsa::{SigningKey, VerifyingKey};
use ripemd::Ripemd160;
Expand Down
3 changes: 2 additions & 1 deletion rust/utils/run-locally/src/cosmos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{env, fs};

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

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

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

Expand Down Expand Up @@ -35,12 +36,6 @@ pub struct TxResponse {
pub logs: Vec<TxLog>,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Coin {
pub denom: String,
pub amount: String,
}

#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Codes {
pub hpl_hook_merkle: u64,
Expand Down Expand Up @@ -73,7 +68,7 @@ pub struct Deployments {

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

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

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -164,6 +160,10 @@ impl AgentConfig {
key: format!("0x{}", hex::encode(validator.priv_key.to_bytes())),
prefix: "osmo".to_string(),
},
minimum_gas_price: RawCosmosAmount {
denom: "uosmo".to_string(),
amount: "0.02".to_string(),
},
index: AgentConfigIndex {
from: 1,
chunk: 100,
Expand Down
Loading