@@ -6,7 +6,9 @@ use std::{str::FromStr, sync::Arc};
66use server:: DipsServerContext ;
77use thegraph_core:: alloy:: {
88 core:: primitives:: Address ,
9- primitives:: { b256, ChainId , PrimitiveSignature as Signature , Uint , B256 } ,
9+ primitives:: {
10+ b256, ruint:: aliases:: U256 , ChainId , PrimitiveSignature as Signature , Uint , B256 ,
11+ } ,
1012 signers:: SignerSync ,
1113 sol,
1214 sol_types:: { eip712_domain, Eip712Domain , SolStruct , SolValue } ,
@@ -18,6 +20,7 @@ pub mod ipfs;
1820pub mod price;
1921#[ cfg( feature = "rpc" ) ]
2022pub mod proto;
23+ pub mod registry;
2124#[ cfg( feature = "rpc" ) ]
2225pub mod server;
2326pub mod signers;
@@ -145,8 +148,12 @@ pub enum DipsError {
145148 InvalidSubgraphManifest ( String ) ,
146149 #[ error( "chainId {0} is not supported" ) ]
147150 UnsupportedChainId ( String ) ,
148- #[ error( "price per block is below configured price for chain {0}, minimum: {1}, offered: {2}" ) ]
149- PricePerBlockTooLow ( String , u64 , String ) ,
151+ #[ error( "price per epoch is below configured price for chain {0}, minimum: {1}, offered: {2}" ) ]
152+ PricePerEpochTooLow ( String , u64 , String ) ,
153+ #[ error(
154+ "price per entity is below configured price for chain {0}, minimum: {1}, offered: {2}"
155+ ) ]
156+ PricePerEntityTooLow ( String , u64 , String ) ,
150157 // cancellation
151158 #[ error( "cancelled_by is expected to match the signer" ) ]
152159 UnexpectedSigner ,
@@ -304,6 +311,7 @@ pub async fn validate_and_create_agreement(
304311 ipfs_fetcher,
305312 price_calculator,
306313 signer_validator,
314+ registry,
307315 } = ctx. as_ref ( ) ;
308316 let decoded_voucher = SignedIndexingAgreementVoucher :: abi_decode ( voucher. as_ref ( ) , true )
309317 . map_err ( |e| DipsError :: AbiDecoding ( e. to_string ( ) ) ) ?;
@@ -330,19 +338,33 @@ pub async fn validate_and_create_agreement(
330338 }
331339 }
332340
333- let offered_price = metadata. pricePerEntity ;
341+ let network = match registry. get_network_by_id ( & metadata. chainId ) {
342+ Some ( network) => network. id . clone ( ) ,
343+ None => return Err ( DipsError :: UnsupportedChainId ( metadata. chainId ) ) ,
344+ } ;
345+
346+ let offered_epoch_price = metadata. basePricePerEpoch ;
334347 match price_calculator. get_minimum_price ( & metadata. chainId ) {
335- Some ( price) if offered_price . lt ( & Uint :: from ( price) ) => {
336- return Err ( DipsError :: PricePerBlockTooLow (
337- metadata . chainId ,
348+ Some ( price) if offered_epoch_price . lt ( & Uint :: from ( price) ) => {
349+ return Err ( DipsError :: PricePerEpochTooLow (
350+ network ,
338351 price,
339- offered_price . to_string ( ) ,
352+ offered_epoch_price . to_string ( ) ,
340353 ) )
341354 }
342355 Some ( _) => { }
343356 None => return Err ( DipsError :: UnsupportedChainId ( metadata. chainId ) ) ,
344357 }
345358
359+ let offered_entity_price = metadata. pricePerEntity ;
360+ if offered_entity_price < U256 :: from ( price_calculator. entity_price ( ) ) {
361+ return Err ( DipsError :: PricePerEntityTooLow (
362+ network,
363+ price_calculator. entity_price ( ) ,
364+ offered_entity_price. to_string ( ) ,
365+ ) ) ;
366+ }
367+
346368 store
347369 . create_agreement ( decoded_voucher. clone ( ) , metadata)
348370 . await ?;
@@ -754,7 +776,17 @@ mod test {
754776 subgraphDeploymentId : voucher_ctx. deployment_id . clone ( ) ,
755777 } ;
756778
757- let low_price_voucher = voucher_ctx. test_voucher ( metadata) ;
779+ let low_entity_price_voucher = voucher_ctx. test_voucher ( metadata) ;
780+
781+ let metadata = SubgraphIndexingVoucherMetadata {
782+ basePricePerEpoch : U256 :: from ( 10_u64 ) ,
783+ pricePerEntity : U256 :: from ( 10000_u64 ) ,
784+ protocolNetwork : "eip155:42161" . to_string ( ) ,
785+ chainId : "mainnet" . to_string ( ) ,
786+ subgraphDeploymentId : voucher_ctx. deployment_id . clone ( ) ,
787+ } ;
788+
789+ let low_epoch_price_voucher = voucher_ctx. test_voucher ( metadata) ;
758790
759791 let metadata = SubgraphIndexingVoucherMetadata {
760792 basePricePerEpoch : U256 :: from ( 10000_u64 ) ,
@@ -775,11 +807,16 @@ mod test {
775807 Err ( DipsError :: InvalidSubgraphManifest (
776808 voucher_ctx. deployment_id. clone( ) ,
777809 ) ) ,
778- Err ( DipsError :: PricePerBlockTooLow (
810+ Err ( DipsError :: PricePerEntityTooLow (
779811 "mainnet" . to_string( ) ,
780812 100 ,
781813 "10" . to_string( ) ,
782814 ) ) ,
815+ Err ( DipsError :: PricePerEpochTooLow (
816+ "mainnet" . to_string( ) ,
817+ 200 ,
818+ "10" . to_string( ) ,
819+ ) ) ,
783820 Err ( DipsError :: SignerNotAuthorised ( signer. address( ) ) ) ,
784821 Ok ( valid_voucher
785822 . voucher
@@ -790,7 +827,8 @@ mod test {
790827 ] ;
791828 let cases = vec ! [
792829 no_network_voucher,
793- low_price_voucher,
830+ low_entity_price_voucher,
831+ low_epoch_price_voucher,
794832 valid_voucher_invalid_signer,
795833 valid_voucher,
796834 ] ;
0 commit comments