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
Prev Previous commit
Next Next commit
fix: PR comments
  • Loading branch information
daniel-savu committed Dec 15, 2023
commit 781cc79d4d3195c72e93b0ae82821d19d21cb9ea
657 changes: 362 additions & 295 deletions rust/Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions rust/agents/relayer/src/msg/gas_payment/mod.rs
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ use async_trait::async_trait;
use eyre::Result;
use hyperlane_base::db::HyperlaneRocksDB;
use hyperlane_core::{
BigFloat, GasPaymentKey, HyperlaneMessage, InterchainGasExpenditure, InterchainGasPayment,
TxCostEstimate, TxOutcome, U256,
FixedPointNumber, GasPaymentKey, HyperlaneMessage, InterchainGasExpenditure,
InterchainGasPayment, TxCostEstimate, TxOutcome, U256,
};
use tracing::{debug, error, trace};

@@ -135,7 +135,8 @@ impl GasPaymentEnforcer {
self.db.process_gas_expenditure(InterchainGasExpenditure {
message_id: message.id(),
gas_used: outcome.gas_used,
tokens_used: (BigFloat::try_from(outcome.gas_used)? * outcome.gas_price).try_into()?,
tokens_used: (FixedPointNumber::try_from(outcome.gas_used)? * outcome.gas_price)
.try_into()?,
})?;
Ok(())
}
3 changes: 0 additions & 3 deletions rust/chains/hyperlane-cosmos/src/error.rs
Original file line number Diff line number Diff line change
@@ -34,9 +34,6 @@ 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 {
11 changes: 8 additions & 3 deletions rust/chains/hyperlane-cosmos/src/providers/grpc.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,9 @@ use cosmrs::{
tx::{self, Fee, MessageExt, SignDoc, SignerInfo},
Coin,
};
use hyperlane_core::{BigFloat, ChainCommunicationError, ChainResult, ContractLocator, U256};
use hyperlane_core::{
ChainCommunicationError, ChainResult, ContractLocator, FixedPointNumber, U256,
};
use serde::Serialize;
use tonic::transport::{Channel, Endpoint};

@@ -123,7 +125,7 @@ impl WasmGrpcProvider {
}

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

@@ -148,10 +150,13 @@ impl WasmGrpcProvider {
);
let signer_info = SignerInfo::single_direct(Some(signer.public_key), account_info.sequence);

let amount: u128 = (FixedPointNumber::from(gas_limit) * self.gas_price())
.ceil_to_integer()
.try_into()?;
let auth_info = signer_info.auth_info(Fee::from_amount_and_gas(
Coin::new(
// The fee to pay is the gas limit * the gas price
(BigFloat::from(gas_limit) * self.gas_price()).try_into()?,
amount,
self.conf.get_canonical_asset().as_str(),
)
.map_err(Into::<HyperlaneCosmosError>::into)?,
8 changes: 4 additions & 4 deletions rust/chains/hyperlane-cosmos/src/trait_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use derive_new::new;
use hyperlane_core::{BigFloat, ChainCommunicationError};
use hyperlane_core::{ChainCommunicationError, FixedPointNumber};

/// Cosmos connection configuration
#[derive(Debug, Clone)]
@@ -16,7 +16,7 @@ pub struct ConnectionConf {
prefix: String,
/// Canoncial Assets Denom
canonical_asset: String,
/// The gas price set by the cosmos-sdk validator. Not that this represents the
/// The gas price set by the cosmos-sdk validator. Note that this represents the
/// minimum price set by the validator.
/// More details here: https://docs.cosmos.network/main/learn/beginner/gas-fees#antehandler
gas_price: RawCosmosAmount,
@@ -37,15 +37,15 @@ pub struct CosmosAmount {
/// Coin denom (e.g. `untrn`)
pub denom: String,
/// Amount in the given denom
pub amount: BigFloat,
pub amount: FixedPointNumber,
}

impl TryFrom<RawCosmosAmount> for CosmosAmount {
type Error = ChainCommunicationError;
fn try_from(raw: RawCosmosAmount) -> Result<Self, ChainCommunicationError> {
Ok(Self {
denom: raw.denom,
amount: BigFloat::from_str(&raw.amount)?,
amount: FixedPointNumber::from_str(&raw.amount)?,
})
}
}
6 changes: 3 additions & 3 deletions rust/chains/hyperlane-sealevel/src/mailbox.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@ use jsonrpc_core::futures_util::TryFutureExt;
use tracing::{debug, info, instrument, warn};

use hyperlane_core::{
accumulator::incremental::IncrementalMerkle, BigFloat, ChainCommunicationError, ChainResult,
Checkpoint, ContractLocator, Decode as _, Encode as _, HyperlaneAbi, HyperlaneChain,
accumulator::incremental::IncrementalMerkle, ChainCommunicationError, ChainResult, Checkpoint,
ContractLocator, Decode as _, Encode as _, FixedPointNumber, HyperlaneAbi, HyperlaneChain,
HyperlaneContract, HyperlaneDomain, HyperlaneMessage, HyperlaneProvider, Indexer, LogMeta,
Mailbox, MerkleTreeHook, SequenceIndexer, TxCostEstimate, TxOutcome, H256, H512, U256,
};
@@ -484,7 +484,7 @@ impl Mailbox for SealevelMailbox {
// TODO use correct data upon integrating IGP support
Ok(TxCostEstimate {
gas_limit: U256::zero(),
gas_price: BigFloat::zero(),
gas_price: FixedPointNumber::zero(),
l2_gas_limit: None,
})
}
4 changes: 2 additions & 2 deletions rust/config/mainnet3_config.json
Original file line number Diff line number Diff line change
@@ -432,8 +432,8 @@
"grpcUrl": "https://grpc-kralum.neutron-1.neutron.org:80",
"canonicalAsset": "untrn",
"prefix": "neutron",
"minimumGasPrice": {
"amount": "1",
"gasPrice": {
"amount": "0.5",
"denom": "untrn"
},
"index": {
4 changes: 2 additions & 2 deletions rust/config/testnet4_config.json
Original file line number Diff line number Diff line change
@@ -1026,8 +1026,8 @@
"grpcUrl": "http://52.43.22.152:9090",
"canonicalAsset": "token",
"prefix": "dual",
"minimumGasPrice": {
"amount": "1",
"gasPrice": {
"amount": "0.1",
"denom": "udual"
},
"index": {
Original file line number Diff line number Diff line change
@@ -94,9 +94,9 @@ pub fn build_cosmos_connection_conf(
None
};

let minimum_gas_price = chain
let gas_price = chain
.chain(err)
.get_opt_key("minimumGasPrice")
.get_opt_key("gasPrice")
.and_then(parse_cosmos_gas_price)
.end();

@@ -110,7 +110,7 @@ pub fn build_cosmos_connection_conf(
chain_id.unwrap().to_string(),
prefix.unwrap().to_string(),
canonical_asset.unwrap(),
minimum_gas_price.unwrap(),
gas_price.unwrap(),
)))
}
}
6 changes: 3 additions & 3 deletions rust/hyperlane-core/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ pub use routing_ism::*;
pub use signing::*;
pub use validator_announce::*;

use crate::{BigFloat, U256};
use crate::{FixedPointNumber, U256};

mod aggregation_ism;
mod ccip_read_ism;
@@ -44,7 +44,7 @@ pub struct TxOutcome {
/// Amount of gas used on this transaction.
pub gas_used: crate::U256,
/// Price paid for the gas
pub gas_price: BigFloat,
pub gas_price: FixedPointNumber,
// TODO: more? What can be abstracted across all chains?
}

@@ -58,7 +58,7 @@ impl From<ethers_core::types::TransactionReceipt> for TxOutcome {
gas_price: t
.effective_gas_price
.and_then(|price| U256::from(price).try_into().ok())
.unwrap_or(BigFloat::zero()),
.unwrap_or(FixedPointNumber::zero()),
}
}
}
2 changes: 1 addition & 1 deletion rust/hyperlane-core/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -224,7 +224,7 @@ pub struct TxCostEstimate {
/// The gas limit for the transaction.
pub gas_limit: U256,
/// The gas price for the transaction.
pub gas_price: BigFloat,
pub gas_price: FixedPointNumber,
/// The amount of L2 gas for the transaction.
/// If Some, `gas_limit` is the sum of the gas limit
/// covering L1 costs and the L2 gas limit.
35 changes: 24 additions & 11 deletions rust/hyperlane-core/src/types/primitive_types.rs
Original file line number Diff line number Diff line change
@@ -344,30 +344,43 @@ impl From<solana_sdk::signature::Signature> for H512 {

/// Wrapper type around `BigDecimal` to implement various traits on it
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BigFloat(BigDecimal);
pub struct FixedPointNumber(BigDecimal);

impl BigFloat {
impl FixedPointNumber {
/// Zero
pub fn zero() -> Self {
Self(BigDecimal::zero())
}

/// Round up to the nearest integer
pub fn ceil_to_integer(&self) -> Self {
Self(self.0.with_scale(0))
}

/// Ceil
pub fn ceil(&self, fractional_digit_count: i64) -> Self {
Self(
self.0
.with_scale_round(fractional_digit_count, bigdecimal::RoundingMode::Ceiling),
)
}
}

impl Default for BigFloat {
impl Default for FixedPointNumber {
fn default() -> Self {
Self::zero()
}
}

impl TryFrom<U256> for BigFloat {
impl TryFrom<U256> for FixedPointNumber {
type Error = ChainCommunicationError;
fn try_from(val: U256) -> Result<Self, Self::Error> {
let u256_string = val.to_string();
Ok(Self(BigDecimal::from_str(&u256_string)?))
}
}

impl TryInto<U256> for BigFloat {
impl TryInto<U256> for FixedPointNumber {
type Error = ChainCommunicationError;

fn try_into(self) -> Result<U256, Self::Error> {
@@ -378,7 +391,7 @@ impl TryInto<U256> for BigFloat {
}
}

impl TryInto<u128> for BigFloat {
impl TryInto<u128> for FixedPointNumber {
type Error = ChainCommunicationError;

fn try_into(self) -> Result<u128, Self::Error> {
@@ -387,7 +400,7 @@ impl TryInto<u128> for BigFloat {
}
}

impl<T> From<T> for BigFloat
impl<T> From<T> for FixedPointNumber
where
T: Into<BigDecimal>,
{
@@ -396,19 +409,19 @@ where
}
}

impl<T> Mul<T> for BigFloat
impl<T> Mul<T> for FixedPointNumber
where
T: Into<BigFloat>,
T: Into<FixedPointNumber>,
{
type Output = BigFloat;
type Output = FixedPointNumber;

fn mul(self, rhs: T) -> Self::Output {
let rhs = rhs.into();
Self(self.0 * rhs.0)
}
}

impl FromStr for BigFloat {
impl FromStr for FixedPointNumber {
type Err = ChainCommunicationError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
6 changes: 3 additions & 3 deletions rust/utils/run-locally/src/cosmos/types.rs
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ pub struct AgentConfig {
pub prefix: String,
pub signer: AgentConfigSigner,
pub index: AgentConfigIndex,
pub minimum_gas_price: RawCosmosAmount,
pub gas_price: RawCosmosAmount,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
@@ -160,9 +160,9 @@ impl AgentConfig {
key: format!("0x{}", hex::encode(validator.priv_key.to_bytes())),
prefix: "osmo".to_string(),
},
minimum_gas_price: RawCosmosAmount {
gas_price: RawCosmosAmount {
denom: "uosmo".to_string(),
amount: "0.02".to_string(),
amount: "0.05".to_string(),
},
index: AgentConfigIndex {
from: 1,
Loading
Oops, something went wrong.