@@ -11,6 +11,7 @@ use alloy::{
11
11
use delegate:: delegate;
12
12
use serde:: Deserialize ;
13
13
use services:: {
14
+ state_committer:: port:: l1:: Priority ,
14
15
types:: {
15
16
BlockSubmissionTx , Fragment , FragmentsSubmitted , L1Height , L1Tx , NonEmpty ,
16
17
TransactionResponse , U256 ,
@@ -110,6 +111,7 @@ impl services::state_committer::port::l1::Api for WebsocketClient {
110
111
& self ,
111
112
fragments: NonEmpty <Fragment >,
112
113
previous_tx: Option <services:: types:: L1Tx >,
114
+ priority: Priority
113
115
) -> Result <( L1Tx , FragmentsSubmitted ) >;
114
116
}
115
117
}
@@ -158,6 +160,58 @@ impl L1Keys {
158
160
pub struct TxConfig {
159
161
pub tx_max_fee : u128 ,
160
162
pub send_tx_request_timeout : Duration ,
163
+ pub acceptable_priority_fee_percentage : AcceptablePriorityFeePercentages ,
164
+ }
165
+
166
+ #[ cfg( feature = "test-helpers" ) ]
167
+ impl Default for TxConfig {
168
+ fn default ( ) -> Self {
169
+ Self {
170
+ tx_max_fee : u128:: MAX ,
171
+ send_tx_request_timeout : Duration :: from_secs ( 10 ) ,
172
+ acceptable_priority_fee_percentage : AcceptablePriorityFeePercentages :: default ( ) ,
173
+ }
174
+ }
175
+ }
176
+
177
+ #[ derive( Debug , Clone , Copy ) ]
178
+ pub struct AcceptablePriorityFeePercentages {
179
+ min : f64 ,
180
+ max : f64 ,
181
+ }
182
+
183
+ #[ cfg( feature = "test-helpers" ) ]
184
+ impl Default for AcceptablePriorityFeePercentages {
185
+ fn default ( ) -> Self {
186
+ Self :: new ( 20. , 20. ) . expect ( "valid reward percentile range" )
187
+ }
188
+ }
189
+
190
+ impl AcceptablePriorityFeePercentages {
191
+ pub fn new ( min : f64 , max : f64 ) -> Result < Self > {
192
+ if min > max {
193
+ return Err ( services:: Error :: Other (
194
+ "min reward percentile must be less than or equal to max reward percentile"
195
+ . to_string ( ) ,
196
+ ) ) ;
197
+ }
198
+
199
+ if min <= 0.0 || max > 100.0 {
200
+ return Err ( services:: Error :: Other (
201
+ "reward percentiles must be > 0 and <= 100" . to_string ( ) ,
202
+ ) ) ;
203
+ }
204
+
205
+ Ok ( Self { min, max } )
206
+ }
207
+
208
+ pub fn apply ( & self , priority : Priority ) -> f64 {
209
+ let min = self . min ;
210
+
211
+ let increase = ( self . max - min) * priority. get ( ) / 100. ;
212
+
213
+ ( min + increase) . min ( self . max )
214
+ }
161
215
}
162
216
163
217
// This trait is needed because you cannot write `dyn TraitA + TraitB` except when TraitB is an
@@ -268,14 +322,7 @@ impl WebsocketClient {
268
322
. map ( |signer| TxSigner :: address ( & signer) ) ;
269
323
let contract_caller_address = TxSigner :: address ( & signers. main ) ;
270
324
271
- let provider = WsConnection :: connect (
272
- url,
273
- contract_address,
274
- signers,
275
- tx_config. tx_max_fee ,
276
- tx_config. send_tx_request_timeout ,
277
- )
278
- . await ?;
325
+ let provider = WsConnection :: connect ( url, contract_address, signers, tx_config) . await ?;
279
326
280
327
Ok ( Self {
281
328
inner : HealthTrackingMiddleware :: new ( provider, unhealthy_after_n_errors) ,
@@ -327,10 +374,11 @@ impl WebsocketClient {
327
374
& self ,
328
375
fragments : NonEmpty < Fragment > ,
329
376
previous_tx : Option < services:: types:: L1Tx > ,
377
+ priority : Priority ,
330
378
) -> Result < ( L1Tx , FragmentsSubmitted ) > {
331
379
Ok ( self
332
380
. inner
333
- . submit_state_fragments ( fragments, previous_tx)
381
+ . submit_state_fragments ( fragments, previous_tx, priority )
334
382
. await ?)
335
383
}
336
384
@@ -366,6 +414,7 @@ impl RegistersMetrics for WebsocketClient {
366
414
#[ cfg( test) ]
367
415
mod tests {
368
416
use pretty_assertions:: assert_eq;
417
+ use services:: state_committer:: port:: l1:: Priority ;
369
418
370
419
use super :: L1Key ;
371
420
@@ -392,4 +441,40 @@ mod tests {
392
441
// then
393
442
assert_eq ! ( key, L1Key :: Kms ( "0x1234" . to_owned( ) ) ) ;
394
443
}
444
+
445
+ #[ test]
446
+ fn lowest_priority_gives_min_priority_fee_perc ( ) {
447
+ // given
448
+ let sut = super :: AcceptablePriorityFeePercentages :: new ( 20. , 40. ) . unwrap ( ) ;
449
+
450
+ // when
451
+ let fee_perc = sut. apply ( Priority :: MIN ) ;
452
+
453
+ // then
454
+ assert_eq ! ( fee_perc, 20. ) ;
455
+ }
456
+
457
+ #[ test]
458
+ fn medium_priority_gives_middle_priority_fee_perc ( ) {
459
+ // given
460
+ let sut = super :: AcceptablePriorityFeePercentages :: new ( 20. , 40. ) . unwrap ( ) ;
461
+
462
+ // when
463
+ let fee_perc = sut. apply ( Priority :: new ( 50. ) . unwrap ( ) ) ;
464
+
465
+ // then
466
+ assert_eq ! ( fee_perc, 30. ) ;
467
+ }
468
+
469
+ #[ test]
470
+ fn highest_priority_gives_max_priority_fee_perc ( ) {
471
+ // given
472
+ let sut = super :: AcceptablePriorityFeePercentages :: new ( 20. , 40. ) . unwrap ( ) ;
473
+
474
+ // when
475
+ let fee_perc = sut. apply ( Priority :: MAX ) ;
476
+
477
+ // then
478
+ assert_eq ! ( fee_perc, 40. ) ;
479
+ }
395
480
}
0 commit comments