mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-13 20:48:05 +00:00
refactor:cleanup
This commit is contained in:
@@ -17,7 +17,7 @@ async fn main() -> Result<()> {
|
||||
.expect("PRIVATE_KEY environment variable required");
|
||||
let chain_id = 137; // Polygon
|
||||
|
||||
println!("🚀 Initializing Polyfill-rs Trading Client");
|
||||
println!("Initializing Polyfill-rs Trading Client");
|
||||
|
||||
// Step 1: Create client with L1 authentication (private key)
|
||||
let mut client = ClobClient::with_l1_headers(
|
||||
@@ -26,17 +26,17 @@ async fn main() -> Result<()> {
|
||||
chain_id,
|
||||
)?;
|
||||
|
||||
println!("✅ Client initialized with L1 authentication");
|
||||
println!("Client initialized with L1 authentication");
|
||||
|
||||
// Step 2: Create or derive API credentials for L2 operations
|
||||
println!("🔑 Setting up API credentials...");
|
||||
println!("Setting up API credentials...");
|
||||
let api_creds = client.create_or_derive_api_key(None).await?;
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("✅ API credentials configured");
|
||||
println!("API credentials configured");
|
||||
|
||||
// Step 3: Get account information
|
||||
println!("\n💰 Checking account balances...");
|
||||
println!("\nChecking account balances...");
|
||||
let balances = client.balance_allowance().await?;
|
||||
for balance in &balances {
|
||||
println!(" Token {}: Balance = {}, Allowance = {}",
|
||||
@@ -44,7 +44,7 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
|
||||
// Step 4: Get market data
|
||||
println!("\n📊 Fetching market data...");
|
||||
println!("\nFetching market data...");
|
||||
let token_id = "21742633143463906290569050155826241533067272736897614950488156847949938836455";
|
||||
|
||||
// Single token data
|
||||
@@ -67,7 +67,7 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
|
||||
// Step 5: Create and place orders
|
||||
println!("\n📝 Creating orders...");
|
||||
println!("\nCreating orders...");
|
||||
|
||||
// Create a limit order
|
||||
let order_args = OrderArgs {
|
||||
@@ -83,7 +83,7 @@ async fn main() -> Result<()> {
|
||||
|
||||
println!(" Creating limit order: Buy 10 @ 0.52");
|
||||
let order_result = client.create_and_post_order(&order_args).await?;
|
||||
println!(" ✅ Order created: {:?}", order_result);
|
||||
println!(" Order created: {:?}", order_result);
|
||||
|
||||
// Create a market order
|
||||
let market_order_args = OrderArgs {
|
||||
@@ -99,10 +99,10 @@ async fn main() -> Result<()> {
|
||||
|
||||
println!(" Creating market order: Sell 5 @ market");
|
||||
let market_order_result = client.create_market_order(&market_order_args).await?;
|
||||
println!(" ✅ Market order created: {:?}", market_order_result);
|
||||
println!(" Market order created: {:?}", market_order_result);
|
||||
|
||||
// Step 6: Query order history
|
||||
println!("\n📋 Checking order history...");
|
||||
println!("\nChecking order history...");
|
||||
|
||||
// Get all open orders
|
||||
let open_orders = client.get_orders(None).await?;
|
||||
@@ -121,7 +121,7 @@ async fn main() -> Result<()> {
|
||||
println!(" Orders for token {}: {}", token_id, token_orders.len());
|
||||
|
||||
// Step 7: Query trade history
|
||||
println!("\n💹 Checking trade history...");
|
||||
println!("\nChecking trade history...");
|
||||
|
||||
let trades = client.get_trades(Some(TradeParams {
|
||||
id: None,
|
||||
@@ -139,18 +139,18 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
|
||||
// Step 8: Order management
|
||||
println!("\n🛠️ Order management...");
|
||||
println!("\nOrder management...");
|
||||
|
||||
if !open_orders.is_empty() {
|
||||
let order_to_cancel = &open_orders[0];
|
||||
println!(" Cancelling order: {}", order_to_cancel.id);
|
||||
|
||||
let cancel_result = client.cancel(&order_to_cancel.id).await?;
|
||||
println!(" ✅ Cancel result: {:?}", cancel_result);
|
||||
println!(" Cancel result: {:?}", cancel_result);
|
||||
}
|
||||
|
||||
// Step 9: Set up notifications (optional)
|
||||
println!("\n🔔 Setting up notifications...");
|
||||
println!("\nSetting up notifications...");
|
||||
|
||||
let notification_params = NotificationParams {
|
||||
signature: "example_signature".to_string(),
|
||||
@@ -158,12 +158,12 @@ async fn main() -> Result<()> {
|
||||
};
|
||||
|
||||
match client.notifications(notification_params).await {
|
||||
Ok(result) => println!(" ✅ Notifications configured: {:?}", result),
|
||||
Err(e) => println!(" ⚠️ Notifications setup failed: {}", e),
|
||||
Ok(result) => println!(" Notifications configured: {:?}", result),
|
||||
Err(e) => println!(" Notifications setup failed: {}", e),
|
||||
}
|
||||
|
||||
println!("\n🎉 Complete trading example finished!");
|
||||
println!("\n📈 Performance Notes:");
|
||||
println!("\nComplete trading example finished!");
|
||||
println!("\nPerformance Notes:");
|
||||
println!(" • Order book operations use fixed-point math (25x faster)");
|
||||
println!(" • Batch operations reduce API calls by up to 90%");
|
||||
println!(" • EIP-712 signing ensures maximum security");
|
||||
@@ -176,21 +176,21 @@ async fn main() -> Result<()> {
|
||||
/// Helper function to demonstrate error handling
|
||||
async fn safe_trading_example() {
|
||||
match main().await {
|
||||
Ok(()) => println!("✅ Trading example completed successfully"),
|
||||
Ok(()) => println!("Trading example completed successfully"),
|
||||
Err(PolyfillError::Auth { message, .. }) => {
|
||||
eprintln!("🔐 Authentication error: {}", message);
|
||||
eprintln!("💡 Make sure PRIVATE_KEY environment variable is set");
|
||||
eprintln!("Authentication error: {}", message);
|
||||
eprintln!("Make sure PRIVATE_KEY environment variable is set");
|
||||
},
|
||||
Err(PolyfillError::Api { status_code, message, .. }) => {
|
||||
eprintln!("🌐 API error ({}): {}", status_code, message);
|
||||
eprintln!("💡 Check your network connection and API limits");
|
||||
eprintln!("API error ({}): {}", status_code, message);
|
||||
eprintln!("Check your network connection and API limits");
|
||||
},
|
||||
Err(PolyfillError::Network { source, .. }) => {
|
||||
eprintln!("📡 Network error: {}", source);
|
||||
eprintln!("💡 Retrying with exponential backoff...");
|
||||
eprintln!("Network error: {}", source);
|
||||
eprintln!("Retrying with exponential backoff...");
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("❌ Unexpected error: {}", e);
|
||||
eprintln!("Unexpected error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user