StableMath JS
TypeScript port of the StableMath library from mStable.
Installation
yarn add @mstable/stablemath-js
Usage
import { BigNumber } from '@ethersproject/bignumber'
import {
divPrecisely,
mulTruncate,
mulRatioTruncate,
divRatioPrecisely,
toSimple,
} from '@mstable/stablemath-js'
const a = BigNumber.from('1000000000000000000')
const b = BigNumber.from('2000000000000000000')
// Multiplies two precise units, and then truncates by the full scale.
mulTruncate(a, b)
// Multiplies and truncates a token ratio, essentially flooring the result
// i.e. How much mAsset is this bAsset worth?
mulRatioTruncate(a, b)
// Precisely divides two units, by first scaling the left hand operand.
// Useful for finding percentage weightings, i.e. 8e18/10e18 = 80% (or 8e17)
divPrecisely(b, a)
// Precisely divides two ratioed units, by first scaling the left hand operand
// i.e. How much bAsset is this mAsset worth?
divRatioPrecisely(b, a)
// Convert an exact BigNumber (with decimals) to a simple JavaScript number (losing precision)
toSimple(a, 18)
Local Development
This project was bootstrapped with TSDX.
Below is a list of commands you will probably find useful.
npm start
or yarn start
Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.
Your library will be rebuilt if you make edits.
npm run build
or yarn build
Bundles the package to the dist
folder.
The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).
npm test
or yarn test
Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.