Skip to content

Commit

Permalink
fix main errors (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: supernovahs <supernovahs@proton.me>
  • Loading branch information
supernovahs and supernovahs authored Aug 19, 2024
1 parent ddb0136 commit ecd79e0
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions crates/chainio/txmanager/src/simple_tx_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_primitives::Address;
use alloy_provider::{PendingTransactionBuilder, Provider, ProviderBuilder, RootProvider};
use alloy_rpc_types_eth::{TransactionInput, TransactionReceipt, TransactionRequest};
use alloy_signer_local::PrivateKeySigner;
use eigen_logging::{logger::Logger, tracing_logger::TracingLogger};
use eigen_logging::logger::SharedLogger;
use eigen_signer::signer::Config;
use k256::ecdsa::SigningKey;
use reqwest::Url;
Expand All @@ -29,14 +29,14 @@ pub enum TxManagerError {
InvalidUrlError,
}

pub struct SimpleTxManager<'log> {
logger: &'log TracingLogger,
pub struct SimpleTxManager {
logger: SharedLogger,
gas_limit_multiplier: f64,
private_key: String,
provider: RootProvider<Transport>,
}

impl<'log> SimpleTxManager<'log> {
impl SimpleTxManager {
/// Creates a new SimpleTxManager.
///
/// # Arguments
Expand All @@ -54,13 +54,13 @@ impl<'log> SimpleTxManager<'log> {
///
/// - If the URL is invalid.
pub fn new(
logger: &'log TracingLogger,
logger: SharedLogger,
gas_limit_multiplier: f64,
private_key: &str,
rpc_url: &str,
) -> Result<SimpleTxManager<'log>, TxManagerError> {
) -> Result<SimpleTxManager, TxManagerError> {
let url = Url::parse(rpc_url)
.inspect_err(|err| logger.error("Failed to parse url", &[err]))
.inspect_err(|err| logger.error("Failed to parse url", &err.to_string()))
.map_err(|_| TxManagerError::InvalidUrlError)?;
let provider = ProviderBuilder::new().on_http(url);
Ok(SimpleTxManager {
Expand All @@ -82,7 +82,10 @@ impl<'log> SimpleTxManager<'log> {
/// - If the private key is invalid.
pub fn get_address(&self) -> Result<Address, TxManagerError> {
let private_key_signing_key = SigningKey::from_slice(self.private_key.as_bytes())
.inspect_err(|err| self.logger.error("Failed to parse private key", &[err]))
.inspect_err(|err| {
self.logger
.error("Failed to parse private key", &err.to_string())
})
.map_err(|_| TxManagerError::AddressError)?;
Ok(Address::from_private_key(&private_key_signing_key))
}
Expand All @@ -94,7 +97,10 @@ impl<'log> SimpleTxManager<'log> {
fn create_local_signer(&self) -> Result<PrivateKeySigner, TxManagerError> {
let config = Config::PrivateKey(self.private_key.clone());
Config::signer_from_config(config)
.inspect_err(|err| self.logger.error("Failed to create signer", &[err]))
.inspect_err(|err| {
self.logger
.error("Failed to create signer", &err.to_string())
})
.map_err(|_| TxManagerError::SignerError)
}

Expand All @@ -115,12 +121,12 @@ impl<'log> SimpleTxManager<'log> {
tx: &mut TransactionRequest,
) -> Result<TransactionReceipt, TxManagerError> {
// Estimating gas and nonce
self.logger.debug("Estimating gas and nonce", &[()]);
self.logger.debug("Estimating gas and nonce", "");

let tx = self
.estimate_gas_and_nonce(tx)
.await
.inspect_err(|err| self.logger.error("Failed to estimate gas", &[err]))?;
let tx = self.estimate_gas_and_nonce(tx).await.inspect_err(|err| {
self.logger
.error("Failed to estimate gas", &err.to_string())
})?;

let signer = self.create_local_signer()?;
let wallet = EthereumWallet::from(signer);
Expand All @@ -130,7 +136,7 @@ impl<'log> SimpleTxManager<'log> {
.await
.inspect_err(|err| {
self.logger
.error("Failed to build and sign transaction", &[err])
.error("Failed to build and sign transaction", &err.to_string())
})
.map_err(|_| TxManagerError::SendTxError)?;

Expand All @@ -139,12 +145,13 @@ impl<'log> SimpleTxManager<'log> {
.provider
.send_transaction(signed_tx.into())
.await
.inspect_err(|err| self.logger.error("Failed to get receipt", &[err]))
.inspect_err(|err| self.logger.error("Failed to get receipt", &err.to_string()))
.map_err(|_| TxManagerError::SendTxError)?;

self.logger
.debug("Transaction sent. Pending transaction: ", &[&pending_tx]);

self.logger.debug(
"Transaction sent. Pending transaction: ",
&pending_tx.tx_hash().to_string(),
);
// wait for the transaction to be mined
SimpleTxManager::wait_for_receipt(self, pending_tx).await
}
Expand Down Expand Up @@ -173,7 +180,7 @@ impl<'log> SimpleTxManager<'log> {
let gas_tip_cap = self.provider.get_max_priority_fee_per_gas().await
.inspect_err(|err|
self.logger.info("eth_maxPriorityFeePerGas is unsupported by current backend, using fallback gasTipCap",
&[err]))
&err.to_string()))
.unwrap_or(FALLBACK_GAS_TIP_CAP);

let header = self
Expand All @@ -184,10 +191,7 @@ impl<'log> SimpleTxManager<'log> {
.flatten()
.map(|block| block.header)
.ok_or(TxManagerError::SendTxError)
.inspect_err(|_| {
self.logger
.error("Failed to get latest block header", &[()])
})?;
.inspect_err(|_| self.logger.error("Failed to get latest block header", ""))?;

// 2*baseFee + gas_tip_cap makes sure that the tx remains includeable for 6 consecutive 100% full blocks.
// see https://www.blocknative.com/blog/eip-1559-fees
Expand Down Expand Up @@ -260,7 +264,7 @@ impl<'log> SimpleTxManager<'log> {
pending_tx
.get_receipt()
.await
.inspect_err(|err| self.logger.error("Failed to get receipt", &[err]))
.inspect_err(|err| self.logger.error("Failed to get receipt", &err.to_string()))
.map_err(|_| TxManagerError::WaitForReceiptError)
}
}
Expand Down

0 comments on commit ecd79e0

Please sign in to comment.