
    E#iA                     b   S SK r S SKrS SKrS SKJr  SSKJrJrJr  SSK	J
r
JrJr  SSKJrJrJr  SSKJrJrJr   " S	 S
\R*                  5      r " S S\
\ R.                  S9r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\ 5      r! " S  S!\ 5      r" " S" S#\ 5      r#\#" S$S%9r$ " S& S'\ 5      r% " S( S)\ 5      r& " S* S+\ 5      r' " S, S-\'5      r( " S. S/\'5      r) " S0 S1\5      r* " S2 S3\*5      r+g)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                   >   ^  \ rS rSrSrU 4S jrS rS rS rSr	U =r
$ )ContextFramesBytesIO   a  
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                 B   > [         TU ]  " U0 UD6  / U l        SU l        g )Nr   )super__init___frames_total_offset)selfargskwargs	__class__s      h/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/static_dependencies/ethereum/abi/decoding.pyr   ContextFramesBytesIO.__init__K   s$    $)&)    c                 J    U R                   " U R                  U-   /UQ70 UD6  g)zF
Seeks relative to the total offset of the current contextual frames.
N)seekr   )r   posr   r   s       r   seek_in_frame"ContextFramesBytesIO.seek_in_frameQ   s%     			$$$s*<T<V<r    c                     U R                   R                  XR                  5       45        U =R                  U-  sl        U R	                  S5        g)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_frameContextFramesBytesIO.push_frameW   s>     	VYY[12f$1r    c                      U R                   R                  5       u  pU =R                  U-  sl        U R	                  U5        g! [         a    [        S5      ef = f)zk
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_frameContextFramesBytesIO.pop_frameb   sX    
	1!%!1!1!3F 	f$		*	  	1/00	1s   A A)r   r   )__name__
__module____qualname____firstlineno____doc__r   r$   r*   r0   __static_attributes____classcell__r   s   @r   r   r      s"    )V=	 r    r   c                   Z    \ rS rSrSr\R                  S\S\4S j5       r	S\S\4S jr
Srg)	BaseDecoderp   z
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                     g)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   r=   s     r   decodeBaseDecoder.decodew   s     	r    c                 $    U R                  U5      $ N)rB   rA   s     r   __call__BaseDecoder.__call__   s    {{6""r    r@   N)r2   r3   r4   r5   r6   abcabstractmethodr   r   rB   rF   r7   r@   r    r   r;   r;   p   sD     	1 c  #3 # #r    r;   )	metaclassc                   6   ^  \ rS rSrSrSrU 4S jrS rSrU =r	$ )HeadTailDecoder   TNc                 R   > [         TU ]  5         U R                  c  [        S5      eg )NzNo `tail_decoder` set)r   validatetail_decoder
ValueErrorr   r   s    r   rO   HeadTailDecoder.validate   +    $455 %r    c                     [        U5      nUR                  U5        U R                  U5      nUR                  5         U$ rE   )decode_uint_256r*   rP   r0   )r   r=   	start_posvalues       r   rB   HeadTailDecoder.decode   s;    #F+	)$!!&)r    r@   )
r2   r3   r4   r5   
is_dynamicrP   rO   rB   r7   r8   r9   s   @r   rL   rL      s    JL6 r    rL   c                   X   ^  \ rS rSrSrU 4S jrU 4S jr\S 5       r\	S 5       r
SrU =r$ )TupleDecoder   Nc                    > [         TU ]  " S0 UD6  [        S U R                   5       5      U l        [	        S U R                   5       5      U l        g )Nc              3   \   #    U  H"  n[        US S5      (       a	  [        US9OUv   M$     g7f)rZ   FrP   N)getattrrL   .0ds     r   	<genexpr>(TupleDecoder.__init__.<locals>.<genexpr>   s0      
" 07q,/N/NO+TUU"s   *,c              3   <   #    U  H  n[        US S5      v   M     g7f)rZ   FN)ra   rb   s     r   re   rf      s     U}!gau==}s   r@   )r   r   tupledecodersanyrZ   r   r   r   s     r   r   TupleDecoder.__init__   sH    "6" 
]]
 

 Ut}}UUr    c                 R   > [         TU ]  5         U R                  c  [        S5      eg )NzNo `decoders` set)r   rO   ri   rQ   rR   s    r   rO   TupleDecoder.validate   s)    == 011 !r    c              #   F   #    U R                    H  nU" U5      v   M     g 7frE   ri   )r   r=   decoders      r   rB   TupleDecoder.decode   s     }}G&/! %s   !c                 N   ^ [        U4S jUR                   5       5      nU " US9$ )Nc              3   b   >#    U  H$  nTR                  UR                  5       5      v   M&     g 7frE   )get_decoderto_type_str)rc   cregistrys     r   re   -TupleDecoder.from_type_str.<locals>.<genexpr>   s)      
