Skip to content

Commit 77c2332

Browse files
authored
refactor(fortuna): Move more utilities into evm_utils (#2376)
* mostly fixed * submit tx with retry * move escalation policy * stuff * cleanup * hm
1 parent aa9dd64 commit 77c2332

File tree

9 files changed

+460
-277
lines changed

9 files changed

+460
-277
lines changed

apps/fortuna/Cargo.lock

Lines changed: 114 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/fortuna/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fortuna"
3-
version = "7.4.1"
3+
version = "7.4.2"
44
edition = "2021"
55

66
[lib]

apps/fortuna/src/chain/ethereum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub type MiddlewaresWrapper<T> = LegacyTxMiddleware<
4141
EthProviderOracle<Provider<T>>,
4242
>,
4343
>;
44+
4445
pub type SignablePythContractInner<T> = PythRandom<MiddlewaresWrapper<T>>;
4546
pub type SignablePythContract = SignablePythContractInner<Http>;
4647
pub type InstrumentedSignablePythContract = SignablePythContractInner<TracedClient>;

apps/fortuna/src/config.rs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use {
22
crate::{
33
api::ChainId,
44
chain::reader::{BlockNumber, BlockStatus},
5+
eth_utils::utils::EscalationPolicy,
56
},
67
anyhow::{anyhow, Result},
78
clap::{crate_authors, crate_description, crate_name, crate_version, Args, Parser},
@@ -259,39 +260,15 @@ impl Default for EscalationPolicyConfig {
259260
}
260261

261262
impl EscalationPolicyConfig {
262-
pub fn get_gas_multiplier_pct(&self, num_retries: u64) -> u64 {
263-
self.apply_escalation_policy(
264-
num_retries,
265-
self.initial_gas_multiplier_pct,
266-
self.gas_multiplier_pct,
267-
self.gas_multiplier_cap_pct,
268-
)
269-
}
270-
271-
pub fn get_fee_multiplier_pct(&self, num_retries: u64) -> u64 {
272-
self.apply_escalation_policy(
273-
num_retries,
274-
100,
275-
self.fee_multiplier_pct,
276-
self.fee_multiplier_cap_pct,
277-
)
278-
}
279-
280-
fn apply_escalation_policy(
281-
&self,
282-
num_retries: u64,
283-
initial: u64,
284-
multiplier: u64,
285-
cap: u64,
286-
) -> u64 {
287-
let mut current = initial;
288-
let mut i = 0;
289-
while i < num_retries && current < cap {
290-
current = current.saturating_mul(multiplier) / 100;
291-
i += 1;
263+
pub fn to_policy(&self) -> EscalationPolicy {
264+
EscalationPolicy {
265+
gas_limit_tolerance_pct: self.gas_limit_tolerance_pct,
266+
initial_gas_multiplier_pct: self.initial_gas_multiplier_pct,
267+
gas_multiplier_pct: self.gas_multiplier_pct,
268+
gas_multiplier_cap_pct: self.gas_multiplier_cap_pct,
269+
fee_multiplier_pct: self.fee_multiplier_pct,
270+
fee_multiplier_cap_pct: self.fee_multiplier_cap_pct,
292271
}
293-
294-
current.min(cap)
295272
}
296273
}
297274

apps/fortuna/src/eth_utils/nonce_manager.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Copied from: https://github.com/gakonst/ethers-rs/blob/34ed9e372e66235aed7074bc3f5c14922b139242/ethers-middleware/src/nonce_manager.rs
33

44
use {
5+
super::legacy_tx_middleware::LegacyTxMiddleware,
56
axum::async_trait,
7+
ethers::prelude::GasOracle,
68
ethers::{
9+
middleware::gas_oracle::GasOracleMiddleware,
710
providers::{Middleware, MiddlewareError, PendingTransaction},
811
types::{transaction::eip2718::TypedTransaction, *},
912
},
@@ -72,15 +75,6 @@ where
7275
Ok(nonce)
7376
} // guard dropped here
7477

75-
/// Resets the initialized flag so the next usage of the manager will reinitialize the nonce
76-
/// based on the chain state.
77-
/// This is useful when the RPC does not return an error if the transaction is submitted with
78-
/// an incorrect nonce.
79-
/// This is the only new method compared to the original NonceManagerMiddleware.
80-
pub fn reset(&self) {
81-
self.initialized.store(false, Ordering::SeqCst);
82-
}
83-
8478
async fn get_transaction_count_with_manager(
8579
&self,
8680
block: Option<BlockId>,
@@ -100,6 +94,33 @@ where
10094
}
10195
}
10296

97+
pub trait NonceManaged {
98+
fn reset(&self);
99+
}
100+
101+
impl<M: Middleware> NonceManaged for NonceManagerMiddleware<M> {
102+
/// Resets the initialized flag so the next usage of the manager will reinitialize the nonce
103+
/// based on the chain state.
104+
/// This is useful when the RPC does not return an error if the transaction is submitted with
105+
/// an incorrect nonce.
106+
/// This is the only new method compared to the original NonceManagerMiddleware.
107+
fn reset(&self) {
108+
self.initialized.store(false, Ordering::SeqCst);
109+
}
110+
}
111+
112+
impl<M: NonceManaged + Middleware, G: GasOracle> NonceManaged for GasOracleMiddleware<M, G> {
113+
fn reset(&self) {
114+
self.inner().reset();
115+
}
116+
}
117+
118+
impl<T: NonceManaged + Middleware> NonceManaged for LegacyTxMiddleware<T> {
119+
fn reset(&self) {
120+
self.inner().reset();
121+
}
122+
}
123+
103124
#[derive(Error, Debug)]
104125
/// Thrown when an error happens at the Nonce Manager
105126
pub enum NonceManagerError<M: Middleware> {

0 commit comments

Comments
 (0)