FuturesWebSocketConfig
using the reconnect(bool)
setter (default setting is true).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.
rust
let fus: FuturesUsdStream = FuturesUsdStream::default()
.with_book_depth("btcusdt", BookDepthUpdateSpeed::Millis500)
.with_book_tickers()
.start();
Set up your Binance API authentication using the ApiAuth struct. This is only required when requesting User Data Streams.
rust
let api_auth: ApiAuth = ApiAuth::new(
"YOUR_API_KEY".to_string(),
"YOUR_API_SECRET".to_string(),
);
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.
rust
let config: FuturesWebSocketConfig = FuturesWebSocketConfig::with_config(config)
.with_api_auth(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.
rust
let fus: FuturesUsdStream = FuturesUsdStream::with_config(config)
.with_book_depth("btcusdt", BookDepthUpdateSpeed::Millis500)
.with_book_tickers()
.start();
Start consuming events from the FuturesUsdStream
using the consume()
method and handle them as needed.
rust
for event in fus.consume() {
match event {
BookDepthEvent(book_depth) => {
println!("{:?}", book_depth)
}
BookTickersEvent(book_tickers) => {
for book_ticker in book_tickers.data {
println!("{:?}", book_ticker)
}
}
// Only available with `with_api_auth(api_auth)` called on `FuturesWebsocketConfig`
AccountUpdateEvent(account_update) => {
println!("{:?}", account_update)
}
_ => {}
}
}
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/futuresusd/enums/binance.rs_
BookTicker response struct as example reference. See src/futuresusd/response.rs_
rust
pub struct BookTicker {
pub event_type: EventType,
pub event_time: u64,
pub symbol: String,
pub update_id: u64,
pub bid_price: f64,
pub bid_quantity: f64,
pub ask_price: f64,
pub ask_quantity: f64,
pub transaction_time: u64,
}
rust
/* MARKET_DATA */
BookTickerEvent(BookTicker),
BookTickersEvent(BookTickers),
AggTradeEvent(AggTrade),
MarkPriceUpdateEvent(MarkPriceUpdate),
MarkPriceUpdatesEvent(MarkPriceUpdates),
KlineEvent(Kline),
ContinuousKlineEvent(ContinuousKline),
MiniTickerEvent(MiniTicker),
MiniTickersEvent(MiniTickers),
TickerEvent(Ticker),
TickersEvent(Tickers),
ForceOrderEvent(ForceOrder),
BookDepthEvent(BookDepth),
CompositeIndexEvent(CompositeIndex),
ContractInfoEvent(ContractInfo),
AssetIndexUpdateEvent(AssetIndexUpdate),
AssetIndexUpdatesEvent(AssetIndexUpdates),
/* USER_DATA */
OrderTradeUpdateEvent(OrderTradeUpdate),
AccountUpdateEvent(AccountUpdate),
MarginCallEvent(MarginCall),
AccountConfigUpdateEvent(AccountConfigUpdate),
StrategyUpdateEvent(StrategyUpdate),
GridUpdateEvent(GridUpdate),
ConditionalOrderTriggerRejectEvent(ConditionalOrderTriggerReject),
/* SYSTEM */
SubscribeResponseEvent,
All errors are propagated to a BinanceConnectError. See src/error.rs
This project is licensed under the MIT License. See the LICENSE file for details.