
    (if                     4   d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d dl&m'Z'm(Z( d d	l)m*Z*m+Z+ d d
l,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD deEdedee   fdZFdeGdedee   fdZHdej                  dee   deeEef   dedee   f
dZJdedefdZKdedefdZLdedefdZMd eEd!ee   deEfd"ZNd#ed$eGdedededeEfd%ZO	 	 d<dd&d'eDd#ed(eGd$eGdee   dee   dee   deEfd)ZPdee   deEfd*ZQd+edeEfd,ZRdd&d'eDdee   dee   dee   deed-ee   f      f
d.ZSdd&d#ed'eDdeee      dee   deeeEef      defd/ZTdd&d#ed'eDdee   dee   dee   defd0ZUdd&d1edeee      dee   deeeEef      deVf
d2ZW e9d3      	 d=d#ed4eEd5eeeE      defd6       ZXd7ed8ee3   dee3   fd9ZYd:e	eeeEf   deZfd;Z[y)>    N)	AnyCallableDictListOptionalSequenceTupleUnioncast)codec)ABICodec)registry)
ABIABICallableABIConstructor
ABIElementABIElementInfoABIEventABIFallback
ABIReceiveHexStr
Primitives)abi_to_signatureevent_abi_to_log_topicfilter_abi_by_namefilter_abi_by_typefunction_abi_to_4byte_selectorget_abi_input_typesget_aligned_abi_inputsget_normalized_abi_inputs)is_binary_addressis_checksum_addresshexstr_if_strto_bytes)
encode_hex)pipe)is_list_like)HexBytes)filter_by_argument_nameget_abi_element_signature$get_name_from_abi_element_identifier)deprecated_for)validate_abi)ABIConstructorNotFoundABIFallbackNotFoundABIReceiveNotFoundMismatchedABIWeb3ValidationErrorWeb3ValueError)ABIElementIdentifier	signaturecontract_abireturnc                 J    |D cg c]  }t        |      | k(  s| c}S c c}w N)r   )r6   r7   abis      ?/var/www/br/venv/lib/python3.12/site-packages/web3/utils/abi.py_filter_by_signaturer=   Z   s#    'NC+;C+@I+MCNNNs     num_argumentsc           	          |D cg c]2  }|d   dk7  r(|d   dk7  r t        |j                  dg             | k(  r|4 c}S c c}w )Ntypefallbackreceiveinputs)lenget)r>   r7   r;   s      r<   _filter_by_argument_countrF   ^   sT    
  v;*$K9$"%&-7	 	  s   7?	abi_codecargskwargsc                 j    |D cg c]#  }t        |g|d| i|rt        t        |      % c}S c c}w )NrG   )!check_if_arguments_can_be_encodedr   r   )rG   rH   rI   r7   function_abis        r<   _filter_by_encodabilityrM   j   sN     ),

+4
8>
 	[,'  s   (0c                 p    t        d|       }t        |      dkD  rt        d      |r|d   S t        d      )@
    Return the receive function ABI from the contract ABI.
    constructor   z9Multiple constructor functions found in the contract ABI.r   z6No constructor function was found in the contract ABI.)r   rD   r2   r/   r7   filtered_abiss     r<   _get_constructor_function_abirT   y   sG     '}lCM
=AWXXQ$D
 	
    c                 p    t        d|       }t        |      dkD  rt        d      |r|d   S t        d      )rO   rB   rQ   z5Multiple receive functions found in the contract ABI.r   z2No receive function was found in the contract ABI.)r   rD   r2   r1   rR   s     r<   _get_receive_function_abirW      sC     'y,?M
=ASTTQ !UVVrU   c                 p    t        d|       }t        |      dkD  rt        d      |r|d   S t        d      )zA
    Return the fallback function ABI from the contract ABI.
    rA   rQ   z6Multiple fallback functions found in the contract ABI.r   z3No fallback function was found in the contract ABI.)r   rD   r2   r0   rR   s     r<   _get_fallback_function_abirY      sC     'z<@M
