-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdex_trading.rs
More file actions
86 lines (73 loc) · 2.68 KB
/
Copy pathdex_trading.rs
File metadata and controls
86 lines (73 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! DEX Trading Example
//!
//! Demonstrates decentralized exchange operations:
//! - Hyperliquid perpetual futures
//! - dYdX v4 trading
//!
//! NOTE: DEX trading requires wallet setup and proper credentials.
use ccxt_rust::client::ExchangeConfig;
use ccxt_rust::exchanges::dex::Hyperliquid;
use ccxt_rust::types::Exchange;
use ccxt_rust::CcxtResult;
use std::env;
#[tokio::main]
async fn main() -> CcxtResult<()> {
println!("=== DEX Trading Example ===\n");
// Example 1: Hyperliquid (public endpoints)
hyperliquid_example().await?;
println!("\n=== Example Complete ===");
Ok(())
}
async fn hyperliquid_example() -> CcxtResult<()> {
println!("--- Hyperliquid ---");
// Check for private key (optional)
let private_key = env::var("HYPERLIQUID_PRIVATE_KEY").ok();
let config = ExchangeConfig::new();
let exchange = if let Some(pk) = private_key {
println!("Using authenticated mode");
Hyperliquid::from_private_key(config, &pk)?
} else {
println!("Using public mode (no trading available)");
Hyperliquid::new(config)?
};
// Fetch markets
println!("\nFetching markets...");
let markets = exchange.fetch_markets().await?;
println!("Available markets: {}", markets.len());
// Show perpetual markets
println!("\nPerpetual markets (first 10):");
for market in markets.iter().take(10) {
println!(" {} - Type: {:?}", market.symbol, market.market_type);
}
// Fetch ticker for BTC
println!("\nFetching BTC ticker...");
let ticker = exchange.fetch_ticker("BTC/USDC:USDC").await?;
println!("BTC Perpetual:");
println!(" Last: {:?}", ticker.last);
println!(" Bid: {:?}", ticker.bid);
println!(" Ask: {:?}", ticker.ask);
println!(" Mark Price: {:?}", ticker.mark_price);
println!(" Index Price: {:?}", ticker.index_price);
// Fetch order book
println!("\nFetching order book...");
let orderbook = exchange.fetch_order_book("BTC/USDC:USDC", Some(5)).await?;
println!("Top 5 Bids:");
for bid in orderbook.bids.iter().take(5) {
println!(" {} @ {}", bid.amount, bid.price);
}
println!("Top 5 Asks:");
for ask in orderbook.asks.iter().take(5) {
println!(" {} @ {}", ask.amount, ask.price);
}
// Fetch funding rate
println!("\nFetching funding rate...");
match exchange.fetch_funding_rate("BTC/USDC:USDC").await {
Ok(funding) => {
println!("BTC Funding Rate:");
println!(" Current Rate: {:?}", funding.funding_rate);
println!(" Next Funding: {:?}", funding.funding_datetime);
},
Err(e) => println!("Could not fetch funding rate: {}", e),
}
Ok(())
}