Skip to content

Commit

Permalink
chore:: optimize Gas Estimation and Fee Calculation using tokio::join! (
Browse files Browse the repository at this point in the history
#32)

This PR improves the performance of gas estimation and fee calculation
by running both tasks concurrently using tokio::join!. This change
reduces the overall execution time by allowing the two independent
operations to proceed in parallel, rather than sequentially
  • Loading branch information
malik672 authored Oct 14, 2024
1 parent 6c929ec commit aff5101
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions crates/wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,18 @@ where
request.chain_id = Some(self.chain_id());

// set gas limit
let estimate =
EthCall::estimate_gas_at(&self.inner.eth_api, request.clone(), BlockId::latest(), None)
.await
.map_err(Into::into)?;
let (estimate, base_fee) = tokio::join!(
EthCall::estimate_gas_at(&self.inner.eth_api, request.clone(), BlockId::latest(), None),
LoadFee::eip1559_fees(&self.inner.eth_api, None, None)
);

let estimate = estimate.map_err(Into::into)?;
if estimate >= U256::from(350_000) {
return Err(OdysseyWalletError::GasEstimateTooHigh { estimate: estimate.to() }.into());
}
request.gas = Some(estimate.to());

// set gas fees
let (base_fee, _) = LoadFee::eip1559_fees(&self.inner.eth_api, None, None)
.await
.map_err(|_| OdysseyWalletError::InvalidTransactionRequest)?;
let (base_fee, _) = base_fee.map_err(|_| OdysseyWalletError::InvalidTransactionRequest)?;
let max_priority_fee_per_gas = 1_000_000_000; // 1 gwei
request.max_fee_per_gas = Some(base_fee.to::<u128>() + max_priority_fee_per_gas);
request.max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
Expand Down

0 comments on commit aff5101

Please sign in to comment.