@@ -7,15 +7,12 @@ use anyhow::bail;
77use axum:: async_trait;
88use build_info:: chrono:: { DateTime , Utc } ;
99use indexer_dips:: {
10- store:: AgreementStore , DipsError , SignedCancellationRequest , SignedIndexingAgreementVoucher , SubgraphIndexingVoucherMetadata
10+ store:: AgreementStore , DipsError , SignedCancellationRequest , SignedIndexingAgreementVoucher ,
11+ SubgraphIndexingVoucherMetadata ,
1112} ;
1213use sqlx:: { types:: BigDecimal , PgPool } ;
13- use thegraph_core:: alloy:: {
14- core:: primitives:: U256 as uint256,
15- sol_types:: SolType
16- } ;
14+ use thegraph_core:: alloy:: { core:: primitives:: U256 as uint256, hex:: ToHexExt , sol_types:: SolType } ;
1715use uuid:: Uuid ;
18- use thegraph_core:: alloy:: hex:: ToHexExt ;
1916
2017#[ derive( Debug ) ]
2118pub struct PsqlAgreementStore {
@@ -31,10 +28,12 @@ fn uint32_to_i64(uint32: u32) -> i64 {
3128 uint32. try_into ( ) . unwrap ( )
3229}
3330
34-
3531#[ async_trait]
3632impl AgreementStore for PsqlAgreementStore {
37- async fn get_by_id ( & self , id : Uuid ) -> anyhow:: Result < Option < ( SignedIndexingAgreementVoucher , bool ) > > {
33+ async fn get_by_id (
34+ & self ,
35+ id : Uuid ,
36+ ) -> anyhow:: Result < Option < ( SignedIndexingAgreementVoucher , bool ) > > {
3837 let item = sqlx:: query!( "SELECT * FROM indexing_agreements WHERE id=$1" , id, )
3938 . fetch_one ( & self . pool )
4039 . await ;
@@ -45,7 +44,8 @@ impl AgreementStore for PsqlAgreementStore {
4544 Err ( err) => bail ! ( err) ,
4645 } ;
4746
48- let signed = SignedIndexingAgreementVoucher :: abi_decode ( & mut item. signed_payload . as_ref ( ) , true ) ?;
47+ let signed =
48+ SignedIndexingAgreementVoucher :: abi_decode ( & mut item. signed_payload . as_ref ( ) , true ) ?;
4949 let cancelled = item. cancelled_at . is_some ( ) ;
5050 Ok ( Some ( ( signed, cancelled) ) )
5151 }
@@ -57,12 +57,14 @@ impl AgreementStore for PsqlAgreementStore {
5757 let id = Uuid :: from_bytes ( agreement. voucher . agreement_id . into ( ) ) ;
5858 let bs = agreement. encode_vec ( ) ;
5959 let now = Utc :: now ( ) ;
60- let deadline = DateTime :: from_timestamp ( agreement. voucher . deadline . try_into ( ) . unwrap ( ) , 0 ) . unwrap ( ) ;
60+ let deadline =
61+ DateTime :: from_timestamp ( agreement. voucher . deadline . try_into ( ) . unwrap ( ) , 0 ) . unwrap ( ) ;
6162 let base_price_per_epoch = uint256_to_bigdecimal ( & metadata. basePricePerEpoch ) ;
6263 let price_per_entity = uint256_to_bigdecimal ( & metadata. pricePerEntity ) ;
6364 let duration_epochs = uint32_to_i64 ( agreement. voucher . durationEpochs ) ;
6465 let max_initial_amount = uint256_to_bigdecimal ( & agreement. voucher . maxInitialAmount ) ;
65- let max_ongoing_amount_per_epoch = uint256_to_bigdecimal ( & agreement. voucher . maxOngoingAmountPerEpoch ) ;
66+ let max_ongoing_amount_per_epoch =
67+ uint256_to_bigdecimal ( & agreement. voucher . maxOngoingAmountPerEpoch ) ;
6668 let min_epochs_per_collection = uint32_to_i64 ( agreement. voucher . minEpochsPerCollection ) ;
6769 let max_epochs_per_collection = uint32_to_i64 ( agreement. voucher . maxEpochsPerCollection ) ;
6870 sqlx:: query!(
@@ -117,14 +119,17 @@ impl AgreementStore for PsqlAgreementStore {
117119#[ cfg( test) ]
118120pub ( crate ) mod test {
119121 use std:: sync:: Arc ;
120- use super :: * ;
122+
121123 use build_info:: chrono:: Duration ;
122124 use indexer_dips:: { CancellationRequest , IndexingAgreementVoucher } ;
123125 use sqlx:: PgPool ;
124- use thegraph_core:: alloy:: primitives:: { ruint:: aliases:: U256 , Address } ;
126+ use thegraph_core:: alloy:: {
127+ primitives:: { ruint:: aliases:: U256 , Address } ,
128+ sol_types:: { SolType , SolValue } ,
129+ } ;
125130 use uuid:: Uuid ;
126- use thegraph_core :: alloy :: sol_types :: SolValue ;
127- use thegraph_core :: alloy :: sol_types :: SolType ;
131+
132+ use super :: * ;
128133
129134 #[ sqlx:: test( migrations = "../../migrations" ) ]
130135 async fn test_store_agreement ( pool : PgPool ) {
@@ -149,26 +154,26 @@ pub(crate) mod test {
149154 payer : Address :: from_str ( "1234567890123456789012345678901234567890" ) . unwrap ( ) ,
150155 recipient : Address :: from_str ( "2345678901234567890123456789012345678901" ) . unwrap ( ) ,
151156 service : Address :: from_str ( "3456789012345678901234567890123456789012" ) . unwrap ( ) ,
152- durationEpochs : 30 , // 30 epochs duration
157+ durationEpochs : 30 , // 30 epochs duration
153158 maxInitialAmount : U256 :: from ( 1000 ) ,
154159 maxOngoingAmountPerEpoch : U256 :: from ( 100 ) ,
155160 maxEpochsPerCollection : 5 ,
156161 minEpochsPerCollection : 1 ,
157- metadata : metadata. abi_encode ( ) . into ( ) , // Convert Vec<u8> to Bytes
162+ metadata : metadata. abi_encode ( ) . into ( ) , // Convert Vec<u8> to Bytes
158163 } ,
159164 } ;
160165
161166 // Store agreement
162- store. create_agreement ( agreement. clone ( ) , metadata) . await . unwrap ( ) ;
167+ store
168+ . create_agreement ( agreement. clone ( ) , metadata)
169+ . await
170+ . unwrap ( ) ;
163171
164172 // Verify stored agreement
165- let row = sqlx:: query!(
166- "SELECT * FROM indexing_agreements WHERE id = $1" ,
167- id
168- )
169- . fetch_one ( & store. pool )
170- . await
171- . unwrap ( ) ;
173+ let row = sqlx:: query!( "SELECT * FROM indexing_agreements WHERE id = $1" , id)
174+ . fetch_one ( & store. pool )
175+ . await
176+ . unwrap ( ) ;
172177
173178 assert_eq ! ( row. id, id) ;
174179 assert_eq ! ( row. signature, agreement. signature) ;
@@ -210,27 +215,53 @@ pub(crate) mod test {
210215 } ;
211216
212217 // Store agreement
213- store. create_agreement ( agreement. clone ( ) , metadata. clone ( ) ) . await . unwrap ( ) ;
218+ store
219+ . create_agreement ( agreement. clone ( ) , metadata. clone ( ) )
220+ . await
221+ . unwrap ( ) ;
214222
215223 // Retrieve agreement
216224 let retrieved = store. get_by_id ( id) . await . unwrap ( ) . unwrap ( ) ;
217225
218226 let retrieved_voucher = & retrieved. 0 . voucher ;
219227 let cancelled = retrieved. 1 ;
220- let retrieved_metadata = <indexer_dips:: SubgraphIndexingVoucherMetadata as SolType >:: abi_decode ( & mut retrieved_voucher. metadata . as_ref ( ) , true ) . unwrap ( ) ;
228+ let retrieved_metadata =
229+ <indexer_dips:: SubgraphIndexingVoucherMetadata as SolType >:: abi_decode (
230+ & mut retrieved_voucher. metadata . as_ref ( ) ,
231+ true ,
232+ )
233+ . unwrap ( ) ;
221234 // Verify retrieved agreement matches original
222235 assert_eq ! ( retrieved. 0 . signature, agreement. signature) ;
223- assert_eq ! ( retrieved_voucher. durationEpochs, agreement. voucher. durationEpochs) ;
236+ assert_eq ! (
237+ retrieved_voucher. durationEpochs,
238+ agreement. voucher. durationEpochs
239+ ) ;
224240 assert_eq ! ( retrieved_metadata. protocolNetwork, metadata. protocolNetwork) ;
225241 assert_eq ! ( retrieved_metadata. chainId, metadata. chainId) ;
226- assert_eq ! ( retrieved_metadata. subgraphDeploymentId, metadata. subgraphDeploymentId) ;
242+ assert_eq ! (
243+ retrieved_metadata. subgraphDeploymentId,
244+ metadata. subgraphDeploymentId
245+ ) ;
227246 assert_eq ! ( retrieved_voucher. payer, agreement. voucher. payer) ;
228247 assert_eq ! ( retrieved_voucher. recipient, agreement. voucher. recipient) ;
229248 assert_eq ! ( retrieved_voucher. service, agreement. voucher. service) ;
230- assert_eq ! ( retrieved_voucher. maxInitialAmount, agreement. voucher. maxInitialAmount) ;
231- assert_eq ! ( retrieved_voucher. maxOngoingAmountPerEpoch, agreement. voucher. maxOngoingAmountPerEpoch) ;
232- assert_eq ! ( retrieved_voucher. maxEpochsPerCollection, agreement. voucher. maxEpochsPerCollection) ;
233- assert_eq ! ( retrieved_voucher. minEpochsPerCollection, agreement. voucher. minEpochsPerCollection) ;
249+ assert_eq ! (
250+ retrieved_voucher. maxInitialAmount,
251+ agreement. voucher. maxInitialAmount
252+ ) ;
253+ assert_eq ! (
254+ retrieved_voucher. maxOngoingAmountPerEpoch,
255+ agreement. voucher. maxOngoingAmountPerEpoch
256+ ) ;
257+ assert_eq ! (
258+ retrieved_voucher. maxEpochsPerCollection,
259+ agreement. voucher. maxEpochsPerCollection
260+ ) ;
261+ assert_eq ! (
262+ retrieved_voucher. minEpochsPerCollection,
263+ agreement. voucher. minEpochsPerCollection
264+ ) ;
234265 assert_eq ! ( cancelled, false ) ;
235266 }
236267
@@ -267,8 +298,11 @@ pub(crate) mod test {
267298 } ;
268299
269300 // Store agreement
270- store. create_agreement ( agreement. clone ( ) , metadata) . await . unwrap ( ) ;
271-
301+ store
302+ . create_agreement ( agreement. clone ( ) , metadata)
303+ . await
304+ . unwrap ( ) ;
305+
272306 // Cancel agreement
273307 let cancellation = SignedCancellationRequest {
274308 signature : vec ! [ 1 , 2 , 3 ] . into ( ) ,
@@ -279,15 +313,15 @@ pub(crate) mod test {
279313 store. cancel_agreement ( cancellation. clone ( ) ) . await . unwrap ( ) ;
280314
281315 // Verify stored agreement
282- let row = sqlx:: query!(
283- "SELECT * FROM indexing_agreements WHERE id = $1" ,
284- id
285- )
286- . fetch_one ( & store. pool )
287- . await
288- . unwrap ( ) ;
316+ let row = sqlx:: query!( "SELECT * FROM indexing_agreements WHERE id = $1" , id)
317+ . fetch_one ( & store. pool )
318+ . await
319+ . unwrap ( ) ;
289320
290321 assert ! ( row. cancelled_at. is_some( ) ) ;
291- assert_eq ! ( row. signed_cancellation_payload, Some ( cancellation. encode_vec( ) ) ) ;
322+ assert_eq ! (
323+ row. signed_cancellation_payload,
324+ Some ( cancellation. encode_vec( ) )
325+ ) ;
292326 }
293327}
0 commit comments