o
    ¢ØÆh-P  ã                   @   sÎ  d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	 d dl
mZmZmZ d dlmZmZmZ d dlmZmZmZ G dd„ dejƒZG d	d
„ d
ee jd�ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ de ƒZ!G dd„ de!ƒZ"G dd„ de!ƒZ#G d d!„ d!e!ƒZ$e$d"d#�Z%G d$d%„ d%e!ƒZ&G d&d'„ d'e!ƒZ'G d(d)„ d)e!ƒZ(G d*d+„ d+e(ƒZ)G d,d-„ d-e(ƒZ*G d.d/„ d/eƒZ+G d0d1„ d1e+ƒZ,dS )2é    N)ÚAnyÚ	Generator)Úbig_endian_to_intÚto_normalized_addressÚto_tuple)Ú	BaseCoderÚparse_tuple_type_strÚparse_type_str)ÚInsufficientDataBytesÚInvalidPointerÚNonEmptyPaddingBytes)ÚTENÚabi_decimal_contextÚceil32c                       sT   e Zd ZdZ‡ fdd„Zdedededdfd	d
„Zdeddfdd„Zdd„ Z	‡  Z
S )ÚContextFramesBytesIOa•  
    A byte stream which can track a series of contextual frames in a stack. This
    data structure is necessary to perform nested decodings using the
    :py:class:``HeadTailDecoder`` since offsets present in head sections are
    relative only to a particular encoded object.  These offsets can only be
    used to locate a position in a decoding stream if they are paired with a
    contextual offset that establishes the position of the object in which they
    are found.

    For example, consider the encoding of a value for the following type::

        type: (int,(int,int[]))
        value: (1,(2,[3,3]))

    There are two tuples in this type: one inner and one outer.  The inner tuple
    type contains a dynamic type ``int[]`` and, therefore, is itself dynamic.
    This means that its value encoding will be placed in the tail section of the
    outer tuple's encoding.  Furthermore, the inner tuple's encoding will,
    itself, contain a tail section with the encoding for ``[3,3]``.  All
    together, the encoded value of ``(1,(2,[3,3]))`` would look like this (the
    data values are normally 32 bytes wide but have been truncated to remove the
    redundant zeros at the beginnings of their encodings)::

                       offset data
        --------------------------
             ^              0 0x01
             |             32 0x40 <-- Offset of object A in global frame (64)
        -----|--------------------
        Global frame ^     64 0x02 <-- Beginning of object A (64 w/offset 0 = 64)
             |       |     96 0x40 <-- Offset of object B in frame of object A (64)
        -----|-Object A's frame---
             |       |    128 0x02 <-- Beginning of object B (64 w/offset 64 = 128)
             |       |    160 0x03
             v       v    192 0x03
        --------------------------

    Note that the offset of object B is encoded as 64 which only specifies the
    beginning of its encoded value relative to the beginning of object A's
    encoding.  Globally, object B is located at offset 128.  In order to make
    sense out of object B's offset, it needs to be positioned in the context of
    its enclosing object's frame (object A).
    c                    s"   t ƒ j|i |¤Ž g | _d| _d S )Nr   )ÚsuperÚ__init__Ú_framesÚ_total_offset)ÚselfÚargsÚkwargs©Ú	__class__© ú]/var/www/html/HyperOracle/crypto_trader/venv/lib/python3.10/site-packages/eth_abi/decoding.pyr   L   s   
zContextFramesBytesIO.__init__Úposr   r   ÚreturnNc                 O   s"   | j | j| g|¢R i |¤Ž dS )zV
        Seeks relative to the total offset of the current contextual frames.
        N)Úseekr   )r   r   r   r   r   r   r   Úseek_in_frameR   s   "z"ContextFramesBytesIO.seek_in_frameÚoffsetc                 C   s0   | j  ||  ¡ f¡ |  j|7  _|  d¡ dS )z½
        Pushes a new contextual frame onto the stack with the given offset and a
        return position at the current cursor position then seeks to the new
        total offset.
        r   N)r   ÚappendÚtellr   r   )r   r    r   r   r   Ú
