@@ -173,6 +173,18 @@ def decode(self, value_bytes: bytes) -> FieldValues:
173173 """
174174 return FieldValues (list (zip (self .names , decode_args (self .types , value_bytes ), strict = True )))
175175
176+ def to_json (self ) -> ABI_JSON :
177+ """Returns this object's JSON ABI."""
178+ args = []
179+ for name , tp in zip (self .names , self .types , strict = True ):
180+ args .append (
181+ {
182+ "name" : name if name is not None else "" ,
183+ "type" : tp .canonical_form ,
184+ }
185+ )
186+ return args
187+
176188 def __str__ (self ) -> str :
177189 fields = ", " .join (
178190 tp .canonical_form + ((" " + name ) if name is not None else "" )
@@ -297,6 +309,19 @@ def decode_log_entry(self, topics: Sequence[bytes], data: bytes) -> FieldValues:
297309
298310 return FieldValues (decoded_data )
299311
312+ def to_json (self ) -> ABI_JSON :
313+ """Returns this object's JSON ABI."""
314+ args : list [ABI_JSON ] = []
315+ for name , tp , indexed in zip (self .names , self .types , self .indexed , strict = True ):
316+ args .append (
317+ {
318+ "indexed" : indexed ,
319+ "name" : name if name is not None else "" ,
320+ "type" : tp .canonical_form ,
321+ }
322+ )
323+ return args
324+
300325 def __str__ (self ) -> str :
301326 params = []
302327 for name , tp , indexed in zip (self .names , self .types , self .indexed , strict = True ):
@@ -359,6 +384,14 @@ def __call__(self, *args: Any, **kwargs: Any) -> "ConstructorCall":
359384 input_bytes = self .inputs .encode (self ._inputs_signature .bind (* args , ** kwargs ).args )
360385 return ConstructorCall (input_bytes )
361386
387+ def to_json (self ) -> ABI_JSON :
388+ """Returns this object's JSON ABI."""
389+ return {
390+ "type" : "constructor" ,
391+ "stateMutability" : "payable" if self .payable else "nonpayable" ,
392+ "inputs" : self .inputs .to_json (),
393+ }
394+
362395 def __str__ (self ) -> str :
363396 return f"constructor{ self .inputs } " + ("payable" if self .payable else "nonpayable" )
364397
@@ -515,6 +548,16 @@ def with_method(self, method: "Method") -> "MultiMethod":
515548 """Returns a multimethod resulting from joining this method with `method`."""
516549 return MultiMethod (self , method )
517550
551+ def to_json (self ) -> ABI_JSON :
552+ """Returns this object's JSON ABI."""
553+ return {
554+ "type" : "function" ,
555+ "name" : self .name ,
556+ "stateMutability" : self ._mutability .value ,
557+ "inputs" : self .inputs .to_json (),
558+ "outputs" : self .outputs .to_json (),
559+ }
560+
518561 def __str__ (self ) -> str :
519562 returns = "" if not self .outputs .names else f" returns { self .outputs } "
520563 return f"function { self .name } { self .inputs } { self ._mutability .value } { returns } "
@@ -586,6 +629,10 @@ def __call__(self, *args: Any, **kwds: Any) -> "MethodCall":
586629
587630 raise TypeError ("Could not find a suitable overloaded method for the given arguments" )
588631
632+ def to_json (self ) -> list [ABI_JSON ]:
633+ """Returns this object's JSON ABI."""
634+ return [method .to_json () for method in self ._methods .values ()]
635+
589636 def __str__ (self ) -> str :
590637 return "; " .join (str (method ) for method in self ._methods .values ())
591638
@@ -689,6 +736,15 @@ def decode_log_entry(self, log_entry: LogEntry) -> FieldValues:
689736
690737 return self .fields .decode_log_entry ([bytes (topic ) for topic in topics ], log_entry .data )
691738
739+ def to_json (self ) -> ABI_JSON :
740+ """Returns this object's JSON ABI."""
741+ return {
742+ "type" : "event" ,
743+ "name" : self .name ,
744+ "inputs" : self .fields .to_json (),
745+ "anonymous" : self .anonymous ,
746+ }
747+
692748 def __str__ (self ) -> str :
693749 return f"event { self .name } { self .fields } " + (" anonymous" if self .anonymous else "" )
694750
@@ -743,6 +799,14 @@ def decode_fields(self, data_bytes: bytes) -> FieldValues:
743799 """Decodes the error fields from the given packed data."""
744800 return self .fields .decode (data_bytes )
745801
802+ def to_json (self ) -> ABI_JSON :
803+ """Returns this object's JSON ABI."""
804+ return {
805+ "type" : "error" ,
806+ "name" : self .name ,
807+ "inputs" : self .fields .to_json (),
808+ }
809+
746810 def __str__ (self ) -> str :
747811 return f"error { self .name } { self .fields } "
748812
@@ -773,6 +837,13 @@ def from_json(cls, method_entry: ABI_JSON) -> "Fallback":
773837 def __init__ (self , * , payable : bool = False ):
774838 self .payable = payable
775839
840+ def to_json (self ) -> ABI_JSON :
841+ """Returns this object's JSON ABI."""
842+ return {
843+ "type" : "fallback" ,
844+ "stateMutability" : "payable" if self .payable else "nonpayable" ,
845+ }
846+
776847 def __str__ (self ) -> str :
777848 return "fallback() " + ("payable" if self .payable else "nonpayable" )
778849
@@ -801,6 +872,13 @@ def from_json(cls, method_entry: ABI_JSON) -> "Receive":
801872 def __init__ (self , * , payable : bool = False ):
802873 self .payable = payable
803874
875+ def to_json (self ) -> ABI_JSON :
876+ """Returns this object's JSON ABI."""
877+ return {
878+ "type" : "receive" ,
879+ "stateMutability" : "payable" if self .payable else "nonpayable" ,
880+ }
881+
804882 def __str__ (self ) -> str :
805883 return "receive() " + ("payable" if self .payable else "nonpayable" )
806884
@@ -996,6 +1074,27 @@ def resolve_error(self, error_data: bytes) -> tuple[Error, FieldValues]:
9961074
9971075 raise UnknownError (f"Could not find an error with selector { selector .hex ()} in the ABI" )
9981076
1077+ def to_json (self ) -> ABI_JSON :
1078+ """Returns the serialized list of contract items (methods, errors, events)."""
1079+ all_items : Iterable [
1080+ Constructor | Fallback | Receive | Method | MultiMethod | Event | Error
1081+ ] = chain (
1082+ [self .constructor ] if self .constructor else [],
1083+ [self .fallback ] if self .fallback else [],
1084+ [self .receive ] if self .receive else [],
1085+ self .method ,
1086+ self .event ,
1087+ self .error ,
1088+ )
1089+ entries : list [ABI_JSON ] = []
1090+ for item in all_items :
1091+ if isinstance (item , MultiMethod ):
1092+ entries .extend (item .to_json ())
1093+ else :
1094+ entries .append (item .to_json ())
1095+
1096+ return entries
1097+
9991098 def __str__ (self ) -> str :
10001099 all_items : Iterable [
10011100 Constructor | Fallback | Receive | Method | MultiMethod | Event | Error
0 commit comments