=ATUUQ!"WXXrU   element_nameelementsc                     |D cg c]+  }|j                  dd      t        |       k(  rt        |      - }}t        |      dk(  r|d   S t        |      dkD  r|  d}||vr|d   S |S yc c}w )a[  
    Find an ABI identifier signature by element name. A signature identifier is
    returned, "name(arg1Type,arg2Type,...)".

    If multiple ABIs match the name and every one contain arguments, the first
    result is returned. Otherwise the signature without arguments is returned.
    Returns None if no ABI exists with the provided name.
    name rQ   r   ()N)rE   r,   r   rD   )rZ   r[   elementelement_signatures_with_namesignature_without_argss        r<    _get_any_abi_signature_with_namerc      s      $;;vr"&J<&XX 	!$  $ '(A-+A..	)	*Q	.$0>!4!)EE/22))#$s   0A)r;   num_argsc                H   t        ddddd      }| D ];  }t        |      }t        |      }t        |      }	t	               }
t	               }t        |      |k(  r|dk(  r7|d   s|dxx   d|	 dz  cc<   |dxx   dt        |       d|d	    d
z  cc<   n	 t        |g|i |}t        ||      \  }
}d}t        t        |
|      d      D ]6  \  }\  }}|j                  ||      r|d| d| dz  }(|d| d| d| dz  }8 |dk7  s|dxx   d| d|d	    d
|z   z  cc<   |dxx   d| d|d	    dt        |       d| d	z  cc<   > dj                  |j                               S # t        $ r'}|dxx   d| d|d	    d| d| d
	z  cc<   Y d}~d}~ww xY w)z?
    Build a string representation of the ABI input error.
    r^   )	zero_argsinvalid_argsencodingunexpected_argsr   rf   zIThe provided identifier matches multiple elements.
If you meant to call `z(()`, please specify the full signature.
z - signature: z, type: r@   
rg   zSignature: z"
Arguments do not match types in `z
`.
Error: NrQ   )startz	Argument z value `z` is valid.
z` is not compatible with type `z`.
rh   ri   z

Expected z argument(s) but received  argument(s).
)dictr   r   r,   tuplerD   r    r   	TypeError	enumeratezipis_encodablejoinvalues)r;   rd   rG   rH   rI   errorsabi_elementabi_element_input_typesabi_signatureabi_element_nametypesaligned_args	argumentseargument_errorsposition_typeargs                     r<   _build_abi_input_errorr      sr    "!		
F  4"5k"B(5?N!&(-&'831}k*;'11A0B C??' {#$%5k%B$C D(015#
 9+ W WPV WI*@i*X'E< !O*3C|4LTU*V &,5#))%5#8*HSE'WWO##H:XcU ;!!&t-O	 "$z"!-V9L8MRP%&" $%m_H[5H4I J 7899S*O-%a4l 776==?##= ! >*%m_H[=P<Q R<<I? K""#B(**s   E11	F!:FF!)rG   abi_element_identifiernum_matchesc          
      J   t        |       }t        ||      }t        ||      D cg c]  }t        |       }	}t	        |	      }
|t        t              }d}|dk(  r|
dk(  r|d| d| dz  }n:|dkD  s|
dkD  r|d| d| dz  }n#|
dk(  r|d	|
 d
| d| dz  }n|dk(  r	|d| dz  }t        | }t        |j                         D ch c]  \  }}|t        |g      f c}}      }|d| d| dz  }|r|d| dz  }|t        ||g|d|i|z  }d| S c c}w c c}}w )z
    Raise a ``MismatchedABI`` when a function ABI lookup results in an error.

    An error may result from multiple functions matching the provided signature and
    arguments or no functions are identified.
    zABI Not Found!
r   zNo element named `z` with rl   rQ   zFound multiple elements named `z` that accept zFound z element(s) named `z4 argument(s).
The provided arguments are not valid.
z!Unable to find an element named `z;` that matches the provided identifier and argument types.
zProvided argument types: (z#)
Provided keyword argument types: z

z,Tried to find a matching ABI element named `z+`, but encountered the following problems:
rG   rj   )r,   r   rF   r   rD   r   default_registry_extract_argument_typesrm   itemsr   )r   r;   r   rd   rG   rH   rI   r]   abis_matching_namesabis_matching_arg_countnum_abis_matching_arg_counterror	arg_typeskvkwarg_typess                   r<   _mismatched_abi_error_diagnosisr     s    00FGD,T37 -X7JK 	  #&&=">-.	Ea71<%dV78*OLL	q7!;-dV>( L 	
 
