Skip to content

Commit

Permalink
Merge pull request #8 from negi-grass/feature-bybit
Browse files Browse the repository at this point in the history
Support Bybit HTTP API
  • Loading branch information
negi-grass authored Jan 5, 2023
2 parents ab8caba + 851e377 commit 2fe9344
Show file tree
Hide file tree
Showing 19 changed files with 904 additions and 14 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ autoexamples = false
all-features = true

[features]
full = ["binance", "bitflyer"]
full = ["binance", "bitflyer", "bybit"]
binance = ["dep:crypto-botters-binance"]
bitflyer = ["dep:crypto-botters-bitflyer"]
bybit = ["dep:crypto-botters-bybit"]

[dependencies]
generic-api-client = { version = "0.1", path = "generic-api-client" }
crypto-botters-api = { version = "0.1", path = "crypto-botters-api" }
crypto-botters-binance = { version = "0.2", path = "crypto-botters-binance", optional = true }
crypto-botters-bitflyer = { version = "0.2", path = "crypto-botters-bitflyer", optional = true }
crypto-botters-bybit = { version = "0.1", path = "crypto-botters-bybit", optional = true }

[dev-dependencies]
crypto-botters = { path = ".", features = ["full"] }
Expand Down Expand Up @@ -73,3 +75,51 @@ path = "examples/bitflyer/bitflyer_websocket_private.rs"
[[example]]
name = "bitflyer_websocket_public"
path = "examples/bitflyer/bitflyer_websocket_public.rs"

[[example]]
name = "bybit_unified_margin"
path = "examples/bybit/bybit_unified_margin.rs"

[[example]]
name = "bybit_contract"
path = "examples/bybit/bybit_contract.rs"

[[example]]
name = "bybit_inverse_perpetual"
path = "examples/bybit/bybit_inverse_perpetual.rs"

[[example]]
name = "bybit_usdt_perpetual"
path = "examples/bybit/bybit_usdt_perpetual.rs"

[[example]]
name = "bybit_inverse_futures"
path = "examples/bybit/bybit_inverse_futures.rs"

[[example]]
name = "bybit_spot_v3"
path = "examples/bybit/bybit_spot_v3.rs"

[[example]]
name = "bybit_spot_v1"
path = "examples/bybit/bybit_spot_v1.rs"

[[example]]
name = "bybit_account_asset_v3"
path = "examples/bybit/bybit_account_asset_v3.rs"

[[example]]
name = "bybit_account_asset_v1"
path = "examples/bybit/bybit_account_asset_v1.rs"

[[example]]
name = "bybit_copy_trading"
path = "examples/bybit/bybit_copy_trading.rs"

[[example]]
name = "bybit_usdc_option"
path = "examples/bybit/bybit_usdc_option.rs"

[[example]]
name = "bybit_usdc_perpetual"
path = "examples/bybit/bybit_usdc_perpetual.rs"
14 changes: 7 additions & 7 deletions crypto-botters-binance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ pub enum BinanceHandlerError {
ParseError,
}

#[derive(Deserialize, Debug)]
pub struct BinanceError {
pub code: i32,
pub msg: String,
}

/// A `struct` that implements [RequestHandler]
pub struct BinanceRequestHandler<'a, R: DeserializeOwned> {
options: BinanceOptions,
Expand All @@ -143,12 +149,6 @@ pub struct BinanceWebSocketHandler<H: FnMut(serde_json::Value) + Send + 'static>
options: BinanceOptions,
}

#[derive(Deserialize, Debug)]
pub struct BinanceError {
pub code: i32,
pub msg: String,
}

// https://binance-docs.github.io/apidocs/spot/en/#general-api-information
impl<'a, B, R> RequestHandler<B> for BinanceRequestHandler<'a, R>
where
Expand All @@ -170,7 +170,7 @@ where
fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {
if let Some(body) = request_body {
let encoded = serde_urlencoded::to_string(body).or(
Err("could not parse body as application/x-www-form-urlencoded"),
Err("could not serialize body as application/x-www-form-urlencoded"),
)?;
builder = builder
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
Expand Down
8 changes: 4 additions & 4 deletions crypto-botters-bitflyer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum BitFlyerOption {
/// Whether [BitFlyerRequestHandler] should perform authentication
HttpAuth(bool),
/// [RequestConfig] used when sending requests.
/// `url_prefix` will be overridden by [HttpUrl](Self::HttpUrl) unless `HttpUrl` is [BinanceHttpUrl::None].
/// `url_prefix` will be overridden by [HttpUrl](Self::HttpUrl) unless `HttpUrl` is [BitFlyerHttpUrl::None].
RequestConfig(RequestConfig),
/// Base url for WebSocket connections
WebSocketUrl(BitFlyerWebSocketUrl),
Expand All @@ -38,8 +38,8 @@ pub enum BitFlyerOption {
/// The channels to be subscribed by [BitFlyerWebSocketHandler].
WebSocketChannels(Vec<String>),
/// [WebSocketConfig] used for creating [WebSocketConnection]s
/// `url_prefix` will be overridden by [WebSocketUrl](Self::WebSocketUrl) unless `WebSocketUrl` is [BinanceWebSocketUrl::None].
/// By default, `refresh_after` is set to 12 hours and `ignore_duplicate_during_reconnection` is set to `true`.
/// `url_prefix` will be overridden by [WebSocketUrl](Self::WebSocketUrl) unless `WebSocketUrl` is [BitFlyerWebSocketUrl::None].
/// By default, ignore_duplicate_during_reconnection` is set to `true`.
WebSocketConfig(WebSocketConfig),
}

Expand Down Expand Up @@ -129,7 +129,7 @@ where

fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {
if let Some(body) = request_body {
let json = serde_json::to_vec(body).or(Err("could not serialize body as JSON"))?;
let json = serde_json::to_vec(body).or(Err("could not serialize body as application/json"))?;
builder = builder
.header(header::CONTENT_TYPE, "application/json")
.body(json);
Expand Down
25 changes: 25 additions & 0 deletions crypto-botters-bybit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "crypto-botters-bybit"
version = "0.1.0"
authors = ["negi_grass"]
edition = "2021"
description = "A crate for interacting with the bybit API."
license = "MIT"
readme = "README.md"
homepage = "https://github.com/negi-grass/crypto-botters"
documentation = "https://docs.rs/crypto-botters-bybit"
repository = "https://github.com/negi-grass/crypto-botters"
keywords = ["cryptocurrency", "Bybit", "trading", "client", "websocket"]
categories = ["api-bindings", "asynchronous", "cryptography::cryptocurrencies", "authentication"]

[dependencies]
generic-api-client = { version = "0.1", path = "../generic-api-client" }
crypto-botters-api = { version = "0.1", path = "../crypto-botters-api" }
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
serde_urlencoded = "0.7.1"
hmac = "0.12.1"
sha2 = "0.10.6"
hex = "0.4.3"
rand = "0.8.5"
log = "0.4.17"
Loading

0 comments on commit 2fe9344

Please sign in to comment.