Skip to content

Commit

Permalink
feat(deps): remove ethers dependency
Browse files Browse the repository at this point in the history
remove dependency on ethers for formatting

BREAKING CHANGE: API
  • Loading branch information
Rubilmax committed Nov 29, 2023
1 parent 585156e commit a20e6db
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 755 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ on:

jobs:
test:
uses: rubilmax/ethers-maths/.github/workflows/test.yml@main
uses: rubilmax/evm-maths/.github/workflows/test.yml@main

release:
needs: test

uses: rubilmax/ethers-maths/.github/workflows/release.yml@main
uses: rubilmax/evm-maths/.github/workflows/release.yml@main
secrets: inherit
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ethers-maths
# evm-maths

[![npm package][npm-img]][npm-url]
[![Build Status][build-img]][build-url]
Expand All @@ -8,16 +8,16 @@
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]

> ➗ Useful ethers-based math libraries to ease your journey through off-chain fixed-point arithmetics
> ➗ Useful bigint math libraries to ease your journey through off-chain fixed-point arithmetics
## Install

```bash
npm install ethers-maths
npm install evm-maths
```

```bash
yarn add ethers-maths
yarn add evm-maths
```

---
Expand All @@ -27,7 +27,7 @@ yarn add ethers-maths
Just import the module and you'll benefit from an augmented, and typed, `BigInt` class!

```typescript
import "ethers-maths";
import "evm-maths";

const WAD = BigInt.pow10(18);

Expand All @@ -38,9 +38,9 @@ BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
If you choose to avoid prototype pollution, you can always import specific utilities:

```typescript
import * as WadMath from "ethers-maths/lib/wad";
import * as RayMath from "ethers-maths/lib/ray";
import * as PercentMath from "ethers-maths/lib/percent";
import * as WadMath from "evm-maths/lib/wad";
import * as RayMath from "evm-maths/lib/ray";
import * as PercentMath from "evm-maths/lib/percent";
```

---
Expand Down Expand Up @@ -133,7 +133,7 @@ Returns whether the BigNumber is approximately close to the given BigNumber, wit

```typescript
// only if you want to avoid BigNumber prototype pollution
import { approxEqAbs } from "ethers-maths/lib/utils";
import { approxEqAbs } from "evm-maths/lib/utils";

// Returns whether the BigNumber is approximately close to the given BigNumber, within the given tolerance: true
approxEqAbs(0, 1, "1");
Expand All @@ -147,7 +147,7 @@ Returns the minimum between input BigNumberish, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { min } from "ethers-maths/lib/utils";
import { min } from "evm-maths/lib/utils";

// Returns the minimum between input BigNumberish, as a BigInt: 0
min(0, 1, "2", ...);
Expand All @@ -161,7 +161,7 @@ Returns the maximum between input BigNumberish, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { max } from "ethers-maths/lib/utils";
import { max } from "evm-maths/lib/utils";

// Returns the maximum between input BigNumberish, as a BigInt: 2
max(0, 1, "2", ...);
Expand All @@ -175,7 +175,7 @@ Returns the sum of input BigNumberish array, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { sum } from "ethers-maths/lib/utils";
import { sum } from "evm-maths/lib/utils";

