npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solidity-fixed-point-arithmetic

v1.0.1

Published

[Ethers.js](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjN8MCPj5qDAxWik4kEHQm9ACQQFnoECBUQAQ&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fethers&usg=AOvVaw3OQZJdPG47nauSZCacDnY6&opi=89978449) is commonly used to send and manag

Downloads

3

Readme

Solidity Rounding

Ethers.js is commonly used to send and manage blockchain transactions in Javascript. Though Ethers.js is great for sending simple transactions, it falls considerably short for modeling on-chain arithmetic (e.g. from AAVE's Wad Ray Math, or Open Zeppelin's Math library) in javascript. This repo contains a tiny library that adds functions to divide and multiply arbitrarily formatted decimals represented as floats. This library is intended to supplement Ethers.js and thus the inputs and outputs of most functions are of type BigNumber.

Usage

Multiplication

DecimalMulBN can be used to multiply two numbers, and output to arbitrary decimal formatting

Let's say we want to multiply and divide balances in ETH and USDC. ETH balances are always formatted to 18 decimals, and USDC balances are formatted to 6 decimals. Let's say we want to multiply a balance of 0.5 ETH and 0.5 USDC. We can do the following:

import { utils } from "ethers";
import { decimalMulBN } from "solidity-rounding";

const a = utils.parseEther(".5"); // ETH balance
const b = utils.parseUnits(".5", 6); // USDC balance

const product = decimalMulBN(a, b, 18, 6);

console.log("a mul b", product.toString()); // Logs 0.25e18

The outputs of decimalMulBN defaults to 18 decimals. If we want to output to another decimal format, for example, to 6 decimals, we can pass in another argument to the decimalMulBN function:

const product6Decimals = decimalMulBN(a, b, 18, 6, 6)
console.log("a mul b", product6Decimals.toString()); // Logs 0.25e6

Division

Division works similarly to multiplication. The first argument is the dividend, and the second is the divisor. Both are of BigNumber types.

Let's say we want to multiply a balance of 0.5 ETH (represented as 0.5 e18) and 0.5 USDC (represented as 0.5e6). We can do the following:

import { utils } from "ethers";
import { decimalDivBN } from "solidity-rounding";

const a = utils.parseEther(".5");
const b = utils.parseUnits(".5", 6);

const quotient = decimalDivBN(a, b, 18, 6);

console.log("a mul b", quotient.toString()); // Logs 1e18

Similarly to decimalMulBN, The outputs of decimalDivBN defaults to 18 decimals. If we want to output to another decimal format, for example, to 6 decimals, we can pass in another argument:

const quotient6Decimals = decimalDivBN(a, b, 18, 6, 6)
console.log("a mul b", quotient6Decimals.toString()); // Logs 1e6

Other functions

In additional to decimalMulBN and decimalDivBN, this library also adds min, max, abs, and convertDecimals functions.