Skip to content

Commit a26f335

Browse files
devin-ai-integration[bot]Jayant Krishnamurthyjayantk
authored
feat: add configurable priority fee multiplier (#2195)
* feat: add configurable priority fee multiplier Co-Authored-By: Jayant Krishnamurthy <jayant@dourolabs.xyz> * hm * fix this up a bit * fix this up a bit * fix this up a bit * fix this up a bit * chore: fix rust formatting Co-Authored-By: Jayant Krishnamurthy <jayant@dourolabs.xyz> * sample --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Jayant Krishnamurthy <jayant@dourolabs.xyz> Co-authored-by: Jayant Krishnamurthy <jayantkrishnamurthy@gmail.com>
1 parent cc16040 commit a26f335

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

apps/fortuna/Cargo.lock

Lines changed: 1 addition & 1 deletion
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 = "6.5.5"
3+
version = "6.6.0"
44
edition = "2021"
55

66
[dependencies]

apps/fortuna/config.sample.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ chains:
1212
# How much to charge in fees
1313
fee: 1500000000000000
1414

15+
# Multiplier for the priority fee estimate, as a percentage (i.e., 100 = no change).
16+
# Defaults to 100 if the field is omitted.
17+
priority_fee_multiplier_pct: 100
18+
1519
# Configuration for dynamic fees under high gas prices. The keeper will set
1620
# on-chain fees to make between [min_profit_pct, max_profit_pct] of the max callback
1721
# cost in profit per transaction.

apps/fortuna/src/chain/eth_gas_oracle.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ pub const EIP1559_FEE_ESTIMATION_THRESHOLD_MAX_CHANGE: i64 = 200;
3939
#[must_use]
4040
pub struct EthProviderOracle<M: Middleware> {
4141
provider: M,
42+
priority_fee_multiplier_pct: u64,
4243
}
4344

4445
impl<M: Middleware> EthProviderOracle<M> {
45-
pub fn new(provider: M) -> Self {
46-
Self { provider }
46+
pub fn new(provider: M, priority_fee_multiplier_pct: u64) -> Self {
47+
Self {
48+
provider,
49+
priority_fee_multiplier_pct,
50+
}
4751
}
4852
}
4953

@@ -61,10 +65,19 @@ where
6165
}
6266

6367
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> {
64-
self.provider
68+
let (max_fee_per_gas, max_priority_fee_per_gas) = self
69+
.provider
6570
.estimate_eip1559_fees(Some(eip1559_default_estimator))
6671
.await
67-
.map_err(|err| GasOracleError::ProviderError(Box::new(err)))
72+
.map_err(|err| GasOracleError::ProviderError(Box::new(err)))?;
73+
74+
// Apply the multiplier to max_priority_fee_per_gas
75+
let max_priority_fee_per_gas = max_priority_fee_per_gas
76+
.checked_mul(U256::from(self.priority_fee_multiplier_pct))
77+
.and_then(|x| x.checked_div(U256::from(100)))
78+
.unwrap_or(max_priority_fee_per_gas);
79+
80+
Ok((max_fee_per_gas, max_priority_fee_per_gas))
6881
}
6982
}
7083

@@ -79,12 +92,14 @@ pub fn eip1559_default_estimator(base_fee_per_gas: U256, rewards: Vec<Vec<U256>>
7992
U256::from(EIP1559_FEE_ESTIMATION_DEFAULT_PRIORITY_FEE),
8093
)
8194
};
95+
8296
let potential_max_fee = base_fee_surged(base_fee_per_gas);
8397
let max_fee_per_gas = if max_priority_fee_per_gas > potential_max_fee {
8498
max_priority_fee_per_gas + potential_max_fee
8599
} else {
86100
potential_max_fee
87101
};
102+
88103
(max_fee_per_gas, max_priority_fee_per_gas)
89104
}
90105

apps/fortuna/src/chain/ethereum.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ impl<T: JsonRpcClient + 'static + Clone> SignablePythContractInner<T> {
211211
provider: Provider<T>,
212212
) -> Result<SignablePythContractInner<T>> {
213213
let chain_id = provider.get_chainid().await?;
214-
let gas_oracle = EthProviderOracle::new(provider.clone());
214+
let gas_oracle =
215+
EthProviderOracle::new(provider.clone(), chain_config.priority_fee_multiplier_pct);
215216
let wallet__ = private_key
216217
.parse::<LocalWallet>()?
217218
.with_chain_id(chain_id.as_u64());

apps/fortuna/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ pub struct EthereumConfig {
166166
/// Maximum number of hashes to record in a request.
167167
/// This should be set according to the maximum gas limit the provider supports for callbacks.
168168
pub max_num_hashes: Option<u32>,
169+
170+
/// The percentage multiplier to apply to the priority fee (100 = no change, e.g. 150 = 150% of base fee)
171+
#[serde(default = "default_priority_fee_multiplier_pct")]
172+
pub priority_fee_multiplier_pct: u64,
169173
}
170174

171175
/// A commitment that the provider used to generate random numbers at some point in the past.
@@ -215,6 +219,10 @@ fn default_chain_sample_interval() -> u64 {
215219
1
216220
}
217221

222+
fn default_priority_fee_multiplier_pct() -> u64 {
223+
100
224+
}
225+
218226
/// Configuration values for the keeper service that are shared across chains.
219227
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
220228
pub struct KeeperConfig {

apps/fortuna/src/keeper.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use {
22
crate::{
33
api::{self, BlockchainState, ChainId},
44
chain::{
5-
eth_gas_oracle::eip1559_default_estimator,
65
ethereum::{
76
InstrumentedPythContract, InstrumentedSignablePythContract, PythContractCall,
87
},
@@ -1208,9 +1207,11 @@ pub async fn estimate_tx_cost(
12081207
.try_into()
12091208
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?
12101209
} else {
1211-
let (max_fee_per_gas, max_priority_fee_per_gas) = middleware
1212-
.estimate_eip1559_fees(Some(eip1559_default_estimator))
1213-
.await?;
1210+
// This is not obvious but the implementation of estimate_eip1559_fees in ethers.rs
1211+
// for a middleware that has a GasOracleMiddleware inside is to ignore the passed-in callback
1212+
// and use whatever the gas oracle returns.
1213+
let (max_fee_per_gas, max_priority_fee_per_gas) =
1214+
middleware.estimate_eip1559_fees(None).await?;
12141215

12151216
(max_fee_per_gas + max_priority_fee_per_gas)
12161217
.try_into()

0 commit comments

Comments
 (0)