Spaces:
Running

andervier commited on
Commit
5153ac4
·
1 Parent(s): 8bd9e53

fix: p2p rates now match Binance UI (first eligible ad, correct tradeType)

Browse files
Files changed (1) hide show
  1. main.py +14 -12
main.py CHANGED
@@ -148,7 +148,9 @@ async def fetch_p2p() -> dict:
148
  }
149
 
150
  async with httpx.AsyncClient(timeout=10) as client:
151
- buy_res, sell_res = await asyncio.gather(
 
 
152
  client.post(
153
  "https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search",
154
  json={**body_base, "tradeType": "BUY"},
@@ -160,12 +162,11 @@ async def fetch_p2p() -> dict:
160
  headers=headers,
161
  ),
162
  )
163
- buy_res.raise_for_status()
164
- sell_res.raise_for_status()
165
 
166
- def best_price_for(ads: list, target: float) -> float | None:
167
- """Retorna el precio más alto entre anuncios que aceptan `target` USDT."""
168
- eligible = []
169
  for item in ads:
170
  adv = item.get("adv", {})
171
  try:
@@ -173,18 +174,19 @@ async def fetch_p2p() -> dict:
173
  surplus = float(adv.get("surplusAmount", 0))
174
  min_fiat = float(adv.get("minSingleTransAmount", 0))
175
  max_fiat = float(adv.get("maxSingleTransAmount", float("inf")))
176
- # Verifica que el anuncio tenga suficiente stock y acepte 10 USDT
177
  fiat_needed = price * target
178
  if surplus >= target and min_fiat <= fiat_needed <= max_fiat:
179
- eligible.append(price)
180
  except (KeyError, ValueError, TypeError):
181
  continue
182
- return max(eligible) if eligible else None
183
 
184
- buy_max = best_price_for(buy_res.json().get("data", []), target_usdt)
185
- sell_max = best_price_for(sell_res.json().get("data", []), target_usdt)
 
 
186
 
187
- data = {"p2pBuyMax": buy_max, "p2pSellMax": sell_max}
188
  _store("p2p", data)
189
  return data
190
 
 
148
  }
149
 
150
  async with httpx.AsyncClient(timeout=10) as client:
151
+ # BUY = compradores de USDT (tab "Vender" en Binance, ordenado de mayor a menor)
152
+ # SELL = vendedores de USDT (tab "Comprar" en Binance, ordenado de menor a mayor)
153
+ buy_ads_res, sell_ads_res = await asyncio.gather(
154
  client.post(
155
  "https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search",
156
  json={**body_base, "tradeType": "BUY"},
 
162
  headers=headers,
163
  ),
164
  )
165
+ buy_ads_res.raise_for_status()
166
+ sell_ads_res.raise_for_status()
167
 
168
+ def first_eligible_price(ads: list, target: float) -> float | None:
169
+ """Primer anuncio elegible — Binance ya ordena del mejor al peor precio."""
 
170
  for item in ads:
171
  adv = item.get("adv", {})
172
  try:
 
174
  surplus = float(adv.get("surplusAmount", 0))
175
  min_fiat = float(adv.get("minSingleTransAmount", 0))
176
  max_fiat = float(adv.get("maxSingleTransAmount", float("inf")))
 
177
  fiat_needed = price * target
178
  if surplus >= target and min_fiat <= fiat_needed <= max_fiat:
179
+ return price
180
  except (KeyError, ValueError, TypeError):
181
  continue
182
+ return None
183
 
184
+ # Compra: vendedores de USDT (SELL ads), primer precio = el más barato disponible
185
+ p2p_buy = first_eligible_price(sell_ads_res.json().get("data", []), target_usdt)
186
+ # Venta: compradores de USDT (BUY ads), primer precio = el más alto disponible
187
+ p2p_sell = first_eligible_price(buy_ads_res.json().get("data", []), target_usdt)
188
 
189
+ data = {"p2pBuyMax": p2p_buy, "p2pSellMax": p2p_sell}
190
  _store("p2p", data)
191
  return data
192