1.2.0 • Published 3 years ago

evm-fp v1.2.0

Weekly downloads
-
License
Unlicense
Repository
-
Last release
3 years ago

Evm Fp Coverage Status Styled with Prettier Commitizen Friendly license: Unlicense

Convert fixed-point numbers to big numbers and vice-versa. This package is useful for EVM related projects, because 1 ETH = 1e18 wei. In addition, the most popular token standard involves a decimals field that represents how many fractional digits the tokens amounts have.

Features

  • Accepts scientific notation.
  • Limits the precision to 78 digits.
  • Enforces 60 integer digits and 18 fractional digits.
  • Designed to be used alongside @ethersproject/bignumber.
  • Slices the fractional digits automatically at position n + 1 and above, with n the number of decimals, rounding down in the process.

Install

With yarn:

$ yarn add evm-fp

Or npm:

$ npm install evm-fp

Usage

To Fp

import type { BigNumber } from "@ethersproject/bignumber";
import { toFp } from "evm-fp";

// 3141500000000000000
const foo: BigNumber = toFp("3.1415");

// 115792089237316195423570985008687907853269984665640564039457584007913129639935
const bar: BigNumber = toFp("115792089237316195423570985008687907853269984665640564039457.584007913129639935");

// 100000000000000
const baz: BigNumber = toFp("100e6", 6);

From Fp

import type { BigNumber } from "@ethersproject/bignumber";
import { fromFp } from "evm-fp";

// 3.1415
const foo: BigNumber = fromFp(BigNumber.from("3141500000000000000"));

// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
const bar: BigNumber = fromFp(
  BigNumber.from("115792089237316195423570985008687907853269984665640564039457584007913129639935"),
);

// 100000000
const baz: BigNumber = fromFp(BigNumber.from("100000000000000"), 6);

Contributing

Feel free to dive in! Open an issue, start a discussion or submit a PR.

Set Up

Install the dependencies:

$ yarn install

License

Unlicense © Paul Razvan Berg