@@ -5,37 +5,37 @@ use std::time::Duration;
55
66use alloy:: primitives:: Address ;
77use anyhow:: anyhow;
8- use eventuals:: { Eventual , EventualExt } ;
98use graphql_client:: GraphQLQuery ;
10- use indexer_common:: subgraph_client:: SubgraphClient ;
9+ use indexer_common:: { subgraph_client:: SubgraphClient , watcher :: new_watcher } ;
1110use tap_core:: receipt:: {
1211 checks:: { Check , CheckError , CheckResult } ,
1312 state:: Checking ,
1413 ReceiptWithState ,
1514} ;
16- use tokio:: time:: sleep;
17- use tracing:: error;
15+ use tokio:: sync:: watch:: Receiver ;
1816
1917pub struct AllocationId {
20- tap_allocation_redeemed : Eventual < bool > ,
18+ tap_allocation_redeemed : Receiver < bool > ,
2119 allocation_id : Address ,
2220}
2321
2422impl AllocationId {
25- pub fn new (
23+ pub async fn new (
2624 indexer_address : Address ,
2725 escrow_polling_interval : Duration ,
2826 sender_id : Address ,
2927 allocation_id : Address ,
3028 escrow_subgraph : & ' static SubgraphClient ,
3129 ) -> Self {
32- let tap_allocation_redeemed = tap_allocation_redeemed_eventual (
30+ let tap_allocation_redeemed = tap_allocation_redeemed_watcher (
3331 allocation_id,
3432 sender_id,
3533 indexer_address,
3634 escrow_subgraph,
3735 escrow_polling_interval,
38- ) ;
36+ )
37+ . await
38+ . expect ( "Failed to initialize tap_allocation_redeemed_watcher" ) ;
3939
4040 Self {
4141 tap_allocation_redeemed,
@@ -60,46 +60,33 @@ impl Check for AllocationId {
6060 } ;
6161
6262 // Check that the allocation ID is not redeemed yet for this consumer
63- match self . tap_allocation_redeemed . value ( ) . await {
64- Ok ( false ) => Ok ( ( ) ) ,
65- Ok ( true ) => Err ( CheckError :: Failed ( anyhow ! (
63+ match * self . tap_allocation_redeemed . borrow ( ) {
64+ false => Ok ( ( ) ) ,
65+ true => Err ( CheckError :: Failed ( anyhow ! (
6666 "Allocation {} already redeemed" ,
6767 allocation_id
6868 ) ) ) ,
69- Err ( e) => Err ( CheckError :: Retryable ( anyhow ! (
70- "Could not get allocation escrow redemption status from eventual: {:?}" ,
71- e
72- ) ) ) ,
7369 }
7470 }
7571}
7672
77- fn tap_allocation_redeemed_eventual (
73+ async fn tap_allocation_redeemed_watcher (
7874 allocation_id : Address ,
7975 sender_address : Address ,
8076 indexer_address : Address ,
8177 escrow_subgraph : & ' static SubgraphClient ,
8278 escrow_polling_interval : Duration ,
83- ) -> Eventual < bool > {
84- eventuals:: timer ( escrow_polling_interval) . map_with_retry (
85- move |_| async move {
86- query_escrow_check_transactions (
87- allocation_id,
88- sender_address,
89- indexer_address,
90- escrow_subgraph,
91- )
92- . await
93- . map_err ( |e| e. to_string ( ) )
94- } ,
95- move |error : String | {
96- error ! (
97- "Failed to check the escrow redeem status for allocation {} and sender {}: {}" ,
98- allocation_id, sender_address, error
99- ) ;
100- sleep ( escrow_polling_interval. div_f32 ( 2. ) )
101- } ,
102- )
79+ ) -> anyhow:: Result < Receiver < bool > > {
80+ new_watcher ( escrow_polling_interval, move || async move {
81+ query_escrow_check_transactions (
82+ allocation_id,
83+ sender_address,
84+ indexer_address,
85+ escrow_subgraph,
86+ )
87+ . await
88+ } )
89+ . await
10390}
10491
10592#[ derive( GraphQLQuery ) ]
0 commit comments