Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[!feat] Refactor Htx #69

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion Cargo.lock

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

12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ exclude = [".github/"]


[workspace]
members = ["bothan-core", "bothan-binance", "bothan-coingecko", "bothan-kraken", "bothan-api/server", "bothan-api/server-cli"]
members = [
"bothan-api/server",
"bothan-api/server-cli",
"bothan-binance",
"bothan-coingecko",
"bothan-core",
"bothan-htx",
"bothan-kraken",
]
exclude = ["bothan-api", "bothan-api-proxy"]
resolver = "2"

Expand Down Expand Up @@ -44,6 +52,6 @@ bothan-binance = { path = "bothan-binance" }
bothan-coingecko = { path = "bothan-coingecko" }
#bothan-coinmarketcap = { path = "bothan-coinmarketcap" }
#bothan-cryptocompare = { path = "bothan-cryptocompare" }
#bothan-htx = { path = "bothan-htx" }
bothan-htx = { path = "bothan-htx" }
bothan-kraken = { path = "bothan-kraken" }
#bothan-okx = { path = "bothan-okx" }
12 changes: 7 additions & 5 deletions bothan-htx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ license.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
flate2 = "1.0.25"
warittornc marked this conversation as resolved.
Show resolved Hide resolved
futures-util = "0.3.29"

async-trait = { workspace = true }
bothan-core = { workspace = true }
futures = { workspace = true }
humantime-serde = { workspace = true }
reqwest = { workspace = true }
chrono = { workspace = true }
rust_decimal = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tokio-tungstenite = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
url = { workspace = true }

[dev-dependencies]
mockito = { workspace = true }
ws-mock = { git = "https://github.com/bandprotocol/ws-mock.git", branch = "master" }
41 changes: 29 additions & 12 deletions bothan-htx/examples/htx_basic.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
use std::time::Duration;

use tokio::time::sleep;
use tracing_subscriber::fmt::init;

use bothan_core::service::Service;
use bothan_htx::HtxServiceBuilder;
use bothan_core::registry::Registry;
use bothan_core::store::SharedStore;
use bothan_core::worker::{AssetWorker, AssetWorkerBuilder};
use bothan_htx::{HtxWorkerBuilder, HtxWorkerBuilderOpts};

#[tokio::main]
async fn main() {
init();
let service_result = HtxServiceBuilder::default().build().await;

if let Ok(mut service) = service_result {
loop {
let data = service
.get_price_data(&["btcusdt", "ethusdt", "bandusdt"])
.await;
println!("{:?}", data);
tokio::time::sleep(Duration::from_secs(5)).await;
}
let path = std::env::current_dir().unwrap();
let registry = Registry::default().validate().unwrap();
let store = SharedStore::new(registry, path.as_path()).await.unwrap();

let worker_store = store.create_worker_store(HtxWorkerBuilder::worker_name());
let opts = HtxWorkerBuilderOpts::default();

let worker = HtxWorkerBuilder::new(worker_store, opts)
.build()
.await
.unwrap();

worker
.set_query_ids(vec!["btcusdt".to_string(), "ethusdt".to_string()])
.await
.unwrap();

sleep(Duration::from_secs(2)).await;

loop {
let btc_data = worker.get_asset("btcusdt").await;
let eth_data = worker.get_asset("ethusdt").await;
println!("{:?} {:?}", btc_data, eth_data);
sleep(Duration::from_secs(5)).await;
}
}
7 changes: 3 additions & 4 deletions bothan-htx/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub use builder::HtxRestAPIBuilder;
pub use rest::HtxRestAPI;
pub use error::{ConnectionError, MessageError, SendError};
pub use websocket::{HtxWebSocketConnection, HtxWebSocketConnector};

pub mod builder;
pub mod error;
pub mod rest;
pub mod types;
pub mod websocket;
52 changes: 0 additions & 52 deletions bothan-htx/src/api/builder.rs

This file was deleted.

47 changes: 19 additions & 28 deletions bothan-htx/src/api/error.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
/// Errors that can occur while building the `HtxRestAPI`.
pub enum BuilderError {
/// The URL provided is invalid.
#[error("invalid url")]
InvalidURL(#[from] url::ParseError),
use tokio_tungstenite::tungstenite;

/// An error occurred with the `reqwest` client.
#[error("reqwest error: {0}")]
Reqwest(String),
}
#[derive(Debug, thiserror::Error)]
pub enum ConnectionError {
#[error("failed to connect to endpoint {0}")]
ConnectionFailure(#[from] tungstenite::Error),

impl From<reqwest::Error> for BuilderError {
fn from(e: reqwest::Error) -> Self {
BuilderError::Reqwest(e.to_string())
}
#[error("received unsuccessful HTTP response: {0}")]
UnsuccessfulHttpResponse(tungstenite::http::StatusCode),
}

#[derive(Clone, Debug, PartialEq, thiserror::Error)]
/// Errors that can occur while interacting with the REST API.
pub enum RestAPIError {
/// An HTTP error occurred.
#[error("http error: {0}")]
Http(reqwest::StatusCode),
#[derive(Debug, thiserror::Error)]
pub enum MessageError {
#[error("failed to parse message")]
Parse(#[from] serde_json::Error),

/// An error occurred with the `reqwest` client.
#[error("reqwest error: {0}")]
Reqwest(String),
}
#[error("channel closed")]
ChannelClosed,

impl From<reqwest::Error> for RestAPIError {
fn from(e: reqwest::Error) -> Self {
RestAPIError::Reqwest(e.to_string())
}
#[error("unsupported message")]
UnsupportedMessage,
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct SendError(#[from] tungstenite::Error);
Loading
Loading