Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRBerg committed Sep 27, 2021
0 parents commit d0c796b
Show file tree
Hide file tree
Showing 31 changed files with 8,399 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .commitlintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- "@commitlint/config-conventional"
3 changes: 3 additions & 0 deletions .czrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "cz-conventional-changelog"
}
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig http://EditorConfig.org

# top-most EditorConfig file
root = true

# All files
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
17 changes: 17 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# directories
.yarn/
**/.nyc_output
**/build
**/coverage
**/dist
**/node_modules

# files
*.env
*.log
*.tsbuildinfo
.pnp.*
coverage.json
npm-debug.log*
yarn-debug.log*
yarn-error.log*
22 changes: 22 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends:
- "eslint:recommended"
- "plugin:@typescript-eslint/eslint-recommended"
- "plugin:@typescript-eslint/recommended"
- "plugin:@typescript-eslint/recommended-requiring-type-checking"
- "prettier"
parser: "@typescript-eslint/parser"
parserOptions:
project: "tsconfig.json"
plugins:
- "@typescript-eslint"
root: true
rules:
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-inferrable-types": "off"
"@typescript-eslint/no-unsafe-assignment": "off"
"@typescript-eslint/no-unsafe-call": "off"
"@typescript-eslint/no-unused-vars":
- error
- argsIgnorePattern: _
varsIgnorePattern: _
1 change: 1 addition & 0 deletions .github/FUNDING.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: ["https://gitcoin.co/grants/1657/paulrberg-open-source-engineering"]
41 changes: 41 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "Continuous Integration"

env:
COVERAGE_GIT_BRANCH: "main"
COVERAGE_SERVICE_NAME: "github-actions-ci"

on:
pull_request:
branches:
- "main"
push:
branches:
- "main"

jobs:
ci:
runs-on: "ubuntu-latest"
steps:
- name: "Check out the repo"
uses: "actions/checkout@v2"

- name: "Install Node.js"
uses: "actions/setup-node@v2"
with:
cache: "yarn"
node-version: "16"

- name: "Install the dependencies"
run: "yarn install --immutable"

- name: "Lint the code"
run: "yarn lint"

- name: "Run the tests and generate the coverage report"
run: "yarn coverage"

- name: "Upload the coverage report to Coveralls"
uses: "coverallsapp/github-action@master"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: "./coverage/lcov.info"
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# directories
.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
**/.nyc_output
**/build
**/coverage
**/dist
**/node_modules

# files
*.env
*.log
*.tsbuildinfo
.pnp.*
coverage.json
npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
5 changes: 5 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn dlx commitlint --edit $1

4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn dlx lint-staged
5 changes: 5 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.{js,json,md,sol,ts}": [
"prettier --config ./.prettierrc.yaml --write"
]
}
8 changes: 8 additions & 0 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extension: "ts"
recursive: true
require:
- "earljs/mocha"
- "ts-node/register/transpile-only"
- "source-map-support/register"
spec: "test/**/*.test.ts"
watchExtension: "ts"
8 changes: 8 additions & 0 deletions .nycrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
check-coverage: false
include:
- "src/**/*.ts"
extends: "@istanbuljs/nyc-config-typescript"
report-dir: "coverage"
reporter:
- "html"
- "lcov"
16 changes: 16 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# directories
.yarn/
**/.nyc_output
**/build
**/coverage
**/dist
**/node_modules

# files
*.env
*.log
*.tsbuildinfo
.pnp.*
npm-debug.log*
yarn-debug.log*
yarn-error.log*
7 changes: 7 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
arrowParens: avoid
bracketSpacing: true
endOfLine: auto
printWidth: 120
singleQuote: false
tabWidth: 2
trailingComma: all
363 changes: 363 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

631 changes: 631 additions & 0 deletions .yarn/releases/yarn-3.0.2.cjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.0.2.cjs
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2021-09-27

### Added

- First release of the package.

[1.0.0]: https://github.com/paulrberg/evm-bn/releases/tag/v1.0.0
24 changes: 24 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Evm Bn [![Coverage Status](https://coveralls.io/repos/github/paulrberg/evm-bn/badge.svg?branch=main)](https://coveralls.io/github/paulrberg/evm-bn?branch=main) [![Styled with Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io) [![Commitizen Friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![license: Unlicense](https://img.shields.io/badge/license-Unlicense-yellow.svg)](https://unlicense.org/)

Convert fixed-point numbers to [ethers](https://github.com/ethers-io/ethers.js) big numbers and vice-versa. This is
useful for [EVM](https://ethereum.org)-related projects, since 1 ETH is 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](https://github.com/ethers-io/ethers.js/tree/master/packages/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:

```sh
$ yarn add evm-bn
```

Or npm:

```sh
$ npm install evm-bn
```

## Usage

### To Bn

```ts
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

```ts
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](https://github.com/paulrberg/evm-bn/issues/new) an issue, [start](https://github.com/paulrberg/evm-bn/discussions/new) a discussion or submit a PR.

### Set Up

Install the dependencies:

```bash
$ yarn install
```

## License

[Unlicense](./LICENSE.md) © Paul Razvan Berg
76 changes: 76 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "evm-bn",
"description": "Convert fixed-point numbers to big numbers and vice-versa",
"version": "2.0.0",
"author": {
"name": "Paul Razvan Berg",
"url": "https://paulrberg.com"
},
"dependencies": {
"@ethersproject/bignumber": "^5.4.2",
"from-exponential": "^1.1.1"
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/mocha": "^9.0.0",
"@types/mocha-each": "^2.0.0",
"@types/node": "^16.10.1",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"earljs": "^0.1.10",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"husky": "^7.0.2",
"lint-staged": "^11.1.2",
"mocha": "^9.1.2",
"mocha-each": "^2.0.1",
"nyc": "^15.1.0",
"pinst": "^2.1.6",
"prettier": "^2.4.1",
"shx": "^0.3.3",
"source-map-support": "^0.5.20",
"ts-node": "^10.2.1",
"typescript": "~4.4.3"
},
"files": [
"/dist/**/*.d.ts",
"/dist/**/*.js",
"CHANGELOG.md"
],
"keywords": [
"bignumber",
"blockchain",
"ethereum",
"ethers",
"evm",
"number-formatting"
],
"license": "Unlicense",
"main": "./dist/index.js",
"peerDependencies": {
"@ethersproject/bignumber": "^5.4.2"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc --build ./tsconfig.prod.json",
"clean": "shx rm -rf ./coverage ./dist",
"coverage": "yarn nyc --nycrc-path ./.nycrc.yaml mocha",
"lint": "yarn lint:ts && yarn prettier:check && yarn typecheck",
"lint:ts": "eslint --config ./.eslintrc.yaml --ignore-path ./.eslintignore --ext .ts .",
"postinstall": "husky install",
"postpublish": "pinst --enable",
"prepack": "yarn build",
"prepublishOnly": "pinst --disable",
"prettier": "prettier --config ./.prettierrc.yaml --write \"**/*.{js,json,md,ts}\"",
"prettier:check": "prettier --check --config ./.prettierrc.yaml \"**/*.{js,json,md,ts}\"",
"test": "mocha --config ./.mocharc.yaml",
"typecheck": "tsc --incremental false --noEmit"
},
"types": "./dist/index.d.ts"
}

0 comments on commit d0c796b

Please sign in to comment.