%	)011DTF KZ  66	

 
	/v 6/ /	
 (.IfllnUda3QC89UVK	
$YK 0,,7=	>E
 :4& A( (	

 	'
 
  	

 
 	
 w<c: Vs   D	D
c                  \   g }| D ]  }t        |      rng }|D ]F  }t        |      r|j                  dt        |       d       -|j                  t        |             H |j                  dj	                  |             ||j                  t        |              dj	                  |      S )z
    Takes a list of arguments and returns a string representation of the argument types,
    appropriately collapsing `tuple` types into the respective nested types.
    (),)r(   appendr   _get_argument_readable_typers   )rH   collapsed_argsr   collapsed_nestednesteds        r<   r   r   X  s    
 N 
D! Q'$++a0G0O/PPQ,RS$++,G,OP	Q
 !!#((+;"<=!!"=c"BC
D 88N##rU   r   c                 \    t        |       st        |       ry| j                  j                  S )z]
    Returns the class name of the argument, or `address` if the argument is an address.
    address)r"   r!   	__class____name__)r   s    r<   r   r   n  s'     3#4S#9==!!!rU   .c                    t        | t              st        |       } | dv rt        j                  t
        |       gS g }d}|s|rt        |      t        |      z   }d| vr+|r)|j                  t        j                  t        |             |dkD  r|j                  t        j                  t        t        |                    |s|r<|t        t              }|j                  t        j                  t        |||             d| v r)|j                  t        j                  t        |              |S |j                  t        j                  t        t        |                    d| v r)|j                  t        j                  t        |              |S )a$  
    Build a list of ABI filters to find an ABI element within a contract ABI. Each
    filter is a partial function that takes a contract ABI and returns a filtered list.
    Each parameter is checked before applying the relevant filter.

    When the ``abi_element_identifier`` is a function name or signature and no arguments
    are provided, the returned filters include the function name or signature.

    A function ABI may take arguments and keyword arguments. When the ``args`` and
    ``kwargs`` values are passed, several filters are combined together. Available
    filters include the function name, argument count, argument name, argument type,
    and argument encodability.

    ``constructor``, ``fallback``, and ``receive`` ABI elements are handled only with a
    filter by type.
    )rP   rA   rB   r   r_   r   )
