7 releases

0.1.6 Oct 14, 2024
0.1.5 Apr 28, 2024
0.1.3 Feb 3, 2024
0.1.2 Jan 2, 2024

#63 in WebSocket

Download history 30/week @ 2024-07-29 12/week @ 2024-09-30 1/week @ 2024-10-07 194/week @ 2024-10-14 2/week @ 2024-11-04

196 downloads per month

MIT license

160KB
4K SLoC

Web Client for SFox.com API

CI

Description

sfox provides typed, asynchronous wrappers around the SFox.com API HTTP calls as well as Serde types for websocket message deserialization.

FIX is not implemented.

Installation

Complete the steps in this section to make the sfox client available in your Rust application.

Environment

Set SFOX_AUTH_TOKEN (created in the SFox web console) in your environment:

SFOX_AUTH_TOKEN=<AUTH-TOKEN>

Note: The server URLs SFOX_HTTP_SERVER_URL and SFOX_WS_SERVER_URL are also overridable for testing and development.

Dependency

Add the following line under [dependencies] in your project's Cargo.toml:

sfox = "0.1.5"

Usage

The sfox::http module performs asynchronous calls to the SFox API and returns typed responses.

HTTP

use sfox::http::{self, v1::order_book::OrderBook};
let sfox = http::new().unwrap();
let order_book: OrderBook = sfox.order_book("btcusd").await.unwrap();
println!("Order book currency: {:?}", order_book.bids[0]);

The terminal should then print a response like:

Order book currency: OpenOrder { price: 35000.012, volume: 1.0, exchange: "some-exchange" }

Websocket

Usage of the WebSocket client includes instantiating the client, authenticating with the server, and subscribing/unsubscribing to feeds.

use sfox::websocket::Client;
let sfox_ws = Client::new().await?;
let (mut write, mut read) = sfox_ws.stream.split();

// Start a task to read messages from the SFox stream
let _sfox_handle = tokio::spawn(async move { handle_incoming_message(&mut read).await });

// Subscribe to a feed on the websocket server
let _ticker_subscription = Client::subscribe(&mut write, Feed::Ticker, vec!["btcusd".to_string()]).await;

// Authenticate to access private feeds
let _authentication_attempt = Client::authenticate(&mut write).await;

// Subscribe to a private feed
let _balance_subscription = Client::subscribe(&mut write, Feed::Balances, vec![]).await;

where handle_incoming_message could be implemented like:

async fn handle_incoming_message(read: &mut SplitStream<WssStream> ) {
    while let Some(message) = read.next().await {
        println!("Received message: {:?}", message);
    }
}

Minimum Supported Rust Version (MSRV)

The current MSRV is 1.69. This version may change in future minor versions, so use a restricted version requirement if a specific Rust version is required.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thank you!

  1. Fork the Project
  2. Create your Feature Branch git checkout -b feature/AmazingFeature
  3. Commit your Changes git commit -m 'Add some AmazingFeature'
  4. Push to the Branch git push origin feature/AmazingFeature
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Dependencies

~9–23MB
~352K SLoC