Skip to content

Commit

Permalink
implement coingecko source
Browse files Browse the repository at this point in the history
  • Loading branch information
colmazia committed Feb 27, 2024
1 parent 42a9c32 commit fa59475
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions bothan-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
63 changes: 49 additions & 14 deletions bothan-api/src/main.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,13 +13,15 @@ pub mod price {
use price::price_service_server::{PriceService, PriceServiceServer}; // Import the generated server module.

pub struct PriceServiceImpl {
service: Arc<Mutex<Binance>>,
binance_service: Arc<Mutex<Binance>>,
coingecko_service: Arc<Mutex<CoinGeckoService>>,
}

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)),
}
}
}
Expand All @@ -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<price::PriceData> = 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,
Expand All @@ -59,14 +86,22 @@ impl PriceService for PriceServiceImpl {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

0 comments on commit fa59475

Please sign in to comment.