isinstancestrr+   	functoolspartialr   rD   r   rF   r   r,   r   r   rM   r=   )r   rG   rH   rI   filters	arg_counts         r<   _build_abi_filtersr   x  sw   , ,c2!:;Q!R!GG!!"46LMNN9;GIvIF+	 ))iy(()BINO1}"45KL	
 6 $%56	NN!!+	 ((NN!!"68NO N 	"45KL	
 ((NN!!"68NO NrU   c                   t        | |g|d|i|}t        t        |            }t               }|d   dk(  s|d   dk(  rt	        ||t                     S t        |g|i |}t        ||      \  }}	t	        |||	      S )af  
    Information about the function ABI, selector and input arguments.

    Returns the ABI which matches the provided identifier, named arguments (``args``)
    and keyword args (``kwargs``).

    :param abi: Contract ABI.
    :type abi: `ABI`
    :param abi_element_identifier: Find an element ABI with matching identifier.
    :type abi_element_identifier: `ABIElementIdentifier`
    :param args: Find a function ABI with matching args.
    :type args: `Optional[Sequence[Any]]`
    :param abi_codec: Codec used for encoding and decoding. Default with     `strict_bytes_type_checking` enabled.
    :type abi_codec: `Optional[Any]`
    :param kwargs: Find an element ABI with matching kwargs.
    :type kwargs: `Optional[Dict[str, Any]]`
    :return: Element information including the ABI, selector and args.
    :rtype: `ABIElementInfo`

    .. doctest::

        >>> from web3.utils.abi import get_abi_element_info
        >>> abi = [
        ...     {
        ...         "constant": False,
        ...         "inputs": [
        ...             {"name": "a", "type": "uint256"},
        ...             {"name": "b", "type": "uint256"},
        ...         ],
        ...         "name": "multiply",
        ...         "outputs": [{"name": "result", "type": "uint256"}],
        ...         "payable": False,
        ...         "stateMutability": "nonpayable",
        ...         "type": "function",
        ...     }
        ... ]
        >>> fn_info = get_abi_element_info(abi, "multiply", *[7, 3])
        >>> fn_info["abi"]
        {'constant': False, 'inputs': [{'name': 'a', 'type': 'uint256'}, {'name': 'b', 'type': 'uint256'}], 'name': 'multiply', 'outputs': [{'name': 'result', 'type': 'uint256'}], 'payable': False, 'stateMutability': 'nonpayable', 'type': 'function'}
        >>> fn_info["selector"]
        '0x165c4a16'
        >>> fn_info["arguments"]
        (7, 3)
    rG   r@   rA   rB   )r;   selectorr|   )get_abi_elementr&   r   rn   r   r    r   )
r;   r   rG   rH   rI   fn_abifn_selector	fn_inputs_aligned_fn_inputss
             r<   get_abi_element_infor     s    n #&*6?CIF ;FCDK!&If~#vf~'B&;%'RR-fFtFvF	5fiH8I
 	
rU   c          	         t        |        |t        t              }t        | gt	        |g|d|i| }t        |      }|dk7  r4t        || |t        |      t        |      z   g|d|i|}t        |      |d   S )a	  
    Return the interface for an ``ABIElement`` from the ``abi`` that matches the
    provided identifier and arguments.

    ``abi`` may be a list of all ABI elements in a contract or a subset of elements.
    Passing only functions or events can be useful when names are not deterministic.
    For example, if names overlap between functions and events.

    The ``ABIElementIdentifier`` value may be a function name, signature, or a
    ``FallbackFn`` or ``ReceiveFn``. When named arguments (``args``) and/or keyword args
    (``kwargs``) are provided, they are included in the search filters.

    The `abi_codec` may be overridden if custom encoding and decoding is required. The
    default is used if no codec is provided. More details about customizations are in
    the `eth-abi Codecs Doc <https://eth-abi.readthedocs.io/en/latest/codecs.html>`__.

    :param abi: Contract ABI.
    :type abi: `ABI`
    :param abi_element_identifier: Find an element ABI with matching identifier. The     identifier may be a function name, signature, or ``FallbackFn`` or ``ReceiveFn``.     A function signature is in the form ``name(arg1Type,arg2Type,...)``.
    :type abi_element_identifier: `ABIElementIdentifier`
    :param args: Find an element ABI with matching args.
    :type args: `Optional[Sequence[Any]]`
    :param abi_codec: Codec used for encoding and decoding. Default with     `strict_bytes_type_checking` enabled.
    :type abi_codec: `Optional[Any]`
    :param kwargs: Find an element ABI with matching kwargs.
    :type kwargs: `Optional[Dict[str, Any]]`
    :return: ABI element for the specific ABI element.
    :rtype: `ABIElement`

    .. doctest::

        >>> from web3.utils.abi import get_abi_element
        >>> abi = [
        ...     {
        ...         "constant": False,
        ...         "inputs": [
        ...             {"name": "a", "type": "uint256"},
        ...             {"name": "b", "type": "uint256"},
        ...         ],
        ...         "name": "multiply",
        ...         "outputs": [{"name": "result", "type": "uint256"}],
        ...         "payable": False,
        ...         "stateMutability": "nonpayable",
        ...         "type": "function",
        ...     }
        ... ]
        >>> get_abi_element(abi, "multiply", *[7, 3])
        {'constant': False, 'inputs': [{'name': 'a', 'type': 'uint256'}, {'name': 'b', 'type': 'uint256'}], 'name': 'multiply', 'outputs': [{'name': 'result', 'type': 'uint256'}], 'payable': False, 'stateMutability': 'nonpayable', 'type': 'function'}
    rG   rQ   r   )r.   r   r   r'   r   rD   r   r2   )r;   r   rG   rH   rI   abi_element_matchesr   error_diagnosiss           r<   r   r     s    | -.	041	"



  

 	

