test_perps_commands.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. """
  3. Test script for new perps trading commands
  4. """
  5. import sys
  6. import os
  7. from pathlib import Path
  8. # Add the project root and src directory to the path
  9. project_root = Path(__file__).parent.parent
  10. sys.path.insert(0, str(project_root))
  11. sys.path.insert(0, str(project_root / 'src'))
  12. from hyperliquid_client import HyperliquidClient
  13. from config import Config
  14. def test_perps_commands():
  15. """Test the perps trading functionality."""
  16. print("🧪 Testing Perps Trading Commands")
  17. print("=" * 50)
  18. try:
  19. # Test configuration
  20. if not Config.validate():
  21. print("❌ Configuration validation failed!")
  22. return False
  23. print(f"✅ Configuration valid")
  24. print(f"🌐 Network: {'Testnet' if Config.HYPERLIQUID_TESTNET else 'Mainnet'}")
  25. print()
  26. # Initialize client
  27. print("🔧 Initializing Hyperliquid client...")
  28. client = HyperliquidClient(use_testnet=Config.HYPERLIQUID_TESTNET)
  29. if not client.sync_client:
  30. print("❌ Failed to initialize client!")
  31. return False
  32. print("✅ Client initialized successfully")
  33. print()
  34. # Test token symbol conversion
  35. tokens_to_test = ['BTC', 'ETH', 'SOL']
  36. for token in tokens_to_test:
  37. print(f"📊 Testing {token}...")
  38. # Convert to full symbol
  39. symbol = f"{token}/USDC:USDC"
  40. print(f" Symbol: {token} → {symbol}")
  41. # Test market data fetching
  42. market_data = client.get_market_data(symbol)
  43. if market_data:
  44. price = float(market_data['ticker'].get('last', 0))
  45. print(f" ✅ Current price: ${price:,.2f}")
  46. # Test calculation
  47. usdc_amount = 100
  48. token_amount = usdc_amount / price
  49. print(f" 📈 Long ${usdc_amount} USDC = {token_amount:.6f} {token}")
  50. print(f" 📉 Short ${usdc_amount} USDC = {token_amount:.6f} {token}")
  51. else:
  52. print(f" ❌ Could not fetch price for {token}")
  53. print()
  54. return True
  55. except Exception as e:
  56. print(f"💥 Test failed with error: {e}")
  57. import traceback
  58. traceback.print_exc()
  59. return False
  60. if __name__ == "__main__":
  61. success = test_perps_commands()
  62. if success:
  63. print("🎉 Perps commands test PASSED!")
  64. print("\n📱 Ready to test on Telegram:")
  65. print(" /long BTC 100")
  66. print(" /short ETH 50")
  67. sys.exit(0)
  68. else:
  69. print("💥 Perps commands test FAILED!")
  70. sys.exit(1)