
    (iRP                     :   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 d dl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 d dl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% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ d dlAmBZBmCZCmDZDmEZE d dlFmGZGmHZHmIZImJZJ erd dlKmLZL d dlMmNZN  G d de2      ZO G d de3eO         ZP G d de4      ZQ G d de5eQ         ZR G d de/      ZS G d d e0      ZT G d! d"e1      ZUy#)$    )
TYPE_CHECKINGAnyCallableDictIterableListOptionalSequenceTypecast)ABIChecksumAddress)abi_to_signaturecombomethodfilter_abi_by_typeget_abi_input_names)partial)HexBytes)fallback_func_abi_existsreceive_func_abi_exists)
FallbackFn	ReceiveFn)Self)parse_block_identifier)PropertyCheckingFactory)EventFilterBuilderget_event_data)	LogFilter)normalize_abinormalize_addressnormalize_bytecode)fill_transaction_defaults)	BaseContractBaseContractCallerBaseContractConstructorBaseContractEventBaseContractEventsBaseContractFunctionBaseContractFunctionsNonExistentFallbackFunctionNonExistentReceiveFunction)build_transaction_for_functioncall_contract_functionestimate_gas_for_functionfind_events_by_identifierfind_functions_by_identifierget_event_by_identifierget_function_by_identifiertransact_with_contract_function)Web3AttributeErrorWeb3TypeErrorWeb3ValidationErrorWeb3ValueError)BlockIdentifier	EventDataStateOverrideTxParams)ENS)Web3c                       e Zd ZU ded<   e	 	 	 	 ddeeeef      dee	   dee	   dee
   dee   f
d	       Zeddd
ddddeeeef      dee	   de	dee   deee      defd       Zedefd       Zy)ContractEventr=   w3Nargument_filters
from_blockto_block
block_hashreturnc                    
  j                         
|8t        
      t        fd|j                         D              st	        d       j                  
||||      } j                  j                  j                  |      }t        
 fd|D              } j                  
||      }t        |d       }	t        |	d       }	|	S )a/  
        Get events for this contract instance using eth_getLogs API.

        This is a stateless method, as opposed to create_filter.
        It can be safely called against nodes which do not provide
        eth_newFilter API, like Infura nodes.

        If there are many events,
        like ``Transfer`` events for a popular token,
        the Ethereum node might be overloaded and timeout
        on the underlying JSON-RPC call.

        Example - how to get all ERC-20 token transactions
        for the latest 10 blocks:

        .. code-block:: python

            from = max(my_contract.web3.eth.block_number - 10, 1)
            to = my_contract.web3.eth.block_number

            events = my_contract.events.Transfer.get_logs(from_block=from, to_block=to)

            for e in events:
                print(e["args"]["from"],
                    e["args"]["to"],
                    e["args"]["value"])

        The returned processed log values will look like:

        .. code-block:: python

            (
                AttributeDict({
                 'args': AttributeDict({}),
                 'event': 'LogNoArguments',
                 'logIndex': 0,
                 'transactionIndex': 0,
                 'transactionHash': HexBytes('...'),
                 'address': '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b',
                 'blockHash': HexBytes('...'),
                 'blockNumber': 3
                }),
                AttributeDict(...),
                ...
            )

        See also: :func:`web3.middleware.filter.LocalFilterMiddleware`.

        :param argument_filters: Filter by argument values. Indexed arguments are
          filtered by the node while non-indexed arguments are filtered by the library.
        :param from_block: block number or "latest", defaults to "latest"
        :param to_block: block number or "latest". Defaults to "latest"
        :param block_hash: block hash. block_hash cannot be set at the
          same time as ``from_block`` or ``to_block``
        :yield: Tuple of :class:`AttributeDict` instances
        c              3   &   K   | ]  }|v  
 y wN ).0argevent_arg_namess     G/var/www/br/venv/lib/python3.12/site-packages/web3/contract/contract.py	<genexpr>z)ContractEvent.get_logs.<locals>.<genexpr>   s     Q#so-Qs   zaWhen filtering by argument names, all argument names must be present in the contract's event ABI.c              3   `   K   | ]%  }t        j                  j                  |       ' y wrH   )r   r@   codec)rJ   entry	event_abiselfs     rM   rN   z)ContractEvent.get_logs.<locals>.<genexpr>   s'      
