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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! O2 Exchange SDK for Rust.
//!
//! A production-quality SDK for interacting with the O2 Exchange,
//! a fully on-chain order book DEX on the Fuel Network.
//!
//! # What This SDK Provides
//!
//! - High-level workflow client: [`O2Client`]
//! - Typed REST API access: [`api::O2Api`]
//! - Typed WebSocket streams: [`TypedStream`]
//! - Strong domain models for markets, balances, orders, and sessions
//!
//! # Quick Start
//!
//! ```rust,no_run
//! use o2_sdk::{O2Client, Network};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), o2_sdk::O2Error> {
//! let mut client = O2Client::new(Network::Testnet);
//!
//! // Generate a wallet
//! let wallet = client.generate_wallet()?;
//!
//! // Setup account (idempotent)
//! let _account = client.setup_account(&wallet).await?;
//!
//! // Fetch markets
//! let _markets = client.get_markets().await?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Recommended Workflow
//!
//! 1. Create an [`O2Client`] with the target [`Network`].
//! 2. Create or load a wallet.
//! 3. Run [`O2Client::setup_account`] once at startup.
//! 4. Create a signed trading session with [`O2Client::create_session`].
//! 5. Submit typed actions with [`O2Client::create_order`], or compose a batch with
//! [`O2Client::actions_for`] then send via [`O2Client::batch_actions`].
//! 6. Stream updates with [`O2Client::stream_depth`] / [`O2Client::stream_orders`] / [`O2Client::stream_nonce`].
//!
//! # Common Tasks
//!
//! ## Wallet + Account Setup
//!
//! ```rust,no_run
//! use o2_sdk::{Network, O2Client};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), o2_sdk::O2Error> {
//! let mut client = O2Client::new(Network::Testnet);
//! let wallet = client.generate_wallet()?;
//! let account = client.setup_account(&wallet).await?;
//!
//! println!("trade account id: {:?}", account.trade_account_id);
//! Ok(())
//! }
//! ```
//!
//! ## Market Discovery + Session Creation
//!
//! ```rust,no_run
//! use o2_sdk::{Network, O2Client};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), o2_sdk::O2Error> {
//! let mut client = O2Client::new(Network::Testnet);
//! let owner = client.generate_wallet()?;
//! client.setup_account(&owner).await?;
//!
//! let mut session = client.create_session(&owner, &["fuel/usdc"], std::time::Duration::from_secs(7 * 24 * 3600)).await?;
//! println!("session nonce: {}", session.nonce);
//! Ok(())
//! }
//! ```
//!
//! ## Place and Cancel Orders
//!
//! ```rust,no_run
//! use o2_sdk::{Network, O2Client, OrderType, Side};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), o2_sdk::O2Error> {
//! let mut client = O2Client::new(Network::Testnet);
//! let owner = client.generate_wallet()?;
//! client.setup_account(&owner).await?;
//!
//! let market = "fuel/usdc";
//! let mut session = client.create_session(&owner, &[market], std::time::Duration::from_secs(7 * 24 * 3600)).await?;
//! let market_info = client.get_market(market).await?;
//! let price = market_info.price("100")?;
//! let quantity = market_info.quantity("2")?;
//!
//! let response = client
//! .create_order(
//! &mut session,
//! market,
//! Side::Buy,
//! price,
//! quantity,
//! OrderType::Market,
//! false,
//! true,
//! )
//! .await?;
//!
//! if let Some(order_id) = response
//! .orders
//! .as_ref()
//! .and_then(|orders| orders.first())
//! .map(|o| o.order_id.clone())
//! {
//! let _ = client.cancel_order(&mut session, &order_id, market).await?;
//! }
//! Ok(())
//! }
//! ```
//!
//! ## Balances and Nonce Management
//!
//! ```rust,no_run
//! use o2_sdk::{Network, O2Client};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), o2_sdk::O2Error> {
//! let mut client = O2Client::new(Network::Testnet);
//! let wallet = client.generate_wallet()?;
//! let account = client.setup_account(&wallet).await?;
//! let trade_account_id = account.trade_account_id.unwrap();
//!
//! let balances = client.get_balances(&trade_account_id).await?;
//! for (symbol, balance) in balances {
//! println!("{symbol}: {}", balance.trading_account_balance);
//! }
//!
//! let nonce = client.get_nonce(trade_account_id.as_str()).await?;
//! println!("nonce: {nonce}");
//! Ok(())
//! }
//! ```
//!
//! # Logging
//!
//! This crate emits debug-level logs through the [`log`](https://docs.rs/log/) facade
//! for API and client calls. Configure any compatible logger in your binary, then set
//! `RUST_LOG=debug` to inspect request flow and setup behavior.
//!
//! Market metadata refresh can be configured via [`MetadataPolicy`] and
//! [`O2Client::set_metadata_policy`].
//!
//! # Errors
//!
//! All fallible operations return [`O2Error`]. Match specific variants for robust handling:
//!
//! - Preflight/API validation failures (`code`/`message` style errors)
//! - On-chain revert failures (`OnChainRevert`)
//! - Transport/serialization failures (`HttpError`, `JsonError`, etc.)
//!
//! See [`guides::error_handling`] for recovery patterns.
//!
//! # Guides
//!
//! The [`guides`] module contains integration guides covering common
//! workflows and patterns:
//!
//! - [`guides::trading`] — Order types, batch actions, and market maker patterns
//! - [`guides::market_data`] — Fetching depth, trades, candles, and balances
//! - [`guides::websocket_streams`] — Real-time data with `TypedStream`
//! - [`guides::error_handling`] — Error types and recovery patterns
//! - [`guides::external_signers`] — Integrating KMS/HSM via the `SignableWallet` trait
// Re-export primary types for convenience.
pub use ;
pub use ;
pub use ;
pub use UnsignedDecimal;
pub use O2Error;
pub use *;
pub use ;
pub use ;