
    (i(4                        d dl Z d dlZd dlmZmZmZmZmZ d dl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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 m!Z!m"Z"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/m0Z0 d d
l1m2Z2 d dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z= deee
f   de>fdZ?de	ddfdZ@deddfdZAdededdfdZBdedeCfdZDdeddfdZEdededeCfdZFdededdfdZGdhZHdZIde=ddfdZJd)de=d e>ddfd!ZK	 d*de=d"eej                     defd#ZM	 	 	 d+de=d$eed%ef      d&eCd"eej                     d'ee   ddfd(ZNy),    N)AnyCallableDictNoReturnOptional)ABIABIFunctionHexStrTypeStr)abi_to_signaturefilter_abi_by_typefunction_abi_to_4byte_selectoris_0x_prefixedis_binary_address
is_booleanis_bytesis_checksum_addressis_dictis_hex_address
is_integeris_list_like	is_string)apply_formatter_to_array)
encode_hex)composegroupby	valfiltervalmap)is_valid_ens_name)
is_address_typeis_array_typeis_bool_typeis_bytes_typeis_int_typeis_recognized_typeis_string_typeis_uint_typelength_of_array_typesub_type_of_array_type)apply_error_formatters)BadResponseFormatInvalidAddressMethodUnavailableRequestTimedOutTransactionNotFoundWeb3RPCErrorWeb3TypeErrorWeb3ValueError)RPCResponse
duplicatesreturnc                     t        t        t              |       }t        d |      }|j                         D cg c]  \  }}|dz   |z    }}}dj	                  |      S c c}}w )Nc                 $    dj                  |       S )N, )joinfuncss    G/var/www/br/venv/lib/python3.12/site-packages/web3/_utils/validation.py<lambda>z1_prepare_selector_collision_msg.<locals>.<lambda>P   s    		%(8     z have selector z and
)r   r   r   itemsr9   )r4   dup_seljoined_funcsselr;   func_sel_msg_lists         r<   _prepare_selector_collision_msgrD   N   sl    -.>?LG8'BL:F:L:L:N,6C!!C'  ==*++s   A abic                 D   t        |       st        d      t        d | D              st        d      t        d | D              st        d      t        d|       }t	        t        t        t              |      }t        d |      }|rt        dt        |             y	)
z/
    Helper function for validating an ABI
    z'abi' is not a listc              3   2   K   | ]  }t        |        y wN)r   .0es     r<   	<genexpr>zvalidate_abi.<locals>.<genexpr>^   s     'awqz's   z#'abi' is not a list of dictionariesc              3   $   K   | ]  }d |v  
 yw)typeN rI   s     r<   rL   zvalidate_abi.<locals>.<genexpr>a   s     (qv{(s   z6'abi' must contain a list of elements each with a typefunctionc                     t        |       dkD  S )N   )lenr:   s    r<   r=   zvalidate_abi.<locals>.<lambda>f   s    Ua r>   z;Abi contains functions with colliding selectors. Functions N)
r   r2   allr   r   r   r   r   r   rD   )rE   	functions	selectorsr4   s       r<   validate_abirW   W   s     233'3''BCC(C((UVV":s3I
,JKYWI7CJ8DEG
 	
 r>   abi_typec                 6    t        |       st        d|        y)z4
    Helper function for validating an abi_type
    zUnrecognized abi_type: N)r%   r2   )rX   s    r<   validate_abi_typerZ   n   s$     h'6xjABB (r>   valuec                 p   t        |       rjt        |      r_t        |       }|3|dk  rt        d|  d      |t	        |      k7  rt        d|  d|       t        |       }|D ]  }t        ||        yt        |       rt        |      ryt        |       rt        |      r|dk\  ryt        |       rt        |      ryt        |       rt        |       yt        |       r.t        |      ryt!        |      r.t#        |      ryt        d      t%        |       rt!        |      ryt        d	|  d
