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

token-amount

v0.3.0

Published

[<img src="https://img.shields.io/npm/v/token-amount" alt="" />](https://www.npmjs.com/package/token-amount) [<img src="https://img.shields.io/bundlephobia/minzip/token-amount" alt="" />](https://bundlephobia.com/result?p=token-amount)

Downloads

394

Readme

💸 TokenAmount

A transportable object for token amounts with formatting.

Usage

Add it to your project:

yarn add token-amount

Use it:

import TokenAmount from 'token-amount'

const amount = new TokenAmount('9388295879707883945', 18, { symbol: 'ANT' })

console.log(amount.format()) // '9.39 ANT'

API

Constructor

new TokenAmount(value, decimals, { symbol })

Instantiates a new TokenAmount with the given value and decimals. The options are optional, and can take a symbol (e.g. "ANT").

Parameters
  • value: the amount value, as a BigInt, String, Number or BigInt-like (e.g. BN.js).
  • decimals: the amount of decimals, as a BigInt, String, Number or BigInt-like (e.g. BN.js).
  • symbol: the token symbol, as a String.

Formatting

TokenAmount#format(options)

Formats the token amount.

Parameters
  • options.symbol: the token symbol, as a String. Overrides the value set in the constructor.
  • options.commify: whether the formatted amount should include comma separators
  • options.digits: the number of digits to display. Defaults to 2.
  • options.displaySign: whether the sign (- or +) should be displayed for the amount.

TokenAmount#toString(options)

Alias to TokenAmount#format().

TokenAmount.format(amount, decimals, options)

Static equivalent of TokenAmount#format(), with the TokenAmount instance passed as a first parameter.

Converting

TokenAmount#convert(rate, targetDecimals, options)

Converts from a rate, returning a new TokenAmount instance with the desired decimals and set options. The conversion rate is expressed as the amount of the output token obtained per unit of the input token. An example would be:

  • Input token: ANT
  • Output token: ETH
  • Amount of ANT: 10
  • Conversion rate: 0.5 ETH per ANT. (1 ANT = 0.5 ETH)
  • Converted Amount = 10 * 0.50 = 5 ETH.
Parameters
  • rate: the rate to convert from, as a String or a Number.
  • targetDecimals: the target amount of decimals for the output, as a BigInt, String, Number or BigInt-like (e.g. BN.js).
  • options.symbol: the token symbol, as a String.

TokenAmount.convert(amount, rate, targetDecimals, options)

Static equivalent of TokenAmount#convert(), with the TokenAmount instance passed as a first parameter.

Transporting

TokenAmount#export()

Exports the object into a string that can get stored or transported.

TokenAmount.import()

Instantiates a new instance by importing a string generated by TokenAmount#export().