a
    DL—j€,  ã                   @   s<  d Z ddlZddlZddlZddlmZ ddlZdZddddd	d
dddddddddddddddddddddd d!œZh d"£Z	i Z
d#ZzddlZW n ey¤   dZY n0 d$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd<eed-œd.d/„Zeeeee d0œd1d2„Zeeeeee d3œd4d5„Zd=eeeee d6œd7d8„Zeee d9œd:d;„ZdS )>u  
Polymarket US â€” market lookup (public gateway API, no auth needed).

Maps our internal league codes to Polymarket US league slugs and finds the
specific per-team market for a fixture, mirroring polymarket_live.py's role
for polymarket.com but against the DIFFERENT gateway.polymarket.us catalog.

LEAGUE_SLUG only lists codes VERIFIED against live events -- unmapped leagues
return None and are silently skipped by the auto-trade hook (no market
attempted), not guessed at.

PAGE THE CATALOG. /v2/leagues takes limit+offset and returns 221 leagues; the
original 2026-07-23 pass believed it capped at 50 and so missed nine leagues we
trade. Verify a slug by reading TEAM NAMES out of its events, never by matching
the league name -- 'Serie B' is ambiguous between Italy and Brazil.
é    N)ÚOptionalzhttps://gateway.polymarket.usZeplZlalZbunZseaZmlsZbraZbrbZlpaZpl1ZlcoZkl1ZcslZalsvZelsZlmxZuslcZnwslZpdcZeflchZlg1Zlal2ZligporZtslZswslZscpZflcZsldZekst)ZE0ZSP1ZD1ZI1ZMLSZBSAZBR2ZAR1ZPE1ZCO1ZKO1ZCN1ZSE1ZNO1ZMX1ZUSLZNWSLZCL1ÚE1ÚF1ÚSP2ÚP1ÚT1ZCH1ZSC0ZCZ1ZDK1ZPL1>   ZE2ZG1ZN1ZSC3ZD2ZECZB1r   ZSC2ZE3ZF2ZJP1ZSC1r   ZI2r   ZHR1ZCANr   r   éx   c                 C   sv   | g}t d urrzPt  | ¡\}}|r,| |¡ |r\| d¡rH| |d ¡ | | d¡pXg ¡ W n typ   Y n0 |S )NZfdcoÚaliases)Ú_mapsZ_lookupÚappendÚgetÚextendÚ	Exception)ÚteamÚoutÚkeyÚinfo© r   úmain/poly_us_prices.pyÚ	_variantsc   s    

r   c                 C   s2   t  d| pd¡ dd¡ ¡  ¡ } t dd| ¡ ¡ S )NZNFKDÚ ÚasciiÚignorez
[^a-z0-9 ]ú )ÚunicodedataZ	normalizeÚencodeÚdecodeÚlowerÚreÚsubÚstrip)Úsr   r   r   Ú_norms   s     r"   c                 C   sP   t | ƒt |ƒ }}|r|sdS ||kr*dS ||v s:||v oNtt|ƒt|ƒƒdkS )NFTé   )r"   ÚminÚlen)ÚaÚbZnaZnbr   r   r   Ú_names_match_rawx   s    r(   c                    s   t ‡ fdd„t|ƒD ƒƒS )z•a: raw name from Polymarket US. b: our team name -- checked against every
    known variant (team_map key/fdco/aliases), not just the literal string.c                 3   s   | ]}t ˆ |ƒV  qd S )N)r(   )Ú.0Úv©r&   r   r   Ú	<genexpr>„   ó    z_names_match.<locals>.<genexpr>)Úanyr   )r&   r'   r   r+   r   Ú_names_match�   s    r/   F)Úleague_slugÚreturnc                 C   s²   t   ¡ }t | ¡}|r2|s2||d  tk r2|d S g }d}tjt› d| › d�d|dœdd�}|jd	krhq¢| ¡  d
g ¡}||7 }t|ƒdk rŽq¢|d7 }|dkr:q¢q:||ft| < |S )Nr   é   z/v2/leagues/z/eventsé2   )ÚlimitÚoffseté   )ÚparamsÚtimeoutéÈ   Úeventsi,  )	ÚtimeÚ_events_cacher   Ú
_CACHE_TTLÚrequestsÚGATEWAY_BASEÚstatus_codeÚjsonr%   )r0   ZforceZnowÚcachedr:   r5   ÚrZbatchr   r   r   Úleague_events‡   s(    

ÿ
rD   )ÚdivÚhomeÚawayr1   c           	      C   sn   t  | ¡}|sd S t|ƒD ]N}| dd¡}t d|¡}t|ƒdkrDq|\}}t||ƒrt||ƒr|  S qd S )NÚtitler   z\s+vs\.?\s+é   )ÚLEAGUE_SLUGr   rD   r   Úsplitr%   r/   )	rE   rF   rG   r0   ÚerH   ÚpartsZehZear   r   r   Ú
find_eventž   s    

