gateio_rs/api/spot/mod.rs
1//! # Gate.io Spot Trading API
2//!
3//! This module provides a comprehensive interface to Gate.io's Spot trading API endpoints.
4//! All functions return request builders that can be configured with additional parameters
5//! before sending via the HTTP client.
6//!
7//! ## Categories
8//!
9//! ### Market Data (Public)
10//! - [`get_currencies`] - List all supported currencies
11//! - [`get_currency`] - Get specific currency details
12//! - [`get_currency_pairs`] - List all trading pairs
13//! - [`get_currency_pair`] - Get specific pair details
14//! - [`get_ticker`] - Get ticker information
15//! - [`get_orderbook`] - Get order book data
16//! - [`get_market_trades`] - Get recent market trades
17//! - [`get_candlesticks`] - Get historical price data
18//! - [`get_server_time`] - Get server timestamp
19//!
20//! ### Account Management (Private)
21//! - [`get_account`] - List spot account balances
22//! - [`get_account_book`] - Query account transaction history
23//! - [`get_fee`] - Get trading fee rates
24//! - [`get_batch_user_fee`] - Get fee rates for multiple pairs
25//!
26//! ### Order Management (Private)
27//! - [`create_order`] - Place a new order
28//! - [`create_batch_orders`] - Place multiple orders
29//! - [`get_orders`] - List order history
30//! - [`get_order`] - Get specific order details
31//! - [`get_open_orders`] - List active orders
32//! - [`amend_order`] - Modify an existing order
33//! - [`amend_batch_orders`] - Modify multiple orders
34//! - [`cancel_order`] - Cancel a specific order
35//! - [`cancel_batch_orders`] - Cancel multiple orders
36//! - [`cancel_all_open_orders`] - Cancel all open orders
37//! - [`countdown_cancel_all`] - Set auto-cancel timer
38//!
39//! ### Advanced Orders (Private)
40//! - [`create_price_order`] - Create stop/trigger orders
41//! - [`get_price_orders`] - List active trigger orders
42//! - [`get_price_order`] - Get specific trigger order
43//! - [`cancel_price_order`] - Cancel trigger order
44//! - [`cancel_all_price_orders`] - Cancel all trigger orders
45//! - [`create_cross_liquidate_orders`] - Cross-currency liquidation
46//!
47//! ### Trading History (Private)
48//! - [`get_my_trades`] - Get personal trade history
49//! - [`get_insurance_history`] - Get insurance fund data
50//!
51//! ## Example Usage
52//!
53//! ```rust,no_run
54//! use gateio_rs::{
55//! api::spot::{get_ticker, create_order},
56//! http::Credentials,
57//! ureq::GateHttpClient,
58//! };
59//!
60//! // Public API - no authentication needed
61//! let client = GateHttpClient::default();
62//! let ticker_req = get_ticker().currency_pair("BTC_USDT");
63//! let response = client.send(ticker_req)?;
64//!
65//! // Private API - requires authentication
66//! let credentials = Credentials::new("api_key", "api_secret");
67//! let client = GateHttpClient::default().credentials(credentials);
68//! let order_req = create_order("BTC_USDT", "buy", "0.001").price("50000");
69//! let response = client.send(order_req)?;
70//! # Ok::<(), Box<dyn std::error::Error>>(()).expect("");
71//! ```
72//!
73//! For detailed parameter documentation, see the [Gate.io API Documentation](https://www.gate.com/docs/developers/apiv4/#spot).
74
75/// Amend multiple orders in batch
76pub mod amend_batch_orders;
77/// Amend a single order
78pub mod amend_order;
79/// Cancel all open orders
80pub mod cancel_all_open_orders;
81/// Cancel all price orders
82pub mod cancel_all_price_orders;
83/// Cancel multiple orders in batch
84pub mod cancel_batch_orders;
85/// Cancel a single order
86pub mod cancel_order;
87/// Cancel a price order
88pub mod cancel_price_order;
89/// Set countdown timer to cancel all orders
90pub mod countdown_cancel_all;
91/// Create multiple orders in batch
92pub mod create_batch_orders;
93/// Create cross liquidate orders
94pub mod create_cross_liquidate_orders;
95/// Create a single order
96pub mod create_order;
97/// Create a price order
98pub mod create_price_order;
99/// Get account information
100pub mod get_account;
101/// Get account transaction history
102pub mod get_account_book;
103/// Get batch user fee rates
104pub mod get_batch_user_fee;
105/// Get candlestick data
106pub mod get_candlesticks;
107/// Get all supported currencies
108pub mod get_currencies;
109/// Get single currency information
110pub mod get_currency;
111/// Get currency pair details
112pub mod get_currency_pair;
113/// Get all currency pairs
114pub mod get_currency_pairs;
115/// Get trading fees
116pub mod get_fee;
117/// Get insurance fund history
118pub mod get_insurance_history;
119/// Get market trade history
120pub mod get_market_trades;
121/// Get user's trade history
122pub mod get_my_trades;
123/// Get open orders
124pub mod get_open_orders;
125/// Get single order details
126pub mod get_order;
127/// Get order book
128pub mod get_orderbook;
129/// Get order history
130pub mod get_orders;
131/// Get single price order
132pub mod get_price_order;
133/// Get price order history
134pub mod get_price_orders;
135/// Get server time
136pub mod get_server_time;
137/// Get ticker information
138pub mod get_ticker;
139/// Order data structures
140pub mod order;
141
142use get_account::GetAccount;
143use get_account_book::GetAccountBook;
144use get_batch_user_fee::GetBatchUserFee;
145use get_candlesticks::GetCandlesticks;
146use get_currencies::GetCurrencies;
147use get_currency::GetCurrency;
148use get_currency_pair::GetCurrencyPair;
149use get_currency_pairs::GetCurrencyPairs;
150use get_market_trades::GetMarketTrades;
151use get_orderbook::GetOrderbook;
152use get_ticker::GetTicker;
153
154use amend_batch_orders::AmendBatchOrders;
155pub use amend_batch_orders::OrderAmendment;
156use amend_order::AmendOrder;
157use cancel_all_open_orders::CancelAllOpenOrders;
158use cancel_all_price_orders::CancelAllPriceOrders;
159use cancel_batch_orders::CancelBatchOrders;
160pub use cancel_batch_orders::CancelOrderRequest;
161use cancel_order::CancelOrder;
162use cancel_price_order::CancelPriceOrder;
163use countdown_cancel_all::CountdownCancelAll;
164use create_batch_orders::CreateBatchOrders;
165use create_cross_liquidate_orders::CreateCrossLiquidateOrders;
166pub use create_cross_liquidate_orders::CrossLiquidateOrder;
167use create_order::CreateOrder;
168use create_price_order::CreatePriceOrder;
169use get_fee::GetFee;
170use get_insurance_history::GetInsuranceHistory;
171use get_my_trades::GetMyTrades;
172use get_open_orders::GetOpenOrders;
173use get_order::GetOrder;
174use get_orders::GetOrders;
175use get_price_order::GetPriceOrder;
176use get_price_orders::GetPriceOrders;
177use get_server_time::GetServerTime;
178pub use order::Order;
179
180/// List all currencies' details <br/>
181/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-all-currencies-details)
182pub fn get_currencies() -> GetCurrencies {
183 GetCurrencies::new()
184}
185
186/// Get details of a specific currency <br/>
187/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#get-details-of-a-specific-currency)
188pub fn get_currency(currency: &str) -> GetCurrency {
189 GetCurrency::new(currency)
190}
191
192/// List all currency pairs supported <br/>
193/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-all-currency-pairs-supported)
194pub fn get_currency_pairs() -> GetCurrencyPairs {
195 GetCurrencyPairs::new()
196}
197
198/// Get details of a specific currency pair <br/>
199/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#get-details-of-a-specifc-currency-pair)
200pub fn get_currency_pair(currency_pair: &str) -> GetCurrencyPair {
201 GetCurrencyPair::new(currency_pair)
202}
203
204/// Retrieve ticker information for currency pairs.
205///
206/// Returns 24hr trading statistics including price, volume, and changes.
207/// Can be used to get data for a specific pair or all pairs at once.
208///
209/// # Parameters
210/// - `currency_pair`: Optional. Specific trading pair (e.g., "BTC_USDT")
211/// - `timezone`: Optional. Timezone for calculation ("utc0" to "utc12", "utc-12" to "utc-1")
212///
213/// # Examples
214///
215/// ```rust,no_run
216/// use gateio_rs::{api::spot::get_ticker, ureq::GateHttpClient};
217///
218/// let client = GateHttpClient::default();
219///
220/// // Get ticker for specific pair
221/// let request = get_ticker().currency_pair("BTC_USDT");
222/// let response = client.send(request)?;
223///
224/// // Get all tickers
225/// let request = get_ticker();
226/// let response = client.send(request)?;
227/// # Ok::<(), Box<dyn std::error::Error>>(()).expect("");
228/// ```
229///
230/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#retrieve-ticker-information)
231pub fn get_ticker() -> GetTicker {
232 GetTicker::new()
233}
234
235/// Retrieve order book <br/>
236/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#retrieve-order-book)
237pub fn get_orderbook(currency_pair: &str) -> GetOrderbook {
238 GetOrderbook::new(currency_pair)
239}
240
241/// Retrieve market trades
242/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#retrieve-market-trades)
243pub fn get_market_trades(currency_pair: &str) -> GetMarketTrades {
244 GetMarketTrades::new(currency_pair)
245}
246
247/// Market candlesticks
248/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#market-candlesticks)
249pub fn get_candlesticks(currency_pair: &str) -> GetCandlesticks {
250 GetCandlesticks::new(currency_pair)
251}
252
253/// Query a batch of user trading fee rates
254/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#query-a-batch-of-user-trading-fee-rates)
255pub fn get_batch_user_fee(currency_pairs: &str) -> GetBatchUserFee {
256 GetBatchUserFee::new(currency_pairs)
257}
258
259/// List spot account balances and information.
260///
261/// Returns account balances for all currencies in the spot trading account.
262/// Shows available balance, locked balance, and total balance for each currency.
263///
264/// # Authentication
265/// This endpoint requires API key authentication.
266///
267/// # Optional Parameters (via builder methods)
268/// - `currency`: Filter by specific currency (e.g., "BTC", "USDT")
269///
270/// # Examples
271///
272/// ```rust,no_run
273/// use gateio_rs::{
274/// api::spot::get_account,
275/// http::Credentials,
276/// ureq::GateHttpClient,
277/// };
278///
279/// let credentials = Credentials::new("api_key", "api_secret");
280/// let client = GateHttpClient::default().credentials(credentials);
281///
282/// // Get all account balances
283/// let request = get_account();
284/// let response = client.send(request)?;
285///
286/// // Get specific currency balance
287/// let request = get_account().currency("BTC");
288/// let response = client.send(request)?;
289/// # Ok::<(), Box<dyn std::error::Error>>(()).expect("");
290/// ```
291///
292/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-spot-accounts)
293pub fn get_account() -> GetAccount {
294 GetAccount::new()
295}
296
297/// Query account book <br/>
298/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#query-account-book)
299pub fn get_account_book() -> GetAccountBook {
300 GetAccountBook::new()
301}
302
303/// List all open orders <br/>
304/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-all-open-orders)
305pub fn get_open_orders() -> GetOpenOrders {
306 GetOpenOrders::new()
307}
308
309/// Create a new trading order.
310///
311/// Places a buy or sell order on the specified trading pair. Supports various order types
312/// including market, limit, and immediate-or-cancel orders.
313///
314/// # Parameters
315/// - `currency_pair`: Trading pair (e.g., "BTC_USDT")
316/// - `side`: Order side ("buy" or "sell")
317/// - `amount`: Order amount in base currency
318///
319/// # Optional Parameters (via builder methods)
320/// - `price`: Order price (required for limit orders)
321/// - `order_type`: "limit", "market", "ioc", "poc", "fok" (default: "limit")
322/// - `account`: Account type ("spot", "margin", "cross_margin", "unified")
323/// - `time_in_force`: "gtc", "ioc", "poc", "fok" (default: "gtc")
324/// - `text`: Custom order ID for identification
325///
326/// # Examples
327///
328/// ```rust,no_run
329/// use gateio_rs::{
330/// api::spot::create_order,
331/// http::Credentials,
332/// ureq::GateHttpClient,
333/// };
334///
335/// let credentials = Credentials::new("api_key", "api_secret");
336/// let client = GateHttpClient::default().credentials(credentials);
337///
338/// // Limit buy order
339/// let request = create_order("BTC_USDT", "buy", "0.001")
340/// .price("50000")
341/// .order_type("limit")
342/// .time_in_force("gtc");
343/// let response = client.send(request)?;
344///
345/// // Market sell order
346/// let request = create_order("BTC_USDT", "sell", "0.001")
347/// .order_type("market");
348/// let response = client.send(request)?;
349/// # Ok::<(), Box<dyn std::error::Error>>(()).expect("");
350/// ```
351///
352/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#create-an-order)
353pub fn create_order(currency_pair: &str, side: &str, amount: &str) -> CreateOrder {
354 CreateOrder::new(currency_pair, side, amount)
355}
356
357/// List orders <br/>
358/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-orders)
359pub fn get_orders() -> GetOrders {
360 GetOrders::new()
361}
362
363/// Get a single order <br/>
364/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#get-a-single-order)
365pub fn get_order(order_id: &str, currency_pair: &str) -> GetOrder {
366 GetOrder::new(order_id, currency_pair)
367}
368
369/// Cancel a batch of orders <br/>
370/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#cancel-a-batch-of-orders-with-an-id-list)
371pub fn cancel_batch_orders(orders: Vec<CancelOrderRequest>) -> CancelBatchOrders {
372 CancelBatchOrders::new(orders)
373}
374
375/// List personal trading history <br/>
376/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#list-personal-trading-history)
377pub fn get_my_trades() -> GetMyTrades {
378 GetMyTrades::new()
379}
380
381/// Query user trading fee rates <br/>
382/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#query-user-trading-fee-rates)
383pub fn get_fee() -> GetFee {
384 GetFee::new()
385}
386
387/// Cancel a single order <br/>
388/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#cancel-a-single-order)
389pub fn cancel_order(order_id: &str, currency_pair: &str) -> CancelOrder {
390 CancelOrder::new(order_id, currency_pair)
391}
392
393/// Amend an order <br/>
394/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#amend-an-order)
395pub fn amend_order(order_id: &str, currency_pair: &str) -> AmendOrder {
396 AmendOrder::new(order_id, currency_pair)
397}
398
399/// Create a batch of orders <br/>
400/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#create-a-batch-of-orders)
401pub fn create_batch_orders(orders: Vec<Order>) -> CreateBatchOrders {
402 CreateBatchOrders::new(orders)
403}
404
405/// Create a price-triggered order <br/>
406/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#create-a-price-triggered-order)
407pub fn create_price_order(
408 market: &str,
409 trigger_price: &str,
410 trigger_rule: &str,
411 order_side: &str,
412 order_price: &str,
413 order_amount: &str,
414) -> CreatePriceOrder {
415 CreatePriceOrder::new(
416 market,
417 trigger_price,
418 trigger_rule,
419 order_side,
420 order_price,
421 order_amount,
422 )
423}
424
425/// Retrieve running auto order list <br/>
426/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#retrieve-running-auto-order-list)
427pub fn get_price_orders() -> GetPriceOrders {
428 GetPriceOrders::new()
429}
430
431/// Cancel all price-triggered orders <br/>
432/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#cancel-all-price-triggered-orders)
433pub fn cancel_all_price_orders() -> CancelAllPriceOrders {
434 CancelAllPriceOrders::new()
435}
436
437/// Get a price-triggered order <br/>
438/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#get-a-price-triggered-order)
439pub fn get_price_order(order_id: &str) -> GetPriceOrder {
440 GetPriceOrder::new(order_id)
441}
442
443/// Cancel a price-triggered order <br/>
444/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#cancel-a-price-triggered-order)
445pub fn cancel_price_order(order_id: &str) -> CancelPriceOrder {
446 CancelPriceOrder::new(order_id)
447}
448
449/// Get server current time <br/>
450/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#get-server-current-time)
451pub fn get_server_time() -> GetServerTime {
452 GetServerTime::new()
453}
454
455/// Cancel all open orders <br/>
456/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#cancel-all-open-orders)
457pub fn cancel_all_open_orders() -> CancelAllOpenOrders {
458 CancelAllOpenOrders::new()
459}
460
461/// Countdown cancel orders <br/>
462/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#countdown-cancel-orders)
463pub fn countdown_cancel_all(timeout: i64) -> CountdownCancelAll {
464 CountdownCancelAll::new(timeout)
465}
466
467/// Amend multiple orders <br/>
468/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#batch-modification-of-orders)
469pub fn amend_batch_orders(orders: Vec<OrderAmendment>) -> AmendBatchOrders {
470 AmendBatchOrders::new(orders)
471}
472
473/// Create cross liquidation orders <br/>
474/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#close-position-when-cross-currency-is-disabled)
475pub fn create_cross_liquidate_orders(
476 orders: Vec<CrossLiquidateOrder>,
477) -> CreateCrossLiquidateOrders {
478 CreateCrossLiquidateOrders::new(orders)
479}
480
481/// Query insurance fund history <br/>
482/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#query-spot-insurance-fund-historical-data)
483pub fn get_insurance_history(
484 business: &str,
485 currency: &str,
486 from: i64,
487 to: i64,
488) -> GetInsuranceHistory {
489 GetInsuranceHistory::new(business, currency, from, to)
490}