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

feat(fortuna): add eip1559_fee_multiplier_pct to adjust gas fees #2191

Closed
10 changes: 10 additions & 0 deletions apps/fortuna/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ pub struct EthereumConfig {
/// Maximum number of hashes to record in a request.
/// This should be set according to the maximum gas limit the provider supports for callbacks.
pub max_num_hashes: Option<u32>,

/// Multiplier for EIP1559 fee estimates, represented as a percentage.
/// For example, 100 means no change, 200 means double the fees.
#[serde(default = "default_eip1559_fee_multiplier_pct")]
pub eip1559_fee_multiplier_pct: u64,
}

/// Default value for eip1559_fee_multiplier_pct (100 = no change to fees)
fn default_eip1559_fee_multiplier_pct() -> u64 {
100
}

/// A commitment that the provider used to generate random numbers at some point in the past.
Expand Down
56 changes: 37 additions & 19 deletions apps/fortuna/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,25 @@ pub async fn run_keeper_threads(
);

// Spawn a thread that periodically adjusts the provider fee.
let config_for_fee = chain_eth_config.clone();
let provider_address = chain_state.provider_address;
let contract_for_fee = contract.clone();
spawn(
adjust_fee_wrapper(
contract.clone(),
chain_state.provider_address,
ADJUST_FEE_INTERVAL,
chain_eth_config.legacy_tx,
chain_eth_config.gas_limit,
chain_eth_config.min_profit_pct,
chain_eth_config.target_profit_pct,
chain_eth_config.max_profit_pct,
chain_eth_config.fee,
)
async move {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this async move , the await, and the clones above are unnecessary

adjust_fee_wrapper(
contract_for_fee,
provider_address,
ADJUST_FEE_INTERVAL,
config_for_fee.legacy_tx,
config_for_fee.gas_limit,
config_for_fee.min_profit_pct,
config_for_fee.target_profit_pct,
config_for_fee.max_profit_pct,
config_for_fee.fee,
config_for_fee.eip1559_fee_multiplier_pct,
)
.await
}
.in_current_span(),
);

Expand Down Expand Up @@ -807,8 +814,7 @@ pub async fn process_backlog(
tracing::info!("Backlog processed");
}

/// tracks the balance of the given address on the given chain
/// if there was an error, the function will just return
/// Track the balance of an account. If there was an error, the function will just return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change this comment

#[tracing::instrument(skip_all)]
pub async fn track_balance(
chain_id: String,
Expand Down Expand Up @@ -838,8 +844,7 @@ pub async fn track_balance(
.set(balance);
}

/// tracks the collected fees and the hashchain data of the given provider address on the given chain
/// if there is a error the function will just return
/// Track the provider info. If there is a error the function will just return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change this comment

#[tracing::instrument(skip_all)]
pub async fn track_provider(
chain_id: ChainId,
Expand Down Expand Up @@ -997,6 +1002,7 @@ pub async fn adjust_fee_wrapper(
target_profit_pct: u64,
max_profit_pct: u64,
min_fee_wei: u128,
eip1559_fee_multiplier_pct: u64,
) {
// The maximum balance of accrued fees + provider wallet balance. None if we haven't observed a value yet.
let mut high_water_pnl: Option<U256> = None;
Expand All @@ -1014,6 +1020,7 @@ pub async fn adjust_fee_wrapper(
min_fee_wei,
&mut high_water_pnl,
&mut sequence_number_of_last_fee_update,
eip1559_fee_multiplier_pct,
)
.in_current_span()
.await
Expand Down Expand Up @@ -1097,6 +1104,7 @@ pub async fn adjust_fee_if_necessary(
min_fee_wei: u128,
high_water_pnl: &mut Option<U256>,
sequence_number_of_last_fee_update: &mut Option<u64>,
eip1559_fee_multiplier_pct: u64,
) -> Result<()> {
let provider_info = contract
.get_provider_info(provider_address)
Expand All @@ -1109,9 +1117,14 @@ pub async fn adjust_fee_if_necessary(
}

// Calculate target window for the on-chain fee.
let max_callback_cost: u128 = estimate_tx_cost(contract.clone(), legacy_tx, gas_limit.into())
.await
.map_err(|e| anyhow!("Could not estimate transaction cost. error {:?}", e))?;
let max_callback_cost: u128 = estimate_tx_cost(
contract.clone(),
legacy_tx,
gas_limit.into(),
eip1559_fee_multiplier_pct,
)
.await
.map_err(|e| anyhow!("Could not estimate transaction cost. error {:?}", e))?;
let target_fee_min = std::cmp::max(
(max_callback_cost * (100 + u128::from(min_profit_pct))) / 100,
min_fee_wei,
Expand Down Expand Up @@ -1197,6 +1210,7 @@ pub async fn estimate_tx_cost(
contract: Arc<InstrumentedSignablePythContract>,
use_legacy_tx: bool,
gas_used: u128,
eip1559_fee_multiplier_pct: u64,
) -> Result<u128> {
let middleware = contract.client();

Expand All @@ -1212,7 +1226,11 @@ pub async fn estimate_tx_cost(
.estimate_eip1559_fees(Some(eip1559_default_estimator))
.await?;

(max_fee_per_gas + max_priority_fee_per_gas)
let multiplier = U256::from(eip1559_fee_multiplier_pct);
let base = max_fee_per_gas + max_priority_fee_per_gas;
let adjusted = (base * multiplier) / U256::from(100);

adjusted
.try_into()
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?
};
Expand Down
Loading