|
1 | 1 | use std::str::FromStr; |
| 2 | +use std::sync::Arc; |
2 | 3 |
|
| 4 | +use crate::{ |
| 5 | + config::ECDSAConfig, |
| 6 | + retry::{ |
| 7 | + batcher_retryables::{get_current_nonce_retryable, get_gas_price_retryable}, |
| 8 | + retry_function, |
| 9 | + }, |
| 10 | +}; |
| 11 | +use aligned_sdk::core::constants::{ |
| 12 | + DEFAULT_BACKOFF_FACTOR, DEFAULT_MAX_RETRIES, DEFAULT_MIN_RETRY_DELAY, |
| 13 | + GAS_PRICE_INCREMENT_PERCENTAGE_PER_ITERATION, OVERRIDE_GAS_PRICE_PERCENTAGE_MULTIPLIER, |
| 14 | + PERCENTAGE_DIVIDER, |
| 15 | +}; |
| 16 | +use ethers::prelude::*; |
3 | 17 | use ethers::providers::{Http, Provider}; |
| 18 | +use log::error; |
4 | 19 |
|
5 | | -pub(crate) const GAS_MULTIPLIER: f64 = 1.125; // Multiplier for the gas price for gas escalator |
6 | | -pub(crate) const GAS_ESCALATOR_INTERVAL: u64 = 12; // Time in seconds between gas escalations |
| 20 | +use super::payment_service::SignerMiddlewareT; |
7 | 21 |
|
8 | 22 | pub fn get_provider(eth_rpc_url: String) -> Result<Provider<Http>, anyhow::Error> { |
9 | 23 | let provider = Http::from_str(eth_rpc_url.as_str()) |
10 | 24 | .map_err(|e| anyhow::Error::msg(format!("Failed to create provider: {}", e)))?; |
11 | 25 | Ok(Provider::new(provider)) |
12 | 26 | } |
| 27 | + |
| 28 | +pub async fn get_batcher_signer( |
| 29 | + provider: Provider<Http>, |
| 30 | + ecdsa_config: ECDSAConfig, |
| 31 | +) -> anyhow::Result<Arc<SignerMiddlewareT>> { |
| 32 | + let chain_id = provider.get_chainid().await?; |
| 33 | + |
| 34 | + // get private key from keystore |
| 35 | + let wallet = Wallet::decrypt_keystore( |
| 36 | + &ecdsa_config.private_key_store_path, |
| 37 | + &ecdsa_config.private_key_store_password, |
| 38 | + )? |
| 39 | + .with_chain_id(chain_id.as_u64()); |
| 40 | + |
| 41 | + let signer = Arc::new(SignerMiddleware::new(provider, wallet)); |
| 42 | + Ok(signer) |
| 43 | +} |
| 44 | + |
| 45 | +/// Calculates an increased gas price for retrying a transaction override. |
| 46 | +/// The gas price rises with each retry by applying a multiplier based on the iteration count. |
| 47 | +pub fn calculate_bumped_gas_price( |
| 48 | + previous_gas_price: U256, |
| 49 | + current_gas_price: U256, |
| 50 | + iteration: usize, |
| 51 | +) -> U256 { |
| 52 | + let override_gas_multiplier = U256::from(OVERRIDE_GAS_PRICE_PERCENTAGE_MULTIPLIER) |
| 53 | + + (GAS_PRICE_INCREMENT_PERCENTAGE_PER_ITERATION * iteration); |
| 54 | + let bumped_previous_gas_price = |
| 55 | + previous_gas_price * override_gas_multiplier / U256::from(PERCENTAGE_DIVIDER); |
| 56 | + |
| 57 | + let bumped_current_gas_price = |
| 58 | + current_gas_price * override_gas_multiplier / U256::from(PERCENTAGE_DIVIDER); |
| 59 | + // Return the maximum of the previous and current gas prices |
| 60 | + // to avoid sending a transaction with a gas price lower than the previous one. |
| 61 | + bumped_current_gas_price.max(bumped_previous_gas_price) |
| 62 | +} |
| 63 | + |
| 64 | +pub async fn get_current_nonce( |
| 65 | + eth_http_provider: &Provider<Http>, |
| 66 | + eth_http_provider_fallback: &Provider<Http>, |
| 67 | + addr: H160, |
| 68 | +) -> Result<U256, ProviderError> { |
| 69 | + retry_function( |
| 70 | + || get_current_nonce_retryable(eth_http_provider, eth_http_provider_fallback, addr), |
| 71 | + DEFAULT_MIN_RETRY_DELAY, |
| 72 | + DEFAULT_BACKOFF_FACTOR, |
| 73 | + DEFAULT_MAX_RETRIES, |
| 74 | + ) |
| 75 | + .await |
| 76 | + .map_err(|e| { |
| 77 | + error!("Could't get nonce: {:?}", e); |
| 78 | + e.inner() |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +/// Gets the current gas price from Ethereum using exponential backoff. |
| 83 | +pub async fn get_gas_price( |
| 84 | + eth_http_provider: &Provider<Http>, |
| 85 | + eth_http_provider_fallback: &Provider<Http>, |
| 86 | +) -> Result<U256, ProviderError> { |
| 87 | + retry_function( |
| 88 | + || get_gas_price_retryable(eth_http_provider, eth_http_provider_fallback), |
| 89 | + DEFAULT_MIN_RETRY_DELAY, |
| 90 | + DEFAULT_BACKOFF_FACTOR, |
| 91 | + DEFAULT_MAX_RETRIES, |
| 92 | + ) |
| 93 | + .await |
| 94 | + .map_err(|e| { |
| 95 | + error!("Could't get gas price: {e}"); |
| 96 | + e.inner() |
| 97 | + }) |
| 98 | +} |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_get_bumped_gas_price_initial_iteration() { |
| 105 | + let previous_gas_price = U256::from(1000); |
| 106 | + let current_gas_price = U256::from(1200); |
| 107 | + let iteration = 0; |
| 108 | + |
| 109 | + let expected = U256::from(1440); // (1200 * (120 + 0)) / 100 |
| 110 | + |
| 111 | + assert_eq!( |
| 112 | + calculate_bumped_gas_price(previous_gas_price, current_gas_price, iteration), |
| 113 | + expected |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_get_bumped_gas_price_with_iteration() { |
| 119 | + let previous_gas_price = U256::from(1000); |
| 120 | + let current_gas_price = U256::from(1200); |
| 121 | + let iteration = 2; |
| 122 | + |
| 123 | + let expected = U256::from(1560); // (1200 * (120 + 10) / 100 |
| 124 | + |
| 125 | + assert_eq!( |
| 126 | + calculate_bumped_gas_price(previous_gas_price, current_gas_price, iteration), |
| 127 | + expected |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_get_bumped_gas_price_previous_higher() { |
| 133 | + let previous_gas_price = U256::from(1500); |
| 134 | + let current_gas_price = U256::from(1200); |
| 135 | + let iteration = 1; |
| 136 | + |
| 137 | + let expected = U256::from(1875); // (1500 * (120 + 5) / 100 |
| 138 | + |
| 139 | + assert_eq!( |
| 140 | + calculate_bumped_gas_price(previous_gas_price, current_gas_price, iteration), |
| 141 | + expected |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
0 commit comments