
    (io#                     @   d dl Z d dlZd dl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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mZmZmZmZ  e j>                  dg d      Z  e j>                  dddg      Z!dede"de#fdZ$dede"de#fdZ%dede"deee	eef      fdZ&deee	eef      dee    fdZ'edee    de"de"dee!   fd       Z(dee!   de#defdZ)e	 	 	 d)de"de"d e"d!e*def
d"       Z+ e+d#d$      Z, e+d%d$      Z- e+d&d$      Z. e+d'd($      Z/y)*    N)IterableSequenceTuplecast)ChecksumAddress)to_tuple)currygroupbysliding_window)HexBytes)Web3
percentile)InsufficientDataWeb3ValidationError)BlockNumberGasPriceStrategyTxDataTxParamsWei	MinerData)miner
num_blocksmin_gas_pricelow_percentile_gas_priceProbability	gas_priceprobw3sample_sizereturnc                     | j                   j                  d      }t        ||d         }|dk(  rt        d      | j                   j                  t	        |d   |z
              }|d   |d   z
  |z  S )Nlatestnumberr   Constrained sample size is 0	timestamp)eth	get_blockminr   r   )r   r    r#   constrained_sample_sizeoldests        O/var/www/br/venv/lib/python3.12/site-packages/web3/gas_strategies/time_based.py_get_avg_block_timer-   2   s{    VVh'F!+vh/?@!#!"@AAVVk&*:=T*TUVF;&"559PPP    c                    | j                   j                  d      d   }t        ||      }|dk(  rt        d      | j                   j                  t	        ||z
              }|d   }|d   }d}d}t        |dz   |dz         D ]E  }	| j                   j                  t	        |	            d   }
|
|z
  }|	|z
  |z  }|||z  z  }||z  }|
}G ||z  S )Nr#   r$   r   r%   r&   g           )r'   r(   r)   r   r   range)r   r    latest_block_numberr*   oldest_blockoldest_block_numberprev_timestampweighted_sumsum_of_weightsicurr_timestamptimeweights                r,   _get_weighted_avg_block_timer<   =   s   &&**84X>!+/BC!#!"@AA66##'*AABL 'x0!+.NLN&*,?!,CD ())+a.9+F.))-DDv%& '( .((r.   c              #   l  K   | j                   j                  dd      }|d   D ]"  }t        t        |      }|d   |d   |d   f $ |}t	        |dz
        D ]V  }|d	   d
k(  r y | j                   j                  |d   d      }|d   D ]"  }t        t        |      }|d   |d   |d   f $ X y w)Nr#   T)full_transactionstransactionsr   hashgasPricer0   r$   r   
parentHash)r'   r(   r   r   r1   )r   r    r#   transactionblock_s         r,   _get_raw_miner_datarF   S   s      VVh$?Fn- I6;/gvJ0GHHI E;?# 	K?a   |!4 M 0 	KKv{3K>5=+j2IJJ	K	Ks   B2B4raw_datac           	   #   .  K   t        d|       }|j                         D ]X  \  }}t        t        t	        |       \  }}}	 t        |d      }t        |t        t        |            t        |      |       Z y # t        $ r t        |      }Y Dw xY ww)Nr      r   )
r
   itemsmapsetzipr   r   r)   r   len)rG   data_by_minerr   
miner_datarE   block_hashes
gas_pricesprice_percentiles           r,   _aggregate_miner_datarT   j   s      Ax(M*002 
z&)#sJ/?&@#<	/)*D L!"
O	
 	


   	/":	/s)   =B A;.B;BBBBrP   wait_blocksc              #     K   t        t        | t        j                  d      d            }t	        t        |            D ]D  }||   j                  }t        d ||d D              }||z
  |z  }d||z  z
  }t        ||       F yw)zv
    Computes the probabilities that a txn will be accepted at each of the gas
    prices accepted by the miners.
    r   T)keyreversec              3   4   K   | ]  }|j                     y w)N)r   ).0ms     r,   	<genexpr>z)_compute_probabilities.<locals>.<genexpr>   s      )
