-
Notifications
You must be signed in to change notification settings - Fork 232
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
(chore): Fortuna refactor #2372
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8edd37c
extract middleware to new file
jayantk 072ed0b
whoops
jayantk 85c3992
move to new module
jayantk 40da2cd
missed files
jayantk 5ceec73
fix build
jayantk 77ba43a
fix imports
jayantk 0f05a0b
move file
jayantk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
pub(crate) mod eth_gas_oracle; | ||
pub(crate) mod ethereum; | ||
mod nonce_manager; | ||
pub(crate) mod reader; | ||
pub(crate) mod traced_client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod eth_gas_oracle; | ||
pub mod legacy_tx_middleware; | ||
pub mod nonce_manager; | ||
pub mod traced_client; | ||
pub mod utils; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use { | ||
axum::async_trait, | ||
ethers::{ | ||
middleware::{Middleware, MiddlewareError}, | ||
prelude::{BlockId, PendingTransaction, TransactionRequest}, | ||
types::transaction::eip2718::TypedTransaction, | ||
}, | ||
thiserror::Error, | ||
}; | ||
|
||
/// Middleware that converts a transaction into a legacy transaction if use_legacy_tx is true. | ||
/// We can not use TransformerMiddleware because keeper calls fill_transaction first which bypasses | ||
/// the transformer. | ||
#[derive(Clone, Debug)] | ||
pub struct LegacyTxMiddleware<M> { | ||
use_legacy_tx: bool, | ||
inner: M, | ||
} | ||
|
||
impl<M> LegacyTxMiddleware<M> { | ||
pub fn new(use_legacy_tx: bool, inner: M) -> Self { | ||
Self { | ||
use_legacy_tx, | ||
inner, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum LegacyTxMiddlewareError<M: Middleware> { | ||
#[error("{0}")] | ||
MiddlewareError(M::Error), | ||
} | ||
|
||
impl<M: Middleware> MiddlewareError for LegacyTxMiddlewareError<M> { | ||
type Inner = M::Error; | ||
|
||
fn from_err(src: M::Error) -> Self { | ||
LegacyTxMiddlewareError::MiddlewareError(src) | ||
} | ||
|
||
fn as_inner(&self) -> Option<&Self::Inner> { | ||
match self { | ||
LegacyTxMiddlewareError::MiddlewareError(e) => Some(e), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<M: Middleware> Middleware for LegacyTxMiddleware<M> { | ||
type Error = LegacyTxMiddlewareError<M>; | ||
type Provider = M::Provider; | ||
type Inner = M; | ||
fn inner(&self) -> &M { | ||
&self.inner | ||
} | ||
|
||
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>( | ||
&self, | ||
tx: T, | ||
block: Option<BlockId>, | ||
) -> std::result::Result<PendingTransaction<'_, Self::Provider>, Self::Error> { | ||
let mut tx = tx.into(); | ||
if self.use_legacy_tx { | ||
let legacy_request: TransactionRequest = tx.into(); | ||
tx = legacy_request.into(); | ||
} | ||
self.inner() | ||
.send_transaction(tx, block) | ||
.await | ||
.map_err(MiddlewareError::from_err) | ||
} | ||
|
||
async fn fill_transaction( | ||
&self, | ||
tx: &mut TypedTransaction, | ||
block: Option<BlockId>, | ||
) -> std::result::Result<(), Self::Error> { | ||
if self.use_legacy_tx { | ||
let legacy_request: TransactionRequest = (*tx).clone().into(); | ||
*tx = legacy_request.into(); | ||
} | ||
self.inner() | ||
.fill_transaction(tx, block) | ||
.await | ||
.map_err(MiddlewareError::from_err) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use { | ||
anyhow::{anyhow, Result}, | ||
ethers::{contract::ContractCall, middleware::Middleware}, | ||
std::sync::Arc, | ||
tracing, | ||
}; | ||
|
||
pub async fn send_and_confirm<A: Middleware>(contract_call: ContractCall<A, ()>) -> Result<()> { | ||
let call_name = contract_call.function.name.as_str(); | ||
let pending_tx = contract_call | ||
.send() | ||
.await | ||
.map_err(|e| anyhow!("Error submitting transaction({}) {:?}", call_name, e))?; | ||
|
||
let tx_result = pending_tx | ||
.await | ||
.map_err(|e| { | ||
anyhow!( | ||
"Error waiting for transaction({}) receipt: {:?}", | ||
call_name, | ||
e | ||
) | ||
})? | ||
.ok_or_else(|| { | ||
anyhow!( | ||
"Can't verify the transaction({}), probably dropped from mempool", | ||
call_name | ||
) | ||
})?; | ||
|
||
tracing::info!( | ||
transaction_hash = &tx_result.transaction_hash.to_string(), | ||
"Confirmed transaction({}). Receipt: {:?}", | ||
call_name, | ||
tx_result, | ||
); | ||
Ok(()) | ||
} | ||
|
||
/// Estimate the cost (in wei) of a transaction consuming gas_used gas. | ||
pub async fn estimate_tx_cost<T: Middleware + 'static>( | ||
middleware: Arc<T>, | ||
use_legacy_tx: bool, | ||
gas_used: u128, | ||
) -> Result<u128> { | ||
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))? | ||
} else { | ||
// This is not obvious but the implementation of estimate_eip1559_fees in ethers.rs | ||
// for a middleware that has a GasOracleMiddleware inside is to ignore the passed-in callback | ||
// and use whatever the gas oracle returns. | ||
let (max_fee_per_gas, max_priority_fee_per_gas) = | ||
middleware.estimate_eip1559_fees(None).await?; | ||
|
||
(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) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: the function signatures of this function and the one below changed very slightly so that they don't depend on the pyth-specific type declarations in the chain/ module.