1 )*K a9"IF#	

 
  
 
 O,,q!!rU   rv   c                P   | d   dk(  s| d   dk(  ry	 t        | g|i |}t        | j                  dd            t        |      k7  ry	 t	        | |      \  }}t        t              t        fdt        ||      D              S # t        $ r Y yw xY w# t        $ r Y yw xY w)	a  
    Check if the provided arguments can be encoded with the element ABI.

    :param abi_element: The ABI element.
    :type abi_element: `ABIElement`
    :param args: Positional arguments.
    :type args: `Optional[Sequence[Any]]`
    :param abi_codec: Codec used for encoding and decoding. Default with     `strict_bytes_type_checking` enabled.
    :type abi_codec: `Optional[Any]`
    :param kwargs: Keyword arguments.
    :type kwargs: `Optional[Dict[str, Any]]`
    :return: True if the arguments can be encoded, False otherwise.
    :rtype: `bool`

    .. doctest::

            >>> from web3.utils.abi import check_if_arguments_can_be_encoded
            >>> abi = {
            ...     "constant": False,
            ...     "inputs": [
            ...         {"name": "a", "type": "uint256"},
            ...         {"name": "b", "type": "uint256"},
            ...     ],
            ...     "name": "multiply",
            ...     "outputs": [{"name": "result", "type": "uint256"}],
            ...     "payable": False,
            ...     "stateMutability": "nonpayable",
            ...     "type": "function",
            ... }
            >>> check_if_arguments_can_be_encoded(abi, *[7, 3], **{})
            True
    r@   rA   rB   TFrC    c              3   H   K   | ]  \  }}j                  ||        y wr:   )rr   ).0r   r   rG   s      r<   	<genexpr>z4check_if_arguments_can_be_encoded.<locals>.<genexpr>  s'      /9uc	uc*s   ")	r    ro   rD   rE   r   r   r   allrq   )rv   rG   rH   rI   r|   rz   r{   s    `     r<   rK   rK   n  s    N 6j(K,?9,L-kKDKFK	 ;??8R()S^;4[)L| -.	 =@=U      s#   B
 B 
	BB	B%$B%zuse get_abi_element instead
