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

@melonproject/token-math

v0.1.6

Published

A small helper library to do precision safe calculations

Downloads

416

Readme

token-math

A small helper library to do precision safe calculations and work with tokens.

This is heavily work in progress. But feel free to open issues/pull-request with ideas.

Design goals

  • Fully type script typed
  • Reusable
  • Reuse JS-native functions like: toFixed, toNumber, ...

High-level concepts

This package contains 5 top-level namespaces with associated functions:

  • address: Contains a minimal class Address to achieve type-safety with Ethereum addresses.
  • bigInteger: Helper functions to do math with big integers. Note: Integers are always rounded. So be careful with divide. Usually, you should not use the bigInteger functions directly, it's always more safe to use the quantity/price functions.
  • token: The basic building block: A token consists of a symbol and a number of decimals, normally 18. Optionally, a token can also have an Address where its contract is deployed.
  • quantity: A quantity is an amount of a token. This library provides some helper functions to calculate with quantities. Be aware that all these functions also check, if the tokens are the same. It does not make sense to "add" 2 MLNs and 3 ETH.
  • price. A price consist of a base quantity and quote quantity. Note that the quantity also includes the token. A price is internally represented as a fraction instead of a float to avoid rounding errors.

Usage

import { createToken, isEqual } from "@melonproject/token-math";

const mlnToken = createToken("MLN");
const mlnToken2 = createToken("MLN");

expect(isEqual(mlnToken, mlnToken2)).toBe(true);

Development

This repository holds a special structure to facilitate TypeScript module development. And has therefore 3 package.jsons:

  • ./package.json: The root package which is a lerna workspace package. It should contain all devDependencies (add with lerna add -D -W package)
  • ./src/package.json: The actual TypeScript source. Contains dependencies and all other important fields. You should be able to yarn link inside this folder to link directly the TypeScript files into your consuming package without modifying tsconfig.json.
  • ./dist/package.json: The src/package.json copied over and slightly modified.

To learn more about this workflow, look at scripts/syncPkg.ts.

Flexible API

It seems to be confusing to have both styles in the same repo. But it should be straight forward to create a OO wrapper on top of the functions. Here is an example:

import { createQuantity } from "../Quantity";

import appendDecimals from "./appendDecimals";
import Token from "./Token";
import isSameToken from "./isSameToken";
import createToken from "./createToken";

class Token implements Token {
  readonly symbol: string;
  readonly address?: string;
  readonly decimals: number;

  // constructor(symbol: string);
  constructor(tokenOrSymbol: Token | string) {
    const token =
      typeof tokenOrSymbol === "string"
        ? createToken(tokenOrSymbol)
        : tokenOrSymbol;

    this.symbol = token.symbol;
    this.decimals = token.decimals;
    this.address = token.address;
  }

  static createToken = createToken;

  createQuantity = number => createQuantity(this, number);

  static appendDecimals = appendDecimals;
  appendDecimals = number => appendDecimals(this, number);

  static isSameToken = isSameToken;
  isSameToken = compareToken => isSameToken(this, compareToken);
}

export default Token;

Or here before we removed the OO API from this repo: https://github.com/melonproject/token-math/tree/before-ooapi-remove