@@ -5,14 +5,15 @@ use std::str::FromStr;
5
5
6
6
use axum:: async_trait;
7
7
use build_info:: chrono:: { DateTime , Utc } ;
8
- use indexer_dips:: {
8
+ use sqlx:: { types:: BigDecimal , PgPool } ;
9
+ use thegraph_core:: alloy:: { core:: primitives:: U256 as uint256, hex:: ToHexExt , sol_types:: SolType } ;
10
+ use uuid:: Uuid ;
11
+
12
+ use crate :: {
9
13
store:: { AgreementStore , StoredIndexingAgreement } ,
10
14
DipsError , SignedCancellationRequest , SignedIndexingAgreementVoucher ,
11
15
SubgraphIndexingVoucherMetadata ,
12
16
} ;
13
- use sqlx:: { types:: BigDecimal , PgPool } ;
14
- use thegraph_core:: alloy:: { core:: primitives:: U256 as uint256, hex:: ToHexExt , sol_types:: SolType } ;
15
- use uuid:: Uuid ;
16
17
17
18
#[ derive( Debug ) ]
18
19
pub struct PsqlAgreementStore {
@@ -51,6 +52,47 @@ impl AgreementStore for PsqlAgreementStore {
51
52
last_allocation_id : item. last_allocation_id ,
52
53
} ) )
53
54
}
55
+ async fn get_by_last_allocation_id (
56
+ & self ,
57
+ allocation_id : String ,
58
+ ) -> Result < Vec < StoredIndexingAgreement > , DipsError > {
59
+ let items = sqlx:: query!(
60
+ "SELECT * FROM indexing_agreements WHERE last_allocation_id=$1" ,
61
+ allocation_id,
62
+ )
63
+ . fetch_all ( & self . pool )
64
+ . await ;
65
+
66
+ let items = match items {
67
+ Ok ( items) => items,
68
+ Err ( sqlx:: Error :: RowNotFound ) => return Ok ( vec ! [ ] ) ,
69
+ Err ( err) => return Err ( DipsError :: UnknownError ( err. into ( ) ) ) ,
70
+ } ;
71
+
72
+ // Note: we discard any agreements that fail to decode
73
+ let agreements = items
74
+ . into_iter ( )
75
+ . map ( |item| {
76
+ let signed =
77
+ SignedIndexingAgreementVoucher :: abi_decode ( item. signed_payload . as_ref ( ) , true )
78
+ . map_err ( |e| DipsError :: AbiDecoding ( e. to_string ( ) ) ) ?;
79
+ let metadata = SubgraphIndexingVoucherMetadata :: abi_decode (
80
+ signed. voucher . metadata . as_ref ( ) ,
81
+ true ,
82
+ )
83
+ . map_err ( |e| DipsError :: AbiDecoding ( e. to_string ( ) ) ) ?;
84
+ Ok ( StoredIndexingAgreement {
85
+ voucher : signed,
86
+ metadata,
87
+ cancelled : item. cancelled_at . is_some ( ) ,
88
+ current_allocation_id : item. current_allocation_id ,
89
+ last_allocation_id : item. last_allocation_id ,
90
+ } )
91
+ } )
92
+ . filter_map ( |agreement : Result < StoredIndexingAgreement , DipsError > | agreement. ok ( ) )
93
+ . collect ( ) ;
94
+ Ok ( agreements)
95
+ }
54
96
async fn create_agreement (
55
97
& self ,
56
98
agreement : SignedIndexingAgreementVoucher ,
@@ -133,7 +175,6 @@ pub(crate) mod test {
133
175
use std:: sync:: Arc ;
134
176
135
177
use build_info:: chrono:: Duration ;
136
- use indexer_dips:: { CancellationRequest , IndexingAgreementVoucher } ;
137
178
use sqlx:: PgPool ;
138
179
use thegraph_core:: alloy:: {
139
180
primitives:: { ruint:: aliases:: U256 , Address } ,
@@ -142,6 +183,7 @@ pub(crate) mod test {
142
183
use uuid:: Uuid ;
143
184
144
185
use super :: * ;
186
+ use crate :: { CancellationRequest , IndexingAgreementVoucher } ;
145
187
146
188
#[ sqlx:: test( migrations = "../../migrations" ) ]
147
189
async fn test_store_agreement ( pool : PgPool ) {
0 commit comments