;NaH  11;Ns   ,/rp   )rh   
components)clsabi_typerx   ri   s     ` r   from_type_strTupleDecoder.from_type_str   s-     
;C;N;N
 
 H%%r    )ri   rZ   )r2   r3   r4   r5   ri   r   rO   r   rB   r
   r}   r7   r8   r9   s   @r   r\   r\      s;    HV2 " " & &r    r\   c                   D   ^  \ rS rSrSrU 4S jrS rS rS rS r	Sr
U =r$ )	SingleDecoder   Nc                 R   > [         TU ]  5         U R                  c  [        S5      eg )NzNo `decoder_fn` set)r   rO   
decoder_fnrQ   rR   s    r   rO   SingleDecoder.validate   s)    ??"233 #r    c                     [        S5      eNz!Must be implemented by subclassesNotImplementedErrorr   rX   padding_bytess      r   validate_padding_bytes$SingleDecoder.validate_padding_bytes       !"EFFr    c                     U R                  U5      nU R                  U5      u  p4U R                  U5      nU R                  XT5        U$ rE   )read_data_from_streamsplit_data_and_paddingr   r   )r   r=   raw_datadatar   rX   s         r   rB   SingleDecoder.decode   sF    --f5"99(C%##E9r    c                     [        S5      er   r   rA   s     r   r   #SingleDecoder.read_data_from_stream   r   r    c                 
    US4$ )Nr    r@   )r   r   s     r   r   $SingleDecoder.split_data_and_padding   s    }r    r@   )r2   r3   r4   r5   r   rO   r   rB   r   r   r7   r8   r9   s   @r   r   r      s(    J4GG r    r   c                   P   ^  \ rS rSrSrU 4S jrU 4S jr\" SS9S 5       rSr	U =r
$ )	BaseArrayDecoder   Nc                    > [         TU ]  " S0 UD6  U R                  R                  (       a  [	        U R                  S9U l        g g )Nr`   r@   )r   r   item_decoderrZ   rL   rk   s     r   r   BaseArrayDecoder.__init__   s?    "6" '' /!..!D (r    c                 R   > [         TU ]  5         U R                  c  [        S5      eg )NzNo `item_decoder` set)r   rO   r   rQ   rR   s    r   rO   BaseArrayDecoder.validate   rT   r    T)with_arrlistc                     UR                  UR                  R                  5       5      nUR                  S   n[	        U5      S:X  a  [        US   US9$ [        US9$ )Nr   r   )
array_sizer   r   )ru   	item_typerv   arrlistlenSizedArrayDecoderDynamicArrayDecoder)r{   r|   rx   r   
array_specs        r   r}   BaseArrayDecoder.from_type_str   sa    ++H,>,>,J,J,LM%%b)
z?a$%a=)  'LAAr    r   )r2   r3   r4   r5   r   r   rO   r   r}   r7   r8   r9   s   @r   r   r      s-    L6 &B 'Br    r   c                   <   ^  \ rS rSrSrU 4S jr\S 5       rSrU =r	$ )r      Nc                 \   > [         TU ]  " S0 UD6  U R                  R                  U l        g )Nr@   )r   r   r   rZ   rk   s     r   r   SizedArrayDecoder.__init__   s&    "6"++66r    c              #   j   #    [        U R                  5       H  nU R                  U5      v   M     g 7frE   )ranger   r   )r   r=   _s      r   rB   SizedArrayDecoder.decode   s)     t'A##F++ (s   13)rZ   )
r2   r3   r4   r5   r   r   r   rB   r7   r8   r9   s   @r   r   r      s!    J7
 , ,r    r   c                   (    \ rS rSrSr\S 5       rSrg)r   i  Tc              #      #    [        U5      nUR                  S5        [        U5       H  nU R                  U5      v   M     UR	                  5         g 7f)N    )rV   r*   r   r   r0   )r   r=   r   r   s       r   rB   DynamicArrayDecoder.decode	  sI     $V,