|       )z
    Helper function for validating a value against the expected abi_type
    Note: abi_type 'bytes' must either be python3 'bytes' object or ''
    NrR   zInvalid abi-type: z6. Length of fixed sized arrays must be greater than 0.zPThe following array length does not match the length specified by the abi-type, z: r   zaABI values of abi-type 'bytes' must be eithera python3 'bytes' object or an '0x' prefixed string.z"The following abi value is not a 'z': )r!   r   r(   r1   rS   r)   validate_abi_valuer"   r   r'   r   r$   r    validate_addressr#   r   r   r   r&   )rX   r[   specified_lengthsub_typevs        r<   r]   r]   v   sV   
 X<#6/9'!##(
 35 5   3u:-#((0zE7<  *(3 	,Ax+	,	h	Ju$5	h	Ju$5%1*	X	:e#4		"	x	 E?ue$#K  
	!i&6
<XJc%Q
RRr>   c                 l    t        |       xr( t        |        xr t        |        xr t        |        S rH   )r   r   r   r   r[   s    r<   is_not_address_stringrd      s@    % 	&	&#E**	& u%%	r>   c                 n   t        |       rt        |       st        d|  d      yt        |       rt	        |       st        d|       yt        | t              st        d|  d      t        |       st        d|       t        |       s+| | j                         k(  rt        d|       t        d	|       y)
z3
    Helper function for validating an address
    zENS name: 'z' is invalid.Nz1Address must be 20 bytes when input type is byteszAddress z must be provided as a stringz:Address must be 20 bytes, as a hex string with a 0x prefixa  web3.py only accepts checksum addresses. The software that gave you this non-checksum address should be considered unsafe, please file it as a bug on their platform. Try using an ENS name instead. Or, if you must accept lower safety, use Web3.to_checksum_address(lower_case_address).ziAddress has an invalid EIP-55 checksum. After looking up the address from the original source, try again.)rd   r   r,   r   r   
isinstancestrr1   r   r   lowerrc   s    r<   r^   r^      s     U# ' ;ug]!CDD ' CU  	eS!hug-JKLL% H%
 	
 u%EKKM! D
   !T  &r>   argskwargsc                      t        j                  | |j                               }t        t	        d |            }t        |      dk(  S )Nc                 
    | d uS rH   rO   )vals    r<   r=   zhas_one_val.<locals>.<lambda>   s
    4 r>   rR   )	itertoolschainvalueslistfilterrS   )ri   rj   vals	not_noness       r<   has_one_valru      s9    ??41DV7>?Iy>Qr>   c                  <    t        | i |st        d| d|      y )NzIExactly one of the passed values can be specified. Instead, values were: r8   )ru   r1   )ri   rj   s     r<   assert_one_valrw      s6    ''%%)HBvj:
 	
 (r>   zrequest timed outiresponsec                 p    | d   }|d   }t        |t              st        |      dk(  st        | d       y y y )Nparamssubscription"   z>eth_subscription 'params' must include a 'subscription' field.)rf   rg   rS   _raise_bad_response_format)rx   rz   r{   s      r<   _validate_subscription_fieldsr~      sC    hF.)LlC(\1Bb1H"V	
 2I(r>   errorc                     d}d|  }|2|dk7  r-|j                  d      r|d d n|}| d| d| }t        |      | d| }t        |      )NzAThe response was in an unexpected format and unable to be parsed.zThe raw response is:  . z. )endswithr+   )rx   r   messageraw_responses       r<   r}   r}      sw    QG*8*5LUb[#nnS1cr
uIQugR~6 G
$$ IQ|n-
G
$$r>   loggerc                 v    | j                  d      }|t        | d       t        | d d|g        t        d      )Nr   z_Batch response must be formatted as a list of responses or as a single JSON-RPC error response.F)is_subscription_responser   rz   zCBatch response was in an unexpected format and unable to be parsed.)getr}   (validate_rpc_response_and_raise_if_errorr+   )rx   r   r   s      r<   raise_error_for_batch_responser   	  sR     LL!E}"3	

 -!& M r>   error_formatters.r   rz   c                 x    d vs d   dk7  rt         d        j                  d      }d v rId}|d v rnZt        |t        t        f      st         |       n7t        |t              r'	 t	        |       n|rt                nt         d       t         fdd	D              rt         d
       y t         fdd	D              s|st         d       y d v r0d } d   }|t        |t              st         d       |j                  d      t        t              st         d       n%dk(  r |d   }	t        t        |       d|	d      }|j                  d      }
