Roadmap
USD-M Futures WebSocket✅- USD-M Futures Rest Api
- SpotMarket WebSocket
- SpotMarket Rest Api
- Coin-M Futures WebSocket
- Coin-M Futures Rest Api
binance_connect::futures_usd
Features
- Connects to the Binance USD-M Futures WebSocket to receive real-time market and account updates via an event consumer.
- Events that are consumed contain a (sanitized) struct representation of the returned Binance data.
- ListenKey creation and keep-alive is managed by the library.
- WebSocket connection drops are caught and managed by the library. This because Binance forcefully drops connections after the 24h mark. This can be configured in the
FuturesWebSocketConfigusing thereconnect(bool)setter (default setting is true).
Getting Started
Without config
Create an instance of the FuturesUsdStream and add the desired streams for real-time updates. In this example, we're subscribing to the book depth for the BTC/USDT trading pair and all book tickers.
let fus: FuturesUsdStream = default
.with_book_depth
.with_book_tickers
.start;
To manually close the connection thread you can call.
fus.close;
With config
Set up your Binance API authentication using the ApiAuth struct. This is only required when requesting User Data Streams.
let api_auth: ApiAuth = new;
A FuturesWebsocketConfig can be created to pass configuration options to the FuturesUsdStream. For example; You can specify whether to use the Binance testnet or the live environment. When an authenticated connection is required the FuturesWebsocketConfig is mandatory.
let config: FuturesWebSocketConfig = with_config
.with_api_auth
.use_testnet; // Use the testnet environment (remove for live trading)
Create an instance of the FuturesUsdStream using the with_config() builder and add the desired streams for real-time updates. In this example, we're subscribing to the book depth for the BTC/USDT trading pair, all book tickers and because the with_api_auth method is called on the FuturesWebsocketConfig all User Data Streams.
let fus: FuturesUsdStream = with_config
.with_book_depth
.with_book_tickers
.start;
Consuming Events
Start consuming events from the FuturesUsdStream using the consume() method and handle them as needed.
for event in fus.consume
The structs that are returned when consuming Events follow a predictable property name convention as opposed to the single letter convention used by Binance. Property values can be an Enum. See src/futures_usd/enums/binance.rs
BookTicker response struct as example reference. See src/futures_usd/response.rs
Events
/* MARKET_DATA */
BookTickerEvent,
BookTickersEvent,
AggTradeEvent,
MarkPriceUpdateEvent,
MarkPriceUpdatesEvent,
KlineEvent,
ContinuousKlineEvent,
MiniTickerEvent,
MiniTickersEvent,
TickerEvent,
TickersEvent,
ForceOrderEvent,
BookDepthEvent,
CompositeIndexEvent,
ContractInfoEvent,
AssetIndexUpdateEvent,
AssetIndexUpdatesEvent,
/* USER_DATA */
OrderTradeUpdateEvent,
AccountUpdateEvent,
MarginCallEvent,
AccountConfigUpdateEvent,
StrategyUpdateEvent,
GridUpdateEvent,
ConditionalOrderTriggerRejectEvent,
/* SYSTEM */
SubscribeResponseEvent,
Errors
All errors are propagated to a BinanceConnectError. See src/error.rs
License
This project is licensed under the MIT License. See the LICENSE file for details.