@EN477==)U;
s   +.c                     | d   S )NlogIndexrI   es    rM   <lambda>z(ContractEvent.get_logs.<locals>.<lambda>   s
    !J-     )keyc                     | d   S )NblockNumberrI   rV   s    rM   rX   z(ContractEvent.get_logs.<locals>.<lambda>   s    -8H rY   )_get_event_abir   allkeysr6   _get_event_filter_paramsr@   ethget_logstuple"_process_get_logs_argument_filterssorted)rS   rA   rB   rC   rD   _filter_paramslogsall_event_logsfiltered_logssorted_logsrR   rL   s   `         @@rM   rb   zContractEvent.get_logsj   s    @ '')	'1)<OQ9I9N9N9PQQ); 
 66'Xz
 ww{{##N3  
IM
 
 ??

 ]0GH[.HIrY   latest)rA   rB   rC   addresstopicsrl   rm   c                    | j                         }t        || j                  j                        }| j	                  ||||||       |j                  | j                        }t        | j                  j                  |      |_        ||_        |S )zW
        Create filter object that tracks logs emitted by this contract event.
        )	r]   r   r@   rP   _set_up_filter_builderdeployr   log_entry_formatterbuilder)	rS   rA   rB   rC   rl   rm   abifilter_builder
log_filters	            rM   create_filterzContractEvent.create_filter   s     !!#+C?##	
 $**4773
)7s)K
&+
rY   c                     | j                         }t        || j                  j                  t	        | j                  j                  |            }| j
                  |_        |S )N)	formatter)r]   r   r@   rP   r   rl   )rS   rs   rr   s      rM   build_filterzContractEvent.build_filter   sL    !!#$GGMM$TWW]]C8

 ,,rY   NNNN)__name__
__module____qualname____annotations__r   r	   r   strr   r8   r   r   r9   rb   r   r
   r   rv   r   ry   rI   rY   rM   r?   r?   f   s   J 6:04.2)-Z"4S>2Z _-Z ?+	Z
 X&Z 
)	Z Zx  6:04$,-1*. #4S>2 _-	
 " /* #' 
 8 0  rY   r?   c            	       :     e Zd Z	 ddedddee   ddf fdZ xZS )	ContractEventsNrs   r@   r=   rl   rE   c                 2    t         |   ||t        |       y rH   )super__init__r?   )rS   rs   r@   rl   	__class__s       rM   r   zContractEvents.__init__   s     	b-9rY   rH   )r{   r|   r}   r   r	   r   r   __classcell__r   s   @rM   r   r      s5    IM::":-5o-F:	: :rY   r   c                      e Zd ZU ded<   	 	 	 	 ddee   dee   dee   dee   de	f
