1.0.2 • Published 3 years ago

prb-math.js v1.0.2

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

PRBMath.Js npm

JavaScript SDK for the PRBMath smart contract library for advanced fixed-point math.

Background

This SDK is complementary to the PRBMath smart contract library for advanced fixed-point math. It provides TypeScript implementations for the mathematical functions originally written in Solidity.

Install

Node.Js

With yarn:

$ yarn add prb-math.js @ethersproject/bignumber evm-bn

Or npm:

$ npm install prb-math.js @ethersproject/bignumber evm-bn

The two trailing packages are the peer dependencies of prb-math.js.

Browser

<script type="text/javascript" src="https://unpkg.com/prb-math.js/dist/browser/prb-math.min.js"></script>

<script type="text/javascript">
  window.PRBMath; // or PRBMath
</script>

Usage

Whatever functions there are in the Solidity library, you should find them implemented in prb-math.js. The only exception are the fromUint and toUint convertors, which are replicated via an external package called evm-bn.

Here's an example for how to calculate the binary logarithm:

import type { BigNumber } from "@ethersproject/bignumber";
import { fromBn, toBn } from "evm-bn";
import { log2 } from "prb-math.js";

(async function () {
  const x: BigNumber = toBn("16");
  const result: BigNumber = log2(x);
  console.log({ result: fromBn(result) });
})();

Pro tip: see how the SDK is used in the tests I wrote for PRBMath.

License

Unlicense © Paul Razvan Berg