Crates.io | currency-prices |
lib.rs | currency-prices |
version | 0.1.2 |
source | src |
created_at | 2024-01-19 04:48:35.944918 |
updated_at | 2024-01-24 18:59:31.091642 |
description | A small library to get the cryptocurreny prices |
homepage | |
repository | https://github.com/wiringbits/currency-prices |
max_upload_size | |
id | 1104980 |
size | 17,093 |
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.
Add the following line to your Cargo.toml
file:
[dependencies]
currency-prices = "0.1.2"
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),
}