fix: two-pass P2P price filter skipping sponsored first ad
Browse files- Skip 1st ad (usually sponsored/overpriced), use 2nd as price reference
- Subtract 15 Bs from reference, multiply by 10 to get fiat threshold
- Find first ad accepting that fiat amount (minSingleTransAmount <= fiat_ref <= maxSingleTransAmount)
- Fallback to first ad with surplus > 0
main.py
CHANGED
|
@@ -134,8 +134,6 @@ async def fetch_p2p() -> dict:
|
|
| 134 |
if cached:
|
| 135 |
return cached
|
| 136 |
|
| 137 |
-
target_usdt = 10.0
|
| 138 |
-
|
| 139 |
headers = {
|
| 140 |
"User-Agent": "Mozilla/5.0",
|
| 141 |
"Content-Type": "application/json",
|
|
@@ -171,26 +169,52 @@ async def fetch_p2p() -> dict:
|
|
| 171 |
buy_ads_res.raise_for_status()
|
| 172 |
sell_ads_res.raise_for_status()
|
| 173 |
|
| 174 |
-
def first_eligible_price(ads: list
|
| 175 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
for item in ads:
|
| 177 |
adv = item.get("adv", {})
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
min_fiat = float(adv.get("minSingleTransAmount", 0))
|
| 182 |
-
max_fiat = float(adv.get("maxSingleTransAmount", float("inf")))
|
| 183 |
-
fiat_needed = price * target
|
| 184 |
-
if surplus >= target and min_fiat <= fiat_needed <= max_fiat:
|
| 185 |
-
return price
|
| 186 |
except (KeyError, ValueError, TypeError):
|
| 187 |
continue
|
| 188 |
return None
|
| 189 |
|
| 190 |
# Compra: vendedores de USDT (SELL ads), primer precio = el más barato disponible
|
| 191 |
-
p2p_buy = first_eligible_price(sell_ads_res.json().get("data", [])
|
| 192 |
# Venta: compradores de USDT (BUY ads), primer precio = el más alto disponible
|
| 193 |
-
p2p_sell = first_eligible_price(buy_ads_res.json().get("data", [])
|
| 194 |
|
| 195 |
data = {"p2pBuyMax": p2p_buy, "p2pSellMax": p2p_sell}
|
| 196 |
_store("p2p", data)
|
|
|
|
| 134 |
if cached:
|
| 135 |
return cached
|
| 136 |
|
|
|
|
|
|
|
| 137 |
headers = {
|
| 138 |
"User-Agent": "Mozilla/5.0",
|
| 139 |
"Content-Type": "application/json",
|
|
|
|
| 169 |
buy_ads_res.raise_for_status()
|
| 170 |
sell_ads_res.raise_for_status()
|
| 171 |
|
| 172 |
+
def first_eligible_price(ads: list) -> float | None:
|
| 173 |
+
"""
|
| 174 |
+
Dos pasadas:
|
| 175 |
+
1. Toma el 2do anuncio como referencia (el 1ro suele ser patrocinado).
|
| 176 |
+
2. Calcula fiat_ref = precio_ref * 10 USDT y busca el primer anuncio
|
| 177 |
+
que acepte esa transacción (minSingleTransAmount <= fiat_ref <= maxSingleTransAmount).
|
| 178 |
+
Fallback: primer anuncio con surplus > 0.
|
| 179 |
+
"""
|
| 180 |
+
# Pasada 1: precio de referencia del 2do anuncio
|
| 181 |
+
ref_price = None
|
| 182 |
+
if len(ads) >= 2:
|
| 183 |
+
adv = ads[1].get("adv", {})
|
| 184 |
+
try:
|
| 185 |
+
ref_price = float(adv["price"])
|
| 186 |
+
except (KeyError, ValueError, TypeError):
|
| 187 |
+
pass
|
| 188 |
+
|
| 189 |
+
if ref_price is not None:
|
| 190 |
+
fiat_ref = (ref_price - 15) * 10 # bajamos 15 Bs al ref antes de calcular el monto
|
| 191 |
+
# Pasada 2: primer anuncio que acepte esa transacción
|
| 192 |
+
for item in ads:
|
| 193 |
+
adv = item.get("adv", {})
|
| 194 |
+
try:
|
| 195 |
+
price = float(adv["price"])
|
| 196 |
+
surplus = float(adv.get("surplusAmount", 0))
|
| 197 |
+
min_fiat = float(adv.get("minSingleTransAmount", 0))
|
| 198 |
+
max_fiat = float(adv.get("maxSingleTransAmount", float("inf")))
|
| 199 |
+
if surplus > 0 and min_fiat <= fiat_ref <= max_fiat:
|
| 200 |
+
return price
|
| 201 |
+
except (KeyError, ValueError, TypeError):
|
| 202 |
+
continue
|
| 203 |
+
|
| 204 |
+
# Fallback: primer anuncio con stock
|
| 205 |
for item in ads:
|
| 206 |
adv = item.get("adv", {})
|
| 207 |
try:
|
| 208 |
+
if float(adv.get("surplusAmount", 0)) > 0:
|
| 209 |
+
return float(adv["price"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
except (KeyError, ValueError, TypeError):
|
| 211 |
continue
|
| 212 |
return None
|
| 213 |
|
| 214 |
# Compra: vendedores de USDT (SELL ads), primer precio = el más barato disponible
|
| 215 |
+
p2p_buy = first_eligible_price(sell_ads_res.json().get("data", []))
|
| 216 |
# Venta: compradores de USDT (BUY ads), primer precio = el más alto disponible
|
| 217 |
+
p2p_sell = first_eligible_price(buy_ads_res.json().get("data", []))
|
| 218 |
|
| 219 |
data = {"p2pBuyMax": p2p_buy, "p2pSellMax": p2p_sell}
|
| 220 |
_store("p2p", data)
|