Skip to content

Commit b4e8087

Browse files
bug: fix conversion from l1 tx to MaxTxFeesPerGas (#192)
1 parent 847a195 commit b4e8087

File tree

1 file changed

+95
-3
lines changed

1 file changed

+95
-3
lines changed

packages/adapters/eth/src/websocket/connection/estimation.rs

+95-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use services::types::L1Tx;
1010

1111
use crate::error::{Error, Result};
1212

13-
#[derive(Debug, Clone, Copy)]
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1414
pub struct MaxTxFeesPerGas {
1515
pub normal: u128,
1616
pub priority: u128,
@@ -21,8 +21,8 @@ impl<'a> From<&'a L1Tx> for MaxTxFeesPerGas {
2121
fn from(value: &'a L1Tx) -> Self {
2222
Self {
2323
normal: value.max_fee,
24-
priority: value.blob_fee,
25-
blob: value.priority_fee,
24+
priority: value.priority_fee,
25+
blob: value.blob_fee,
2626
}
2727
}
2828
}
@@ -113,3 +113,95 @@ fn estimate_max_priority_fee_per_gas(fee_history: &FeeHistory) -> Result<u128> {
113113

114114
Ok(std::cmp::max(median, EIP1559_MIN_PRIORITY_FEE))
115115
}
116+
117+
#[cfg(test)]
118+
mod tests {
119+
use super::*;
120+
121+
#[test]
122+
fn correctly_reads_fees_from_l1_tx() {
123+
// given
124+
let normal = 100;
125+
let blob = 50;
126+
let priority = 25;
127+
let l1tx = gen_l1_tx(normal, blob, priority);
128+
129+
// when
130+
let fees: MaxTxFeesPerGas = (&l1tx).into();
131+
132+
// then
133+
assert_eq!(
134+
fees,
135+
MaxTxFeesPerGas {
136+
normal,
137+
priority,
138+
blob,
139+
}
140+
);
141+
}
142+
143+
#[test]
144+
fn double_method_doubles_all_fees() {
145+
// given
146+
let fees = MaxTxFeesPerGas {
147+
normal: 100,
148+
priority: 50,
149+
blob: 25,
150+
};
151+
152+
// when
153+
let doubled = fees.double();
154+
155+
// then
156+
assert_eq!(
157+
doubled,
158+
MaxTxFeesPerGas {
159+
normal: 200,
160+
priority: 100,
161+
blob: 50,
162+
}
163+
);
164+
}
165+
166+
#[test]
167+
fn retain_max_method_returns_max_of_each_fee() {
168+
// given
169+
let fees1 = MaxTxFeesPerGas {
170+
normal: 100,
171+
priority: 50,
172+
blob: 25,
173+
};
174+
let fees2 = MaxTxFeesPerGas {
175+
normal: 150,
176+
priority: 40,
177+
blob: 30,
178+
};
179+
180+
// when
181+
let retained = fees1.retain_max(fees2);
182+
// normal: max(100, 150) = 150
183+
// priority: max(50, 40) = 50
184+
// blob: max(25, 30) = 30
185+
assert_eq!(
186+
retained,
187+
MaxTxFeesPerGas {
188+
normal: 150,
189+
priority: 50,
190+
blob: 30,
191+
},
192+
);
193+
}
194+
195+
fn gen_l1_tx(max_fee: u128, blob_fee: u128, priority_fee: u128) -> L1Tx {
196+
L1Tx {
197+
max_fee,
198+
blob_fee,
199+
priority_fee,
200+
id: None,
201+
hash: Default::default(),
202+
nonce: Default::default(),
203+
created_at: Default::default(),
204+
state: services::types::TransactionState::Pending,
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)