70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
"""
|
|
End-to-end test: open $11 BTC long, then immediately close it.
|
|
Pulls the stored API key from the DB for the given wallet.
|
|
|
|
Usage: cd backend && python test_hl_trade.py 0xYOURWALLET
|
|
"""
|
|
import asyncio
|
|
import sys
|
|
from sqlalchemy import select
|
|
from app.database import AsyncSessionLocal
|
|
from app.models import Subscription
|
|
from app.services.hyperliquid import HyperliquidTrader
|
|
from app.config import settings
|
|
|
|
|
|
async def main(wallet: str):
|
|
wallet = wallet.lower()
|
|
async with AsyncSessionLocal() as db:
|
|
result = await db.execute(select(Subscription).where(Subscription.wallet_address == wallet))
|
|
sub = result.scalar_one_or_none()
|
|
if sub is None or not sub.hl_api_key:
|
|
print(f"❌ No API key stored for {wallet}")
|
|
return
|
|
api_key = sub.hl_api_key
|
|
print(f"✓ Loaded API key for {wallet} (ends …{api_key[-6:]})")
|
|
|
|
trader = HyperliquidTrader(
|
|
api_private_key=api_key,
|
|
account_address=wallet,
|
|
leverage=settings.hl_leverage,
|
|
mainnet=settings.hl_mainnet,
|
|
)
|
|
|
|
bal = await trader.get_balance()
|
|
print(f"✓ Withdrawable (perps): ${bal:.2f} (unified account may show 0 here, ignoring)")
|
|
|
|
print("\n─── OPENING $11 BTC LONG ───")
|
|
try:
|
|
open_result = await trader.open_position("BTC", "long", 11.0)
|
|
print(f"✓ Open fill: {open_result}")
|
|
except Exception as e:
|
|
print(f"❌ Open failed: {e}")
|
|
return
|
|
|
|
await asyncio.sleep(2)
|
|
positions = await trader.get_open_positions()
|
|
print(f"✓ Open positions now: {positions}")
|
|
|
|
print("\n─── CLOSING BTC POSITION ───")
|
|
try:
|
|
close_result = await trader.close_position("BTC")
|
|
print(f"✓ Close fill: {close_result}")
|
|
except Exception as e:
|
|
print(f"❌ Close failed: {e}")
|
|
return
|
|
|
|
await asyncio.sleep(2)
|
|
positions = await trader.get_open_positions()
|
|
print(f"✓ Open positions after close: {positions}")
|
|
|
|
bal2 = await trader.get_balance()
|
|
print(f"\n✓ Balance before: ${bal:.2f} → after: ${bal2:.2f} (diff: ${bal2-bal:+.4f})")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python test_hl_trade.py 0xYOURWALLET")
|
|
sys.exit(1)
|
|
asyncio.run(main(sys.argv[1]))
|