Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cryptocurrency-price-kit

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cryptocurrency-price-kit

The best price kit you need when working with crypto currencies with realtime and multiple providers support.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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");

// Add Providers
Cpk.useProviders([
    BlockchainInfo, // supports bitcoin only
    CoinGecko, // supports almost all coins
    LiveCoinWatch({apiKey: "your-api-key"}), // supports almost all coins
])


async function Run(){
  // Initialize
  const blockchain = new Cpk('blockchain.info');
  const coingecko = new Cpk('coingecko.com');
  const livecoinwatch = new Cpk('livecoinwatch.com');
  
  // Get bitcoin Price and cache for 60 secs by default
  const price = await blockchain.get('BTC');
  // or with custom cache time
  const price2 = await blockchain.get('BTC/EUR', 120); // seconds
  
  console.log("Blockchain - BTC/USD:", price); // The current price of bitcoin in USD
  console.log("Blockchain - BTC/EUR:", price2); // The current price of bitcoin in USD
  
  // OR
  // GET Many Prices and cache for 60 secs
  const prices = await livecoinwatch.getMany(['BTC/USD', 'ETH/USD', "BNB/USD"], 60);
  console.log('LiveCoinWatch - Many:', prices) // {BTC/USD: price, ETH/USD: price, BNB/USD: price}
  
  // Also supports using the symbol your provider supports
  // e.g coingecko supports using code instead of symbol
  // i.e `bitcoin` instead of `BTC`
  const prices2 = await coingecko.getMany(["BITCOIN/EUR", "ETHEREUM", "KADENA"], 60);
  console.log("CoinGecko: Many:", prices2); // {BITCOIN/EUR: price, ETHEREUM/USD: price, KADENA/USD: price}
}

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

  • livecoinwatch.com
  • blockchain.info
  • coingecko.com
  • coinmarketcap.com
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");

// Define a provider
// `config` is an object that contains the configuration passed for the provider.
const CustomProvider = defineCpkProvider((config) => {
    return {
       name: 'provider-domain.com',
       coinsSupported: ['BTC', 'ETH'] || "any", // if any no validation is done
       currenciesSupported: ['USD', 'EUR'] || "any", // if any no validation is done
       
       // This is the function that will be called to get the price
       // It will be called with the coin and currency as arguments
       // The async function should return the price
       async getPrice(coin, currency) {
           // return the price
           return 0;
       },
       
       // This is the function that will be called to get multiple prices
       // It will be called with array of coins and currency as arguments
       // The async function should return the prices as object
       // The object should have the coin/key and the price as value
       // E.g. {"BTC/USD": 0, "ETH/EUR": 0}
       async getPrices(pairs) {
           // return the prices
           for (const pair of pairs) {
               // pair.coin, pair.currency
           }
           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.

Sponsor/support

If you like this project, you can support it. Any amount can keep the coffee going. 😁

CoinAddress
BTCbc1q4el6ukfe0762rng62gw9augvq49evj3rxh6w09
ETH0xb39bD9cF75BF29888cB80Cf374ee0822714E31a5
SolanaBxcHDVsrk1Y9sX5vqskcMJDhHDT8HkqgYQdZGFMuZKPd
Polygon Matic0x14033a7232232cf3c6a0671f00ad015df6a6c220

If you want to be listed as sponsor after sending a donation, please contact hello@trapcode.io

Keywords

FAQs

Package last updated on 06 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc