Skip to content

Commit ce098a8

Browse files
fix: address GitHub PR feedback - remove Option types and refactor transaction logic
Co-Authored-By: Tejas Badadare <[email protected]>
1 parent 68fd3f8 commit ce098a8

File tree

2 files changed

+79
-58
lines changed

2 files changed

+79
-58
lines changed

apps/fortuna/src/command/run.rs

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,31 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
120120
let keeper_config_base = config.keeper.clone();
121121
spawn(async move {
122122
loop {
123-
let keeper_config = if keeper_private_key_option.is_some() {
124-
Some(keeper_config_base.clone())
123+
let setup_result = if keeper_private_key_option.is_some() {
124+
setup_chain_and_run_keeper(
125+
provider_config.clone(),
126+
&chain_id,
127+
chain_config.clone(),
128+
keeper_metrics.clone(),
129+
keeper_config_base.clone(),
130+
chains.clone(),
131+
&secret_copy,
132+
history.clone(),
133+
rpc_metrics.clone(),
134+
)
135+
.await
125136
} else {
126-
None
137+
setup_chain_without_keeper(
138+
provider_config.clone(),
139+
&chain_id,
140+
chain_config.clone(),
141+
keeper_metrics.clone(),
142+
chains.clone(),
143+
&secret_copy,
144+
rpc_metrics.clone(),
145+
)
146+
.await
127147
};
128-
let setup_result = setup_chain_and_run_keeper(
129-
provider_config.clone(),
130-
&chain_id,
131-
chain_config.clone(),
132-
keeper_metrics.clone(),
133-
keeper_config,
134-
chains.clone(),
135-
&secret_copy,
136-
history.clone(),
137-
rpc_metrics.clone(),
138-
)
139-
.await;
140148
match setup_result {
141149
Ok(_) => {
142150
tracing::info!("Chain {} initialized successfully", chain_id);
@@ -180,7 +188,7 @@ async fn setup_chain_and_run_keeper(
180188
chain_id: &ChainId,
181189
chain_config: EthereumConfig,
182190
keeper_metrics: Arc<KeeperMetrics>,
183-
keeper_config: Option<KeeperConfig>,
191+
keeper_config: KeeperConfig,
184192
chains: Arc<RwLock<HashMap<ChainId, ApiBlockChainState>>>,
185193
secret_copy: &str,
186194
history: Arc<History>,
@@ -200,17 +208,42 @@ async fn setup_chain_and_run_keeper(
200208
chain_id.clone(),
201209
ApiBlockChainState::Initialized(state.clone()),
202210
);
203-
if let Some(keeper_config) = keeper_config {
204-
keeper::run_keeper_threads(
205-
keeper_config,
206-
chain_config,
207-
state,
208-
keeper_metrics.clone(),
209-
history,
210-
rpc_metrics.clone(),
211-
)
212-
.await?;
213-
}
211+
keeper::run_keeper_threads(
212+
keeper_config,
213+
chain_config,
214+
state,
215+
keeper_metrics.clone(),
216+
history,
217+
rpc_metrics.clone(),
218+
)
219+
.await?;
220+
Ok(())
221+
}
222+
223+
#[allow(clippy::too_many_arguments)]
224+
async fn setup_chain_without_keeper(
225+
provider_config: ProviderConfig,
226+
chain_id: &ChainId,
227+
chain_config: EthereumConfig,
228+
keeper_metrics: Arc<KeeperMetrics>,
229+
chains: Arc<RwLock<HashMap<ChainId, ApiBlockChainState>>>,
230+
secret_copy: &str,
231+
rpc_metrics: Arc<RpcMetrics>,
232+
) -> Result<()> {
233+
let state = setup_chain_state(
234+
&provider_config.address,
235+
secret_copy,
236+
provider_config.chain_sample_interval,
237+
chain_id,
238+
&chain_config,
239+
rpc_metrics.clone(),
240+
keeper_metrics.clone(),
241+
)
242+
.await?;
243+
chains.write().await.insert(
244+
chain_id.clone(),
245+
ApiBlockChainState::Initialized(state.clone()),
246+
);
214247
Ok(())
215248
}
216249

apps/fortuna/src/keeper/fee.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use {
77
},
88
anyhow::{anyhow, Result},
99
ethers::{
10-
middleware::Middleware,
10+
middleware::{Middleware, SignerMiddleware},
1111
signers::{LocalWallet, Signer},
1212
types::{Address, TransactionRequest, U256},
1313
},
14-
std::{str::FromStr, sync::Arc},
14+
std::sync::Arc,
1515
tokio::time::{self, Duration},
1616
tracing::{self, Instrument},
1717
};
@@ -117,7 +117,8 @@ pub async fn withdraw_fees_if_necessary(
117117
let contract_call = contract.withdraw_as_fee_manager(provider_address, fees);
118118
send_and_confirm(contract_call).await?;
119119

120-
let fee_manager_wallet = LocalWallet::from_str(&fee_manager_private_key)
120+
let fee_manager_wallet = fee_manager_private_key
121+
.parse::<LocalWallet>()
121122
.map_err(|e| anyhow!("Invalid fee manager private key: {:?}", e))?;
122123
let fee_manager_address = fee_manager_wallet.address();
123124
let keeper_address = wallet.address();
@@ -130,43 +131,30 @@ pub async fn withdraw_fees_if_necessary(
130131
);
131132

132133
let transfer_amount = U256::from(fees);
133-
let chain_id = provider
134-
.get_chainid()
135-
.await
136-
.map_err(|e| anyhow!("Failed to get chain ID: {:?}", e))?;
134+
let chain_id = provider.get_chainid().await?;
137135

138-
let gas_price = provider
139-
.get_gas_price()
140-
.await
141-
.map_err(|e| anyhow!("Failed to get gas price: {:?}", e))?;
142-
143-
let nonce = provider
144-
.get_transaction_count(fee_manager_address, None)
145-
.await
146-
.map_err(|e| anyhow!("Failed to get nonce: {:?}", e))?;
136+
let fee_manager_wallet = fee_manager_wallet.with_chain_id(chain_id.as_u64());
137+
let fee_manager_provider = SignerMiddleware::new(provider.clone(), fee_manager_wallet);
147138

148139
let tx = TransactionRequest::new()
149140
.to(keeper_address)
150141
.value(transfer_amount)
151-
.from(fee_manager_address)
152-
.gas(21000) // Standard ETH transfer gas limit
153-
.gas_price(gas_price)
154-
.nonce(nonce)
155-
.chain_id(chain_id.as_u64());
156-
157-
let typed_tx = tx.into();
158-
let signature = fee_manager_wallet
159-
.sign_transaction(&typed_tx)
160-
.await
161-
.map_err(|e| anyhow!("Failed to sign transfer transaction: {:?}", e))?;
142+
.gas(21000);
162143

163-
let signed_tx = typed_tx.rlp_signed(&signature);
164-
let tx_hash = provider
165-
.send_raw_transaction(signed_tx)
144+
let pending_tx = fee_manager_provider
145+
.send_transaction(tx, None)
166146
.await
167147
.map_err(|e| anyhow!("Failed to send transfer transaction: {:?}", e))?;
168148

169-
tracing::info!("Transfer transaction sent: {:?}", tx_hash);
149+
let receipt = pending_tx
150+
.await
151+
.map_err(|e| anyhow!("Failed to get transfer transaction receipt: {:?}", e))?
152+
.ok_or_else(|| anyhow!("Transfer transaction was dropped from mempool"))?;
153+
154+
tracing::info!(
155+
"Transfer transaction confirmed: {:?}",
156+
receipt.transaction_hash
157+
);
170158
}
171159
} else if keeper_balance < min_balance {
172160
// NOTE: This log message triggers a grafana alert. If you want to change the text, please change the alert also.

0 commit comments

Comments
 (0)