npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crypto-resolver

v1.0.4

Published

A CoinGecko cryptocurrency API ID resolver

Downloads

4

Readme

crypto-resolver

A lightweight Node.js package for resolving cryptocurrency names and tickers to their CoinGecko IDs and vice versa, leveraging the CoinGecko API. Perfect for developers working with cryptocurrency data who need a reliable way to translate between different identifiers.

Features

  • Name to ID Resolution: Convert cryptocurrency names or tickers to their unique CoinGecko IDs.
  • ID to Name Resolution: Convert CoinGecko IDs back to cryptocurrency names and tickers.
  • Initialization Check: Ensures the resolver is properly initialized before usage, improving reliability.
  • Error Handling: Provides clear error messages for better debugging and user experience.
  • Lightweight & Fast: Minimal dependencies for quick and efficient performance.
  • Easy to Use: Designed with simplicity in mind for developers of all skill levels.

Installation

Install crypto-resolver using npm:

npm install crypto-resolver

Or yarn:

yarn add crypto-resolver

Usage

First, import and initialize the CryptoResolver class. Ensure you call the init method before using any resolver functionality to fetch the latest cryptocurrency data:

const CryptoResolver = require('crypto-resolver');
const resolver = new CryptoResolver();

async function initialize() {
  try {
    await resolver.init();
    console.log('Initialization successful.');
  } catch (error) {
    console.error('Initialization failed:', error);
  }
}

initialize();

Resolving Name or Ticker to CoinGecko ID

async function resolveNameOrTicker() {
  try {
    const id = await resolver.resolveNameOrTickerToId('btc');
    console.log(`The CoinGecko ID for BTC is ${id}`);
  } catch (error) {
    console.error('Error resolving name or ticker:', error);
  }
}

resolveNameOrTicker();

Resolving CoinGecko ID to Name and Ticker

async function resolveIdToNameAndTicker() {
  try {
    const info = await resolver.resolveIdToNameAndTicker('bitcoin');
    console.log(`Name: ${info.name}, Ticker: ${info.ticker}`);
  } catch (error) {
    console.error('Error resolving ID to name and ticker:', error);
  }
}

resolveIdToNameAndTicker();

Example Code

Below are some "real-life" examples of what you can achieve with crypto-resolver:

Display Multiple Cryptocurrencies Info

Retrieve and display information for multiple cryptocurrencies by name or ticker:

const CryptoResolver = require('crypto-resolver');
const resolver = new CryptoResolver();

async function displayCryptoInfo(names) {
  await resolver.init();
  for (const name of names) {
    const id = await resolver.resolveNameOrTickerToId(name);
    if (id) {
      const info = await resolver.resolveIdToNameAndTicker(id);
      console.log(`${info.name} (${info.ticker}): CoinGecko ID = ${id}`);
    } else {
      console.log(`No CoinGecko ID found for "${name}".`);
    }
  }
}

displayCryptoInfo(['btc', 'eth', 'invalidCrypto']);

Handling Errors Gracefully

Demonstrate handling a common error – using the resolver without initialization:

const CryptoResolver = require('crypto-resolver');
const uninitResolver = new CryptoResolver();

async function attemptResolveWithoutInit() {
  try {
    const id = await uninitResolver.resolveNameOrTickerToId('btc');
    console.log(`CoinGecko ID for BTC: ${id}`);
  } catch (error) {
    console.error('Caught error:', error.message);
  }
}

attemptResolveWithoutInit();

This script shows how crypto-resolver throws an error when trying to resolve a cryptocurrency name or ticker without first calling init(), and how to catch and handle that error.

Error Handling

The crypto-resolver package throws errors if it's not properly initialized or if an issue occurs during the initialization process. Ensure you handle these errors in your application to maintain stability and provide feedback to your users.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub if you have any suggestions, improvements, or bug reports.

License

This project is licensed under the ISC License - see the LICENSE file for details.