
    ":hA                     n   d dl Z d dlZd dlZd dl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
ej*                        Z G d de
e j.                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de       Z! G d  d!e       Z" G d" d#e       Z# e#d$%      Z$ G d& d'e       Z% G d( d)e       Z& G d* d+e       Z' G d, d-e'      Z( G d. d/e'      Z) G d0 d1e      Z* G d2 d3e*      Z+y)4    N)Any   )big_endian_to_intto_normalized_addressto_tuple   )	BaseCoderparse_tuple_type_strparse_type_str)DecodingErrorInsufficientDataBytesNonEmptyPaddingBytes)TENabi_decimal_contextceil32c                   4     e Zd ZdZ fdZd Zd Zd Z x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                 @    t        |   |i | g | _        d| _        y )Nr   )super__init___frames_total_offset)selfargskwargs	__class__s      h/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/static_dependencies/ethereum/abi/decoding.pyr   zContextFramesBytesIO.__init__K   s$    $)&)    c                 J     | j                   | j                  |z   g|i | y)zV
        Seeks relative to the total offset of the current contextual frames.
        N)seekr   )r   posr   r   s       r   seek_in_framez"ContextFramesBytesIO.seek_in_frameQ   s'     			$$$s*<T<V<r   c                     | j                   j                  || j                         f       | xj                  |z  c_        | j	                  d       y)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   offsets     r   
push_framezContextFramesBytesIO.push_frameW   s@     	VTYY[12f$1r   c                     	 | j                   j                         \  }}| xj                  |z  c_        | j	                  |       y# t        $ r t        d      w xY w)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_poss      r   	pop_framezContextFramesBytesIO.pop_frameb   sZ    
	1!%!1!1!3FJ 	f$		*	  	1/00	1s   A A)	__name__
__module____qualname____doc__r   r"   r'   r,   __classcell__r   s   @r   r   r      s    )V=	r   r   c                   N    e Zd ZdZej
                  dedefd       ZdedefdZ	y)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`.
    streamreturnc                      y)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.
        N r   r5   s     r   decodezBaseDecoder.decodew   s     	r   c                 $    | j                  |      S N)r:   r9   s     r   __call__zBaseDecoder.__call__   s    {{6""r   N)
r-   r.   r/   r0   abcabstractmethodr   r   r:   r=   r8   r   r   r4   r4   p   sD     	1 c  #3 # #r   r4   )	metaclassc                   ,     e Zd ZdZdZ fdZd Z xZS )HeadTailDecoderTNc                 P    t         |           | j                  t        d      y )NzNo `tail_decoder` set)r   validatetail_decoder
ValueErrorr   r   s    r   rD   zHeadTailDecoder.validate   +    $455 %r   c                     t        |      }|j                  |       | j                  |      }|j                          |S r<   )decode_uint_256r'   rE   r,   )r   r5   	start_posvalues       r   r:   zHeadTailDecoder.decode   s;    #F+	)$!!&)r   )r-   r.   r/   
is_dynamicrE   rD   r:   r1   r2   s   @r   rB   rB      s    JL6r   rB   c                   L     e Zd ZdZ fdZ fdZed        Zed        Z	 xZ
S )TupleDecoderNc                     t        |   di | t        d | j                  D              | _        t	        d | j                  D              | _        y )Nc              3   R   K   | ]  }t        |d d      rt        |      n| ! yw)rM   FrE   N)getattrrB   .0ds     r   	<genexpr>z(TupleDecoder.__init__.<locals>.<genexpr>   s.      
 07q,/NO+TUU
s   %'c              3   6   K   | ]  }t        |d d        yw)rM   FN)rS   rT   s     r   rW   z(TupleDecoder.__init__.<locals>.<genexpr>   s     U!gau=Us   r8   )r   r   tupledecodersanyrM   r   r   r   s     r   r   zTupleDecoder.__init__   sH    "6" 
]]
 

 Ut}}UUr   c                 P    t         |           | j                  t        d      y )NzNo `decoders` set)r   rD   rZ   rF   rG   s    r   rD   zTupleDecoder.validate   s)    == 011 !r   c              #   B   K   | j                   D ]  } ||        y wr<   rZ   )r   r5   decoders      r   r:   zTupleDecoder.decode   s#     }} 	"G&/!	"s   c                 R    t        fd|j                  D              } | |      S )Nc              3   \   K   | ]#  }j                  |j                                % y wr<   )get_decoderto_type_str)rU   cregistrys     r   rW   z-TupleDecoder.from_type_str.<locals>.<genexpr>   s'      
67H  1
s   ),r_   )rY   
components)clsabi_typerf   rZ   s     ` r   from_type_strzTupleDecoder.from_type_str   s-     
;C;N;N
 
 H%%r   )r-   r.   r/   rZ   r   rD   r   r:   r
   rj   r1   r2   s   @r   rO   rO      s;    HV2 " " & &r   rO   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )SingleDecoderNc                 P    t         |           | j                  t        d      y )NzNo `decoder_fn` set)r   rD   
