A small helper library to do precision safe calculations
npm install @melonproject/token-mathA 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._
- Fully type script typed
- Reusable
- Reuse JS-native functions like: toFixed, toNumber, ...
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.
``typescript
import { createToken, isEqual } from "@melonproject/token-math";
const mlnToken = createToken("MLN");
const mlnToken2 = createToken("MLN");
expect(isEqual(mlnToken, mlnToken2)).toBe(true);
`
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.
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:
`typescript
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