d	Z
ddee   defd
Z	 	 	 ddee   dee   dee   defdZddee   defdZe	 ddedddee   dd fd       Ze	 ddedddee   dd fd       Zy)ContractFunctionr=   r@   Ntransactionblock_identifierstate_overrideccip_read_enabledrE   c                 \   | j                  |      }t        | j                  |      }t        | j                        }t        | j                  | j                  | j                  |||| j                  | j                  ||| j                  g| j                  xs di | j                  xs i S )aE  
        Execute a contract function call using the `eth_call` interface.

        This method prepares a ``Caller`` object that exposes the contract
        functions and public variables as callable Python functions.

        Reading a public ``owner`` address variable example:

        .. code-block:: python

            ContractFactory = w3.eth.contract(
                abi=wallet_contract_definition["abi"]
            )

            # Not a real contract address
            contract = ContractFactory("0x2f70d3d26829e412A602E83FE8EeBF80255AEeA5")

            # Read "owner" public variable
            addr = contract.functions.owner().call()

        :param transaction: Dictionary of transaction info for web3 interface
        :param block_identifier: Block number or string "latest", "pending", "earliest"
        :param state_override: Dictionary of state override values
        :param ccip_read_enabled: Enable CCIP read operations for the call
        :return: ``Caller`` object that has contract public functions
            and variables exposed as Python methods
        rI   )_get_call_txparamsr   r@   r   rs   r-   rl   _return_data_normalizerscontract_abidecode_tuplesargskwargs)rS   r   r   r   r   call_transactionblock_idabi_element_identifiers           rM   callzContractFunction.call   s    D  22;?)$''3CD!1$((!;%GGLL))"HH
 YY_"
 kkR
 	
rY   c                     | j                  |      }t        | j                        }t        | j                  | j
                  ||| j                  | j                  g| j                  xs di | j                  xs i S NrI   )		_transactr   rs   r3   rl   r@   r   r   r   )rS   r   setup_transactionr   s       rM   transactzContractFunction.transact3  sv     NN;7!1$((!;.LLGG"HH	
 YY_"	
 kkR	
 		
rY   c           
         | j                  |      }t        | j                        }t        | j                  | j
                  ||| j                  | j                  ||g| j                  xs di | j                  xs i S r   )	_estimate_gasr   rs   r.   rl   r@   r   r   r   )rS   r   r   r   r   r   s         rM   estimate_gaszContractFunction.estimate_gasB  s     !..{;!1$((!;(LLGG"HH
 YY_"
 kkR
 	
rY   c                     | j                  |      }t        | j                        }t        | j                  | j
                  ||| j                  | j                  g| j                  xs di | j                  xs i S r   )	_build_transactionr   rs   r,   rl   r@   r   r   r   )rS   r   built_transactionr   s       rM   build_transactionz"ContractFunction.build_transactionW  sx     33K@!1$((!;-LLGG"HH	
 YY_"	
 kkR	
 		
rY   rs   rl   c           	          | r=t        |       r2t        d|       d   }t        j                  d|| |t        |             S t        t        t                     S )Nfallbackr   r@   r   rl   r   rs   )r   r   r   factoryr   r   r*   )rs   r@   rl   fallback_abis       rM   get_fallback_functionz&ContractFunction.get_fallback_functionf  sc     +C0-j#>qAL#++ '1  ,    $&A&CDDrY   c           	          | r=t        |       r2t        d|       d   }t        j                  d|| |t        |             S t        t        t                     S )Nreceiver   r   )r   r   r   r   r   r   r+   )rs   r@   rl   receive_abis       rM   get_receive_functionz%ContractFunction.get_receive_functionx  sc     *3/,Y<Q?K#++ '0 ,    $&@&BCCrY   rz   rH   NNN)r{   r|   r}   r~   r	   r;   r8   r:   boolr   r   r   r   intr   r   staticmethodr   r   r   r   rI   rY   rM   r   r      sc   J +/6:26,06
h'6
 #?36
 !/	6

 $D>6
 