decoder_fnrF   rG   s    r   rD   zSingleDecoder.validate   s)    ??"233 #r   c                     t        d      Nz!Must be implemented by subclassesNotImplementedErrorr   rL   padding_bytess      r   validate_padding_bytesz$SingleDecoder.validate_padding_bytes       !"EFFr   c                     | j                  |      }| j                  |      \  }}| j                  |      }| j                  ||       |S r<   )read_data_from_streamsplit_data_and_paddingrn   ru   )r   r5   raw_datadatart   rL   s         r   r:   zSingleDecoder.decode   sJ    --f5"99(Cm%##E=9r   c                     t        d      rp   rq   r9   s     r   rx   z#SingleDecoder.read_data_from_stream   rv   r   c                 
    |dfS )Nr   r8   )r   rz   s     r   ry   z$SingleDecoder.split_data_and_padding   s    }r   )
r-   r.   r/   rn   rD   ru   r:   rx   ry   r1   r2   s   @r   rl   rl      s#    J4GGr   rl   c                   J     e Zd ZdZ fdZ fdZ ed      d        Z xZS )BaseArrayDecoderNc                     t        |   di | | j                  j                  rt	        | j                        | _        y y )NrR   r8   )r   r   item_decoderrM   rB   r\   s     r   r   zBaseArrayDecoder.__init__   s?    "6" '' /!..!D (r   c                 P    t         |           | j                  t        d      y )NzNo `item_decoder` set)r   rD   r   rF   rG   s    r   rD   zBaseArrayDecoder.validate   rH   r   T)with_arrlistc                     |j                  |j                  j                               }|j                  d   }t	        |      dk(  rt        |d   |      S t        |      S )Nr   r   )
array_sizer   )r   )rc   	item_typerd   arrlistlenSizedArrayDecoderDynamicArrayDecoder)rh   ri   rf   r   
array_specs        r   rj   zBaseArrayDecoder.from_type_str   sa    ++H,>,>,J,J,LM%%b)
z?a$%a=)  'LAAr   )	r-   r.   r/   r   r   rD   r   rj   r1   r2   s   @r   r   r      s-    L6 &B 'Br   r   c                   2     e Zd ZdZ fdZed        Z xZS )r   Nc                 Z    t        |   di | | j                  j                  | _        y )Nr8   )r   r   r   rM   r\   s     r   r   zSizedArrayDecoder.__init__   s&    "6"++66r   c              #   f   K   t        | j                        D ]  }| j                  |        y wr<   )ranger   r   )r   r5   _s      r   r:   zSizedArrayDecoder.decode   s/     t' 	,A##F++	,s   /1)r-   r.   r/   r   r   r   r:   r1   r2   s   @r   r   r      s!    J7
 , ,r   r   c                        e Zd ZdZed        Zy)r   Tc              #      K   t        |      }|j                  d       t        |      D ]  }| j                  |        |j	                          y w)N    )rJ   r'   r   r   r,   )r   r5   r   r   s       r   r:   zDynamicArrayDecoder.decode	  sO     $V,
