3939 get_normalized_abi_inputs ,
4040 is_list_like ,
4141 is_text ,
42+ keccak ,
43+ to_bytes ,
4244 to_tuple ,
4345)
4446from hexbytes import (
6769 empty ,
6870)
6971from web3 ._utils .encoding import (
72+ hexstr_if_str ,
7073 to_4byte_hex ,
7174 to_hex ,
7275)
@@ -784,7 +787,7 @@ def encode_abi(
784787 ) -> HexStr :
785788 """
786789 Encodes the arguments using the Ethereum ABI for the contract function
787- that matches the given name and arguments..
790+ that matches the given name and arguments.
788791
789792 :param data: defaults to function selector
790793 """
@@ -811,12 +814,20 @@ def encode_abi(
811814 def all_functions (
812815 self ,
813816 ) -> "BaseContractFunction" :
817+ """
818+ Return all functions in the contract.
819+ """
814820 return self .find_functions_by_identifier (
815821 self .abi , self .w3 , self .address , lambda _ : True
816822 )
817823
818824 @combomethod
819825 def get_function_by_signature (self , signature : str ) -> "BaseContractFunction" :
826+ """
827+ Return a distinct function with matching signature.
828+ Raises a Web3ValueError if the signature is invalid or if there is no match or
829+ more than one is found.
830+ """
820831 if " " in signature :
821832 raise Web3ValueError (
822833 "Function signature should not contain any spaces. "
@@ -833,6 +844,11 @@ def callable_check(fn_abi: ABIFunction) -> bool:
833844
834845 @combomethod
835846 def find_functions_by_name (self , fn_name : str ) -> "BaseContractFunction" :
847+ """
848+ Return all functions with matching name.
849+ Raises a Web3ValueError if there is no match or more than one is found.
850+ """
851+
836852 def callable_check (fn_abi : ABIFunction ) -> bool :
837853 return fn_abi ["name" ] == fn_name
838854
@@ -842,13 +858,22 @@ def callable_check(fn_abi: ABIFunction) -> bool:
842858
843859 @combomethod
844860 def get_function_by_name (self , fn_name : str ) -> "BaseContractFunction" :
861+ """
862+ Return a distinct function with matching name.
863+ Raises a Web3ValueError if there is no match or more than one is found.
864+ """
845865 fns = self .find_functions_by_name (fn_name )
846866 return self .get_function_by_identifier (fns , "name" )
847867
848868 @combomethod
849869 def get_function_by_selector (
850870 self , selector : Union [bytes , int , HexStr ]
851871 ) -> "BaseContractFunction" :
872+ """
873+ Return a distinct function with matching 4byte selector.
874+ Raises a Web3ValueError if there is no match or more than one is found.
875+ """
876+
852877 def callable_check (fn_abi : ABIFunction ) -> bool :
853878 return encode_hex (function_abi_to_4byte_selector (fn_abi )) == to_4byte_hex (
854879 selector
@@ -863,6 +888,9 @@ def callable_check(fn_abi: ABIFunction) -> bool:
863888 def decode_function_input (
864889 self , data : HexStr
865890 ) -> Tuple ["BaseContractFunction" , Dict [str , Any ]]:
891+ """
892+ Return a Tuple of the function selector and decoded arguments.
893+ """
866894 func = self .get_function_by_selector (HexBytes (data )[:4 ])
867895 arguments = decode_transaction_data (
868896 func .abi , data , normalizers = BASE_RETURN_NORMALIZERS
@@ -871,6 +899,11 @@ def decode_function_input(
871899
872900 @combomethod
873901 def find_functions_by_args (self , * args : Any ) -> "BaseContractFunction" :
902+ """
903+ Return all functions with matching args, checking each argument can be encoded
904+ with the type.
905+ """
906+
874907 def callable_check (fn_abi : ABIFunction ) -> bool :
875908 return check_if_arguments_can_be_encoded (
876909 fn_abi ,
@@ -885,20 +918,54 @@ def callable_check(fn_abi: ABIFunction) -> bool:
885918
886919 @combomethod
887920 def get_function_by_args (self , * args : Any ) -> "BaseContractFunction" :
921+ """
922+ Return a distinct function with matching args, checking each argument can be
923+ encoded with the type.
924+ Raises a Web3ValueError if there is no match or more than one is found.
925+ """
888926 fns = self .find_functions_by_args (* args )
889927 return self .get_function_by_identifier (fns , "args" )
890928
891929 #
892930 # Events API
893931 #
894932 @combomethod
895- def all_events (self ) -> "BaseContractEvent" :
933+ def all_events (self ) -> List ["BaseContractEvent" ]:
934+ """
935+ Return all events in the contract.
936+ """
896937 return self .find_events_by_identifier (
897938 self .abi , self .w3 , self .address , lambda _ : True
898939 )
899940
900941 @combomethod
901- def find_events_by_name (self , event_name : str ) -> "BaseContractEvent" :
942+ def get_event_by_signature (self , signature : str ) -> "BaseContractEvent" :
943+ """
944+ Return a distinct event with matching signature.
945+ Raises a Web3ValueError if the signature is invalid or if there is no match or
946+ more than one is found.
947+ """
948+ if " " in signature :
949+ raise Web3ValueError (
950+ "Event signature should not contain any spaces. "
951+ f"Found spaces in input: { signature } "
952+ )
953+
954+ def callable_check (event_abi : ABIEvent ) -> bool :
955+ return abi_to_signature (event_abi ) == signature
956+
957+ events = self .find_events_by_identifier (
958+ self .abi , self .w3 , self .address , callable_check
959+ )
960+ return self .get_event_by_identifier (events , "signature" )
961+
962+ @combomethod
963+ def find_events_by_name (self , event_name : str ) -> List ["BaseContractEvent" ]:
964+ """
965+ Return all events with matching name.
966+ Raises a Web3ValueError if there is no match or more than one is found.
967+ """
968+
902969 def callable_check (fn_abi : ABIFunction ) -> bool :
903970 return fn_abi ["name" ] == event_name
904971
@@ -908,9 +975,68 @@ def callable_check(fn_abi: ABIFunction) -> bool:
908975
909976 @combomethod
910977 def get_event_by_name (self , event_name : str ) -> "BaseContractEvent" :
978+ """
979+ Return a distinct event with matching name.
980+ Raises a Web3ValueError if there is no match or more than one is found.
981+ """
911982 events = self .find_events_by_name (event_name )
912983 return self .get_event_by_identifier (events , "name" )
913984
985+ @combomethod
986+ def find_events_by_selector (
987+ self , selector : Union [bytes , int , HexStr ]
988+ ) -> List ["BaseContractEvent" ]:
989+ """
990+ Return all events with matching selector.
991+ Raises a Web3ValueError if there is no match or more than one is found.
992+ """
993+
994+ def callable_check (event_abi : ABIEvent ) -> bool :
995+ return encode_hex (
996+ keccak (text = abi_to_signature (event_abi ).replace (" " , "" ))
997+ ) == encode_hex (hexstr_if_str (to_bytes , selector ))
998+
999+ return self .find_events_by_identifier (
1000+ self .abi , self .w3 , self .address , callable_check
1001+ )
1002+
1003+ @combomethod
1004+ def get_event_by_selector (
1005+ self , selector : Union [bytes , int , HexStr ]
1006+ ) -> "BaseContractEvent" :
1007+ """
1008+ Return a distinct event with matching keccak selector.
1009+ Raises a Web3ValueError if there is no match or more than one is found.
1010+ """
1011+ events = self .find_events_by_selector (selector )
1012+ return self .get_event_by_identifier (events , "selector" )
1013+
1014+ @combomethod
1015+ def find_events_by_topic (self , topic : HexStr ) -> List ["BaseContractEvent" ]:
1016+ """
1017+ Return all events with matching topic.
1018+ Raises a Web3ValueError if there is no match or more than one is found.
1019+ """
1020+
1021+ def callable_check (event_abi : ABIEvent ) -> bool :
1022+ return (
1023+ encode_hex (keccak (text = abi_to_signature (event_abi ).replace (" " , "" )))
1024+ == topic
1025+ )
1026+
1027+ return self .find_events_by_identifier (
1028+ self .abi , self .w3 , self .address , callable_check
1029+ )
1030+
1031+ @combomethod
1032+ def get_event_by_topic (self , topic : HexStr ) -> "BaseContractEvent" :
1033+ """
1034+ Return a distinct event with matching topic.
1035+ Raises a Web3ValueError if there is no match or more than one is found.
1036+ """
1037+ events = self .find_events_by_topic (topic )
1038+ return self .get_event_by_identifier (events , "topic" )
1039+
9141040 #
9151041 # Private Helpers
9161042 #
0 commit comments