// Returns the sum of input BigNumberish array, as a BigInt: 3
sum([0, 1, "2"]);
Expand Down Expand Up @@ -212,7 +212,7 @@ Returns a 1 followed by the input number of zeros (10 raised to the power of the

```typescript
// only if you want to avoid BigInt prototype pollution
import { pow10 } from "ethers-maths/lib/utils";
import { pow10 } from "evm-maths/lib/utils";

// Returns a 1 followed by the input number of zeros: 100
pow10(2);
Expand All @@ -225,7 +225,7 @@ Performs a multiplication followed by a division, rounded half up

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivHalfUp } from "ethers-maths/lib/utils";
import { mulDivHalfUp } from "evm-maths/lib/utils";

// 1.0 (in wad) * 1 / 1 = 1.0 (in wad)
mulDivHalfUp(BigInt.WAD, 1, 1);
Expand All @@ -238,7 +238,7 @@ Performs a multiplication followed by a division, rounded up

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivUp } from "ethers-maths/lib/utils";
import { mulDivUp } from "evm-maths/lib/utils";

// 0.999999999999999999 * 1 / WAD = 1.0 (in wad, rounded up)
mulDivUp(BigInt.WAD - 1n, 1, BigInt.WAD);
Expand All @@ -251,7 +251,7 @@ Performs a multiplication followed by a division, rounded down

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivDown } from "ethers-maths/lib/utils";
import { mulDivDown } from "evm-maths/lib/utils";

// 1.000000000000000001 * 1 / WAD = 1.0 (in wad, rounded down)
mulDivDown(BigInt.WAD + 1n, 1, BigInt.WAD);
Expand All @@ -270,7 +270,7 @@ _Most commonly used as the ERC20 token unit_

```typescript
// only if you want to avoid BigInt prototype pollution
import { WAD } from "ethers-maths/lib/constants";
import { WAD } from "evm-maths/lib/constants";

// Returns a 1 followed by 18 zeros: 1000000000000000000
WAD;
Expand All @@ -285,7 +285,7 @@ _Most commonly used as Aave's index unit_

```typescript
// only if you want to avoid BigInt prototype pollution
import { RAY } from "ethers-maths/lib/constants";
import { RAY } from "evm-maths/lib/constants";

// Returns a 1 followed by 27 zeros: 1000000000000000000000000000
RAY;
Expand All @@ -300,7 +300,7 @@ _Most commonly used as Aave's `PERCENTAGE_FACTOR`_

```typescript
// only if you want to avoid BigInt prototype pollution
import { PERCENT } from "ethers-maths/lib/constants";
import { PERCENT } from "evm-maths/lib/constants";

// Returns a 1 followed by 4 zeros: 10000
PERCENT;
Expand All @@ -313,7 +313,7 @@ Returns half of the common WAD unit, which is also known as `0.5 ether` in Solid

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_WAD } from "ethers-maths/lib/constants";
import { HALF_WAD } from "evm-maths/lib/constants";

// Returns a 1 followed by 18 zeros: 1000000000000000000
HALF_WAD;
Expand All @@ -326,7 +326,7 @@ Returns half of the common RAY unit, which is also known as `0.5e9 ether` in Sol

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_RAY } from "ethers-maths/lib/constants";
import { HALF_RAY } from "evm-maths/lib/constants";

// Returns a 1 followed by 27 zeros: 1000000000000000000000000000
HALF_RAY;
Expand All @@ -341,7 +341,7 @@ _Most commonly used as Aave's `HALF_PERCENTAGE_FACTOR`_

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_PERCENT } from "ethers-maths/lib/constants";
import { HALF_PERCENT } from "evm-maths/lib/constants";

