diff --git a/bothan-api/Cargo.toml b/bothan-api/Cargo.toml index ee0a03a2..b5cc6d05 100644 --- a/bothan-api/Cargo.toml +++ b/bothan-api/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] bothan-core = { path = "../bothan-core" } bothan-binance = { path = "../bothan-binance" } +bothan-coingecko = { path = "../bothan-coingecko" } tracing = { version = "0.1.40", features = [] } tokio = { version = "1.36.0", features = ["full"] } tracing-subscriber = "0.3.17" diff --git a/bothan-api/src/main.rs b/bothan-api/src/main.rs index a2613fcb..c55860d8 100644 --- a/bothan-api/src/main.rs +++ b/bothan-api/src/main.rs @@ -1,9 +1,10 @@ use bothan_binance::Binance; +use bothan_coingecko::{CoinGeckoService, CoinGeckoServiceBuilder}; use bothan_core::service::Service; -use std::sync::Arc; +use std::collections::HashMap; +use std::{sync::Arc, time::Duration}; use tokio::sync::Mutex; use tonic::{transport::Server, Request, Response, Status}; -use tracing_subscriber::fmt::init; pub mod price { tonic::include_proto!("price"); // Include the generated code. @@ -12,13 +13,15 @@ pub mod price { use price::price_service_server::{PriceService, PriceServiceServer}; // Import the generated server module. pub struct PriceServiceImpl { - service: Arc>, + binance_service: Arc>, + coingecko_service: Arc>, } impl PriceServiceImpl { - fn new(binance_service: Binance) -> Self { + fn new(binance_service: Binance, coingecko_service: CoinGeckoService) -> Self { PriceServiceImpl { - service: Arc::new(Mutex::new(binance_service)), + binance_service: Arc::new(Mutex::new(binance_service)), + coingecko_service: Arc::new(Mutex::new(coingecko_service)), } } } @@ -35,10 +38,34 @@ impl PriceService for PriceServiceImpl { // For this example, let's just print it. println!("Received id: {}", id); - let mut binance_service = self.service.lock().await; - let data_list = binance_service.get_price_data(&[&id]).await; + let mut binance_map: HashMap<&str, &str> = HashMap::new(); + let mut coingecko_map: HashMap<&str, &str> = HashMap::new(); + + binance_map.insert("BTC", "btcusdt"); + coingecko_map.insert("BTC", "bitcoin"); + + let mut binance_service = self.binance_service.lock().await; + let binance_data_list = binance_service + .get_price_data(&[binance_map.get(id.as_str()).unwrap()]) + .await; + + let mut coingecko_service = self.coingecko_service.lock().await; + let coingecko_data_list = coingecko_service + .get_price_data(&[coingecko_map.get(id.as_str()).unwrap()]) + .await; + let mut price_data_list: Vec = Vec::new(); - for data in data_list { + for data in binance_data_list { + let price_data = data.unwrap(); + let price_data = price::PriceData { + id: price_data.id, + price: price_data.price, + timestamp: price_data.timestamp, + }; + price_data_list.push(price_data.clone()) + } + + for data in coingecko_data_list { let price_data = data.unwrap(); let price_data = price::PriceData { id: price_data.id, @@ -59,14 +86,22 @@ impl PriceService for PriceServiceImpl { #[tokio::main] async fn main() -> Result<(), Box> { let addr = "[::1]:50051".parse().unwrap(); + let service_result = CoinGeckoServiceBuilder::default() + .set_update_supported_assets_interval(Duration::from_secs(600)) + .build() + .await; + if let Ok(service) = Binance::default().await { - let price_data_impl = PriceServiceImpl::new(service); // Change to mutable binding - println!("Server running on {}", addr); + if let Ok(mut coingecko_service) = service_result { + let price_data_impl = PriceServiceImpl::new(service, coingecko_service); // Change to mutable binding - Server::builder() - .add_service(PriceServiceServer::new(price_data_impl)) - .serve(addr) - .await?; + println!("Server running on {}", addr); + + Server::builder() + .add_service(PriceServiceServer::new(price_data_impl)) + .serve(addr) + .await?; + } } Ok(()) }