6
p
HX$6 
( 
" +/6:26	
h'
 #?3
 !/	

 

*
Xh-? 
8 
  .2EEE /*E 
	E E"  .2DDD /*D 
	D DrY   r   c                   F     e Zd Z	 	 d	dedddee   dee   ddf
 fdZ xZS )
ContractFunctionsNrs   r@   r=   rl   r   rE   c                 4    t         |   ||t        ||       y rH   )r   r   r   )rS   rs   r@   rl   r   r   s        rM   r   zContractFunctions.__init__  s     	b"2G]KrY   )NF)	r{   r|   r}   r   r	   r   r   r   r   r   s   @rM   r   r     sU    
 .2(-LL L /*	L
  ~L 
L LrY   r   c                   ^   e Zd ZU ded<   dZeed<   dZded<   dZeed<   dde	e
   d	dfd
Ze	 dddde	e   ded	ee   fd       Zededed	dfd       Zededdde
dedef   d	ed   f
d       Zeded   ded	dfd       Zededdde
dedef   d	ed   f
d       Zeded   ded	dfd       Zy)Contractr=   r@   N	functionsContractCallercallereventsrl   rE   c                    | j                   }|t        d      |r%t        t        d|j                        |      | _        | j
                  st        d      t        | j                  || j
                  | j                        | _
        t        | j                  || j
                  | j                  | j                        | _        t        | j                  || j
                        | _        t        j!                  | j                  |t"        | j
                        | _        t        j'                  | j                  |t"        | j
                        | _        y)zu
        Create a new smart contract proxy object.
        :param address: Contract address as 0x hex string
        NzwThe `Contract` class has not been initialized.  Please use the `web3.contract` interface to create your contract class.r<   z;The address argument is required to instantiate a contract.r   r   contract_functions)r@   r4   r    r   _ensrl   r5   r   rs   r   r   r   r   r   r   r   r   r   r   r   r   )rS   rl   _w3s      rM   r   zContract.__init__  s   
 gg;$K 
 ,T%-BGLDL||M  +HHc4<<t7I7I
 %HHLL,,#~~
 %TXXsDLLA 66HHLL	
  44HHLL	
rY   
class_namer   c           	      X   ||d<   t         t        t        |j                        t        t        d}t        t        t           t        |xs | j                  | f||            }|j                  r5|j                  D ]&  }|j                  d      }|dv st        d| d       t        |j                  |j                  |j                        |_        t#        |j                  |j                  |j$                  |j                  |j                   	      |_        t)        |j                  |j                        |_        t,        j/                  |j                  |j                  t0              |_        t,        j5                  |j                  |j                  t0              |_        |S )
Nr@   )rs   rl   bytecodebytecode_runtime)normalizersname)rs   rl   z#Contract contains a reserved word `z ` and could not be instantiated.r   r   )r   r   r    r   r!   r   r   r   r   r{   rs   getr4   r   r@   r   r   r   rl   r   r   r   r   r   r   r   r   r   )clsr@   r   r   r   contractrs   abi_names           rM   r   zContract.factory  su    t !0"'':* 2
 J#*cll'	
 <<|| 776?11,=hZ H9 :  /LL(++X5K5K
 )LLKK"00'11
 )x{{C$::LLKK

 $88LLKK
 rY   r   ContractConstructorc                     | j                   t        d      t        | j                  | j                  | j                   g|i |S )z
        :param args: The contract constructor arguments as positional arguments
        :param kwargs: The contract constructor arguments as keyword arguments
        :return: a contract constructor object
        zVCannot call constructor on a contract that does not have 'bytecode' associated with it)r   r7   r   r@   rs   )r   r   r   s      rM   constructorzContract.constructor  sG     << 0 
 #366377CLLR4R6RRrY   r   callable_check.r   c           
      J    t        t        d   t        ||||t                    S )Nr   )r   r   r0   r   r   r   r@   rl   r   s        rM   r0   z%Contract.find_functions_by_identifier  s.     #$(b'>;K
 	
rY   fns
identifierc                     t        ||      S rH   )r2   )r   r   r   s      rM   r2   z#Contract.get_function_by_identifier#  s     *#z::rY   r?   c                 (    t        ||||t              S rH   )r/   r?   r   s        rM   r/   z"Contract.find_events_by_identifier)  s     )"g~}
 	
rY   c                     t        ||      S rH   )r1   )rS   r   r   s      rM   r1   z Contract.get_event_by_identifier5  s     'vz::rY   rH   )r{   r|   r}   r~   r   r   r   r   r   r	   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r0   r
   r2   r/   r1   rI   rY   rM   r   r     s   J#'I '#F# "FN!+
 9 +
T +
Z 5966%-c]6EH6	d6 6p S Ss S7L S S 

 
 !	

 !c*
 
 	!
 
 ;-.;<?;	; ;
 	
	
 	
 !		

 !c*	
 
o		
 	
 ;/;=@;	; ;rY   r   c                        e Zd ZU ded<   	 	 	 	 	 ddedddedee   dedee	   d	ee	   d
ee
   ddf fdZ	 	 	 ddee   dedee	   dd fdZ xZS )r   r=   r@   Nrs   rl   r   r   r   r   r   rE   c	                 0   t         |   ||||       | j                  rv|i }|t        ||||      }|j                  | _        |j                         D ]<  }	t        | j                  |	|||      }
t        | t        |	j                        |
       > y y )Nr   )rl   r   )r   r   r   )r   r   rs   r   
_functions__iter__r   call_functionsetattrr   r   )rS   rs   r@   rl   r   r   r   r   r   fncaller_methodr   s              rM   r   zContractCaller.__init__@  s     	b'G88" !)%6WM&" 1;;DO(113 M '&& +%5&7! c"";";<mLM rY   c           	          |i } t        |       | j                  | j                  | j                  |||| j                        S )N)r   r   r   r   )typers   r@   rl   r   )rS   r   r   r   s       rM   __call__zContractCaller.__call__a  sI     KtDzHHGGLL#-/,,
 	
rY   )NNNFNr   )r{   r|   r}   r~   r   r   r	   r;   r8   r   r   r   r   r   r   s   @rM   r   r   <  s    J +/,0,0(-:>MM M !	M
 h'M *M $D>M  ~M %%67M 
MF +/,0,0	
h'
 *
 $D>	

 

rY   r   c                       e Zd ZU ded<   ed
dee   defd       Zed
dee   defd       Z	e	 	 ddee   dee
   defd	       Zy)r   r=   r@   Nr   rE   c                 j    | j                   j                  j                  | j                  |            S rH   )r@   ra   send_transaction_get_transaction)rS   r   s     rM   r   zContractConstructor.transacty  s&    ww{{++D,A,A+,NOOrY   c                 P    | j                  |      }t        | j                  |      S )zB
        Build the transaction dictionary without sending
        )r   r"   r@   )rS   r   r   s      rM   r   z%ContractConstructor.build_transaction}  s'    
 !33K@(2CDDrY   r   c                 r    | j                  |      }| j                  j                  j                  ||      S )N)r   )r   r@   ra   r   )rS   r   r   s      rM   r   z ContractConstructor.estimate_gas  s3     ((5ww{{''FV'WWrY   rH   )NN)r{   r|   r}   r~   r   r	   r;   r   r   r   r8   r   r   rI   rY   rM   r   r   u  s    JPHX$6 P( P P EXh-? E8 E E  +/6:Xh'X #?3X 
	X XrY   r   N)Vtypingr   r   r   r   r   r   r	   r
   r   r   
eth_typingr   r   	eth_utilsr   r   r   r   eth_utils.toolzr   hexbytesr   web3._utils.abir   r   #web3._utils.abi_element_identifiersr   r   web3._utils.compatr   web3._utils.contractsr   web3._utils.datatypesr   web3._utils.eventsr   r   web3._utils.filtersr   web3._utils.normalizersr   r    r!   web3._utils.transactionsr"   web3.contract.base_contractr#   r$   r%   r&   r'   r(   r)   r*   r+   web3.contract.utilsr,   r-   r.   r/   r0   r1   r2   r3   web3.exceptionsr4   r5   r6   r7   
web3.typesr8   r9   r:   r;   ensr<   web3r=   r?   r   r   r   r   r   r   rI   rY   rM   <module>r      s      

 
 
	 	 	   G% GT:'6 :QD+ QDhL-.>? Lc;| c;L6
' 6
rX1 XrY   