#price #currency #key #api-key #models #cryptocurreny #currency-price

currency-prices

A small library to get the cryptocurreny prices

3 releases

0.1.2 Jan 24, 2024
0.1.1 Jan 22, 2024
0.1.0 Jan 19, 2024

#52 in #price

21 downloads per month

MIT/Apache

11KB
124 lines

Currency-Prices

currency-prices is a lightweight Rust library designed to fetch cryptocurrency prices from CoinMarketCap. The primary function of this library is to provide a convenient way to obtain real-time currency conversion rates between specified cryptocurrencies.

Usage

Installation

Add the following line to your Cargo.toml file:

[dependencies]
currency-prices = "0.1.2"

Getting Started

To use this library, you'll need to configure it with your CoinMarketCap API key and endpoint. We offer two configurations: one for development and testing, and another for production.

Development Configuration To obtain the development configuration with default settings (using a sandbox API key for testing), call:

use currency_prices::CoinMarketCapConfig;
let dev_config = CoinMarketCapConfig::get_sandbox_config();
let currency_prices_api = CurrencyPrices::new(config);

Production Configuration For production, you need to provide your production API key when obtaining the configuration:

use currency_prices::CoinMarketCapConfig;

let api_key = "your_production_api_key";
let prod_config = CoinMarketCapConfig::get_production_config(api_key);
let currency_prices_api = CurrencyPrices::new(config);

Fetching Prices The library exposes a Struct wich contains the function for fetching cryptocurrency prices. You need to pass the from_currency, to_currency.

use currency_prices::{
    models::{CoinMarketCapConfig, Currency},
    CurrencyPrices,
};

let from_currency = Currency {
    name: String::from("HDN"),
};
let to_currency = Currency {
    name: String::from("USD"),
};

let price = currency_prices_api.get_price(from_currency, to_currency).await?;

the get_price is an async funcion which returns

Result<CurrencyPrice, CurrencyPricesError>
// Where

pub struct CurrencyPrice {
    pub from_currency: Currency,
    pub to_currency: Currency,
    pub price: Decimal,
}

pub enum CurrencyPricesError {
    Request(reqwest::Error),
    Custom(String),
}

Dependencies

~5–17MB
~247K SLoC