fix: p2p price averages ads 2-4 as ref, finds first ad accepting ~20 USDT transaction
Browse files
main.py
CHANGED
|
@@ -171,39 +171,55 @@ async def fetch_p2p() -> dict:
|
|
| 171 |
|
| 172 |
def first_eligible_price(ads: list) -> float | None:
|
| 173 |
"""
|
| 174 |
-
1.
|
| 175 |
-
2.
|
| 176 |
-
|
| 177 |
-
|
|
|
|
| 178 |
"""
|
| 179 |
if not ads:
|
| 180 |
return None
|
| 181 |
|
| 182 |
-
#
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
for item in ads[1:]:
|
| 203 |
adv = item.get("adv", {})
|
| 204 |
try:
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
except (KeyError, ValueError, TypeError):
|
| 208 |
continue
|
| 209 |
return None
|
|
|
|
| 171 |
|
| 172 |
def first_eligible_price(ads: list) -> float | None:
|
| 173 |
"""
|
| 174 |
+
1. Promedia los precios de los anuncios 2, 3 y 4 (índices 1-3) como referencia.
|
| 175 |
+
2. fiat_ref = promedio * 20 (~20 USDT en VES).
|
| 176 |
+
3. Devuelve el primer anuncio donde minSingleTransAmount <= fiat_ref <= maxSingleTransAmount,
|
| 177 |
+
es decir, uno que realmente acepte esa transacción.
|
| 178 |
+
Fallback: primer anuncio desde índice 1 con min <= fiat_ref (sin chequeo de max).
|
| 179 |
"""
|
| 180 |
if not ads:
|
| 181 |
return None
|
| 182 |
|
| 183 |
+
# Paso 1: promedio de ads[1..3]
|
| 184 |
+
sample = []
|
| 185 |
+
for item in ads[1:4]:
|
| 186 |
+
try:
|
| 187 |
+
sample.append(float(item["adv"]["price"]))
|
| 188 |
+
except (KeyError, ValueError, TypeError):
|
| 189 |
+
pass
|
| 190 |
+
|
| 191 |
+
if not sample:
|
| 192 |
+
# sin muestra usamos ads[0] como referencia
|
| 193 |
+
try:
|
| 194 |
+
sample = [float(ads[0]["adv"]["price"])]
|
| 195 |
+
except (KeyError, ValueError, TypeError):
|
| 196 |
+
return None
|
| 197 |
+
|
| 198 |
+
avg_price = sum(sample) / len(sample)
|
| 199 |
+
fiat_ref = avg_price * 20 # ~20 USDT en VES
|
| 200 |
+
|
| 201 |
+
# Paso 2: primer anuncio que acepte la transacción (min <= fiat_ref <= max)
|
| 202 |
+
for item in ads:
|
| 203 |
+
adv = item.get("adv", {})
|
| 204 |
+
try:
|
| 205 |
+
price = float(adv["price"])
|
| 206 |
+
surplus = float(adv.get("surplusAmount", 0))
|
| 207 |
+
min_fiat = float(adv.get("minSingleTransAmount", 0))
|
| 208 |
+
max_fiat = float(adv.get("maxSingleTransAmount", float("inf")))
|
| 209 |
+
if surplus > 0 and min_fiat <= fiat_ref <= max_fiat:
|
| 210 |
+
return price
|
| 211 |
+
except (KeyError, ValueError, TypeError):
|
| 212 |
+
continue
|
| 213 |
+
|
| 214 |
+
# Fallback: primer anuncio desde índice 1 con min <= fiat_ref
|
| 215 |
for item in ads[1:]:
|
| 216 |
adv = item.get("adv", {})
|
| 217 |
try:
|
| 218 |
+
price = float(adv["price"])
|
| 219 |
+
surplus = float(adv.get("surplusAmount", 0))
|
| 220 |
+
min_fiat = float(adv.get("minSingleTransAmount", 0))
|
| 221 |
+
if surplus > 0 and min_fiat <= fiat_ref:
|
| 222 |
+
return price
|
| 223 |
except (KeyError, ValueError, TypeError):
|
| 224 |
continue
|
| 225 |
return None
|