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

[fortuna] Automated fee adjustment based on gas prices #1708

Merged
merged 11 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add target fee parameter
  • Loading branch information
jayantk committed Jun 18, 2024
commit 65a539ef09f1eeb721c2bc0c05f2012fdb63e47b
3 changes: 2 additions & 1 deletion apps/fortuna/config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ chains:
# Configuration for dynamic fees under high gas prices. The keeper will set
# on-chain fees to make between [min_profit_pct, max_profit_pct] of the max callback
# cost in profit per transaction.
min_profit_pct: 10
min_profit_pct: 0
target_profit_pct: 20
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if we want this level of customisation. We can set this as the average of min_profit_pct and max_profit_pct.

max_profit_pct: 100

# Historical commitments -- delete this block for local development purposes
Expand Down
6 changes: 6 additions & 0 deletions apps/fortuna/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ pub struct EthereumConfig {

/// The minimum percentage profit to earn as a function of the callback cost.
/// For example, 20 means a profit of 20% over the cost of the callback.
/// The fee will be raised if the profit is less than this number.
pub min_profit_pct: u64,

/// The target percentage profit to earn as a function of the callback cost.
/// For example, 20 means a profit of 20% over the cost of the callback.
/// The fee will be set to this target whenever it falls outside the min/max bounds.
pub target_profit_pct: u64,

/// The maximum percentage profit to earn as a function of the callback cost.
/// For example, 100 means a profit of 100% over the cost of the callback.
/// The fee will be lowered if it is more profitable than specified here.
Expand Down
39 changes: 25 additions & 14 deletions apps/fortuna/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub async fn run_keeper_threads(
chain_eth_config.legacy_tx,
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe just pass the whole config object down?

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,
)
Expand Down Expand Up @@ -960,14 +961,15 @@ pub async fn withdraw_fees_if_necessary(
Ok(())
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe move this logic to a separate module? we can probably have a separate module for each of these maintenance threads

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i would rather do this separately and refactor all of the similar functions here so we don't end up in an inconsistent state

#[tracing::instrument(name = "adjust_fee", skip_all, fields())]
#[tracing::instrument(name = "adjust_fee", skip_all)]
pub async fn adjust_fee_wrapper(
contract: Arc<InstrumentedSignablePythContract>,
provider_address: Address,
poll_interval: Duration,
legacy_tx: bool,
gas_limit: u64,
min_profit_pct: u64,
target_profit_pct: u64,
max_profit_pct: u64,
min_fee: u128,
) {
Expand All @@ -982,6 +984,7 @@ pub async fn adjust_fee_wrapper(
legacy_tx,
gas_limit,
min_profit_pct,
target_profit_pct,
max_profit_pct,
min_fee,
&mut high_water_pnl,
Expand All @@ -1007,13 +1010,15 @@ pub async fn adjust_fee_wrapper(
/// - either the fee is increasing or the keeper is earning a profit -- i.e., fees only decrease when the keeper is profitable
/// - at least one random number has been requested since the last fee update
///
/// These conditions are designed to prevent the
/// These conditions are intended to make sure that the keeper is profitable while also minimizing the number of fee
/// update transactions.
pub async fn adjust_fee_if_necessary(
contract: Arc<InstrumentedSignablePythContract>,
provider_address: Address,
legacy_tx: bool,
gas_limit: u64,
min_profit_pct: u64,
target_profit_pct: u64,
max_profit_pct: u64,
min_fee: u128,
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe rename this to fixed_min_fee to avoid confusion

high_water_pnl: &mut Option<U256>,
Expand All @@ -1033,10 +1038,14 @@ pub async fn adjust_fee_if_necessary(
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 target_fee = std::cmp::max(
let target_fee_min = std::cmp::max(
(max_callback_cost * (100 + u128::from(min_profit_pct))) / 100,
min_fee,
);
let target_fee = std::cmp::max(
(max_callback_cost * (100 + u128::from(target_profit_pct))) / 100,
min_fee,
);
let target_fee_max = std::cmp::max(
(max_callback_cost * (100 + u128::from(max_profit_pct))) / 100,
min_fee,
Expand Down Expand Up @@ -1067,7 +1076,7 @@ pub async fn adjust_fee_if_necessary(

let provider_fee: u128 = provider_info.fee_in_wei;
if is_chain_active
&& ((provider_fee > target_fee_max && can_reduce_fees) || provider_fee < target_fee)
&& ((provider_fee > target_fee_max && can_reduce_fees) || provider_fee < target_fee_min)
{
tracing::info!(
"Adjusting fees. Current: {:?} Target: {:?}",
Expand Down Expand Up @@ -1096,9 +1105,11 @@ pub async fn adjust_fee_if_necessary(
*sequence_number_of_last_fee_update = Some(provider_info.sequence_number);
} else {
tracing::info!(
"Skipping fee adjustment. Current: {:?} Target: {:?} Current Sequence Number: {:?} Last updated sequence number {:?} Current pnl: {:?} High water pnl: {:?}",
"Skipping fee adjustment. Current: {:?} Target: {:?} [{:?}, {:?}] Current Sequence Number: {:?} Last updated sequence number {:?} Current pnl: {:?} High water pnl: {:?}",
provider_fee,
target_fee,
target_fee_min,
target_fee_max,
provider_info.sequence_number,
sequence_number_of_last_fee_update,
current_pnl,
Expand Down Expand Up @@ -1132,22 +1143,22 @@ pub async fn estimate_tx_cost(
) -> Result<u128> {
let middleware = contract.client();

if use_legacy_tx {
let gas_price: u128 = middleware
let gas_price: u128 = if use_legacy_tx {
middleware
.get_gas_price()
.await
.map_err(|e| anyhow!("Failed to fetch gas price. error: {:?}", e))?
.try_into()
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?;

Ok(gas_used * gas_price)
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?
} else {
let (max_fee_per_gas, max_priority_fee_per_gas) = middleware
.estimate_eip1559_fees(Some(eip1559_default_estimator))
.await?;
let gas_price: u128 = (max_fee_per_gas + max_priority_fee_per_gas)

(max_fee_per_gas + max_priority_fee_per_gas)
.try_into()
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?;
Ok(gas_price * gas_used)
}
.map_err(|e| anyhow!("gas price doesn't fit into 128 bits. error: {:?}", e))?
};

Ok(gas_price * gas_used)
}
Loading