// Returns a 1 followed by 4 zeros: 10000
HALF_PERCENT;
Expand Down Expand Up @@ -836,16 +836,16 @@ Scales the percent-based BigInt up or down to the given scale defined by its num
BigInt.RAY.percentToDecimals(27); // 1 RAY
```

[build-img]: https://github.com/Rubilmax/ethers-maths/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/Rubilmax/ethers-maths/actions/workflows/release.yml
[test-img]: https://github.com/Rubilmax/ethers-maths/actions/workflows/test.yml/badge.svg
[test-url]: https://github.com/Rubilmax/ethers-maths/actions/workflows/test.yml
[downloads-img]: https://img.shields.io/npm/dt/ethers-maths
[downloads-url]: https://www.npmtrends.com/ethers-maths
[npm-img]: https://img.shields.io/npm/v/ethers-maths
[npm-url]: https://www.npmjs.com/package/ethers-maths
[issues-img]: https://img.shields.io/github/issues/Rubilmax/ethers-maths
[issues-url]: https://github.com/Rubilmax/ethers-maths/issues
[build-img]: https://github.com/Rubilmax/evm-maths/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/Rubilmax/evm-maths/actions/workflows/release.yml
[test-img]: https://github.com/Rubilmax/evm-maths/actions/workflows/test.yml/badge.svg
[test-url]: https://github.com/Rubilmax/evm-maths/actions/workflows/test.yml
[downloads-img]: https://img.shields.io/npm/dt/evm-maths
[downloads-url]: https://www.npmtrends.com/evm-maths
[npm-img]: https://img.shields.io/npm/v/evm-maths
[npm-url]: https://www.npmjs.com/package/evm-maths
[issues-img]: https://img.shields.io/github/issues/Rubilmax/evm-maths
[issues-url]: https://github.com/Rubilmax/evm-maths/issues
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
Expand Down
26 changes: 11 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ethers-maths",
"version": "5.0.0",
"description": "➗ Useful ethers-based math libraries to ease the journey through off-chain fixed-point arithmetics",
"name": "evm-maths",
"version": "1.0.0",
"description": "➗ Useful bigint math libraries to ease the journey through off-chain fixed-point arithmetics",
"main": "lib/index.js",
"files": [
"lib/*"
Expand All @@ -17,7 +17,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/Rubilmax/ethers-maths.git"
"url": "git+https://github.com/Rubilmax/evm-maths.git"
},
"license": "MIT",
"author": {
Expand All @@ -30,35 +30,31 @@
},
"keywords": [
"ethers",
"bigint",
"bignumber",
"multicall",
"rpc",
"call",
"evm",
"smart contract"
],
"bugs": {
"url": "https://github.com/Rubilmax/ethers-maths/issues"
},
"homepage": "https://github.com/Rubilmax/ethers-maths#readme",
"dependencies": {
"ethers": "^6.0.0"
"url": "https://github.com/Rubilmax/evm-maths/issues"
},
"homepage": "https://github.com/Rubilmax/evm-maths#readme",
"devDependencies": {
"@jest/globals": "^29.7.0",
"@trivago/prettier-plugin-sort-imports": "4.1.1",
"@types/jest": "^29.5.5",
"@types/jest": "^29.5.10",
"commitizen": "^4.3.0",
"conventional-changelog-conventionalcommits": "^5.0.0",
"cz-conventional-changelog": "^3.3.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^14.0.1",
"prettier": "^3.0.3",
"prettier": "^3.1.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"peerDependencies": {
"ethers": "^6.0.0"
"typescript": "^5.3.2"
},
"config": {
"commitizen": {
Expand Down
9 changes: 4 additions & 5 deletions src/comp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BigNumberish, toBigInt } from "ethers";
import { WAD, WAD_SQUARED } from "./constants";

export const compMul = (x: BigNumberish, other: BigNumberish) => {
return (toBigInt(x) * toBigInt(other)) / WAD;
export const compMul = (x: bigint, other: bigint) => {
return (x * other) / WAD;
};

export const compDiv = (x: BigNumberish, other: BigNumberish) => {
return (toBigInt(x) * WAD_SQUARED) / toBigInt(other) / WAD;
export const compDiv = (x: bigint, other: bigint) => {
return (x * WAD_SQUARED) / other / WAD;
};
6 changes: 3 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { pow10 } from "./utils";

export const PERCENT = pow10(4);
export const WAD = pow10(18);
export const RAY = pow10(27);
export const PERCENT = pow10(4n);
export const WAD = pow10(18n);
export const RAY = pow10(27n);
export const WAD_SQUARED = WAD ** 2n;

export const HALF_PERCENT = PERCENT / 2n;
Expand Down
38 changes: 22 additions & 16 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import { BigNumberish, formatUnits, toBigInt } from "ethers";
import { pow10 } from "./utils";

export const format = (x: BigNumberish, decimals?: number, digits?: number) => {
const formatted = formatUnits(x, decimals);
export const format = (x: bigint, decimals: number = 18, digits?: number) => {
decimals = Math.floor(decimals);
digits = Math.floor(digits ?? decimals);

let dotIndex = formatted.indexOf(".");
if (dotIndex < 0) dotIndex = formatted.length;
if (decimals === 0) return x.toString();

decimals = formatted.length - 1 - dotIndex;
digits ??= decimals;
let negative = "";
if (x < 0n) {
negative = "-";
x *= -1n;
}

const abs = x.toString().padStart(decimals + 1, "0");

const length = abs.length;
const dotIndex = length - decimals;

return digits < decimals
? formatted.slice(0, dotIndex + (digits > 0 ? digits + 1 : 0))
: formatted + "0".repeat(digits - decimals);
let full = negative + abs.substring(0, dotIndex);
if (digits > 0) full += "." + abs.substring(dotIndex, dotIndex + digits).padEnd(digits, "0");

return full;
};

export const toFloat = (x: BigNumberish, decimals?: number) => {
export const toFloat = (x: bigint, decimals?: number) => {
return parseFloat(format(x, decimals));
};

export const toDecimals = (x: BigNumberish, decimals: number, scaleDecimals: number) => {
x = toBigInt(x);

export const toDecimals = (x: bigint, decimals: number, scaleDecimals: number) => {
if (decimals <= scaleDecimals) {
const ratio = pow10(scaleDecimals - decimals);
const ratio = pow10(BigInt(Math.floor(scaleDecimals - decimals)));

return (x + ratio / 2n) / ratio;
}

return x * pow10(decimals - scaleDecimals);
return x * pow10(BigInt(Math.floor(decimals - scaleDecimals)));
};
Loading

0 comments on commit a20e6db

Please sign in to comment.