push_frameX   s   zContextFramesBytesIO.push_framec                 C   sF   z	| j  ¡ \}}W n ty   tdƒ‚w |  j|8  _|  |¡ dS )zƒ
        Pops the current contextual frame off of the stack and returns the
        cursor to the frame's return position.
        zno frames to popN)r   ÚpopÚ
IndexErrorr   r   )r   r    Ú
return_posr   r   r   Ú	pop_framec   s   ÿzContextFramesBytesIO.pop_frame)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úintr   r   r#   r'   Ú__classcell__r   r   r   r   r       s    +r   c                   @   s>   e Zd ZdZdZejdedefdd„ƒZ	dedefdd„Z
d	S )
ÚBaseDecoderz²
    Base class for all decoder classes.  Subclass this if you want to define a
    custom decoder class.  Subclasses must also implement
    :any:`BaseCoder.from_type_str`.
    TÚstreamr   c                 C   s   dS )zÆ
        Decodes the given stream of bytes into a python value.  Should raise
        :any:`exceptions.DecodingError` if a python value cannot be decoded
        from the given byte stream.
        Nr   ©r   r/   r   r   r   Údecodez   s    zBaseDecoder.decodec                 C   s
   |   |¡S ©N©r1   r0   r   r   r   Ú__call__‚   s   
zBaseDecoder.__call__N)r(   r)   r*   r+   ÚstrictÚabcÚabstractmethodr   r   r1   r4   r   r   r   r   r.   q   s    r.   )Ú	metaclassc                       s:   e Zd ZdZdZdZ‡ fdd„Zdedefdd	„Z	‡  Z
S )
ÚHeadTailDecoderaW  
    Decoder for a dynamic element of a dynamic container (a dynamic array, or a sized
    array or tuple that contains dynamic elements). A dynamic element consists of a
    pointer, aka offset, which is located in the head section of the encoded container,
    and the actual value, which is located in the tail section of the encoding.
    TNc                    ó    t ƒ  ¡  | jd u rtdƒ‚d S )NzNo `tail_decoder` set)r   ÚvalidateÚtail_decoderÚ
ValueError©r   r   r   r   r;   ’   ó   

ÿzHeadTailDecoder.validater/   r   c                 C   s:   t |ƒ}| |¡ | jd u rtdƒ‚|  |¡}| ¡  |S )Nz`tail_decoder` is None)Údecode_uint_256r#   r<   ÚAssertionErrorr'   )r   r/   Ú	start_posÚvaluer   r   r   r1   ˜   s   


zHeadTailDecoder.decode)r(   r)   r*   r+   Ú
is_dynamicr<   r;   r   r   r1   r-   r   r   r   r   r9   †   s    r9   c                       sj   e Zd ZdZ‡ fdd„Z‡ fdd„Zdeddfdd	„Zedede	e
ddf fd
d„ƒZedd„ ƒZ‡  ZS )ÚTupleDecoderNc                    sB   t ƒ jdi |¤Ž tdd„ | jD ƒƒ| _tdd„ | jD ƒƒ| _d S )Nc                 s   s*   � | ]}t |d dƒrt|d�n|V  qdS )rD   F©r<   N)Úgetattrr9   ©Ú.0Údr   r   r   Ú	<genexpr>¯   s
   € ÿ
ÿz(TupleDecoder.__init__.<locals>.<genexpr>c                 s   s   � | ]	}t |d dƒV  qdS )rD   FN)rG   rH   r   r   r   rK   ´   s   € r   )r   r   ÚtupleÚdecodersÚanyrD   ©r   r   r   r   r   r   ¬   s
   
þzTupleDecoder.__init__c                    r:   )NzNo `decoders` set)r   r;   rM   r=   r>   r   r   r   r;   ¶   r?   zTupleDecoder.validater/   r   c           	      C   sš   |  ¡ }tdd„ | jD ƒƒ}|d|  }t| ¡ ƒ}| jD ](}t|tƒrAt|ƒ}|| }||k s4||kr@td|  ¡ d › d�ƒ‚q||ƒ q| 	|¡ dS )úS
        Verify that all pointers point to a valid location in the stream.
        c                 s   s$   � | ]}t |d ƒr|jndV  qdS )Ú
