@@ -103,6 +103,23 @@ pub enum VerificationResult {
103
103
UnableToSpoof ,
104
104
}
105
105
106
+ #[ derive( Debug ) ]
107
+ pub enum OpportunityRemovalReason {
108
+ Expired ,
109
+ Invalid ( anyhow:: Error ) ,
110
+ }
111
+
112
+ impl From < OpportunityRemovalReason > for crate :: models:: OpportunityRemovalReason {
113
+ fn from ( val : OpportunityRemovalReason ) -> Self {
114
+ match val {
115
+ OpportunityRemovalReason :: Expired => crate :: models:: OpportunityRemovalReason :: Expired ,
116
+ OpportunityRemovalReason :: Invalid ( _) => {
117
+ crate :: models:: OpportunityRemovalReason :: Invalid
118
+ }
119
+ }
120
+ }
121
+ }
122
+
106
123
pub async fn get_weth_address (
107
124
adapter_contract : Address ,
108
125
provider : Provider < TracedClient > ,
@@ -281,6 +298,7 @@ pub async fn verify_opportunity(
281
298
}
282
299
Ok ( VerificationResult :: Success )
283
300
}
301
+
284
302
impl From < ExecutionParamsWithSignature > for eip712:: TypedData {
285
303
fn from ( val : ExecutionParamsWithSignature ) -> Self {
286
304
let params = val. params ;
@@ -494,31 +512,36 @@ pub async fn make_adapter_calldata(
494
512
const MAX_STALE_OPPORTUNITY_MICROS : i128 = 60_000_000 ;
495
513
496
514
/// Verify an opportunity is still valid by checking staleness and simulating the execution call and checking the result
497
- /// Returns Ok(()) if the opportunity is still valid
515
+ /// Returns None if the opportunity is still valid and Some(OpportunityRemovalReason) if not
498
516
///
499
517
/// # Arguments
500
518
///
501
519
/// * `opportunity`: opportunity to verify
502
520
/// * `store`: server store
503
- async fn verify_with_store ( opportunity : Opportunity , store : & Store ) -> Result < ( ) > {
521
+ async fn verify_with_store (
522
+ opportunity : Opportunity ,
523
+ store : & Store ,
524
+ ) -> Option < OpportunityRemovalReason > {
504
525
let OpportunityParams :: V1 ( params) = opportunity. params ;
505
526
let chain_store = store
506
527
. chains
507
528
. get ( & params. chain_id )
508
- . ok_or ( anyhow ! ( " Chain not found: {}" , params . chain_id ) ) ? ;
529
+ . expect ( "Opportunity Chain not found in store" ) ;
509
530
let relayer = store. relayer . address ( ) ;
510
531
match verify_opportunity ( params. clone ( ) , chain_store, relayer) . await {
511
- Ok ( VerificationResult :: Success ) => Ok ( ( ) ) ,
532
+ Ok ( VerificationResult :: Success ) => None ,
512
533
Ok ( VerificationResult :: UnableToSpoof ) => {
513
- let current_time =
514
- SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) ?. as_micros ( ) as UnixTimestampMicros ;
534
+ let current_time = SystemTime :: now ( )
535
+ . duration_since ( UNIX_EPOCH )
536
+ . expect ( "Current time older than 1970!" )
537
+ . as_micros ( ) as UnixTimestampMicros ;
515
538
if current_time - opportunity. creation_time > MAX_STALE_OPPORTUNITY_MICROS {
516
- Err ( anyhow ! ( "Opportunity is stale and unverifiable" ) )
539
+ Some ( OpportunityRemovalReason :: Expired )
517
540
} else {
518
- Ok ( ( ) )
541
+ None
519
542
}
520
543
}
521
- Err ( e) => Err ( e ) ,
544
+ Err ( e) => Some ( OpportunityRemovalReason :: Invalid ( e ) ) ,
522
545
}
523
546
}
524
547
@@ -540,20 +563,14 @@ pub async fn run_verification_loop(store: Arc<Store>) -> Result<()> {
540
563
for ( _permission_key, opportunities) in all_opportunities. iter( ) {
541
564
// check each of the opportunities for this permission key for validity
542
565
for opportunity in opportunities. iter( ) {
543
- match verify_with_store( opportunity. clone( ) , & store) . await {
544
- Ok ( _) => { }
545
- Err ( e) => {
546
- tracing:: info!(
547
- "Removing Opportunity {} with failed verification: {}" ,
548
- opportunity. id,
549
- e
550
- ) ;
551
- match store. remove_opportunity( opportunity) . await {
552
- Ok ( _) => { }
553
- Err ( e) => {
554
- tracing:: error!( "Failed to remove opportunity: {}" , e) ;
555
- }
556
- }
566
+ if let Some ( reason) = verify_with_store( opportunity. clone( ) , & store) . await {
567
+ tracing:: info!(
568
+ "Removing Opportunity {} for reason {:?}" ,
569
+ opportunity. id,
570
+ reason
571
+ ) ;
572
+ if let Err ( e) = store. remove_opportunity( opportunity, reason. into( ) ) . await {
573
+ tracing:: error!( "Failed to remove opportunity: {}" , e) ;
557
574
}
558
575
}
559
576
}
0 commit comments