krakenrs
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.
- Requests and responses are strongly-typed, conversion done using
serde_json reqwestis used for httpstokio-tungsteniteis used for websocketsRustCryptocrates used for the Kraken authentication schemerust_decimalused to represent Decimal values from krakenlogis used for logging- Robust error handling
Both 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.
Features
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.
Threading
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:
- simplicity
- ease of debugging
- when trying to make multiple private REST API calls in parallel, we often see invalid nonce errors. This is because the nonces are based on timestamps, but when multiple requests are created and sent in parallel, this is inherently racy and sometimes the request with the higher nonce will be processed by kraken first, invalidating the others.
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.
If you are using an async runtime like tokio, you can avoid blocking the executor by wrapping sequences of calls to
the REST API with task::block_in_place or similar.
Examples
REST API:
use ;
use to_string_pretty;
Websockets API:
use ;
use ;
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.
Disclaimer
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.
Rest API Demo
The kraken 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:
- Run
cargo run --example kraken -- --helpfor usage information. For example, you can see the trading system's current status with./kraken system-status, or see asset pairs and current prices with./kraken asset-pairs,./kraken ticker AAVEUSD - If you want to use private APIs, go to your Kraken account and create an API key.
Then create a json file with your credentials, with the following schema:
{ "key": "ASDF", "secret: "jklw==" } - Private APIs are invoked for example like:
cargo run --example kraken path/to/creds get-open-orderscargo run --example kraken path/to/creds --validate market-buy 0.02 AAVEUSD
Websockets Feed Demo
The kraken-feed example target can subscribe to, and print the results of, websockets feeds.
Usage
- Run
cargo run --example kraken-feed --helpfor usage information. For example,./kraken-feed book XBT/USDwill display the bitcoin/USD order book continuously.