
    yi                    P    d Z ddlmZ ddlZddlmZmZ ddZd	dZ	 	 	 	 	 	 d
dZ	y)u   
从 Gamma /events 的 event + market 中解析 Polymarket 参考价（price to beat / final）。

兼容多种字段命名（camelCase / snake_case、event 与 market 层级）。
    )annotationsN)AnyOptionalc                    | i S t        | t              r| S t        | t              r*	 t        j                  |       }t        |t              r|S i S i S # t        j
                  $ r i cY S w xY wN)
isinstancedictstrjsonloadsJSONDecodeError)rawds     )/root/polymarket_arb/gamma_event_parse.pymetadata_as_dictr      sn    
{	#t
#s	

3A"1d+133 I ## 	I	s   &A A A+*A+c                T    | | dk(  ry 	 t        |       S # t        t        f$ r Y y w xY w)N )float	TypeError
ValueError)xs    r   float_or_noner      s5    yAGQxz" s   
 ''c                r   t        | j                  d            }|j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d
      |j                  d      g}|j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      g	}t        d |D        d      }t        d |D        d      }||fS )uY   
    返回 (price_to_beat, final_price)。
    未收盘时 final 通常为 None。
    eventMetadatapriceToBeatprice_to_beat	openPrice
open_priceopenstrikestrikePricereferencePriceline
finalPricefinal_price
closePriceclose_priceresolvedPricec              3  8   K   | ]  }|t        |        y wr   r   .0cs     r   	<genexpr>z%extract_ref_prices.<locals>.<genexpr>H        JQAMa J   Nc              3  8   K   | ]  }|t        |        y wr   r*   r+   s     r   r.   z%extract_ref_prices.<locals>.<genexpr>I   r/   r0   )r   getnext)eventmarketmetaptb_candidatesfin_candidatesptbfins          r   extract_ref_pricesr;   #   sm    EIIo67D 	!!"

;

< 

=!

?#

6

8N" 	!

< 

=!

< 

=!
N J.JD
QC
J.JD
QC8O    )r   r   returndict[str, Any])r   r   r=   zOptional[float])r4   r>   r5   r>   r=   z'tuple[Optional[float], Optional[float]])
__doc__
__future__r   r   typingr   r   r   r   r;    r<   r   <module>rC      s<   
 #   ''#1','r<   