GithubHelp home page GithubHelp logo

paulrberg / evm-bn Goto Github PK

View Code? Open in Web Editor NEW
49.0 4.0 4.0 2.81 MB

Convert fixed-point numbers to ethers big numbers and vice-versa.

License: MIT License

TypeScript 100.00%
blockchain ethereum evm ethersjs bignumber

evm-bn's Introduction

Evm Bn GitHub Actions Coverage Styled with Prettier License: MIT

Evm Bn is a utility for converting between stringified fixed-point numbers and Ethers.js BigNumbers, as well as the reverse process. It is particularly useful for projects based on the Ethereum Virtual Machine (EVM), given that 1 ETH is equivalent to 1e18 wei.

  • 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

$ pnpm add evm-bn

Usage

To Bn

import type { BigNumber } from "@ethersproject/bignumber";
import { toBn } from "evm-bn";

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

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

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

From Bn

import type { BigNumber } from "@ethersproject/bignumber";
import { fromBn } from "evm-bn";

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

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

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

Contributing

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

Set Up

Clone the repositories and install the dependencies:

$ pnpm install

Now you can start making changes.

License

This project is licensed under MIT.

evm-bn's People

Contributors

paulrberg avatar tiagox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

evm-bn's Issues

Some issues with negative exponential numbers (scientific e notation)

I noticed that when representing very small numbers like 10 * 10-18 (10 with 0 decimals), the following line works: fp("1e-17"), while the following line doesn't fp("10e-18"). Apparently, fp("10e-18") is internally interpreted to be equal to 1 * 10-18, so does fp("100e-19") and fp("1000e-20").

BigNumber percentage with calculation with evm-bn

Hello, I'm trying to write some TypeScript versions of functions using PRBMath Solidity Library. One thing I am having trouble with is modeling TypeScript functions the same way as the Solidity ones.

For example, in Solidity you can do something like .25e18 to model 25% out as a Big Number, and multiple X by .25e18 to get 25% of X.

uint256 twentyFivePercent = X.mul(.25e18);

With TypeScript, I have not been able to find an equivalent, even with these wonderful libraries like evm-bn. I believe I'm just doing something wrong here, and would love an example for how I can achieve the above using evm-bn.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.