Skip to content

refactor(fortuna): Move more utilities into evm_utils #2376

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

Merged
merged 7 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
119 changes: 114 additions & 5 deletions apps/fortuna/Cargo.lock

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

2 changes: 1 addition & 1 deletion apps/fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "7.4.1"
version = "7.4.2"
edition = "2021"

[lib]
Expand Down
1 change: 1 addition & 0 deletions apps/fortuna/src/chain/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub type MiddlewaresWrapper<T> = LegacyTxMiddleware<
EthProviderOracle<Provider<T>>,
>,
>;

pub type SignablePythContractInner<T> = PythRandom<MiddlewaresWrapper<T>>;
pub type SignablePythContract = SignablePythContractInner<Http>;
pub type InstrumentedSignablePythContract = SignablePythContractInner<TracedClient>;
Expand Down
41 changes: 9 additions & 32 deletions apps/fortuna/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
crate::{
api::ChainId,
chain::reader::{BlockNumber, BlockStatus},
eth_utils::utils::EscalationPolicy,
},
anyhow::{anyhow, Result},
clap::{crate_authors, crate_description, crate_name, crate_version, Args, Parser},
Expand Down Expand Up @@ -259,39 +260,15 @@ impl Default for EscalationPolicyConfig {
}

impl EscalationPolicyConfig {
pub fn get_gas_multiplier_pct(&self, num_retries: u64) -> u64 {
self.apply_escalation_policy(
num_retries,
self.initial_gas_multiplier_pct,
self.gas_multiplier_pct,
self.gas_multiplier_cap_pct,
)
}

pub fn get_fee_multiplier_pct(&self, num_retries: u64) -> u64 {
self.apply_escalation_policy(
num_retries,
100,
self.fee_multiplier_pct,
self.fee_multiplier_cap_pct,
)
}

fn apply_escalation_policy(
&self,
num_retries: u64,
initial: u64,
multiplier: u64,
cap: u64,
) -> u64 {
let mut current = initial;
let mut i = 0;
while i < num_retries && current < cap {
current = current.saturating_mul(multiplier) / 100;
i += 1;
pub fn to_policy(&self) -> EscalationPolicy {
EscalationPolicy {
gas_limit_tolerance_pct: self.gas_limit_tolerance_pct,
initial_gas_multiplier_pct: self.initial_gas_multiplier_pct,
gas_multiplier_pct: self.gas_multiplier_pct,
gas_multiplier_cap_pct: self.gas_multiplier_cap_pct,
fee_multiplier_pct: self.fee_multiplier_pct,
fee_multiplier_cap_pct: self.fee_multiplier_cap_pct,
}

current.min(cap)
}
}

Expand Down
39 changes: 30 additions & 9 deletions apps/fortuna/src/eth_utils/nonce_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Copied from: https://github.com/gakonst/ethers-rs/blob/34ed9e372e66235aed7074bc3f5c14922b139242/ethers-middleware/src/nonce_manager.rs

use {
super::legacy_tx_middleware::LegacyTxMiddleware,
axum::async_trait,
ethers::prelude::GasOracle,
ethers::{
middleware::gas_oracle::GasOracleMiddleware,
providers::{Middleware, MiddlewareError, PendingTransaction},
types::{transaction::eip2718::TypedTransaction, *},
},
Expand Down Expand Up @@ -72,15 +75,6 @@ where
Ok(nonce)
} // guard dropped here

/// Resets the initialized flag so the next usage of the manager will reinitialize the nonce
/// based on the chain state.
/// This is useful when the RPC does not return an error if the transaction is submitted with
/// an incorrect nonce.
/// This is the only new method compared to the original NonceManagerMiddleware.
pub fn reset(&self) {
self.initialized.store(false, Ordering::SeqCst);
}

async fn get_transaction_count_with_manager(
&self,
block: Option<BlockId>,
Expand All @@ -100,6 +94,33 @@ where
}
}

pub trait NonceManaged {
fn reset(&self);
}

impl<M: Middleware> NonceManaged for NonceManagerMiddleware<M> {
/// Resets the initialized flag so the next usage of the manager will reinitialize the nonce
/// based on the chain state.
/// This is useful when the RPC does not return an error if the transaction is submitted with
/// an incorrect nonce.
/// This is the only new method compared to the original NonceManagerMiddleware.
fn reset(&self) {
self.initialized.store(false, Ordering::SeqCst);
}
}

impl<M: NonceManaged + Middleware, G: GasOracle> NonceManaged for GasOracleMiddleware<M, G> {
fn reset(&self) {
self.inner().reset();
}
}

impl<T: NonceManaged + Middleware> NonceManaged for LegacyTxMiddleware<T> {
fn reset(&self) {
self.inner().reset();
}
}

#[derive(Error, Debug)]
/// Thrown when an error happens at the Nonce Manager
pub enum NonceManagerError<M: Middleware> {
Expand Down
Loading
Loading