#crypto #ftx #rest #exchange #channel #connection #interface

ftx_async

Unofficial asynchronous Rust library for the FTX crypto exchange Websocket and REST API. The websocket interface fully abstracts the connection to FTX by publishing messages onto a MPSC channel and managing authentication, reconnections and keep-alives in the background.

4 releases

0.0.4 Nov 8, 2022
0.0.3 Nov 7, 2022
0.0.2 Nov 6, 2022
0.0.1 Nov 6, 2022

#1082 in Cryptography

Apache-2.0

49KB
1K SLoC

ftx-async

Unofficial Rust implementation of an asynchronous Websocket and REST client for the FTX crypto exchange

ci

06/11/2022 - *** WARNING *** This code base is alpha, though it is now available as a crate.

Example

A basic ticker listener using a FTX websocket. Make sure you include tokio and ftx-async in your Cargo.toml:
[dependencies]
tokio = {version = "*"}
ftx_async = {version = "*"}

Then, on your main.rs:

use ftx_async::ws::{UpdateMessage, WebsocketManager};
use tokio::signal;

#[tokio::main]
async fn main() {
    let api_key = ""; // Set a valid FTX API key!
    let api_secret = ""; // Set a valid FTX secret key!

    let ftx = WebsocketManager::new(api_key, api_secret, "BTC-PERP").await;

    let mut listener = ftx.get_order_channel();
    ftx.subscribe_channel_ticker(true).await;

    let mut terminated = false;
    while !terminated {
        tokio::select! {
            Ok(msg) = listener.recv() => {
                if let UpdateMessage::Ticker {market, bid, ask, bid_size : _, ask_size : _, last_trade : _}= msg {
                    print!("\r{market}: Bid: {:.0}     -     Ask: {:.0}", bid.unwrap(), ask.unwrap());
                }
            }

            _ = signal::ctrl_c() => {
                terminated = true;
            }
        }
    }
}

Running Integration Tests

The crate integration tests use an environment variables to look up credentials in order to establish a connection with the FTX exchange. A read-only key should be created on FTX and its details should be set into 'FTX_API_KEY' and 'FTX_SECRET' environment variables on the machine that will run the tests.

Dependencies

~10–23MB
~345K SLoC