Crates.io | krakenrs |
lib.rs | krakenrs |
version | 5.2.3 |
source | src |
created_at | 2021-09-20 22:50:47.307398 |
updated_at | 2022-01-24 20:40:50.344413 |
description | Rust bindings to the Kraken REST API and websockets API for spot trading |
homepage | |
repository | https://github.com/garbageslam/krakenrs |
max_upload_size | |
id | 454183 |
size | 173,503 |
Unofficial bindings to Kraken spot trading API in Rust.
This library provides a Rust client object implementing many of the calls from the Kraken REST API with an idiomatic Rust interface, including getting ticker info and making market and limit orders. Additionally it provides access to the Kraken WS API, both subscribing to feeds and submitting orders.
serde_json
reqwest
is used for httpstokio-tungstenite
is used for websocketsRustCrypto
crates used for the Kraken authentication schemerust_decimal
used to represent Decimal values from krakenlog
is used for loggingBoth public and private APIs are supported, but not all the calls and options are exposed, only the ones that were needed. If something you need is missing, patches are welcome! Just open a github issue or pull request.
To get the websockets API, the "ws"
feature must be enabled. It is on by default.
Otherwise you only get the REST API, which can do all the same things (and more), but has more strict rate limits.
Unlike some other bindings, these are not async APIs (although the websockets feeds are implicitly asynchronous).
We have chosen to create blocking APIs for the Kraken REST API version for a few reasons:
Additionally, the REST API has quite strict rate limits so making large numbers of requests in parallel isn't really possible.
Instead, it seems better to lean on the Websockets API, which is easy to use whether you want to use an async runtime or not, and not make lots of calls to the REST API.
REST API:
use krakenrs::{KrakenRestConfig, KrakenRestAPI};
use serde_json::to_string_pretty;
fn main() {
let kc_config = KrakenRestConfig::default();
let api = KrakenRestAPI::try_from(kc_config).expect("could not create kraken api");
println!(
to_string_pretty(
api.asset_pairs(vec!["XBT/USD", "SOL/BTC"]).expect("api call failed")
).unwrap()
);
println!(
to_string_pretty(
api.ticker(vec!["XBT/USD"]).expect("api call failed")
).unwrap()
);
}
Websockets API:
use krakenrs::{KrakenWsConfig, KrakenWsAPI};
use std::{
collections::BTreeMap,
time::Duration,
thread,
};
fn main() {
let pairs = vec!["XBT/USD".to_string(), "SOL/XBT".to_string()];
let ws_config = KrakenWsConfig {
subscribe_book: pairs.clone(),
book_depth: 10,
};
let api = KrakenWsAPI::new(ws_config).expect("could not connect to websockets api");
loop {
thread::sleep(Duration::from_millis(500));
let books = pairs
.iter()
.map(|pair| (pair.clone(), api.get_book(pair)))
.collect::<BTreeMap<_, _>>();
for (pair, book) in books {
println!("{}", pair);
println!("{:?}", book);
}
if api.stream_closed() { return; }
}
}
The KrakenWsAPI
object spawns a worker thread internally which drives the websockets connection.
If you don't want that you can import the KrakenWsClient
object instead and arrange the worker
thread as you like, while observing latest feed data in other threads using the handle to the ApiResult
object.
Use at your own risk. If you build trading software using this component and you suffer a loss because of a bug, I am not responsible.
The krak
binary target is a simple demo that can be used to exercise the rest API functionality.
It is a command-line target that can parse a credentials file, connect to kraken and make a single
API call, and print the response.
Usage:
cargo build
../target/debug/krak --help
for usage information.
For example, you can see the trading system's current status with
./krak system-status
, or see asset pairs and current prices with
./krak asset-pairs
, ./krak ticker AAVEUSD
{
"key": "ASDF",
"secret: "jklw=="
}
./krak path/to/creds get-open-orders
./krak path/to/creds --validate market-buy 0.02 AAVEUSD
The krak-feed
binary target can subscribe to, and print the results of, websockets feeds.
Usage
cargo build
../target/debug/krak-feed --help
for usage information.
For example, ./krak-feed book XBT/USD
will display the bitcoin/USD order book continuously.