@@ -5,14 +5,16 @@ use std::str::FromStr;
55
66use axum:: async_trait;
77use build_info:: chrono:: { DateTime , Utc } ;
8- use indexer_dips:: {
9- store:: AgreementStore , DipsError , SignedCancellationRequest , SignedIndexingAgreementVoucher ,
10- SubgraphIndexingVoucherMetadata ,
11- } ;
128use sqlx:: { types:: BigDecimal , PgPool } ;
139use thegraph_core:: alloy:: { core:: primitives:: U256 as uint256, hex:: ToHexExt , sol_types:: SolType } ;
1410use uuid:: Uuid ;
1511
12+ use crate :: {
13+ store:: { AgreementStore , StoredIndexingAgreement } ,
14+ DipsError , SignedCancellationRequest , SignedIndexingAgreementVoucher ,
15+ SubgraphIndexingVoucherMetadata ,
16+ } ;
17+
1618#[ derive( Debug ) ]
1719pub struct PsqlAgreementStore {
1820 pub pool : PgPool ,
@@ -25,10 +27,7 @@ fn uint256_to_bigdecimal(value: &uint256, field: &str) -> Result<BigDecimal, Dip
2527
2628#[ async_trait]
2729impl AgreementStore for PsqlAgreementStore {
28- async fn get_by_id (
29- & self ,
30- id : Uuid ,
31- ) -> Result < Option < ( SignedIndexingAgreementVoucher , bool ) > , DipsError > {
30+ async fn get_by_id ( & self , id : Uuid ) -> Result < Option < StoredIndexingAgreement > , DipsError > {
3231 let item = sqlx:: query!( "SELECT * FROM indexing_agreements WHERE id=$1" , id, )
3332 . fetch_one ( & self . pool )
3433 . await ;
@@ -41,8 +40,18 @@ impl AgreementStore for PsqlAgreementStore {
4140
4241 let signed = SignedIndexingAgreementVoucher :: abi_decode ( item. signed_payload . as_ref ( ) , true )
4342 . map_err ( |e| DipsError :: AbiDecoding ( e. to_string ( ) ) ) ?;
43+ let metadata =
44+ SubgraphIndexingVoucherMetadata :: abi_decode ( signed. voucher . metadata . as_ref ( ) , true )
45+ . map_err ( |e| DipsError :: AbiDecoding ( e. to_string ( ) ) ) ?;
4446 let cancelled = item. cancelled_at . is_some ( ) ;
45- Ok ( Some ( ( signed, cancelled) ) )
47+ Ok ( Some ( StoredIndexingAgreement {
48+ voucher : signed,
49+ metadata,
50+ cancelled,
51+ current_allocation_id : item. current_allocation_id ,
52+ last_allocation_id : item. last_allocation_id ,
53+ last_payment_collected_at : item. last_payment_collected_at ,
54+ } ) )
4655 }
4756 async fn create_agreement (
4857 & self ,
@@ -72,7 +81,7 @@ impl AgreementStore for PsqlAgreementStore {
7281 let min_epochs_per_collection: i64 = agreement. voucher . minEpochsPerCollection . into ( ) ;
7382 let max_epochs_per_collection: i64 = agreement. voucher . maxEpochsPerCollection . into ( ) ;
7483 sqlx:: query!(
75- "INSERT INTO indexing_agreements VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,null,null,null)" ,
84+ "INSERT INTO indexing_agreements VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,null,null,null,null,null )" ,
7685 id,
7786 agreement. signature. as_ref( ) ,
7887 bs,
@@ -126,15 +135,15 @@ pub(crate) mod test {
126135 use std:: sync:: Arc ;
127136
128137 use build_info:: chrono:: Duration ;
129- use indexer_dips:: { CancellationRequest , IndexingAgreementVoucher } ;
130138 use sqlx:: PgPool ;
131139 use thegraph_core:: alloy:: {
132140 primitives:: { ruint:: aliases:: U256 , Address } ,
133- sol_types:: { SolType , SolValue } ,
141+ sol_types:: SolValue ,
134142 } ;
135143 use uuid:: Uuid ;
136144
137145 use super :: * ;
146+ use crate :: { CancellationRequest , IndexingAgreementVoucher } ;
138147
139148 #[ sqlx:: test( migrations = "../../migrations" ) ]
140149 async fn test_store_agreement ( pool : PgPool ) {
@@ -226,19 +235,15 @@ pub(crate) mod test {
226235 . unwrap ( ) ;
227236
228237 // Retrieve agreement
229- let ( retrieved_signed_voucher, cancelled) = store. get_by_id ( id) . await . unwrap ( ) . unwrap ( ) ;
230-
231- let retrieved_voucher = & retrieved_signed_voucher. voucher ;
232- let retrieved_metadata =
233- <indexer_dips:: SubgraphIndexingVoucherMetadata as SolType >:: abi_decode (
234- retrieved_voucher. metadata . as_ref ( ) ,
235- true ,
236- )
237- . unwrap ( ) ;
238+ let stored_agreement = store. get_by_id ( id) . await . unwrap ( ) . unwrap ( ) ;
239+
240+ let retrieved_voucher = & stored_agreement. voucher ;
241+ let retrieved_metadata = stored_agreement. metadata ;
242+
238243 // Verify retrieved agreement matches original
239- assert_eq ! ( retrieved_signed_voucher . signature, agreement. signature) ;
244+ assert_eq ! ( retrieved_voucher . signature, agreement. signature) ;
240245 assert_eq ! (
241- retrieved_voucher. durationEpochs,
246+ retrieved_voucher. voucher . durationEpochs,
242247 agreement. voucher. durationEpochs
243248 ) ;
244249 assert_eq ! ( retrieved_metadata. protocolNetwork, metadata. protocolNetwork) ;
@@ -247,26 +252,29 @@ pub(crate) mod test {
247252 retrieved_metadata. subgraphDeploymentId,
248253 metadata. subgraphDeploymentId
249254 ) ;
250- assert_eq ! ( retrieved_voucher. payer, agreement. voucher. payer) ;
251- assert_eq ! ( retrieved_voucher. recipient, agreement. voucher. recipient) ;
252- assert_eq ! ( retrieved_voucher. service, agreement. voucher. service) ;
255+ assert_eq ! ( retrieved_voucher. voucher. payer, agreement. voucher. payer) ;
256+ assert_eq ! (
257+ retrieved_voucher. voucher. recipient,
258+ agreement. voucher. recipient
259+ ) ;
260+ assert_eq ! ( retrieved_voucher. voucher. service, agreement. voucher. service) ;
253261 assert_eq ! (
254- retrieved_voucher. maxInitialAmount,
262+ retrieved_voucher. voucher . maxInitialAmount,
255263 agreement. voucher. maxInitialAmount
256264 ) ;
257265 assert_eq ! (
258- retrieved_voucher. maxOngoingAmountPerEpoch,
266+ retrieved_voucher. voucher . maxOngoingAmountPerEpoch,
259267 agreement. voucher. maxOngoingAmountPerEpoch
260268 ) ;
261269 assert_eq ! (
262- retrieved_voucher. maxEpochsPerCollection,
270+ retrieved_voucher. voucher . maxEpochsPerCollection,
263271 agreement. voucher. maxEpochsPerCollection
264272 ) ;
265273 assert_eq ! (
266- retrieved_voucher. minEpochsPerCollection,
274+ retrieved_voucher. voucher . minEpochsPerCollection,
267275 agreement. voucher. minEpochsPerCollection
268276 ) ;
269- assert ! ( !cancelled) ;
277+ assert ! ( !stored_agreement . cancelled) ;
270278 }
271279
272280 #[ sqlx:: test( migrations = "../../migrations" ) ]
0 commit comments