Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,28 +1,38 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import os
|
| 3 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
@app.get("/")
|
| 8 |
def home():
|
| 9 |
-
|
| 10 |
-
import hyperliquid
|
| 11 |
-
return {"status": "online", "message": "Hyperliquid est bien installé !"}
|
| 12 |
-
except ImportError:
|
| 13 |
-
return {"status": "error", "message": "Bibliothèque Hyperliquid introuvable"}
|
| 14 |
|
| 15 |
@app.post("/webhook")
|
| 16 |
async def handle_webhook(request: Request):
|
| 17 |
try:
|
| 18 |
-
# Import local pour éviter le crash au démarrage global
|
| 19 |
-
from hyperliquid.exchange import Exchange
|
| 20 |
-
from eth_account import Account
|
| 21 |
-
|
| 22 |
body = await request.body()
|
| 23 |
data = json.loads(body)
|
| 24 |
|
|
|
|
| 25 |
pk = os.getenv("HL_PRIVATE_KEY")
|
|
|
|
|
|
|
|
|
|
| 26 |
account = Account.from_key(pk)
|
| 27 |
exchange = Exchange(account, "https://api.hyperliquid.xyz")
|
| 28 |
|
|
@@ -37,4 +47,4 @@ async def handle_webhook(request: Request):
|
|
| 37 |
|
| 38 |
return {"status": "received", "data": data}
|
| 39 |
except Exception as e:
|
| 40 |
-
return {"error": str(e)}
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
+
import subprocess
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# TENTATIVE D'INSTALLATION FORCEE AU DEMARRAGE
|
| 8 |
+
try:
|
| 9 |
+
from hyperliquid.exchange import Exchange
|
| 10 |
+
from eth_account import Account
|
| 11 |
+
print("Hyperliquid est déjà présent.")
|
| 12 |
+
except ImportError:
|
| 13 |
+
print("Installation forcée de Hyperliquid en cours...")
|
| 14 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "hyperliquid", "eth-account"])
|
| 15 |
+
from hyperliquid.exchange import Exchange
|
| 16 |
+
from eth_account import Account
|
| 17 |
+
print("Installation réussie !")
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
@app.get("/")
|
| 22 |
def home():
|
| 23 |
+
return {"status": "online", "info": "Le bot s'est auto-réparé et est prêt."}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@app.post("/webhook")
|
| 26 |
async def handle_webhook(request: Request):
|
| 27 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
body = await request.body()
|
| 29 |
data = json.loads(body)
|
| 30 |
|
| 31 |
+
# Récupération de la clé depuis les secrets
|
| 32 |
pk = os.getenv("HL_PRIVATE_KEY")
|
| 33 |
+
if not pk:
|
| 34 |
+
return {"error": "Secret HL_PRIVATE_KEY manquant sur Hugging Face"}
|
| 35 |
+
|
| 36 |
account = Account.from_key(pk)
|
| 37 |
exchange = Exchange(account, "https://api.hyperliquid.xyz")
|
| 38 |
|
|
|
|
| 47 |
|
| 48 |
return {"status": "received", "data": data}
|
| 49 |
except Exception as e:
|
| 50 |
+
return {"error": f"Erreur critique : {str(e)}"}
|