"z" 	,A##F++	,s   AAN)r-   r.   r/   rM   r   r:   r8   r   r   r   r     s    J r   r   c                   F     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d Z xZS )FixedByteSizeDecoderNc                    t         |           | j                  t        d      | j                  t        d      | j
                  t        d      | j                  t        d      | j                  dz  dk7  r$t        dj                  | j                              | j                  | j                  dz  kD  rt        d      y )	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   z5Invalid value bit size: {0}.  Must be a multiple of 8z!Value byte size exceeds data size)r   rD   value_bit_sizerF   data_byte_sizern   is_big_endianformatrG   s    r   rD   zFixedByteSizeDecoder.validate  s    &?@@&?@@??";<<%>??"a'GNN''  !4!4q!88@AA 9r   c                     |j                  | j                        }t        |      | j                  k7  r.t        dj	                  | j                  t        |                  |S )N,Tried to read {0} bytes.  Only got {1} bytes)readr   r   r   r   )r   r5   r{   s      r   rx   z*FixedByteSizeDecoder.read_data_from_stream.  sZ    {{4../t9+++'>EE''I  r   c                     | j                         }| j                  |z
  }| j                  r|d | }||d  }||fS |d | }||d  }||fS r<   )_get_value_byte_sizer   r   )r   rz   value_byte_sizepadding_sizert   r{   s         r   ry   z+FixedByteSizeDecoder.split_data_and_padding;  su    335**_<$]l3MLM*D
 ]"" ,_-D$_%56M]""r   c                     | j                         }| j                  |z
  }|d|z  k7  r#t        dj                  t	        |                  y )N    !Padding bytes were not empty: {0}r   r   r   r   repr)r   rL   rt   r   r   s        r   ru   z+FixedByteSizeDecoder.validate_padding_bytesH  sR    335**_<Gl22&3::4;NO  3r   c                 $    | j                   dz  }|S )Nr   r   )r   r   s     r   r   z)FixedByteSizeDecoder._get_value_byte_sizeQ  s    --2r   )r-   r.   r/   rn   r   r   r   rD   rx   ry   ru   r   r1   r2   s   @r   r   r     s1    JNNMB,#r   r   c                       e Zd ZdZy)Fixed32ByteSizeDecoderr   N)r-   r.   r/   r   r8   r   r   r   r   V  s    Nr   r   c                   @    e Zd ZdZdZed        Z ed      d        Zy)BooleanDecoderr   Tc                 `    | dk(  ry| dk(  ryt        dj                  t        |                   )Nr   F   Tz,Boolean must be either 0x0 or 0x1.  Got: {0})r   r   r   r{   s    r   rn   zBooleanDecoder.decoder_fn^  s5    7?W_&>EEd4jQ r   boolc                      |        S r<   r8   rh   ri   rf   s      r   rj   zBooleanDecoder.from_type_stri  	    ur   N)	r-   r.   r/   r   r   staticmethodrn   r   rj   r8   r   r   r   r   Z  s7    NM  F r   r   c                   @    e Zd ZdZdZ ee      Z ed      d        Z	y)AddressDecoder   Taddressc                      |        S r<   r8   r   s      r   rj   zAddressDecoder.from_type_strs  r   r   N)
