
    &(ie$                     j    d dl Z d dlZd dlmZmZ dZde j                  fdZdee   fdZd d da	d	 Z
y)
    N)OptionalDictzhttps://mempool.space/apisessionc                 $  K   dddddddddddd}	 | j                  t         dd      4 d{   }|j                          |j                          d{   }t	        |j                  d	d            |d
<   t	        |j                  dd            |d<   |d   dz  |d<   ddd      d{    	 | j                  t         dd      4 d{   }|j                          |j                          d{   }t	        |j                  dd            |d<   t	        |j                  dd            |d<   t	        |j                  dd            |d<   ddd      d{    	 | j                  t         dd      4 d{   }|j                          |j                          d{   }t        |t              rt        |      dkD  r|d   }t	        |j                  d
d            |d<   t        |      dk\  rg }t        t        dt        |      dz
              D ]G  }	||	   j                  dd      ||	dz      j                  dd      z
  }
|
dkD  s7|j                  |
       I |r#t	        t        |      t        |      z        |d<   ddd      d{    |d   dkD  r|d
   |d   z  }|dz
  d z  |d!<   |S 7 7 r7 # 1 d{  7  sw Y   /xY w# t
        $ r}t        d|        Y d}~Od}~ww xY w7 87 7 # 1 d{  7  sw Y   xY w# t
        $ r}t        d|        Y d}~d}~ww xY w7 7 7 # 1 d{  7  sw Y   xY w# t
        $ r}t        d|        Y d}~d}~ww xY ww)"a  
    Fetch extended mempool data including:
    - Mempool status (tx count, vsize)
    - Fee recommendations (low, medium, high)
    - Recent blocks (for avg block time)
    - Pending transaction details
    
    Returns dict with all metrics, or fallback values on error
            g     @r   )tx_countmempool_vsizemempool_vsize_mbfee_low
fee_mediumfee_highblock_tx_countavg_block_timemempool_growth_rateunconfirmed_valuepending_over_1btcz/mempool   )timeoutNcountr   vsizer	   i   r
   u   [MEMPOOL ERROR] /mempool → z/v1/fees/recommendedhourFeer   halfHourFeer   
fastestFeer   u)   [MEMPOOL ERROR] /v1/fees/recommended → z/blocksr      
      	timestampr   u   [MEMPOOL ERROR] /blocks → d   r   )getMEMPOOL_API_BASEraise_for_statusjsonfloat	Exceptionprint
isinstancelistlenrangeminappendsum)r   resultresmempoolefeesblockslatest_blockblock_timesi	time_diffblocks_worths               (/var/www/html/crypto/mempool_extended.pyfetch_mempool_extendedr9      s     " F"
3;;"2!38<a;H 	Q 	QC  "HHJ&G!&w{{7A'>!?F:&+GKK,C&DF?#)/)@K)PF%&	Q 	Q
?;;"2!33GHRS;T 	B 	BX[  "#D %dhhy!&< =F9#(-)C#DF< !&txxa'@!AF:	B 	B2;;"2!37;Q;G 	^ 	^3  "88:%F&$'CK!O%ay+01A1A*a1P+Q'( v;!#"$K"3r3v;q=#9: :$*1IMM+q$AF1Q3KOOT_abDc$c	$q='..y9:
 #38[9ICP[L\9\3]/0%	^ 	^8 !#j)F3C,DD)5)9S(@$%MA	Q&	Q 	Q 	Q 	Q  3-aS1223	B#	B 	B 	B 	B  ?9!=>>?	^%	^ 	^ 	^ 	^(  2,QC0112s  NK& KK& $KK
AK$K& /K0K& 5L* LL* $L;L<ALL* %L&L* +M, 	M
M, $M1M2B,M8MM, "M#M, ' NK& 
KK& K#KK#K& &	L/L=NLNL* LL* L'LL'"L* *	M3MNMNM, MM, M)M M)%M, ,	N5NNNNmc                    | sddddddddddddddS | j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }| j                  d	d      }| j                  d
d      }| j                  dd      }| j                  dd      }	| j                  dd      }
| j                  dd      }|dkD  rt        j                  |dz        nd}|dkD  rt        j                  |dz        nd}|dkD  rt        j                  |dz        nd}|dkD  rt        j                  |dz
  dz        nd}t        |      t        |      t        |      t        |      t        |      t        |      t        |      t        |	      t        |
      t	        |      t        |      t        |      t        |      t        |      dS )z
    Normalize extended mempool data into standardized metrics
    
    Returns dict with:
    - Raw values (tx_count, fees, etc.)
    - Normalized factors (0-1 range)
    r   iX  )r   r
   r   r   r   r   r   growth_rater   r   	f_mempoolf_feef_blockr   r	   r
   r   r   r   r   r   r   r   r   r   r   2   i	  i,  )r   r
   r   r   r   r   r   r<   r   r   r=   r>   r?   f_block_time)r   mathtanhr#   int)r:   r   r	   r
   r   r   r   block_txr   r<   r   r   r=   r>   r?   rA   s                   r8   normalize_mempool_extendedrF   k   s     !!!"!"
 	
" uuZ#HEE/1-Muu/3eeIq!G|Q'JuuZ#Huu%q)HUU+S1N%%-q1K115115 6F5I		*S01sI )11DIIhm$#E -5qLdii4(cG ?Mq>P499ns2c9:VYL (O!"23>J'(O//[)"#45 !23 9%u>l+#     )r   r   c                     ddl }|j                         }t        d   }t        d   }|dkD  r||kD  r||z
  dz  }| |z
  }|dkD  r||z  nd}nd}| t        d<   |t        d<   t        |      S )zc
    Update mempool growth rate based on previous state
    Returns: tx per minute growth rate
    r   Nr   r   <   )time_previous_mempool_stater#   )current_tx_countrJ   current_timeprev_tx	prev_timetime_diff_minutestx_diffr<   s           r8   update_mempool_growthrR      s    
  99;L%j1G'4I1}	1)I5;"W,5F5Jg 11PQ +;J'+7K(rG   )aiohttprB   typingr   r   r    ClientSessionr9   rF   rK   rR    rG   r8   <module>rW      sS      !. ]'*?*? ]@M(4. Mj  rG   