array_sizeé   N)ÚhasattrrQ   )rI   Údecoderr   r   r   rK   Á   s
   € ÿ
ÿz1TupleDecoder.validate_pointers.<locals>.<genexpr>é    z%Invalid pointer in tuple at location ú in payloadN)
r"   ÚsumrM   ÚlenÚ	getbufferÚ
isinstancer9   r@   r   r   )	r   r/   Úcurrent_locationÚlen_of_headÚend_of_offsetsÚtotal_stream_lengthrT   r    Úindicated_idxr   r   r   Úvalidate_pointers¼   s(   þ


ÿÿû
zTupleDecoder.validate_pointersc                 c   s&   � |   |¡ | jD ]}||ƒV  q	d S r2   )r`   rM   )r   r/   rT   r   r   r   r1   Þ   s
   €

ÿzTupleDecoder.decodec                    s"   t ‡ fdd„|jD ƒƒ}| |d�S )Nc                 3   s   � | ]
}ˆ   | ¡ ¡V  qd S r2   )Úget_decoderÚto_type_str)rI   Úc©Úregistryr   r   rK   æ   s   € 
ÿz-TupleDecoder.from_type_str.<locals>.<genexpr>)rM   )rL   Ú
components)ÚclsÚabi_typere   rM   r   rd   r   Úfrom_type_strä   s   ÿ
zTupleDecoder.from_type_str)r(   r)   r*   rM   r   r;   r   r`   r   r   r   r1   r   ri   r-   r   r   r   r   rE   ©   s    
"rE   c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚSingleDecoderNc                    r:   )NzNo `decoder_fn` set)r   r;   Ú
decoder_fnr=   r>   r   r   r   r;   ð   r?   zSingleDecoder.validatec                 C   ó   t dƒ‚©Nz!Must be implemented by subclasses©ÚNotImplementedError©r   rC   Úpadding_bytesr   r   r   Úvalidate_padding_bytesö   ó   z$SingleDecoder.validate_padding_bytesc                 C   sD   |   |¡}|  |¡\}}| jd u rtdƒ‚|  |¡}|  ||¡ |S )Nz`decoder_fn` is None)Úread_data_from_streamÚsplit_data_and_paddingrk   rA   rr   ©r   r/   Úraw_dataÚdatarq   rC   r   r   r   r1   ù   s   


