Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-savu committed Dec 15, 2023
1 parent 073a6ed commit 4e41e90
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 337 deletions.
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
Expand Up @@ -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};

Expand Down Expand Up @@ -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(())
}
Expand Down
3 changes: 0 additions & 3 deletions rust/chains/hyperlane-cosmos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions rust/chains/hyperlane-cosmos/src/providers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}

Expand All @@ -148,10 +150,14 @@ 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()?;
println!("gas amount: {}", amount);
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)?,
Expand Down
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)]
Expand All @@ -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,
Expand All @@ -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)?,
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/chains/hyperlane-sealevel/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
})
}
Expand Down
4 changes: 2 additions & 2 deletions rust/config/mainnet3_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions rust/config/testnet4_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions rust/hyperlane-base/src/settings/parser/connection_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(),
)))
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/hyperlane-core/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?
}

Expand All @@ -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
Expand Up @@ -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.
Expand Down
35 changes: 24 additions & 11 deletions rust/hyperlane-core/src/types/primitive_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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> {
Expand All @@ -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>,
{
Expand All @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion rust/utils/run-locally/src/cosmos/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fn link_network(
linker,
&validator_addr,
"osmo1l83956lgpak5sun7ggupls7rk7p5cr95499jdf",
"10000000uosmo",
"100000000000uosmo",
);

// TODO
Expand Down
6 changes: 3 additions & 3 deletions rust/utils/run-locally/src/cosmos/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4e41e90

Please sign in to comment.