@melonproject/token-math
A small helper library to do precision safe calculations
Last updated 2 years ago by schmidsi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @melonproject/token-math 
SYNC missed versions from official npm registry.

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

Current Tags

  • 0.1.6                                ...           latest (2 years ago)

40 Versions

  • 0.1.6                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.0.34                                ...           2 years ago
  • 0.0.33                                ...           2 years ago
  • 0.0.32                                ...           2 years ago
  • 0.0.31                                ...           2 years ago
  • 0.0.30                                ...           2 years ago
  • 0.0.29                                ...           2 years ago
  • 0.0.28                                ...           2 years ago
  • 0.0.27                                ...           2 years ago
  • 0.0.25                                ...           2 years ago
  • 0.0.24                                ...           2 years ago
  • 0.0.23                                ...           2 years ago
  • 0.0.22                                ...           2 years ago
  • 0.0.21                                ...           2 years ago
  • 0.0.20                                ...           2 years ago
  • 0.0.19                                ...           2 years ago
  • 0.0.18                                ...           2 years ago
  • 0.0.17                                ...           2 years ago
  • 0.0.16                                ...           2 years ago
  • 0.0.15                                ...           2 years ago
  • 0.0.14                                ...           2 years ago
  • 0.0.13                                ...           2 years ago
  • 0.0.11                                ...           2 years ago
  • 0.0.10                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1-alpha.4                                ...           2 years ago
  • 0.0.1-alpha.3                                ...           2 years ago
  • 0.0.1-alpha.2                                ...           2 years ago
  • 0.0.1-alpha.1                                ...           2 years ago
  • 0.0.1-alpha                                ...           2 years ago
Maintainers (2)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (0)
None

Copyright 2014 - 2017 © taobao.org |