rN   )rE   rF   rG   Úsider1   c           
      C   sÈ   |dvrdS t | ||ƒ}|s dS ||dœ |¡}| dg ¡D ]†}tdd„ | dg ¡D ƒdƒ}|sbq<| d¡pni  d	d
¡}|dkr†| n|o’t||ƒ}	|	r<|d | dd
¡t| d¡p¶dƒdœ  S q<dS )a  side: 'home', 'draw' or 'away'.

    Returns {'slug':..., 'price':..., 'event_title':...} or None if no
    confident match.

    DRAW SUPPORT ADDED 2026-08-22. This used to read

        team_side_name = home if side == 'home' else away

    so ANY side that was not exactly 'home' -- including 'draw' -- silently
    resolved to the AWAY market. Callers got a real slug and a real price back,
    just for the wrong outcome, with no error. Verified live: the draw and away
    lookups for one Argentine fixture both returned `...-gye`.

    Nothing in production was corrupted by it: live_monitor only ever passes
    home/away (xlow is home/away only), and mm_quoter_polyus explicitly works
    around the limitation. But a three-leg maker asking for a draw would have
    quoted one outcome twice and never quoted the draw at all -- collecting two
    legs of a book that can never complete, which is the worst state in the
    strategy.

    The draw market is the one whose Yes side carries NO team; team-win markets
    always have one. That is the same rule get_all_prices already uses, so the
    two functions now agree instead of disagreeing.

    Unknown side values now return None rather than falling through to away --
    the fallthrough was the actual defect, not the missing draw branch.
    )rF   ÚdrawrG   N)rF   rG   Úmarketsc                 s   s    | ]}|  d ¡dkr|V  qdS ©ZdescriptionZYesN©r   ©r)   r!   r   r   r   r,   Ñ   r-   zfind_market.<locals>.<genexpr>ÚmarketSidesr   Únamer   rP   ÚslugrH   Úpricer   )rW   Zevent_titlerX   )rN   r   Únextr/   Úfloat)
rE   rF   rG   rO   rL   Z	want_teamÚmÚyes_sider   Zhitr   r   r   Úfind_market­   s"    ÿr]   )rF   rG   Úleaguer1   c                 C   s  |sdS t |||ƒ}|sdS i }| dg ¡D ]À}tdd„ | dg ¡D ƒdƒ}|sRq,| d¡p^i  dd¡}|srd	}	n"t||ƒr‚d
}	nt||ƒr,d}	nq,t|d ƒ}
|
s¦q,|
 d¡||	› d�< |
 d¡||	› d�< |
 d¡pà|
 d¡||	› d�< q,t|ƒdkrþ|S dS )a  Same shape as polymarket_live.get_all_prices: {home_mid,home_bid,home_ask,
    draw_mid,draw_bid,draw_ask,away_mid,away_bid,away_ask}. One event lookup,
    three BBO calls. Draw market identified by its Yes-side having no team
    (team-win markets always have one).NrQ   c                 s   s    | ]}|  d ¡dkr|V  qdS rR   rS   rT   r   r   r   r,   è   r-   z!get_all_prices.<locals>.<genexpr>rU   r   rV   r   rP   rF   rG   rW   ÚbestBidZ_bidÚbestAskZ_askÚ	currentPxZ_midé   )rN   r   rY   r/   Úget_bbor%   )Z	game_daterF   rG   r^   rL   r   r[   r\   r   r   Zbbor   r   r   Úget_all_pricesÜ   s2    

 rd   )Úmarket_slugr1   c                    sl   t jt› d| › d�dd�}|jdkr(d S | ¡  di ¡‰ ‡ fdd„}|d	ƒ|d
ƒ|dƒˆ  d¡ˆ  d¡dœS )Nz/v1/markets/z/bbor6   )r8   r9   Z
marketDatac              
      s8   zt ˆ  | i ¡ d¡ƒW S  tttfy2   Y d S 0 d S )NÚvalue)rZ   r   Ú	TypeErrorÚ
ValueErrorÚAttributeError)Úk©Údr   r   Ú_v  s    zget_bbo.<locals>._vr_   r`   ra   ÚaskDepthÚbidDepth)r_   r`   ra   rn   ro   )r>   r   r?   r@   rA   )re   rC   rm   r   rk   r   rc   ý   s    
ÿrc   )F)N)Ú__doc__r   r;   r   Útypingr   r>   r?   rJ   ZLEAGUE_NOT_FOUNDr<   r=   Úmapsr
   ÚImportErrorr   r"   r(   r/   ÚstrÚlistrD   ÚdictrN   r]   rd   rc   r   r   r   r   Ú<module>   sL   	Þ3
	/!