1 unstable release

0.1.0 Nov 23, 2023

#7 in #coin-gecko


Used in price-adapter

MIT/Apache

20KB
452 lines

price-explorer

price-explorer is a price query adapter for crypto exchanges and price aggregators.

It provides a unified interface for querying price data from different exchanges and price aggregators. Currently, it supports the following exchanges and price aggregators:

  • Binance
  • CoinGecko

Usage

Coingecko

To use coingecko api, you need to create a CoingeckoPro instance and set the api key.

use price_adapter_raw::CoinGecko;

#[tokio::main]
async fn main() {
    let coingecko = CoinGecko::new_with_api_key("$API_KEY".into());
    let queries = vec!["ethereum"];
    let prices = coingecko.get_prices(&queries).await;
    println!("prices: {:?}", prices);
}

Binance Websocket

To use binance websocket api, you need to create a BinanceWebsocket instance and set the query symbols.

use price_adapter_raw::BinanceWebsocket;
use futures_util::StreamExt;

#[tokio::main]
async fn main() {
    let mut binance_ws = BinanceWebsocket::new("wss://stream.binance.com:9443", &["ethbtc", "btcusdt"]);
    binance_ws.connect().await.unwrap();
    while let Some(data) = binance_ws.next().await {
        match data {
            Ok(price) => {
                println!("price: {}", price);
                # break;
            }
            Err(e) => {
                eprintln!("Error: {}", e);
                break;
            }
        }
    }
}

Or use BinanceWebsocketService to query price data.

use price_adapter_raw::{BinanceWebsocket, BinanceWebsocketService};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let mut binance_ws = BinanceWebsocket::new("wss://stream.binance.com:9443", &["ethbtc", "btcusdt"]);

    let mut service = BinanceWebsocketService::new(binance_ws);
    service.start().await.unwrap();
    tokio::time::sleep(Duration::from_secs(1)).await;

    let price = service.get_prices(&["btcusdt"]).await;
    println!("price: {:?}", price);
}

Dependencies

~10–24MB
~368K SLoC