zSingleDecoder.decodec                 C   rl   rm   rn   r0   r   r   r   rt     rs   z#SingleDecoder.read_data_from_streamc                 C   s   |dfS )Nó    r   )r   rw   r   r   r   ru     rs   z$SingleDecoder.split_data_and_padding)
r(   r)   r*   rk   r;   rr   r1   rt   ru   r-   r   r   r   r   rj   í   s    
rj   c                       sT   e Zd ZdZ‡ fdd„Z‡ fdd„Zedd�dd	„ ƒZd
ede	ddfdd„Z
‡  ZS )ÚBaseArrayDecoderNc                    s0   t ƒ jdi |¤Ž | jjrt| jd�| _d S d S )NrF   r   )r   r   Úitem_decoderrD   r9   rO   r   r   r   r     s   ÿÿzBaseArrayDecoder.__init__c                    r:   )NzNo `item_decoder` set)r   r;   r{   r=   r>   r   r   r   r;     r?   zBaseArrayDecoder.validateT)Úwith_arrlistc                 C   s@   |  |j ¡ ¡}|jd }t|ƒdkrt|d |d�S t|d�S )NéÿÿÿÿrR   r   )rQ   r{   )r{   )ra   Ú	item_typerb   ÚarrlistrX   ÚSizedArrayDecoderÚDynamicArrayDecoder)rg   rh   re   r{   Ú
array_specr   r   r   ri     s   
þ
zBaseArrayDecoder.from_type_strr/   rQ   r   c           	      C   s„   t | jtƒr@| ¡ }|d|  }t| ¡ ƒ}t|ƒD ]}t|ƒ}|| }||k s,||kr8td| ¡ d › d�ƒ‚q| 	|¡ dS dS )rP   rU   z%Invalid pointer in array at location rV   N)
rZ   r{   r9   r"   rX   rY   Úranger@   r   r   )	r   r/   rQ   r[   r]   r^   Ú_r    r_   r   r   r   r`   +  s"   
ÿÿû	îz"BaseArrayDecoder.validate_pointers)r(   r)   r*   r{   r   r;   r	   ri   r   r,   r`   r-   r   r   r   r   rz   
  s    	
rz   c                       s,   e Zd ZdZ‡ fdd„Zedd„ ƒZ‡  ZS )r€   Nc                    s    t ƒ jdi |¤Ž | jj| _d S )Nr   )r   r   r{   rD   rO   r   r   r   r   G  s   zSizedArrayDecoder.__init__c                 c   sB   � | j d u r
tdƒ‚|  || j¡ t| jƒD ]}|   |¡V  qd S )Nú`item_decoder` is None)r{   rA   r`   rQ   rƒ   )r   r/   r„   r   r   r   r1   L  s   €
ÿzSizedArrayDecoder.decode)r(   r)   r*   rQ   r   r   r1   r-   r   r   r   r   r€   D  s
    r€   c                   @   s   e Zd ZdZedd„ ƒZdS )r�   Tc                 c   sX   � t |ƒ}| d¡ | jd u rtdƒ‚|  ||¡ t|ƒD ]}|  |¡V  q| ¡  d S )NrU   r…   )r@   r#   r{   rA   r`   rƒ   r'   )r   r/   rQ   r„   r   r   r   r1   Z  s   €

zDynamicArrayDecoder.decodeN)r(   r)   r*   rD   r   r1   r   r   r   r   r�   V  s    r�   c                       sL   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Zdd„ Z	dd	„ Z
d
d„ Z‡  ZS )ÚFixedByteSizeDecoderNc                    s„   t ƒ  ¡  | jd u rtdƒ‚| jd u rtdƒ‚| jd u r tdƒ‚| jd u r)tdƒ‚| jd dkr4tdƒ‚| j| jd kr@tdƒ‚d S )	Nz `value_bit_size` may not be Nonez `data_byte_size` may not be Nonez`decoder_fn` may not be Nonez`is_big_endian` may not be Noneé   r   zFInvalid value bit size: {self.value_bit_size}. Must be a multiple of 8z!Value byte size exceeds data size)r   r;   Úvalue_bit_sizer=   Údata_byte_sizerk   Úis_big_endianr>   r   r   r   r;   m  s    




ÿÿzFixedByteSizeDecoder.validatec                 C   s:   |  | j¡}t|ƒ| jkrtd| j› dt|ƒ› d�ƒ‚|S )NúTried to read ú bytes, only got z bytes.)Úreadr‰   rX   r
   )r   r/   rx   r   r   r   rt   �  s   
ÿÿz*FixedByteSizeDecoder.read_data_from_streamc                 C   sX   |   ¡ }| j| }| jr|d |… }||d … }||fS |d |… }||d … }||fS r2   )Ú_get_value_byte_sizer‰   rŠ   )r   rw   Úvalue_byte_sizeÚpadding_sizerq   rx   r   r   r   ru   Œ  s   
ýz+FixedByteSizeDecoder.split_data_and_paddingc                 C   s4   |   ¡ }| j| }|d| krtdt|ƒ› �ƒ‚d S )Nó    úPadding bytes were not empty: ©rŽ   r‰   r   Úrepr)r   rC   rq   r�   r�   r   r   r   rr   ™  s   
ÿÿz+FixedByteSizeDecoder.validate_padding_bytesc                 C   s   | j d }|S )Nr‡   ©rˆ   )r   r�   r   r   r   rŽ   ¢  s   
z)FixedByteSizeDecoder._get_value_byte_size)r(   r)   r*   rk   rˆ   r‰   rŠ   r;   rt   ru   rr   rŽ   r-   r   r   r   r   r†   g  s    	r†   c                   @   s   e Zd ZdZdS )ÚFixed32ByteSizeDecoderrU   N)r(   r)   r*   r‰   r   r   r   r   r–   §  s    r–   c                   @   s0   e Zd ZdZdZedd„ ƒZedƒdd„ ƒZdS )	ÚBooleanDecoderr‡   Tc                 C   s*   | dkrdS | dkrdS t dt| ƒ› �ƒ‚)Nr‘   Fó   Tz)Boolean must be either 0x0 or 0x1.  Got: )r   r”   ©rx   r   r   r   rk   ¯  s   ÿzBooleanDecoder.decoder_fnÚboolc                 C   ó   | ƒ S r2   r   ©rg   rh   re   r   r   r   ri   º  ó   zBooleanDecoder.from_type_strN)	r(   r)   r*   rˆ   rŠ   Ústaticmethodrk   r	   ri   r   r   r   r   r—   «  s    