"z"A##F++ #s   AAr@   N)r2   r3   r4   r5   rZ   r   rB   r7   r@   r    r   r   r     s    J r    r   c                   P   ^  \ rS rSrSrSrSrSrU 4S jrS r	S r
S rS rSrU =r$ )	FixedByteSizeDecoderi  Nc                   > [         TU ]  5         U R                  c  [        S5      eU R                  c  [        S5      eU R
                  c  [        S5      eU R                  c  [        S5      eU R                  S-  S:w  a$  [        SR                  U R                  5      5      eU R                  U R                  S-  :  a  [        S5      eg )	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   rO   value_bit_sizerQ   data_byte_sizer   is_big_endianformatrR   s    r   rO   FixedByteSizeDecoder.validate  s    &?@@&?@@??";<<%>??"a'GNN''  !4!4q!88@AA 9r    c                     UR                  U R                  5      n[        U5      U R                  :w  a.  [        SR	                  U R                  [        U5      5      5      eU$ )N,Tried to read {0} bytes.  Only got {1} bytes)readr   r   r   r   )r   r=   r   s      r   r   *FixedByteSizeDecoder.read_data_from_stream.  sZ    {{4../t9+++'>EE''I  r    c                     U R                  5       nU R                  U-
  nU R                  (       a  US U nXS  nXT4$ US U nXS  nXT4$ rE   )_get_value_byte_sizer   r   )r   r   value_byte_sizepadding_sizer   r   s         r   r   +FixedByteSizeDecoder.split_data_and_padding;  sm    335**_<$]l3MM*D
 "" ,_-D$%56M""r    c                     U R                  5       nU R                  U-
  nUSU-  :w  a#  [        SR                  [	        U5      5      5      eg )N    !Padding bytes were not empty: {0}r   r   r   r   repr)r   rX   r   r   r   s        r   r   +FixedByteSizeDecoder.validate_padding_bytesH  sR    335**_<Gl22&3::4;NO  3r    c                 $    U R                   S-  nU$ )Nr   r   )r   r   s     r   r   )FixedByteSizeDecoder._get_value_byte_sizeQ  s    --2r    r@   )r2   r3   r4   r5   r   r   r   r   rO   r   r   r   r   r7   r8   r9   s   @r   r   r     s6    JNNMB,# r    r   c                       \ rS rSrSrSrg)Fixed32ByteSizeDecoderiV  r   r@   N)r2   r3   r4   r5   r   r7   r@   r    r   r   r   V  s    Nr    r   c                   H    \ rS rSrSrSr\S 5       r\" S5      S 5       r	Sr
g)	BooleanDecoderiZ  r   Tc                 d    U S:X  a  gU S:X  a  g[        SR                  [        U 5      5      5      e)Nr   F   Tz,Boolean must be either 0x0 or 0x1.  Got: {0})r   r   r   r   s    r   r   BooleanDecoder.decoder_fn^  s5    7?W_&>EEd4jQ r    boolc                     U " 5       $ rE   r@   r{   r|   rx   s      r   r}   BooleanDecoder.from_type_stri  	    ur    r@   N)r2   r3   r4   r5   r   r   staticmethodr   r   r}   r7   r@   r    r   r   r   Z  s7    NM  F r    r   c                   H    \ rS rSrSrSr\" \5      r\	" S5      S 5       r
Srg)AddressDecoderin     Taddressc                     U " 5       $ rE   r@   r   s      r   r}   AddressDecoder.from_type_strs  r   r    r@   N)r2   r3   r4   r5   r   r   r   r   r   r   r}   r7   r@   r    r   r   r   n  s.    NM34JI r    r   c                   D    \ rS rSr\" \5      rSr\" S5      S 5       r	Sr
g)UnsignedIntegerDecoderi{  Tuintc                 "    U " UR                   S9$ Nr   subr   s      r   r}   $UnsignedIntegerDecoder.from_type_str      (,,//r    r@   N)r2   r3   r4   r5   r   r   r   r   r   r}   r7   r@   r    r   r   r   {  s)    /0JMF0 0r    r      r   c                   @    \ rS rSrSrS rS r\" S5      S 5       rSr	g)	SignedIntegerDecoderi  Tc                 l    [        U5      nUSU R                  S-
  -  :  a  USU R                  -  -
  $ U$ Nr   r   )r   r   )r   r   rX   s      r   r   SignedIntegerDecoder.decoder_fn  s?    !$'A$--1221d11111Lr    c                     U R                  5       nU R                  U-
  nUS:  a  SU-  nOSU-  nX%:w  a#  [        SR                  [	        U5      5      5      eg Nr   r      r   r   r   rX   r   r   r   expected_padding_bytess         r   r   +SignedIntegerDecoder.validate_padding_bytes  h    335**_<A:%,|%;"%,|%;"2&3::4;NO  3r    intc                 "    U " UR                   S9$ r   r   r   s      r   r}   "SignedIntegerDecoder.from_type_str  r   r    r@   N)
