Crate coinbase_pro_rs

source ·
Expand description

Coinbase pro client with sync/async + websocket-feed support

Structure

There are two main structures to work with: Private and Public, which provide interfaces to work with https://docs.pro.coinbase.com . The structures should be parametrised with: Sync or ASync adapter-type, which blocks future and returns result of its execution for Sync adapter or returns Future for ASync adapter.

WSFeed provides futures::Stream of websocket message for different channels.

Examples

Async

extern crate hyper;
extern crate tokio;
extern crate coinbase_pro_rs;

use hyper::rt::Future;
use coinbase_pro_rs::{Public, ASync, SANDBOX_URL};

fn main() {
    let client: Public<ASync> = Public::new_with_keep_alive(SANDBOX_URL, false);
    // if keep_alive is not disables - tokio::run will hold the connection without exiting the example
    let f = client.get_time()
        .map_err(|_| ())
        .and_then(|time| {
            println!("Coinbase.time: {}", time.iso);
            Ok(())
        });

    tokio::run(f);
}

Sync

extern crate coinbase_pro_rs;

use coinbase_pro_rs::{Public, Sync, SANDBOX_URL};

fn main() {
   let client: Public<Sync> = Public::new(SANDBOX_URL);
   let time = client.get_time().unwrap();
   println!("Coinbase.time: {}", time.iso);
}

Websocket

extern crate futures;
extern crate tokio;
extern crate coinbase_pro_rs;

use futures::{Future, Stream};
use coinbase_pro_rs::{WSFeed, WS_SANDBOX_URL};
use coinbase_pro_rs::structs::wsfeed::*;

fn main() {
    let stream = WSFeed::new(WS_SANDBOX_URL,
        &["BTC-USD"], &[ChannelType::Heartbeat]);

    let f = stream
        .take(10)
        .for_each(|msg| {
        match msg {
            Message::Heartbeat {sequence, last_trade_id, time, ..} => println!("{}: seq:{} id{}",
                                                                               time, sequence, last_trade_id),
            Message::Error {message} => println!("Error: {}", message),
            Message::InternalError(_) => panic!("internal_error"),
            other => println!("{:?}", other)
        }
        Ok(())
    });

    tokio::run(f.map_err(|_| panic!("stream fail")));
}

Re-exports

pub use adapters::ASync;
pub use adapters::Sync;
pub use error::CBError;
pub use error::WSError;
pub use private::Private;
pub use public::Public;
pub use wsfeed::WSFeed;

Modules

Contains structure which provides access to Private section of Coinbase api
Contains structure which provides access to Public section of Coinbase api
Contains structure which provides futures::Stream to websocket-feed of Coinbase api

Constants

https://api.pro.coinbase.com
https://api-public.sandbox.pro.coinbase.com
wss://ws-feed-public.sandbox.pro.coinbase.com
wss://ws-feed.pro.coinbase.com

Type Definitions