Skip to content

Commit

Permalink
add default
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKSI committed Nov 21, 2023
1 parent fefb97c commit 701e1bf
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions price-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ thiserror = "1.0.50"
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0.192", features = ["serde_derive"] }
serde_json = "1.0.108"
tokio-tungstenite = { version = "0.20.1", features = ["native-tls"] }
futures-util = "0.3.29"
tokio-util = "0.7.10"
async-trait = "0.1.74"
5 changes: 2 additions & 3 deletions price-adapter/examples/coingecko-basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use price_adapter::mapper::BandStaticMapper;
use price_adapter::types::PriceAdapter;
use price_adapter::CoinGecko;

#[tokio::main]
async fn main() {
let band_static_mapper = BandStaticMapper::from_source("coingecko").unwrap();
let coingecko = CoinGecko::new(band_static_mapper, None);
let coingecko = CoinGecko::default(None).unwrap();
let queries = vec!["ETH", "BAND"];
let prices = coingecko.get_prices(&queries).await;
println!("prices: {:?}", prices);
Expand Down
19 changes: 15 additions & 4 deletions price-adapter/src/coingecko.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::mapper::types::Mapper;
use crate::error::Error;
use crate::types::PriceInfo;
use crate::mapper::types::Mapper;
use crate::mapper::BandStaticMapper;
use crate::types::{PriceAdapter, PriceInfo};
use price_adapter_raw::CoinGecko as CoinGeckoRaw;

// Generic struct `CoinGecko` parameterized over a `Mapper` type.
Expand All @@ -21,11 +22,21 @@ impl<M: Mapper> CoinGecko<M> {

Self { raw, mapper }
}
}

impl CoinGecko<BandStaticMapper> {
pub fn default(api_key: Option<String>) -> Result<Self, Error> {
let mapper = BandStaticMapper::from_source("coingecko")?;
Ok(Self::new(mapper, api_key))
}
}

#[async_trait::async_trait]
impl<M: Mapper> PriceAdapter for CoinGecko<M> {
// Asynchronous function to get prices for symbols.
pub async fn get_prices(&self, symbols: &[&str]) -> Vec<Result<PriceInfo, Error>> {
async fn get_prices(&self, symbols: &[&str]) -> Vec<Result<PriceInfo, Error>> {
// Retrieve the symbol-to-id mapping from the provided mapper.
let mapping = self.mapper.get_mapping();
let mapping = self.mapper.get_mapping().await;

// Match on the result of obtaining the mapping.
if let Ok(mapping) = mapping {
Expand Down
3 changes: 2 additions & 1 deletion price-adapter/src/mapper/band_static_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ impl BandStaticMapper {
}

// Implementing the Mapper trait for BandStaticMapper.
#[async_trait::async_trait]
impl Mapper for BandStaticMapper {
// Retrieve the mapping as a reference, wrapped in a Result.
fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error> {
async fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error> {
Ok(&self.mapping)
}
}
5 changes: 3 additions & 2 deletions price-adapter/src/mapper/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::Error;
use serde_json::Value;
use std::collections::HashMap;

pub trait Mapper {
fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error>;
#[async_trait::async_trait]
pub trait Mapper: Send + Sync + Sized + 'static {
async fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error>;
}
6 changes: 6 additions & 0 deletions price-adapter/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::Error;
use core::fmt;

#[derive(Clone, Debug)]
Expand All @@ -16,3 +17,8 @@ impl fmt::Display for PriceInfo {
)
}
}

#[async_trait::async_trait]
pub trait PriceAdapter: Send + Sync + 'static {
async fn get_prices(&self, symbols: &[&str]) -> Vec<Result<PriceInfo, Error>>;
}

0 comments on commit 701e1bf

Please sign in to comment.