fix(api): usar ticker spot de OKX para BRL en vez de Binance P2P
Browse filesBinance P2P no sirve anuncios publicos para USDT/BRL (data: [] confirmado
en vivo), por lo que Brasil quedaba sin tasa. Se agrega fetch_okx_brl_safe()
como caso especial (mismo patron que VE), usado tanto en el endpoint
rates_v2 como en el history sampler, para que el historico de Brasil
tambien se siga registrando.
main.py
CHANGED
|
@@ -538,6 +538,69 @@ async def fetch_p2p_v2_safe(fiat: str) -> dict:
|
|
| 538 |
return {"p2pBuyMax": None, "p2pSellMax": None}
|
| 539 |
|
| 540 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 542 |
# Muestreador de histΓ³rico: cada 15 min registra BCV + USDT de TODAS las fiats,
|
| 543 |
# sin depender de que un usuario de ese paΓs pida tasas. Los fetchers ya hacen
|
|
@@ -564,7 +627,9 @@ async def _history_sampler():
|
|
| 564 |
except Exception as e:
|
| 565 |
logger.warning("sampler: fetch_bcv fallΓ³: %s", e)
|
| 566 |
for fiat in fiats:
|
| 567 |
-
|
|
|
|
|
|
|
| 568 |
history.record_p2p(fiat, data.get("p2pBuyMax"), data.get("p2pSellMax"))
|
| 569 |
await asyncio.sleep(HISTORY_SAMPLE_SPACING)
|
| 570 |
await asyncio.sleep(HISTORY_SAMPLE_INTERVAL)
|
|
@@ -631,10 +696,14 @@ async def rates_v2(country: str | None = None):
|
|
| 631 |
"local": None,
|
| 632 |
}
|
| 633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
bcv_data, ve_p2p, local_p2p, prev = await asyncio.gather(
|
| 635 |
fetch_bcv(),
|
| 636 |
fetch_p2p_v2_safe("VES"),
|
| 637 |
-
|
| 638 |
_bcv_previous(),
|
| 639 |
)
|
| 640 |
|
|
|
|
| 538 |
return {"p2pBuyMax": None, "p2pSellMax": None}
|
| 539 |
|
| 540 |
|
| 541 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 542 |
+
# v2 β Caso especial BR: Binance P2P no sirve anuncios pΓΊblicos para USDT/BRL
|
| 543 |
+
# (confirmado en vivo: `data: []` de forma consistente). En vez de un fallback
|
| 544 |
+
# genΓ©rico para todos los paΓses, se usa el ticker spot pΓΊblico de OKX
|
| 545 |
+
# (sin autenticaciΓ³n) solo para este paΓs, igual que VE tiene su propia rama
|
| 546 |
+
# especial mΓ‘s abajo en rates_v2.
|
| 547 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 548 |
+
OKX_TICKER_URL = "https://www.okx.com/api/v5/market/ticker"
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
async def fetch_okx_brl() -> dict:
|
| 552 |
+
cache_key = "okx_v2:BRL"
|
| 553 |
+
cached = _cached(cache_key, P2P_V2_TTL)
|
| 554 |
+
if cached:
|
| 555 |
+
return cached
|
| 556 |
+
|
| 557 |
+
async with _lock(cache_key):
|
| 558 |
+
# Double-check: mientras esperΓ‘bamos el lock, otro ya pudo haber refrescado
|
| 559 |
+
cached = _cached(cache_key, P2P_V2_TTL)
|
| 560 |
+
if cached:
|
| 561 |
+
return cached
|
| 562 |
+
|
| 563 |
+
logger.info("fetch_okx_brl instId=USDT-BRL")
|
| 564 |
+
|
| 565 |
+
response = await _http.get(OKX_TICKER_URL, params={"instId": "USDT-BRL"})
|
| 566 |
+
response.raise_for_status()
|
| 567 |
+
payload = response.json()
|
| 568 |
+
|
| 569 |
+
if payload.get("code") != "0":
|
| 570 |
+
raise ValueError(f"OKX code={payload.get('code')!r} msg={payload.get('msg')!r}")
|
| 571 |
+
|
| 572 |
+
rows = payload.get("data") or []
|
| 573 |
+
if not rows:
|
| 574 |
+
raise ValueError("OKX devolviΓ³ data vacΓo para USDT-BRL")
|
| 575 |
+
|
| 576 |
+
ticker = rows[0]
|
| 577 |
+
try:
|
| 578 |
+
ask = float(ticker["askPx"])
|
| 579 |
+
bid = float(ticker["bidPx"])
|
| 580 |
+
except (KeyError, ValueError, TypeError) as e:
|
| 581 |
+
raise ValueError(f"OKX ticker con campos invΓ‘lidos: {ticker}") from e
|
| 582 |
+
|
| 583 |
+
# askPx = precio al que alguien COMPRA USDT (anΓ‘logo a p2pBuyMax).
|
| 584 |
+
# bidPx = precio al que alguien VENDE USDT (anΓ‘logo a p2pSellMax).
|
| 585 |
+
# Mismo contrato de salida que fetch_p2p_v2 para que rates_v2 no tenga
|
| 586 |
+
# que distinguir el origen del dato al armar la respuesta.
|
| 587 |
+
data = {"p2pBuyMax": round(ask, 4), "p2pSellMax": round(bid, 4)}
|
| 588 |
+
_store(cache_key, data)
|
| 589 |
+
return data
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
async def fetch_okx_brl_safe() -> dict:
|
| 593 |
+
try:
|
| 594 |
+
data = await fetch_okx_brl()
|
| 595 |
+
stats.record_fetch_success("okx_brl")
|
| 596 |
+
return data
|
| 597 |
+
except Exception as e:
|
| 598 |
+
reason = _classify_fetch_error(e)
|
| 599 |
+
stats.record_fetch_error("okx_brl", reason, str(e))
|
| 600 |
+
logger.error("fetch_okx_brl failed: %s", e, exc_info=True)
|
| 601 |
+
return {"p2pBuyMax": None, "p2pSellMax": None}
|
| 602 |
+
|
| 603 |
+
|
| 604 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 605 |
# Muestreador de histΓ³rico: cada 15 min registra BCV + USDT de TODAS las fiats,
|
| 606 |
# sin depender de que un usuario de ese paΓs pida tasas. Los fetchers ya hacen
|
|
|
|
| 627 |
except Exception as e:
|
| 628 |
logger.warning("sampler: fetch_bcv fallΓ³: %s", e)
|
| 629 |
for fiat in fiats:
|
| 630 |
+
# BR: mismo caso especial que en rates_v2 β Binance P2P no tiene
|
| 631 |
+
# anuncios para USDT/BRL, se usa el ticker spot de OKX en su lugar.
|
| 632 |
+
data = await fetch_okx_brl_safe() if fiat == "BRL" else await fetch_p2p_v2_safe(fiat)
|
| 633 |
history.record_p2p(fiat, data.get("p2pBuyMax"), data.get("p2pSellMax"))
|
| 634 |
await asyncio.sleep(HISTORY_SAMPLE_SPACING)
|
| 635 |
await asyncio.sleep(HISTORY_SAMPLE_INTERVAL)
|
|
|
|
| 696 |
"local": None,
|
| 697 |
}
|
| 698 |
|
| 699 |
+
# BR: Binance P2P no sirve anuncios pΓΊblicos para USDT/BRL (data: [] confirmado
|
| 700 |
+
# en vivo). Caso especial: se consulta el ticker spot de OKX en su lugar.
|
| 701 |
+
local_fetch = fetch_okx_brl_safe() if cc == "BR" else fetch_p2p_v2_safe(fiat)
|
| 702 |
+
|
| 703 |
bcv_data, ve_p2p, local_p2p, prev = await asyncio.gather(
|
| 704 |
fetch_bcv(),
|
| 705 |
fetch_p2p_v2_safe("VES"),
|
| 706 |
+
local_fetch,
|
| 707 |
_bcv_previous(),
|
| 708 |
)
|
| 709 |
|