Cryptocurrency Price Kit
STAGE: RFC
The best Nodejs price kit you need when working with cryptocurrencies with multiple providers support.
Goal
- To provide a simple and easy to use API for working with cryptocurrency prices.
- To provide a simple API to support multiple data providers
- To provide cache support, so you only send request to the provider once per minute.
- To support as many providers as possible.
Installation
npm install cryptocurrency-price-kit
# OR
yarn add cryptocurrency-price-kit
Easy As
const {Cpk} = require('cryptocurrency-price-kit');
const BlockchainInfo = require("cryptocurrency-price-kit/providers/blockchain.info");
const CoinGecko = require("cryptocurrency-price-kit/providers/coingecko.com");
const LiveCoinWatch = require("cryptocurrency-price-kit/providers/livecoinwatch.com");
Cpk.useProviders([
BlockchainInfo,
CoinGecko,
LiveCoinWatch({apiKey: "your-api-key"}),
])
async function Run(){
const blockchain = new Cpk('blockchain.info');
const coingecko = new Cpk('coingecko.com');
const livecoinwatch = new Cpk('livecoinwatch.com');
const price = await blockchain.get('BTC');
const price2 = await blockchain.get('BTC/EUR', 120);
console.log("Blockchain - BTC/USD:", price);
console.log("Blockchain - BTC/EUR:", price2);
const prices = await livecoinwatch.getMany(['BTC/USD', 'ETH/USD', "BNB/USD"], 60);
console.log('LiveCoinWatch - Many:', prices)
const prices2 = await coingecko.getMany(["BITCOIN/EUR", "ETHEREUM", "KADENA"], 60);
console.log("CoinGecko: Many:", prices2);
}
Run().catch(console.error);
What you should know.
- Default currency is
USD
cache
is enabled by default (tll: 60 seconds)
- If currency is not defined, it will be
USD
- Error is thrown if request is not successful, so you should catch all requests.
- We prefer
cache
over interval because it is more reliable. with interval pulling
you may make unnecessary requests when not needed.
Supported providers
Adding custom provider
This can be achieved in two ways.
- Create an issue on GitHub requesting a certain provider, and we will try to add it. (
Try
if the provider documentation is clear enough.)
- OR See how to create it yourself: How to create a custom provider
What may come in the future.
- Fallback to other providers if the first one fails.
- Command line support: E.g.
npx cpk update-supported-data
should update the providers supported coins and currency array.
This is considered important because the majority of the providers have an endpoint where your can get the coins and currency they support.
if supported coins and currency are updated frequently, it will reduce the amount of error requests that may cost you depending on your provider.
How to create a custom provider
Creating a custom provider is as easy as.
const {defineCpkProvider} = require("cryptocurrency-price-kit/src/provider");
const CustomProvider = defineCpkProvider((config) => {
return {
name: 'provider-domain.com',
coinsSupported: ['BTC', 'ETH'] || "any",
currenciesSupported: ['USD', 'EUR'] || "any",
async getPrice(coin, currency) {
return 0;
},
async getPrices(pairs) {
for (const pair of pairs) {
}
return {};
}
}
})
module.exports = CustomProvider;
That's all 😁, all cache function is handled by the package, so you don't need to worry about it.
Only return the values, and we will handle the rest.
If you like this project, you can support it. Any amount can keep the coffee going. 😁
BTC | bc1q4el6ukfe0762rng62gw9augvq49evj3rxh6w09 |
ETH | 0xb39bD9cF75BF29888cB80Cf374ee0822714E31a5 |
Solana | BxcHDVsrk1Y9sX5vqskcMJDhHDT8HkqgYQdZGFMuZKPd |
Polygon Matic | 0x14033a7232232cf3c6a0671f00ad015df6a6c220 |
If you want to be listed as sponsor after sending a donation, please contact hello@trapcode.io