r2   r3   r4   r5   r   r   r   r   r}   r7   r@   r    r   r   r     s*    M E0 0r    r   c                   D    \ rS rSrSr\S 5       r\" S5      S 5       rSr	g)BytesDecoderi  Fc                     U $ rE   r@   r   s    r   r   BytesDecoder.decoder_fn      r    bytesc                 (    U " UR                   S-  S9$ )Nr   r   r   r   s      r   r}   BytesDecoder.from_type_str  s    (,,"233r    r@   N)
r2   r3   r4   r5   r   r   r   r   r}   r7   r@   r    r   r  r    s2    M  G4 4r    r  c                   0   ^  \ rS rSrSrSrU 4S jrSrU =r$ )BaseFixedDecoderi  NTc                    > [         TU ]  5         U R                  c  [        S5      eU R                  S::  d  U R                  S:  a  [        S5      eg )Nzmust specify `frac_places`r   P   z&`frac_places` must be in range (0, 80])r   rO   frac_placesrQ   rR   s    r   rO   BaseFixedDecoder.validate  sR    #9::q D$4$4r$9EFF %:r    r@   )	r2   r3   r4   r5   r  r   rO   r7   r8   r9   s   @r   r	  r	    s    KMG Gr    r	  c                   6    \ rS rSrS r\" S5      S 5       rSrg)UnsignedFixedDecoderi  c                     [        U5      n[        R                  " [        5         [        R                  " U5      [
        U R                  -  -  nS S S 5        U$ ! , (       d  f       W$ = frE   )r   decimallocalcontextr   Decimalr   r  )r   r   rX   decimal_values       r   r   UnsignedFixedDecoder.decoder_fn  sW    !$'!!"56#OOE2S$:J:J5JJM 7  76 s   +A
A*ufixedc                 *    UR                   u  p4U " X4S9$ N)r   r  r   r{   r|   rx   r   r  s        r   r}   "UnsignedFixedDecoder.from_type_str      &.ll#.JJr    r@   N)r2   r3   r4   r5   r   r   r}   r7   r@   r    r   r  r    s"     HK Kr    r  c                   <    \ rS rSrS rS r\" S5      S 5       rSrg)SignedFixedDecoderi  c                 0   [        U5      nUSU R                  S-
  -  :  a  USU R                  -  -
  nOUn[        R                  " [        5         [        R
                  " U5      [        U R                  -  -  nS S S 5        U$ ! , (       d  f       W$ = fr   )r   r   r  r  r   r  r   r  )r   r   rX   signed_valuer  s        r   r   SignedFixedDecoder.decoder_fn  s    !$'A$--122 1d&9&9#99L L!!"56#OOL9CAQAQ<QQM 7  76 s   +B
Bc                     U R                  5       nU R                  U-
  nUS:  a  SU-  nOSU-  nX%:w  a#  [        SR                  [	        U5      5      5      eg r   r   r   s         r   r   )SignedFixedDecoder.validate_padding_bytes  r   r    fixedc                 *    UR                   u  p4U " X4S9$ r  r   r  s        r   r}    SignedFixedDecoder.from_type_str  r  r    r@   N)	r2   r3   r4   r5   r   r   r   r}   r7   r@   r    r   r  r    s'    
 GK Kr    r  c                   Z    \ rS rSrSr\S 5       r\S 5       rS r\	" S5      S 5       r
Srg	)
ByteStringDecoderi  Tc                     U $ rE   r@   r   s    r   r   ByteStringDecoder.decoder_fn  r  r    c                 $   [        U 5      n[        U5      nU R                  U5      n[        U5      U:  a$  [	        SR                  U[        U5      5      5      eX1S  nUSX!-
  -  :w  a#  [        SR                  [        U5      5      5      eUS U $ )Nr   r   r   )rV   r   r   r   r   r   r   r   )r=   data_lengthpadded_lengthr   r   s        r   r   'ByteStringDecoder.read_data_from_stream   s    %f-{+{{=)t9}$'>EE!I  \*G}'BCC&3::4;NO  L[!!r    c                     g rE   r@   r   s      r   r   (ByteStringDecoder.validate_padding_bytes  s    r    r  c                     U " 5       $ rE   r@   r   s      r   r}   ByteStringDecoder.from_type_str  r   r    r@   N)r2   r3   r4   r5   rZ   r   r   r   r   r   r}   r7   r@   r    r   r'  r'    sK    J  " ". G r    r'  c                   @    \ rS rSr\" S5      S 5       r\S 5       rSrg)StringDecoderi   stringc                     U " 5       $ rE   r@   r   s      r   r}   StringDecoder.from_type_str!  r   r    c                      U R                  S5      nU$ ! [         a=  n[        UR                  UR                  UR
                  UR                  S5      UeS nAff = f)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.)rB   UnicodeDecodeErrorr   encodingobjectstartend)r   rX   es      r   r   StringDecoder.decoder_fn%  sa    	KK(E  " 	


 
	s    
A8AAr@   N)	r2   r3   r4   r5   r   r}   r   r   r7   r@   r    r   r3  r3     s-    H   r    r3  ),rH   r  iotypingr   utilsr   r   r   baser	   r
   r   
exceptionsr   r   r   utils.numericr   r   r   BytesIOr   ABCMetar;   rL   r\   r   r   r   r   r   r   r   r   r   rV   r   r  r	  r  r  r'  r3  r@   r    r   <module>rG     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    