mirror of
https://github.com/luxfi/cli.git
synced 2026-10-30 17:01:20 +00:00
- Migrate Net→Chain terminology across all packages - Add new key backends: K-Chain, WalletConnect, environment - Add chaincmd package for chain management - Add lock/unlock commands for key security - Update SDK to v1.16.29 and node to v1.22.23 - Fix tablewriter API compatibility (v1.0.9) - Fix test build failures in vm, net, prompts packages
408 lines
16 KiB
Go
408 lines
16 KiB
Go
// Copyright (C) 2022-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package dexcmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/luxfi/cli/pkg/ux"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Market command handlers
|
|
|
|
func marketListCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Available Markets:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Spot Markets:")
|
|
ux.Logger.PrintToUser(" Symbol Last Price 24h Volume 24h Change")
|
|
ux.Logger.PrintToUser(" LUX/USDT $12.50 $1.2M +5.2%%")
|
|
ux.Logger.PrintToUser(" BTC/USDT $67,500.00 $45.3M +2.1%%")
|
|
ux.Logger.PrintToUser(" ETH/USDT $3,450.00 $23.1M +3.8%%")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Perpetual Markets:")
|
|
ux.Logger.PrintToUser(" Symbol Mark Price Funding Rate Open Interest")
|
|
ux.Logger.PrintToUser(" BTC-PERP $67,502.50 +0.0012%% $125M")
|
|
ux.Logger.PrintToUser(" ETH-PERP $3,451.20 +0.0008%% $67M")
|
|
ux.Logger.PrintToUser(" LUX-PERP $12.51 +0.0015%% $8.5M")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Use 'lux dex market info [symbol]' for detailed market information")
|
|
return nil
|
|
}
|
|
|
|
func marketInfoCmd(cmd *cobra.Command, args []string) error {
|
|
symbol := args[0]
|
|
ux.Logger.PrintToUser("Market Information: %s", symbol)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Price Statistics:")
|
|
ux.Logger.PrintToUser(" Last Price: $12.50")
|
|
ux.Logger.PrintToUser(" 24h High: $13.25")
|
|
ux.Logger.PrintToUser(" 24h Low: $11.80")
|
|
ux.Logger.PrintToUser(" 24h Volume: $1,234,567")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Order Book (Top 5):")
|
|
ux.Logger.PrintToUser(" Bids Asks")
|
|
ux.Logger.PrintToUser(" $12.49 100.5 LUX $12.51 85.2 LUX")
|
|
ux.Logger.PrintToUser(" $12.48 250.0 LUX $12.52 120.0 LUX")
|
|
ux.Logger.PrintToUser(" $12.47 180.3 LUX $12.53 95.8 LUX")
|
|
ux.Logger.PrintToUser(" $12.46 320.1 LUX $12.54 200.0 LUX")
|
|
ux.Logger.PrintToUser(" $12.45 150.0 LUX $12.55 175.5 LUX")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Spread: $0.02 (0.16%%)")
|
|
return nil
|
|
}
|
|
|
|
func marketCreateCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Creating new market...")
|
|
ux.Logger.PrintToUser("This feature requires validator permissions.")
|
|
ux.Logger.PrintToUser("Use 'lux dex market create --help' for options.")
|
|
return nil
|
|
}
|
|
|
|
// Order command handlers
|
|
|
|
func orderPlaceCmd(cmd *cobra.Command, args []string) error {
|
|
market, _ := cmd.Flags().GetString("market")
|
|
side, _ := cmd.Flags().GetString("side")
|
|
orderType, _ := cmd.Flags().GetString("type")
|
|
price, _ := cmd.Flags().GetFloat64("price")
|
|
amount, _ := cmd.Flags().GetFloat64("amount")
|
|
tif, _ := cmd.Flags().GetString("tif")
|
|
|
|
if market == "" || side == "" || amount == 0 {
|
|
return fmt.Errorf("required flags: --market, --side, --amount")
|
|
}
|
|
|
|
if orderType == "limit" && price == 0 {
|
|
return fmt.Errorf("--price is required for limit orders")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Placing %s %s order...", side, orderType)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Order Details:")
|
|
ux.Logger.PrintToUser(" Market: %s", market)
|
|
ux.Logger.PrintToUser(" Side: %s", side)
|
|
ux.Logger.PrintToUser(" Type: %s", orderType)
|
|
if orderType == "limit" {
|
|
ux.Logger.PrintToUser(" Price: $%.2f", price)
|
|
}
|
|
ux.Logger.PrintToUser(" Amount: %.4f", amount)
|
|
ux.Logger.PrintToUser(" Time in Force: %s", tif)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Order placed successfully!")
|
|
ux.Logger.PrintToUser("Order ID: 0x1234...abcd")
|
|
return nil
|
|
}
|
|
|
|
func orderCancelCmd(cmd *cobra.Command, args []string) error {
|
|
orderID := args[0]
|
|
ux.Logger.PrintToUser("Cancelling order %s...", orderID)
|
|
ux.Logger.PrintToUser("Order cancelled successfully!")
|
|
return nil
|
|
}
|
|
|
|
func orderListCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Open Orders:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" ID Market Side Type Price Amount Filled")
|
|
ux.Logger.PrintToUser(" 0x1234... LUX/USDT buy limit $12.00 100.0 0%%")
|
|
ux.Logger.PrintToUser(" 0x5678... BTC/USDT sell limit $68000 0.5 25%%")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Total: 2 open orders")
|
|
return nil
|
|
}
|
|
|
|
func orderHistoryCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Order History (Last 10):")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Time Market Side Price Amount Status")
|
|
ux.Logger.PrintToUser(" 2025-01-15 10:30:00 LUX/USDT buy $12.50 50.0 filled")
|
|
ux.Logger.PrintToUser(" 2025-01-15 09:15:00 ETH/USDT sell $3,450 1.0 filled")
|
|
ux.Logger.PrintToUser(" 2025-01-14 16:45:00 BTC/USDT buy $67,000 0.1 cancelled")
|
|
return nil
|
|
}
|
|
|
|
// Pool command handlers
|
|
|
|
func poolListCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Liquidity Pools:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Pool ID Pair Type TVL APY Fee")
|
|
ux.Logger.PrintToUser(" 0xabc1... LUX/USDT constant-product $2.5M 12.5%% 0.3%%")
|
|
ux.Logger.PrintToUser(" 0xabc2... USDT/USDC stableswap $15.2M 4.2%% 0.04%%")
|
|
ux.Logger.PrintToUser(" 0xabc3... ETH/LUX concentrated $8.7M 18.3%% 0.3%%")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Total TVL: $26.4M across 3 pools")
|
|
return nil
|
|
}
|
|
|
|
func poolCreateCmd(cmd *cobra.Command, args []string) error {
|
|
token0, _ := cmd.Flags().GetString("token0")
|
|
token1, _ := cmd.Flags().GetString("token1")
|
|
amount0, _ := cmd.Flags().GetFloat64("amount0")
|
|
amount1, _ := cmd.Flags().GetFloat64("amount1")
|
|
poolType, _ := cmd.Flags().GetString("type")
|
|
fee, _ := cmd.Flags().GetUint16("fee")
|
|
|
|
if token0 == "" || token1 == "" || amount0 == 0 || amount1 == 0 {
|
|
return fmt.Errorf("required flags: --token0, --token1, --amount0, --amount1")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Creating liquidity pool...")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Pool Configuration:")
|
|
ux.Logger.PrintToUser(" Token Pair: %s/%s", token0, token1)
|
|
ux.Logger.PrintToUser(" Type: %s", poolType)
|
|
ux.Logger.PrintToUser(" Initial %s: %.4f", token0, amount0)
|
|
ux.Logger.PrintToUser(" Initial %s: %.4f", token1, amount1)
|
|
ux.Logger.PrintToUser(" Fee: %.2f%%", float64(fee)/100)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Pool created successfully!")
|
|
ux.Logger.PrintToUser("Pool ID: 0xnewpool...1234")
|
|
return nil
|
|
}
|
|
|
|
func poolAddLiquidityCmd(cmd *cobra.Command, args []string) error {
|
|
poolID := args[0]
|
|
amount0, _ := cmd.Flags().GetFloat64("amount0")
|
|
amount1, _ := cmd.Flags().GetFloat64("amount1")
|
|
|
|
ux.Logger.PrintToUser("Adding liquidity to pool %s...", poolID)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Amount0: %.4f", amount0)
|
|
ux.Logger.PrintToUser(" Amount1: %.4f", amount1)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Liquidity added! LP tokens received: 150.5")
|
|
return nil
|
|
}
|
|
|
|
func poolRemoveLiquidityCmd(cmd *cobra.Command, args []string) error {
|
|
poolID := args[0]
|
|
percent, _ := cmd.Flags().GetFloat64("percent")
|
|
|
|
ux.Logger.PrintToUser("Removing %.1f%% liquidity from pool %s...", percent, poolID)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Tokens received:")
|
|
ux.Logger.PrintToUser(" Token0: 50.25")
|
|
ux.Logger.PrintToUser(" Token1: 502.50")
|
|
return nil
|
|
}
|
|
|
|
func poolSwapCmd(cmd *cobra.Command, args []string) error {
|
|
from, _ := cmd.Flags().GetString("from")
|
|
to, _ := cmd.Flags().GetString("to")
|
|
amount, _ := cmd.Flags().GetFloat64("amount")
|
|
slippage, _ := cmd.Flags().GetFloat64("slippage")
|
|
|
|
if from == "" || to == "" || amount == 0 {
|
|
return fmt.Errorf("required flags: --from, --to, --amount")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Swapping %s to %s...", from, to)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Swap Details:")
|
|
ux.Logger.PrintToUser(" Input: %.4f %s", amount, from)
|
|
ux.Logger.PrintToUser(" Expected Output: %.4f %s", amount*1.25, to) // Mock calculation
|
|
ux.Logger.PrintToUser(" Price Impact: 0.12%%")
|
|
ux.Logger.PrintToUser(" Max Slippage: %.2f%%", slippage)
|
|
ux.Logger.PrintToUser(" Route: %s -> Pool(0xabc1...) -> %s", from, to)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Swap executed successfully!")
|
|
ux.Logger.PrintToUser("Transaction: 0xtx...hash")
|
|
return nil
|
|
}
|
|
|
|
// Perpetuals command handlers
|
|
|
|
func perpMarketsCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Perpetual Futures Markets:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Symbol Mark Price Index Price Funding OI Long OI Short Max Lev")
|
|
ux.Logger.PrintToUser(" BTC-PERP $67,502.50 $67,500.00 +0.0012%% $75M $50M 100x")
|
|
ux.Logger.PrintToUser(" ETH-PERP $3,451.20 $3,450.00 +0.0008%% $40M $27M 100x")
|
|
ux.Logger.PrintToUser(" LUX-PERP $12.51 $12.50 +0.0015%% $5M $3.5M 50x")
|
|
ux.Logger.PrintToUser(" SOL-PERP $185.30 $185.25 +0.0010%% $25M $18M 50x")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Next funding in: 4h 32m")
|
|
return nil
|
|
}
|
|
|
|
func perpOpenCmd(cmd *cobra.Command, args []string) error {
|
|
market, _ := cmd.Flags().GetString("market")
|
|
side, _ := cmd.Flags().GetString("side")
|
|
size, _ := cmd.Flags().GetFloat64("size")
|
|
leverage, _ := cmd.Flags().GetUint16("leverage")
|
|
marginMode, _ := cmd.Flags().GetString("margin")
|
|
|
|
if market == "" || side == "" || size == 0 {
|
|
return fmt.Errorf("required flags: --market, --side, --size")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Opening %s position on %s...", side, market)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Position Details:")
|
|
ux.Logger.PrintToUser(" Market: %s", market)
|
|
ux.Logger.PrintToUser(" Side: %s", side)
|
|
ux.Logger.PrintToUser(" Size: %.4f", size)
|
|
ux.Logger.PrintToUser(" Leverage: %dx", leverage)
|
|
ux.Logger.PrintToUser(" Margin Mode: %s", marginMode)
|
|
ux.Logger.PrintToUser(" Entry Price: $67,502.50")
|
|
ux.Logger.PrintToUser(" Liquidation Price: $60,752.25")
|
|
ux.Logger.PrintToUser(" Required Margin: $675.03")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Position opened successfully!")
|
|
ux.Logger.PrintToUser("Position ID: 0xpos...1234")
|
|
return nil
|
|
}
|
|
|
|
func perpCloseCmd(cmd *cobra.Command, args []string) error {
|
|
market := args[0]
|
|
percent, _ := cmd.Flags().GetFloat64("percent")
|
|
|
|
ux.Logger.PrintToUser("Closing %.0f%% of %s position...", percent, market)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Realized P&L: +$125.50 (+2.3%%)")
|
|
ux.Logger.PrintToUser("Position closed successfully!")
|
|
return nil
|
|
}
|
|
|
|
func perpPositionsCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Open Perpetual Positions:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Market Side Size Entry Mark Liq Price Margin uPnL")
|
|
ux.Logger.PrintToUser(" BTC-PERP long 0.1 $67,000 $67,502 $60,300 $670 +$50.25")
|
|
ux.Logger.PrintToUser(" ETH-PERP short 2.0 $3,500 $3,451 $3,850 $700 +$98.00")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Total Unrealized P&L: +$148.25")
|
|
ux.Logger.PrintToUser("Total Margin Used: $1,370.00")
|
|
ux.Logger.PrintToUser("Account Margin Ratio: 15.2%%")
|
|
return nil
|
|
}
|
|
|
|
func perpPnLCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Profit & Loss Summary:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Today:")
|
|
ux.Logger.PrintToUser(" Realized P&L: +$523.45")
|
|
ux.Logger.PrintToUser(" Unrealized P&L: +$148.25")
|
|
ux.Logger.PrintToUser(" Funding Paid: -$12.30")
|
|
ux.Logger.PrintToUser(" Net P&L: +$659.40")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("7 Days:")
|
|
ux.Logger.PrintToUser(" Realized P&L: +$2,345.67")
|
|
ux.Logger.PrintToUser(" Funding Paid: -$89.45")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("30 Days:")
|
|
ux.Logger.PrintToUser(" Realized P&L: +$8,901.23")
|
|
ux.Logger.PrintToUser(" Funding Paid: -$345.67")
|
|
return nil
|
|
}
|
|
|
|
func perpFundingCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Funding Rate Information:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Current Rates (8h interval):")
|
|
ux.Logger.PrintToUser(" Market Rate Annual Next Payment")
|
|
ux.Logger.PrintToUser(" BTC-PERP +0.0012%% +10.5%% 4h 32m")
|
|
ux.Logger.PrintToUser(" ETH-PERP +0.0008%% +7.0%% 4h 32m")
|
|
ux.Logger.PrintToUser(" LUX-PERP +0.0015%% +13.1%% 4h 32m")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Recent Funding Payments:")
|
|
ux.Logger.PrintToUser(" Time Market Amount")
|
|
ux.Logger.PrintToUser(" 2025-01-15 08:00 BTC-PERP -$8.10")
|
|
ux.Logger.PrintToUser(" 2025-01-15 08:00 ETH-PERP +$5.60")
|
|
ux.Logger.PrintToUser(" 2025-01-15 00:00 BTC-PERP -$7.85")
|
|
return nil
|
|
}
|
|
|
|
// Account command handlers
|
|
|
|
func accountBalanceCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Account Balances:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Spot Wallet:")
|
|
ux.Logger.PrintToUser(" Token Available In Orders Total USD Value")
|
|
ux.Logger.PrintToUser(" LUX 1,000.00 100.00 1,100.00 $13,750.00")
|
|
ux.Logger.PrintToUser(" USDT 5,000.00 500.00 5,500.00 $5,500.00")
|
|
ux.Logger.PrintToUser(" BTC 0.5 0.1 0.6 $40,500.00")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Margin Account:")
|
|
ux.Logger.PrintToUser(" Balance: $10,000.00")
|
|
ux.Logger.PrintToUser(" Available: $8,630.00")
|
|
ux.Logger.PrintToUser(" Used Margin: $1,370.00")
|
|
ux.Logger.PrintToUser(" Unrealized P&L: +$148.25")
|
|
ux.Logger.PrintToUser(" Margin Ratio: 15.2%%")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Total Account Value: $69,898.25")
|
|
return nil
|
|
}
|
|
|
|
func accountDepositCmd(cmd *cobra.Command, args []string) error {
|
|
token, _ := cmd.Flags().GetString("token")
|
|
amount, _ := cmd.Flags().GetFloat64("amount")
|
|
|
|
if token == "" || amount == 0 {
|
|
return fmt.Errorf("required flags: --token, --amount")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Depositing %.4f %s to trading account...", amount, token)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Deposit successful!")
|
|
ux.Logger.PrintToUser("Transaction: 0xdep...osit")
|
|
ux.Logger.PrintToUser("New balance: %.4f %s", amount+1000, token)
|
|
return nil
|
|
}
|
|
|
|
func accountWithdrawCmd(cmd *cobra.Command, args []string) error {
|
|
token, _ := cmd.Flags().GetString("token")
|
|
amount, _ := cmd.Flags().GetFloat64("amount")
|
|
|
|
if token == "" || amount == 0 {
|
|
return fmt.Errorf("required flags: --token, --amount")
|
|
}
|
|
|
|
ux.Logger.PrintToUser("Withdrawing %.4f %s from trading account...", amount, token)
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Withdrawal successful!")
|
|
ux.Logger.PrintToUser("Transaction: 0xwith...draw")
|
|
return nil
|
|
}
|
|
|
|
func accountHistoryCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Transaction History (Last 10):")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser(" Time Type Token Amount Status")
|
|
ux.Logger.PrintToUser(" 2025-01-15 10:30 deposit USDT 1,000.00 confirmed")
|
|
ux.Logger.PrintToUser(" 2025-01-14 15:45 withdraw LUX 50.00 confirmed")
|
|
ux.Logger.PrintToUser(" 2025-01-14 12:00 deposit BTC 0.1 confirmed")
|
|
ux.Logger.PrintToUser(" 2025-01-13 09:30 deposit USDT 5,000.00 confirmed")
|
|
return nil
|
|
}
|
|
|
|
// Status command handler
|
|
|
|
func statusCmd(cmd *cobra.Command, args []string) error {
|
|
ux.Logger.PrintToUser("Lux DEX Status:")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Network:")
|
|
ux.Logger.PrintToUser(" Status: Online")
|
|
ux.Logger.PrintToUser(" Connected Nodes: 47")
|
|
ux.Logger.PrintToUser(" Block Height: 1,234,567")
|
|
ux.Logger.PrintToUser(" Block Time: 1ms (HFT optimized)")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Markets:")
|
|
ux.Logger.PrintToUser(" Spot Markets: 12")
|
|
ux.Logger.PrintToUser(" Perp Markets: 8")
|
|
ux.Logger.PrintToUser(" Liquidity Pools: 15")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("24h Statistics:")
|
|
ux.Logger.PrintToUser(" Total Volume: $234.5M")
|
|
ux.Logger.PrintToUser(" Trades: 1,234,567")
|
|
ux.Logger.PrintToUser(" Unique Traders: 12,345")
|
|
ux.Logger.PrintToUser(" Open Interest: $450M")
|
|
ux.Logger.PrintToUser("")
|
|
ux.Logger.PrintToUser("Insurance Fund: $5.2M")
|
|
return nil
|
|
}
|