forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurgent_filler.rs
More file actions
122 lines (107 loc) · 4.27 KB
/
Copy pathurgent_filler.rs
File metadata and controls
122 lines (107 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Rolls a custom filler that fetches priority fee information from an external endpoint and fills
//! the EIP-1559 gas fields for urgent inclusion.
use eyre::Result;
use alloy::{
consensus::Transaction,
network::{Network, TransactionBuilder},
primitives::{Address, U256},
providers::{
fillers::{FillerControlFlow, TxFiller},
Provider, ProviderBuilder, SendableTx,
},
rpc::types::TransactionRequest,
transports::{RpcError, TransportErrorKind, TransportResult},
};
use reqwest::Client;
/// The custom filler that fetches gas prices from an external API
/// and fills the EIP-1559 gas fields for urgent inclusion.
#[derive(Clone, Debug, Default)]
pub struct UrgentQueue {
client: Client,
}
impl UrgentQueue {
/// Instantiate a new [`UrgentQueue`] filler.
pub fn new() -> Self {
Self { client: Client::new() }
}
}
/// The fillable type for the [`UrgentQueue`] filler.
#[derive(Debug)]
pub struct GasPriceFillable {
max_fee_per_gas: u128,
max_priority_fee_per_gas: u128,
}
impl<N: Network> TxFiller<N> for UrgentQueue {
type Fillable = GasPriceFillable;
// Implements the status check for the filler.
// indicating whether the filler is ready to fill in the transaction request, or if it is
// missing required properties.
fn status(&self, tx: &<N as Network>::TransactionRequest) -> FillerControlFlow {
if tx.max_fee_per_gas().is_some() && tx.max_priority_fee_per_gas().is_some() {
FillerControlFlow::Finished
} else {
FillerControlFlow::Ready
}
}
fn fill_sync(&self, _tx: &mut SendableTx<N>) {}
// Fills in the transaction request with properties from GasFillable
async fn fill(
&self,
fillable: Self::Fillable,
mut tx: SendableTx<N>,
) -> TransportResult<SendableTx<N>> {
if let Some(builder) = tx.as_mut_builder() {
println!("Filling transaction with gas prices from Blocknative");
builder.set_max_fee_per_gas(fillable.max_fee_per_gas);
builder.set_max_priority_fee_per_gas(fillable.max_priority_fee_per_gas);
} else {
panic!("Expected a builder");
}
Ok(tx)
}
// Prepares the gas fees by fetching the blocknative API.
async fn prepare<P>(
&self,
_provider: &P,
_tx: &<N as Network>::TransactionRequest,
) -> TransportResult<Self::Fillable>
where
P: Provider<N>,
{
println!("Fetching gas prices from Blocknative");
let data =
match self.client.get("https://api.blocknative.com/gasprices/blockprices").send().await
{
Ok(res) => res,
Err(e) => {
return Err(RpcError::Transport(TransportErrorKind::Custom(Box::new(
std::io::Error::other(format!("Failed to fetch gas price, {e}")),
))));
}
};
let body = data.text().await.unwrap();
let json = serde_json::from_str::<serde_json::Value>(&body).unwrap();
let prices = &json["blockPrices"][0]["estimatedPrices"][0];
let max_fee_per_gas = (prices["maxFeePerGas"].as_f64().unwrap() * 1e9) as u128;
let max_priority_fee_per_gas =
(prices["maxPriorityFeePerGas"].as_f64().unwrap() * 1e9) as u128;
let fillable = GasPriceFillable { max_fee_per_gas, max_priority_fee_per_gas };
Ok(fillable)
}
}
#[tokio::main]
async fn main() -> Result<()> {
// Instantiate the provider with the UrgentQueue filler
let provider =
ProviderBuilder::new().filler(UrgentQueue::default()).connect_anvil_with_wallet();
let bob = Address::from([0x42; 20]);
let tx = TransactionRequest::default().with_to(bob).with_value(U256::from(1));
let bob_balance_before = provider.get_balance(bob).await?;
let res = provider.send_transaction(tx).await?.get_receipt().await?;
let bob_balance_after = provider.get_balance(bob).await?;
println!("Balance before: {bob_balance_before}\nBalance after: {bob_balance_after}");
let tx = provider.get_transaction_by_hash(res.transaction_hash).await?.unwrap();
println!("Max fee per gas: {:?}", tx.max_fee_per_gas());
println!("Max priority fee per gas: {:?}", tx.max_priority_fee_per_gas());
Ok(())
}