r—   c                   @   s,   e Zd ZdZdZeeƒZedƒdd„ ƒZ	dS )ÚAddressDecoderé    TÚaddressc                 C   r›   r2   r   rœ   r   r   r   ri   Ä  r�   zAddressDecoder.from_type_strN)
r(   r)   r*   rˆ   rŠ   rž   r   rk   r	   ri   r   r   r   r   rŸ   ¿  s    rŸ   c                   @   s(   e Zd ZeeƒZdZedƒdd„ ƒZdS )ÚUnsignedIntegerDecoderTÚuintc                 C   ó   | |j d�S ©Nr•   ©Úsubrœ   r   r   r   ri   Ð  ó   z$UnsignedIntegerDecoder.from_type_strN)	r(   r)   r*   rž   r   rk   rŠ   r	   ri   r   r   r   r   r¢   Ì  s
    r¢   é   r•   c                   @   s0   e Zd ZdZdd„ Zdd„ Zedƒdd„ ƒZd	S )
ÚSignedIntegerDecoderTc                 C   s,   t |ƒ}|d| jd  kr|d| j  S |S ©Né   rR   )r   rˆ   )r   rx   rC   r   r   r   rk   Þ  s   zSignedIntegerDecoder.decoder_fnc                 C   óJ   |   ¡ }| j| }|dkrd| }nd| }||kr#tdt|ƒ› �ƒ‚d S ©Nr   r‘   ó   ÿr’   r“   ©r   rC   rq   r�   r�   Úexpected_padding_bytesr   r   r   rr   å  ó   

ÿÿz+SignedIntegerDecoder.validate_padding_bytesr,   c                 C   r¤   r¥   r¦   rœ   r   r   r   ri   ó  r¨   z"SignedIntegerDecoder.from_type_strN)r(   r)   r*   rŠ   rk   rr   r	   ri   r   r   r   r   rª   Û  s    rª   c                   @   s,   e Zd ZdZedd„ ƒZedƒdd„ ƒZdS )ÚBytesDecoderFc                 C   ó   | S r2   r   r™   r   r   r   rk   þ  ó   zBytesDecoder.decoder_fnÚbytesc                 C   s   | |j d d�S )Nr‡   r•   r¦   rœ   r   r   r   ri     s   zBytesDecoder.from_type_strN)r(   r)   r*   rŠ   rž   rk   r	   ri   r   r   r   r   r³   û  s    
r³   c                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚBaseFixedDecoderNTc                    s<   t ƒ  ¡  | jd u rtdƒ‚| jdks| jdkrtdƒ‚d S )Nzmust specify `frac_places`r   éP   z&`frac_places` must be in range (0, 80])r   r;   Úfrac_placesr=   r>   r   r   r   r;     s   

