日本語ã¯ä¸‹ã«ã‚ã‚Šã¾ã™
This is a Rust library for communicating with cryptocurrency exchange APIs.
This library: - is asynchronous - supports WebSocket - supports deserializing responses into user-defined types
The following Exchanges are currently supported.
| Exchange | Official API document | Example usages of this library | |------------------------------------|-----------------------------------------------------------------|---------------------------------------------------------------------------------------| | Binance | API document | Examples | | bitFlyer | API document | Examples | | Bybit | API document | Examples | | Coincheck | API document | Examples |
More than 20 examples can be found in the examples directory.
Cargo.toml:
[dependencies]
crypto-botters = { version = "0.5", features = ["binance", "bitflyer", "bybit", "coincheck"] }
Enable the features for the exchanges that you use.
```rust use std::env; use crypto_botters::{Client, binance::{BinanceAuth, BinanceHttpUrl, BinanceOption}};
async fn main() { let key = env::var("BINANCEAPIKEY").expect("no API key found"); let secret = env::var("BINANCEAPISECRET").expect("no API secret found"); let mut client = Client::new(); client.updatedefaultoption(BinanceOption::Key(key)); client.updatedefaultoption(BinanceOption::Secret(secret));
let dusts: serdejson::Value = client.postno_body( "https://api.binance.com/sapi/v1/asset/dust-btc", [BinanceOption::HttpAuth(BinanceAuth::Sign)], ).await.expect("failed get dusts"); println!("My dust assets(BTC):\n{:?}", dusts["totalTransferBtc"]); } ``` The above code queries assets that are convertable into BNB using the Binance API.
When making a request, you pass some options to, for example, the post_no_body
function.
In the example, [BinanceOption::HttpAuth(BinanceAuth::Sign)]
is the options.
You would usually pass an array of options.
The options are for: - setting API key/secret - enabling authentication
etc.
The type of options passed is what determines the exchange used. In the above example, the library knows
the request is for Binance because the type of the option passed is BinanceOption
. When using Bybit,
you would pass an array of BybitOption
s.
Some options are the same across requests. For example, you will probably use the same API key for each request.
For those options, you can set default options for Client
. Default options are applied to all requests.
In the above example, client.update_default_option(BinanceOption::Key(key));
sets the option for Binance API key as a default option.
Because of this, passing an option for API key in post_no_body()
is not required.
Responses are automatically deserialized into the specified type. In the above example, the response is of the type serde_json::Value
because we specified the type of dusts
. Any type that implements DeserializeOwned
is supported.
```rust use std::time::Duration; use log::LevelFilter; use crypto_botters::{binance::{BinanceOption, BinanceWebSocketUrl}, Client};
async fn main() { let client = Client::new();
let connection = client.websocket(
"/ws/btcusdt@trade",
|message| println!("{}", message),
[BinanceOption::WebSocketUrl(BinanceWebSocketUrl::Spot443)],
).await.expect("failed to connect websocket");
// receive messages
tokio::time::sleep(Duration::from_secs(10)).await;
} ``` The above code opens a WebSocket connection and watches BTCUSDT trades happening on Binance.
The Client::websocket()
method returns a WebSocketConnection
. Using this, you can send messages,
request a reconnection, or close the connection.
ã“ã‚Œã¯ä»®æƒ³é€šè²¨å–引所ã®APIã¨é€šä¿¡ã™ã‚‹ãŸã‚ã®Rustライブラリã§ã™ã€‚
特徴: - éžåŒæœŸ - WebSocketã«å¯¾å¿œ - レスãƒãƒ³ã‚¹ã‚’ユーザーã®å®šç¾©ã—ãŸåž‹ã«å¤‰æ›
以下ã®å–引所ã«å¯¾å¿œã—ã¦ã„ã¾ã™ã€‚
| å–引所å | å…¬å¼APIドã‚ュメント | 本ライブラリ使用例 | |------------------------------------|--------------------------------------------------------------|----------------------------------------------------------------------------------| | Binance | APIドã‚ュメント | 使用例 | | bitFlyer | APIドã‚ュメント | 使用例 | | Bybit | APIドã‚ュメント | 使用例 | | Coincheck | APIドã‚ュメント | 使用例 |
examples ディレクトリ ã«ã‚µãƒ³ãƒ—ルãŒ20以上ã‚ã‚Šã¾ã™ã€‚
Cargo.toml:
[dependencies]
crypto-botters = { version = "0.5", features = ["binance", "bitflyer", "bybit", "coincheck"] }
使ã„ãŸã„å–引所ã®featureを有効化ã—ã¦ãã ã•ã„。
```rust use std::env; use crypto_botters::{Client, binance::{BinanceAuth, BinanceHttpUrl, BinanceOption}};
async fn main() { let key = env::var("BINANCEAPIKEY").expect("no API key found"); let secret = env::var("BINANCEAPISECRET").expect("no API secret found"); let mut client = Client::new(); client.updatedefaultoption(BinanceOption::Key(key)); client.updatedefaultoption(BinanceOption::Secret(secret));
let dusts: serdejson::Value = client.postno_body( "https://api.binance.com/sapi/v1/asset/dust-btc", [BinanceOption::HttpAuth(BinanceAuth::Sign)], ).await.expect("failed get dusts"); println!("My dust assets(BTC):\n{:?}", dusts["totalTransferBtc"]); } ``` ã“ã®ä¾‹ã§ã¯ã€Binanceã§BNBã«å¤‰æ›ã§ãる資産をå–å¾—ã—ã¦ã„ã¾ã™ã€‚
リクエストをé€ã‚‹ã¨ãã«ã¯ã€ã‚ªãƒ—ションをè¨å®šã§ãã¾ã™ã€‚ã“ã®ä¾‹ã§ã¯ã€[BinanceOption::HttpAuth(BinanceAuth::Sign)]
ãŒã‚ªãƒ—ションã§ã™ã€‚
オプションã®ã‚¤ãƒ†ãƒ¬ãƒ¼ã‚¿ãªã‚‰ä½•ã§ã‚‚ã„ã„ã§ã™ã€‚ã“ã®ä¾‹ã§ã¯é…列を使ã£ã¦ã„ã¾ã™ã€‚
オプション㯠- APIã‚ーやシークレットを指定ã™ã‚‹ - èªè¨¼ã‚’有効ã«ã™ã‚‹
ãªã©ã®ãŸã‚ã«è¨å®šã—ã¾ã™ã€‚
オプションã®åž‹ãŒã©ã®å–引所を使ã†ã‹ã‚’定ã‚ã¾ã™ã€‚ã“ã®ä¾‹ã§ã¯BinanceOption
型を渡ã—ã¦ã„ã‚‹ãŸã‚ã€Binanceã®èªè¨¼ã‚¢ãƒ«ã‚´ãƒªã‚ºãƒ ãŒç”¨ã„られã¾ã™ã€‚BybitOption
型を渡ã›ã°Bybitã¸ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆã¨ã—ã¦æ‰±ã‚ã‚Œã¾ã™ã€‚
複数ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆé–“ã§å¤‰ã‚らãªã„オプションもã‚ã‚Šã¾ã™ã€‚例ãˆã°ã€ã™ã¹ã¦ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆã§åŒã˜APIã‚ーを使ã†ã“ã¨ãŒå¤šã„ã¨æ€ã„ã¾ã™ã€‚
ãã®ã‚ˆã†ãªã‚ªãƒ—ションã¯ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã‚ªãƒ—ションã¨ã—ã¦Client
ã«è¨å®šã§ãã¾ã™ã€‚デフォルトオプションã¯ã€ãã®Client
ã‚’
使ã£ã¦é€ã‚‰ã‚Œã‚‹ã™ã¹ã¦ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆã«é©ç”¨ã•ã‚Œã¾ã™ã€‚ãã‚Œãžã‚Œã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆã§æ¸¡ã™ã‚ªãƒ—ションã§ä¸Šæ›¸ãã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚
ã“ã®ä¾‹ã§ã¯ã€client.update_default_option(BinanceOption::Key(key));
ã§APIã‚ーã®ã‚ªãƒ—ションをデフォルトオプションã¨ã—ã¦è¨å®š
ã—ã¦ã„ã¾ã™ã€‚ã“ã®ãŸã‚ã€post_no_body()
ã«APIã‚ーã®ã‚ªãƒ—ションを指定ã™ã‚‹å¿…è¦ãŒãªããªã£ã¦ã„ã¾ã™ã€‚
レスãƒãƒ³ã‚¹ã¯æŒ‡å®šã—ãŸåž‹ã«è‡ªå‹•çš„ã«å¤‰æ›ã•ã‚Œã¾ã™ã€‚ã“ã®ä¾‹ã§ã¯ã€dusts
ã®åž‹ã‚’serde_json::Value
ã¨æŒ‡å®šã—ã¦ã„ã‚‹ãŸã‚ã€
レスãƒãƒ³ã‚¹ãŒè‡ªå‹•ã§serde_json::Value
åž‹ã«å¤‰æ›ã•ã‚Œã¦ã„ã¾ã™ã€‚DeserializeOwned
を実装ã—ã¦ã„ã‚‹åž‹ãªã‚‰ã©ã‚“ãªåž‹ã§ã‚‚指定ã§ãã¾ã™ã€‚
```rust use std::time::Duration; use log::LevelFilter; use crypto_botters::{binance::{BinanceOption, BinanceWebSocketUrl}, Client};
async fn main() { let client = Client::new();
let connection = client.websocket(
"/ws/btcusdt@trade",
|message| println!("{}", message),
[BinanceOption::WebSocketUrl(BinanceWebSocketUrl::Spot443)],
).await.expect("failed to connect websocket");
// receive messages
tokio::time::sleep(Duration::from_secs(10)).await;
} ``` ã“ã®ä¾‹ã§ã¯ã€Binanceã®BTCUSDTã®å–引をリアルタイムã§å—ä¿¡ã—ã¦ã„ã¾ã™ã€‚
Client::websocket()
メソッドã¯WebSocketConnection
åž‹ã‚’è¿”ã—ã¾ã™ã€‚ã“ã‚Œã«å¯¾ã—ã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’é€ä¿¡ã™ã‚‹ã€å†æŽ¥ç¶šã‚’è¦æ±‚ã™ã‚‹ã€æŽ¥ç¶šã‚’切æ–ã™ã‚‹ãªã©ã®å‡¦ç†ãŒè¡Œãªãˆã¾ã™ã€‚
開発者:@negi_grass