event_nameargument_namesc                    t        j                  t        d      g}||dk(  rt        d      |j	                  t        j                  t
        |             |)|j	                  t        j                  t        |             t        t        t           t        | g|       }t        |      dk(  r|d   S t        |      dk(  rt        d      t        d      )a  
    .. warning::
        This function is deprecated. It is unable to distinguish between
        overloaded events. Use ``get_abi_element`` instead.

    Find the event interface with the given name and/or arguments.

    :param abi: Contract ABI.
    :type abi: `ABI`
    :param event_name: Find an event abi with matching event name.
    :type event_name: `str`
    :param argument_names: Find an event abi with matching arguments.
    :type argument_names: `Optional[Sequence[str]]`
    :return: ABI for the event interface.
    :rtype: `ABIEvent`

    .. doctest::

        >>> from web3.utils import get_event_abi
        >>> abi = [
        ...   {"type": "function", "name": "myFunction", "inputs": [], "outputs": []},
        ...   {"type": "function", "name": "myFunction2", "inputs": [], "outputs": []},
        ...   {"type": "event", "name": "MyEvent", "inputs": []}
        ... ]
        >>> get_event_abi(abi, 'MyEvent')
        {'type': 'event', 'name': 'MyEvent', 'inputs': []}
    eventr^   z6event_name is required in order to match an event ABI.rQ   r   zNo matching events foundzMultiple events found)r   r   r   r3   r   r   r*   r   r   r   r'   rD   r4   )r;   r   r   r   event_abi_candidatess        r<   get_event_abir     s    D 	,g6>G Z2-!D
 	
 NN9$$%7DE!y(()@.QR 2D4Gw4GH
 A%#A&&	!	"a	'788455rU   	event_abitopicsc                     | d   r|S |rt        |      dk(  rt        d      t        |       t        |d         k7  rt        d      |dd S )ab  
    Return topics for an event ABI.

    :param event_abi: Event ABI.
    :type event_abi: `ABIEvent`
    :param topics: Transaction topics from a `LogReceipt`.
    :type topics: `Sequence[HexBytes]`
    :return: Event topics for the event ABI.
    :rtype: `Sequence[HexBytes]`

    .. doctest::

        >>> from web3.utils import get_event_log_topics
        >>> abi = {
        ...   'type': 'event',
        ...   'anonymous': False,
        ...   'name': 'MyEvent',
        ...   'inputs': [
        ...     {
        ...       'name': 's',
        ...       'type': 'uint256'
        ...     }
        ...   ]
        ... }
        >>> keccak_signature = b'l+Ff\xba\x8d\xa5\xa9W\x17b\x1d\x87\x9aw\xder_=\x81g\t\xb9\xcb\xe9\xf0Y\xb8\xf8u\xe2\x84'  # noqa: E501
        >>> get_event_log_topics(abi, [keccak_signature, '0x1', '0x2'])
        ['0x1', '0x2']
    	anonymousr   z5Expected non-anonymous event to have 1 or more topicsz2The event signature did not match the provided ABIrQ   N)rD   r2   r   log_topic_to_bytes)r   r   s     r<   get_event_log_topicsr     s[    @ s6{a'STT			*.@.K	KPQQabzrU   	log_topicc                 "    t        t        |       S )a\  
    Return topic signature as bytes.

    :param log_topic: Event topic from a `LogReceipt`.
    :type log_topic: `Union[Primitives, HexStr, str]`
    :return: Topic signature as bytes.
    :rtype: `bytes`

    .. doctest::

        >>> from web3.utils import log_topic_to_bytes
        >>> log_topic_to_bytes('0xa12fd1')
        b'\xa1/\xd1'
    r#   )r   s    r<   r   r     s    " 9--rU   )r   r   r:   )\r   typingr   r   r   r   r   r   r	   r
   r   eth_abir   eth_abi.codecr   eth_abi.registryr   r   
eth_typingr   r   r   r   r   r   r   r   r   r   eth_utils.abir   r   r   r   r   r   r   r    eth_utils.addressr!   r"   eth_utils.conversionsr$   r%   eth_utils.hexadecimalr&   eth_utils.toolzr'   eth_utils.typesr(   hexbytesr)   web3._utils.abir*   r+   r,   web3._utils.decoratorsr-   web3._utils.validationr.   web3.exceptionsr/   r0   r1   r2   r3   r4   
web3.typesr5   r   r=   intrF   
ABIEncoderrM   rT   rW   rY   rc   r   r   r   r   r   r   r   boolrK   r   r   bytesr   r   rU   r<   <module>r      sb   
 
 
  	 	 	 
 
OC Os OtJ?O O		&)		*	
3- cN 	
 
+
 
 
"WC WJ WYS Y[ Y!)*!5>I$	I$I$ I$ 	I$
 I$ 	I$^ 	B  $B0B	B B 	B
 C=B }B smB 	BJ$8C= $S $,"S "S "  $K0KC=K }K sm	K
 
(3,,
-.Kd  $	E
	E
0E
 HSM"E
 }	E

 tCH~&E
 E
X  $	]"	]"0]" C=]" }	]"
 sm]" ]"F  $<<HSM"< }< tCH~&	<
 
<~ -. /356	5656 Xc]+56 	56 /56p''X' h'T.Z,-.
.rU   