r-   r.   r/   r   r   r   r   rn   r   rj   r8   r   r   r   r   n  s.    NM34JI r   r   c                   <    e Zd Z ee      ZdZ ed      d        Zy)UnsignedIntegerDecoderTuintc                 (     | |j                         S Nr   subr   s      r   rj   z$UnsignedIntegerDecoder.from_type_str      (,,//r   N)	r-   r.   r/   r   r   rn   r   r   rj   r8   r   r   r   r   {  s)    /0JMF0 0r   r      r   c                   8    e Zd ZdZd Zd Z ed      d        Zy)SignedIntegerDecoderTc                 j    t        |      }|d| j                  dz
  z  k\  r|d| j                  z  z
  S |S Nr   r   )r   r   )r   r{   rL   s      r   rn   zSignedIntegerDecoder.decoder_fn  s?    !$'A$--1221d11111Lr   c                     | j                         }| j                  |z
  }|dk\  rd|z  }nd|z  }||k7  r#t        dj                  t	        |                  y Nr   r      r   r   r   rL   rt   r   r   expected_padding_bytess         r   ru   z+SignedIntegerDecoder.validate_padding_bytes  k    335**_<A:%,|%;"%,|%;"22&3::4;NO  3r   intc                 (     | |j                         S r   r   r   s      r   rj   z"SignedIntegerDecoder.from_type_str  r   r   N)r-   r.   r/   r   rn   ru   r   rj   r8   r   r   r   r     s*    M E0 0r   r   c                   <    e Zd ZdZed        Z ed      d        Zy)BytesDecoderFc                     | S r<   r8   r   s    r   rn   zBytesDecoder.decoder_fn      r   bytesc                 .     | |j                   dz        S )Nr   r   r   r   s      r   rj   zBytesDecoder.from_type_str  s    (,,"233r   N)r-   r.   r/   r   r   rn   r   rj   r8   r   r   r   r     s2    M  G4 4r   r   c                   &     e Zd ZdZdZ fdZ xZS )BaseFixedDecoderNTc                     t         |           | j                  t        d      | j                  dk  s| j                  dkD  rt        d      y )Nzmust specify `frac_places`r   P   z&`frac_places` must be in range (0, 80])r   rD   frac_placesrF   rG   s    r   rD   zBaseFixedDecoder.validate  sR    #9::q D$4$4r$9EFF %:r   )r-   r.   r/   r   r   rD   r1   r2   s   @r   r   r     s    KMG Gr   r   c                   .    e Zd Zd Z ed      d        Zy)UnsignedFixedDecoderc                     t        |      }t        j                  t              5  t        j                  |      t
        | j                  z  z  }d d d        |S # 1 sw Y   S xY wr<   )r   decimallocalcontextr   Decimalr   r   )r   r{   rL   decimal_values       r   rn   zUnsignedFixedDecoder.decoder_fn  s\    !$'!!"56 	K#OOE2S$:J:J5JJM	K 	K s   *AA#ufixedc                 4    |j                   \  }} | ||      S N)r   r   r   rh   ri   rf   r   r   s        r   rj   z"UnsignedFixedDecoder.from_type_str      &.ll#.kJJr   N)r-   r.   r/   rn   r   rj   r8   r   r   r   r     s"     HK Kr   r   c                   4    e Zd Zd Zd Z ed      d        Zy)SignedFixedDecoderc                     t        |      }|d| j                  dz
  z  k\  r|d| j                  z  z
  }n|}t        j                  t              5  t        j
                  |      t        | j                  z  z  }d d d        |S # 1 sw Y   S xY wr   )r   r   r   r   r   r   r   r   )r   r{   rL   signed_valuer   s        r   rn   zSignedFixedDecoder.decoder_fn  s    !$'A$--122 1d&9&9#99L L!!"56 	R#OOL9CAQAQ<QQM	R 	R s   *BBc                     | j                         }| j                  |z
  }|dk\  rd|z  }nd|z  }||k7  r#t        dj                  t	        |                  y r   r   r   s         r   ru   z)SignedFixedDecoder.validate_padding_bytes  r   r   fixedc                 4    |j                   \  }} | ||      S r   r   r   s        r   rj   z SignedFixedDecoder.from_type_str  r   r   N)r-   r.   r/   rn   ru   r   rj   r8   r   r   r   r     s'    
 GK Kr   r   c                   R    e Zd ZdZed        Zed        Zd Z ed      d        Z	y)ByteStringDecoderTc                     | S r<   r8   r   s    r   rn   zByteStringDecoder.decoder_fn  r   r   c                 $   t        |       }t        |      }| j                  |      }t        |      |k  r$t	        dj                  |t        |                  ||d  }|d||z
  z  k7  r#t        dj                  t        |                  |d | S )Nr   r   r   )rJ   r   r   r   r   r   r   r   )r5   data_lengthpadded_lengthr{   rt   s        r   rx   z'ByteStringDecoder.read_data_from_stream   s    %f-{+{{=)t9}$'>EE!I  [\*G}{'BCC&3::4;NO  L[!!r   c                      y r<   r8   rs   s      r   ru   z(ByteStringDecoder.validate_padding_bytes  s    r   r   c                      |        S r<   r8   r   s      r   rj   zByteStringDecoder.from_type_str  r   r   N)
r-   r.   r/   rM   r   rn   rx   ru   r   rj   r8   r   r   r   r     sK    J  " ". G r   r   c                   8    e Zd Z ed      d        Zed        Zy)StringDecoderstringc                      |        S r<   r8   r   s      r   rj   zStringDecoder.from_type_str!  r   r   c                     	 | j                  d      }|S # t        $ r=}t        |j                  |j                  |j
                  |j                  d      |d }~ww xY w)Nzutf-8zThe returned type for this function is string which is expected to be a UTF8 encoded string of text. The returned value could not be decoded as valid UTF8. This is indicative of a broken application which is using incorrect return types for binary data.)r:   UnicodeDecodeErrorr   encodingobjectstartend)r{   rL   es      r   rn   zStringDecoder.decoder_fn%  sa    	KK(E  " 	


 
	s    	A8AAN)r-   r.   r/   r   rj   r   rn   r8   r   r   r   r      s-    H   r   r   ),r>   r   iotypingr   utilsr   r   r   baser	   r
   r   
exceptionsr   r   r   utils.numericr   r   r   BytesIOr   ABCMetar4   rB   rO   rl   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r8   r   r   <module>r
     s`   
  	  
 
 N2:: Nb#)s{{ #(k *&; &BK 6B{ BD,( ,
* 
A= AH1 + (+ 03 0 )<01 0@	4) 	4G- GK+ K K) KJ$ $N% r   