ALL)
s   Nr0   )	tuplesortedoperator
attrgetterr1   rN   r   sumr   )	rP   rU   r    miner_data_by_priceidxr   num_blocks_accepting_priceinv_prob_per_blockprobability_accepteds	            r,   _compute_probabilitiesrg   ~   s       ##$>?	
 S,-. J#6s#;#T#T %( )
"5cd";)
 &
" *,FF+U #5{#BB24HIIJs   BB	probabilitiesdesired_probabilityc                 \   | d   }| d   }||j                   k\  rt        t        |j                              S ||j                   k  rt        t        |j                              S t	        d|       D ]  \  }}||j                   k  r||j                   kD  rt        d      ||j                   z
  }|j                   |j                   z
  }||z  }|j                  |j                  z
  }	t        t        j                  |j                  |	|z  z               }
t        |
      c S  t        d      )a  
    Given a sorted range of ``Probability`` named-tuples returns a gas price
    computed based on where the ``desired_probability`` would fall within the
    range.

    :param probabilities: An iterable of `Probability` named-tuples
        sorted in reverse order.
    :param desired_probability: A floating point representation of the desired
        probability. (e.g. ``85% -> 0.85``)
    r      	Invariant)r   r   intr   r   	Exceptionmathceil)rh   ri   firstlastleftrightadj_probwindow_sizepositiongas_window_sizer   s              r,   _compute_gas_pricerz      s    !EDejj(3u'((				)3t~~&''%a7 %e+ 499, K((&3ii%**,k)..5??:		%//Oh4N"NOP	9~%0 $$r.   x   max_wait_secondsprobabilityweightedc                 D     dt         dt        dt        f fd}|S )a{  
    A gas pricing strategy that uses recently mined block data to derive a gas
    price for which a transaction is likely to be mined within X seconds with
    probability P. If the weighted kwarg is True, more recent block
    times will be more heavily weighted.

    :param max_wait_seconds: The desired maximum number of seconds the
        transaction should take to mine.
    :param sample_size: The number of recent blocks to sample
    :param probability: An integer representation of the desired probability
        that the transaction will be mined within ``max_wait_seconds``.  0 means 0%
        and 100 means 100%.
    r   transaction_paramsr!   c                 \   | j                   j                  d      d   dk(  r| j                   j                  S rt        | 
      }nt	        | 
      }t        t        j                  |z              }t        | 
      }t        |      }t        ||
      }t        |	dz        }|S )Nr#   r$   r   )r    )rU   r    d   )r'   r(   r   r<   r-   rn   rp   rq   rF   rT   rg   rz   )r   r   avg_block_timerU   raw_miner_datarP   rh   r   r|   r}   r    r~   s           r,   time_based_gas_price_strategyzNconstruct_time_based_gas_price_strategy.<locals>.time_based_gas_price_strategy   s    66H%h/1466###9"+VN0MN$))$4~$EFG,R[I*>:
.##
 '}kC6GH	r.   )r   r   r   )r|   r    r}   r~   r   s   ```` r,   'construct_time_based_gas_price_strategyr      s+    *$ H QT  . )(r.   <   )r|   r    iX  i  iQ i  )r{   b   F)0collectionsrp   r_   typingr   r   r   r   
eth_typingr   	eth_utilsr   eth_utils.toolzr	   r
   r   hexbytesr   web3r   web3._utils.mathr   web3.exceptionsr   r   
web3.typesr   r   r   r   r   
namedtupler   r   rn   floatr-   r<   rF   rT   rg   rz   boolr   fast_gas_price_strategymedium_gas_price_strategyslow_gas_price_strategyglacial_gas_price_strategy r.   r,   <module>r      s       
  #K""U	 %k$$][&4IJQD Qs Qu Q)T ) ) ),KKKeOXs234K.
u_h;<=
i
( 
J#J25JDGJkJ 
J0-%K(-%?D-%-%`  	+)+)+) +) 	+)
 +) +)^ B 
 D 
 B 
 E! r.   