Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from hyperliquid.utils import constants
|
| 5 |
+
from hyperliquid.exchange import Exchange
|
| 6 |
+
from eth_account import Account
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Récupération des secrets configurés dans l'onglet Settings
|
| 11 |
+
PRIVATE_KEY = os.getenv("HL_PRIVATE_KEY")
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
return {"status": "online", "bot": "Sentinel V1"}
|
| 16 |
+
|
| 17 |
+
@app.post("/webhook")
|
| 18 |
+
async def handle_webhook(request: Request):
|
| 19 |
+
try:
|
| 20 |
+
body = await request.body()
|
| 21 |
+
data = json.loads(body)
|
| 22 |
+
|
| 23 |
+
action = data.get("action")
|
| 24 |
+
symbol = data.get("symbol", "ETH")
|
| 25 |
+
size = float(data.get("size", 0.01))
|
| 26 |
+
|
| 27 |
+
# Initialisation dynamique pour chaque trade
|
| 28 |
+
account = Account.from_key(PRIVATE_KEY)
|
| 29 |
+
exchange = Exchange(account, constants.MAINNET_API_URL)
|
| 30 |
+
|
| 31 |
+
if action == "buy":
|
| 32 |
+
return exchange.market_open(symbol, True, size)
|
| 33 |
+
elif action == "sell":
|
| 34 |
+
return exchange.market_open(symbol, False, size)
|
| 35 |
+
elif action == "close":
|
| 36 |
+
return exchange.market_close(symbol)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return {"error": str(e)}
|