ÿzBaseFixedDecoder.validate)r(   r)   r*   r¹   rŠ   r;   r-   r   r   r   r   r·     s    r·   c                   @   s$   e Zd Zdd„ Zedƒdd„ ƒZdS )ÚUnsignedFixedDecoderc                 C   sL   t |ƒ}t t¡� t |¡t| j  }W d   ƒ |S 1 sw   Y  |S r2   )r   ÚdecimalÚlocalcontextr   ÚDecimalr   r¹   )r   rx   rC   Údecimal_valuer   r   r   rk     s   
ÿýzUnsignedFixedDecoder.decoder_fnÚufixedc                 C   ó   |j \}}| ||d�S ©N)rˆ   r¹   r¦   ©rg   rh   re   rˆ   r¹   r   r   r   ri     ó   
z"UnsignedFixedDecoder.from_type_strN)r(   r)   r*   rk   r	   ri   r   r   r   r   rº     s    rº   c                   @   s,   e Zd Zdd„ Zdd„ Zedƒdd„ ƒZdS )	ÚSignedFixedDecoderc                 C   sr   t |ƒ}|d| jd  kr|d| j  }n|}t t¡� t |¡t| j  }W d   ƒ |S 1 s2w   Y  |S r«   )r   rˆ   r»   r¼   r   r½   r   r¹   )r   rx   rC   Úsigned_valuer¾   r   r   r   rk   &  s   
ÿýzSignedFixedDecoder.decoder_fnc                 C   r­   r®   r“   r°   r   r   r   rr   2  r²   z)SignedFixedDecoder.validate_padding_bytesÚfixedc                 C   rÀ   rÁ   r¦   rÂ   r   r   r   ri   @  rÃ   z SignedFixedDecoder.from_type_strN)r(   r)   r*   rk   rr   r	   ri   r   r   r   r   rÄ   %  s
    rÄ   c                   @   s<   e Zd ZdZedd„ ƒZdd„ Zdd„ Zedƒd	d
„ ƒZ	dS )ÚByteStringDecoderTc                 C   r´   r2   r   r™   r   r   r   rk   M  rµ   zByteStringDecoder.decoder_fnc                 C   s€   t |ƒ}t|ƒ}| |¡}| jr:t|ƒ|k r#td|› dt|ƒ› d�ƒ‚||d … }|d||  kr:tdt|ƒ› �ƒ‚|d |… S )Nr‹   rŒ   z bytesr‘   r’   )r@   r   r�   r5   rX   r
   r   r”   )r   r/   Údata_lengthÚpadded_lengthrx   rq   r   r   r   rt   Q  s   
ÿÿz'ByteStringDecoder.read_data_from_streamc                 C   s   d S r2   r   rp   r   r   r   rr   e  s   z(ByteStringDecoder.validate_padding_bytesr¶   c                 C   r›   r2   r   rœ   r   r   r   ri   h  r�   zByteStringDecoder.from_type_strN)
r(   r)   r*   rD   rž   rk   rt   rr   r	   ri   r   r   r   r   rÇ   J  s    
rÇ   c                       sD   e Zd Zd‡ fdd„	Zedƒdd„ ƒZdd„ Zedd	d
„ƒZ‡  Z	S )ÚStringDecoderr5   c                    s   || _ tƒ  ¡  d S r2   )Úbytes_errorsr   r   )r   Úhandle_string_errorsr   r   r   r   n  s   zStringDecoder.__init__Ústringc                 C   r›   r2   r   rœ   r   r   r   ri   r  r�   zStringDecoder.from_type_strc                 C   s6   |   |¡}|  |¡\}}|  || j¡}|  ||¡ |S r2   )rt   ru   rk   rË   rr   rv   r   r   r   r1   v  s
   
zStringDecoder.decodec                 C   s   | j d|d�S )Nzutf-8)Úerrorsr3   )rx   rÌ   r   r   r   rk   }  s   zStringDecoder.decoder_fn)r5   )
r(   r)   r*   r   r	   ri   r1   rž   rk   r-   r   r   r   r   rÊ   m  s    
rÊ   )-r6   r»   ÚioÚtypingr   r   Ú	eth_utilsr   r   r   Úeth_abi.baser   r   r	   Úeth_abi.exceptionsr
   r   r   Úeth_abi.utils.numericr   r   r   ÚBytesIOr   ÚABCMetar.   r9   rE   rj   rz   r€   r�   r†   r–   r—   rŸ   r¢   r@   rª   r³   r·   rº   rÄ   rÇ   rÊ   r   r   r   r   Ú<module>   s:    Q#D:@
	 %#