t        |
t              st         d       nP|
t        k(  rt        t        |       d      }n/t        fdt        D              rt        t        |       d      }|t!        t        |             }t#        |        ||j%                  d        |d vr|st                y y y # t
        $ r t         |       Y w xY w)Njsonrpcz2.0z:The "jsonrpc" field must be present with a value of "2.0".idzA"id" must be an integer or a string representation of an integer.r   zVResponse must include an "id" field or be formatted as an `eth_subscription` response.c              3   &   K   | ]  }|v  
 y wrH   rO   rJ   keyrx   s     r<   rL   z;validate_rpc_response_and_raise_if_error.<locals>.<genexpr>F  s     
:s3(?
:   >   r   resultz2Response cannot include both "error" and "result".c              3   &   K   | ]  }|v  
 y wrH   rO   r   s     r<   rL   z;validate_rpc_response_and_raise_if_error.<locals>.<genexpr>K  s     ?Cx?r   z1Response must include either "error" or "result".zVresponse["error"] must be a valid object as defined by the JSON-RPC 2.0 specification.r   z8error["message"] is required and must be a string value.ztransaction not foundr   zTransaction with hash z not found.)rpc_responseuser_messagecodez7error["code"] is required and must be an integer value.zThis method is not available. Check your node provider or your client's API docs to see what methods are supported and / or currently enabled.c              3   B   K   | ]  }|j                         v   y wrH   )rh   )rJ   timeout_strerror_messages     r<   rL   z;validate_rpc_response_and_raise_if_error.<locals>.<genexpr>{  s)      
  =..00
s   zGThe request timed out. Check the connection to your node and try again.)r   zRPC error response: %sr   )r}   r   rf   rg   int
ValueErrorr~   rT   anydictr/   reprMETHOD_NOT_FOUNDr-   KNOWN_REQUEST_TIMEOUT_MESSAGINGr.   r0   r*   debug)rx   r   r   r   rz   response_idint_error_msgweb3_rpc_errorr   transaction_hashr   r   s   `          @r<   r   r   !  sy     HY$75$@"R	
 ,,t$KxO 	 7h#6K#s4&x?S)DK  
"%h/"+	
 
:&9
::"J	
 ?+>??("I	
 
H	15! =
5$ 7&. 		),--&T 55%ay0U% 67G6J+VN yy $$&S %%.U%)	N  
  ?
 

 -U%!	N !)$u+HMN)*:HELL18<		!*B"8, +C	!s  D*8]CDs   -H   H98H9)r   rH   )FNN)Orn   loggingtypingr   r   r   r   r   
eth_typingr   r	   r
   r   	eth_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   eth_utils.curriedr   eth_utils.hexadecimalr   eth_utils.toolzr   r   r   r   	ens.utilsr   web3._utils.abir    r!   r"   r#   r$   r%   r&   r'   r(   r)   web3._utils.formattersr*   web3.exceptionsr+   r,   r-   r.   r/   r0   r1   r2   
web3.typesr3   rg   rD   rW   rZ   r]   boolrd   r^   ru   rw   r   r   r~   r}   Loggerr   r   rO   r>   r<   <module>r      s            	 	 	
,V[5H0I ,c ,
c 
d 
.C CD C0S 0S 0S 0Sf  $C $D $Ns c d 
# 
 
 
 #   
K 
D 

% 
%S 
%$ 
% (,W^^$ 6 &+'+ s-s-xS12s- #s- W^^$	s-
 SMs- 
s-r>   