1- import  json 
21import  importlib 
2+ import  json 
33from  datetime  import  datetime , timedelta 
44from  decimal  import  Decimal 
55from  functools  import  partial 
6- from  typing  import  Any , Dict , Iterable , Iterator , List , Optional , Union , cast , ClassVar 
6+ from  pathlib  import  Path 
7+ from  typing  import  Any , ClassVar , Dict , Iterable , Iterator , List , Optional , Union , cast 
78
89from  ape .api  import  ReceiptAPI 
910from  ape .contracts .base  import  ContractInstance , ContractTransactionHandler 
1011from  ape .exceptions  import  (
1112    CompilerError ,
1213    ContractLogicError ,
14+     ContractNotFoundError ,
1315    DecodingError ,
1416    ProjectError ,
15-     ContractNotFoundError ,
1617)
17- from  ape .types  import  AddressType , ContractLog ,  HexBytes 
18+ from  ape .types  import  AddressType , ContractLog 
1819from  ape .utils  import  BaseInterfaceModel , cached_property 
1920from  ethpm_types  import  ContractType , PackageManifest 
20- from  pydantic  import  ValidationError , validator 
21+ from  hexbytes  import  HexBytes 
22+ from  pydantic  import  ValidationError , ValidationInfo , field_validator 
2123
2224from  .exceptions  import  (
2325    FundsNotClaimable ,
@@ -48,7 +50,7 @@ def __eq__(self, other: Any) -> bool:
4850        # Try __eq__ from the other side. 
4951        return  NotImplemented 
5052
51-     def  validate (self , creator , token , amount_per_second , reason ) ->  bool :
53+     def  validate (self , creator , token , amount_per_second , reason ) ->  bool :   # type: ignore 
5254        try :
5355            self .contract .validate .call (creator , token , amount_per_second , reason )
5456            return  True 
@@ -63,14 +65,16 @@ def validate(self, creator, token, amount_per_second, reason) -> bool:
6365class  StreamManager (BaseInterfaceModel ):
6466    address : AddressType 
6567    contract_type : Optional [ContractType ] =  None 
66-     _local_contracts : ClassVar [Dict [str , ContractType ]]
68+     _local_contracts : ClassVar [Dict [str , ContractType ]]  =   dict () 
6769
68-     @validator ("address" , pre = True ) 
70+     @field_validator ("address" , mode = "before" ) 
71+     @classmethod  
6972    def  normalize_address (cls , value : Any ) ->  AddressType :
7073        return  cls .conversion_manager .convert (value , AddressType )
7174
72-     @validator ("contract_type" , pre = True , always = True ) 
73-     def  fetch_contract_type (cls , value : Any , values : Dict [str , Any ]) ->  ContractType :
75+     @field_validator ("contract_type" , mode = "before" ) 
76+     @classmethod  
77+     def  fetch_contract_type (cls , value : Any , info : ValidationInfo ) ->  Optional [ContractType ]:
7478        # 0. If pre-loaded, default to that type 
7579        if  value :
7680            return  value 
@@ -86,19 +90,24 @@ def fetch_contract_type(cls, value: Any, values: Dict[str, Any]) -> ContractType
8690
8791        # 2. If contract cache has it, use that 
8892        try :
89-             if  values .get ("address" ) and  (
90-                 contract_type  :=  cls .chain_manager .contracts .get (values ["address" ])
93+             if  info . data .get ("address" ) and  (
94+                 contract_type  :=  cls .chain_manager .contracts .get (info . data ["address" ])
9195            ):
9296                return  contract_type 
9397
9498        except  Exception :
9599            pass 
96100
97101        # 3. Most expensive way is through package resources 
98-         cls ._local_contracts  =  PackageManifest .parse_file (
99-             importlib .resources .files ("apepay" ) /  "manifest.json" 
100-         ).contract_types 
101-         return  cls ._local_contracts ["StreamManager" ]
102+         manifest_file  =  Path (__file__ ).parent  /  "manifest.json" 
103+         manifest_text  =  manifest_file .read_text ()
104+         manifest  =  PackageManifest .parse_raw (manifest_text )
105+ 
106+         if  not  manifest  or  not  manifest .contract_types :
107+             raise  ValueError ("Invalid manifest" )
108+ 
109+         cls ._local_contracts  =  manifest .contract_types 
110+         return  cls ._local_contracts .get ("StreamManager" )
102111
103112    @property  
104113    def  contract (self ) ->  ContractInstance :
@@ -157,7 +166,7 @@ def set_validators(
157166
158167    def  add_validators (
159168        self ,
160-         * new_validators : Iterable [ _ValidatorItem ] ,
169+         * new_validators : _ValidatorItem ,
161170        ** txn_kwargs ,
162171    ) ->  ReceiptAPI :
163172        return  self .set_validators (
@@ -167,7 +176,7 @@ def add_validators(
167176
168177    def  remove_validators (
169178        self ,
170-         * validators : Iterable [ _ValidatorItem ] ,
179+         * validators : _ValidatorItem ,
171180        ** txn_kwargs ,
172181    ) ->  ReceiptAPI :
173182        return  self .set_validators (
@@ -307,14 +316,16 @@ class Stream(BaseInterfaceModel):
307316    creation_receipt : Optional [ReceiptAPI ] =  None 
308317    transaction_hash : Optional [HexBytes ] =  None 
309318
310-     @validator ("transaction_hash" , pre = True ) 
319+     @field_validator ("transaction_hash" , mode = "before" ) 
320+     @classmethod  
311321    def  normalize_transaction_hash (cls , value : Any ) ->  Optional [HexBytes ]:
312322        if  value :
313323            return  HexBytes (cls .conversion_manager .convert (value , bytes ))
314324
315325        return  value 
316326
317-     @validator ("creator" , pre = True ) 
327+     @field_validator ("creator" , mode = "before" ) 
328+     @classmethod  
318329    def  validate_addresses (cls , value ):
319330        return  (
320331            value  if  isinstance (value , str ) else  cls .conversion_manager .convert (value , AddressType )
0 commit comments