Skip to content

Commit a20e6db

Browse files
committed
feat(deps): remove ethers dependency
remove dependency on ethers for formatting BREAKING CHANGE: API
1 parent 585156e commit a20e6db

13 files changed

Lines changed: 543 additions & 755 deletions

File tree

.github/workflows/push.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ on:
77

88
jobs:
99
test:
10-
uses: rubilmax/ethers-maths/.github/workflows/test.yml@main
10+
uses: rubilmax/evm-maths/.github/workflows/test.yml@main
1111

1212
release:
1313
needs: test
1414

15-
uses: rubilmax/ethers-maths/.github/workflows/release.yml@main
15+
uses: rubilmax/evm-maths/.github/workflows/release.yml@main
1616
secrets: inherit

README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ethers-maths
1+
# evm-maths
22

33
[![npm package][npm-img]][npm-url]
44
[![Build Status][build-img]][build-url]
@@ -8,16 +8,16 @@
88
[![Commitizen Friendly][commitizen-img]][commitizen-url]
99
[![Semantic Release][semantic-release-img]][semantic-release-url]
1010

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

1515
```bash
16-
npm install ethers-maths
16+
npm install evm-maths
1717
```
1818

1919
```bash
20-
yarn add ethers-maths
20+
yarn add evm-maths
2121
```
2222

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

2929
```typescript
30-
import "ethers-maths";
30+
import "evm-maths";
3131

3232
const WAD = BigInt.pow10(18);
3333

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

4040
```typescript
41-
import * as WadMath from "ethers-maths/lib/wad";
42-
import * as RayMath from "ethers-maths/lib/ray";
43-
import * as PercentMath from "ethers-maths/lib/percent";
41+
import * as WadMath from "evm-maths/lib/wad";
42+
import * as RayMath from "evm-maths/lib/ray";
43+
import * as PercentMath from "evm-maths/lib/percent";
4444
```
4545

4646
---
@@ -133,7 +133,7 @@ Returns whether the BigNumber is approximately close to the given BigNumber, wit
133133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

839-
[build-img]: https://github.com/Rubilmax/ethers-maths/actions/workflows/release.yml/badge.svg
840-
[build-url]: https://github.com/Rubilmax/ethers-maths/actions/workflows/release.yml
841-
[test-img]: https://github.com/Rubilmax/ethers-maths/actions/workflows/test.yml/badge.svg
842-
[test-url]: https://github.com/Rubilmax/ethers-maths/actions/workflows/test.yml
843-
[downloads-img]: https://img.shields.io/npm/dt/ethers-maths
844-
[downloads-url]: https://www.npmtrends.com/ethers-maths
845-
[npm-img]: https://img.shields.io/npm/v/ethers-maths
846-
[npm-url]: https://www.npmjs.com/package/ethers-maths
847-
[issues-img]: https://img.shields.io/github/issues/Rubilmax/ethers-maths
848-
[issues-url]: https://github.com/Rubilmax/ethers-maths/issues
839+
[build-img]: https://github.com/Rubilmax/evm-maths/actions/workflows/release.yml/badge.svg
840+
[build-url]: https://github.com/Rubilmax/evm-maths/actions/workflows/release.yml
841+
[test-img]: https://github.com/Rubilmax/evm-maths/actions/workflows/test.yml/badge.svg
842+
[test-url]: https://github.com/Rubilmax/evm-maths/actions/workflows/test.yml
843+
[downloads-img]: https://img.shields.io/npm/dt/evm-maths
844+
[downloads-url]: https://www.npmtrends.com/evm-maths
845+
[npm-img]: https://img.shields.io/npm/v/evm-maths
846+
[npm-url]: https://www.npmjs.com/package/evm-maths
847+
[issues-img]: https://img.shields.io/github/issues/Rubilmax/evm-maths
848+
[issues-url]: https://github.com/Rubilmax/evm-maths/issues
849849
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
850850
[semantic-release-url]: https://github.com/semantic-release/semantic-release
851851
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg

package.json

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "ethers-maths",
3-
"version": "5.0.0",
4-
"description": "➗ Useful ethers-based math libraries to ease the journey through off-chain fixed-point arithmetics",
2+
"name": "evm-maths",
3+
"version": "1.0.0",
4+
"description": "➗ Useful bigint math libraries to ease the journey through off-chain fixed-point arithmetics",
55
"main": "lib/index.js",
66
"files": [
77
"lib/*"
@@ -17,7 +17,7 @@
1717
},
1818
"repository": {
1919
"type": "git",
20-
"url": "git+https://github.com/Rubilmax/ethers-maths.git"
20+
"url": "git+https://github.com/Rubilmax/evm-maths.git"
2121
},
2222
"license": "MIT",
2323
"author": {
@@ -30,35 +30,31 @@
3030
},
3131
"keywords": [
3232
"ethers",
33+
"bigint",
34+
"bignumber",
3335
"multicall",
3436
"rpc",
3537
"call",
3638
"evm",
3739
"smart contract"
3840
],
3941
"bugs": {
40-
"url": "https://github.com/Rubilmax/ethers-maths/issues"
41-
},
42-
"homepage": "https://github.com/Rubilmax/ethers-maths#readme",
43-
"dependencies": {
44-
"ethers": "^6.0.0"
42+
"url": "https://github.com/Rubilmax/evm-maths/issues"
4543
},
44+
"homepage": "https://github.com/Rubilmax/evm-maths#readme",
4645
"devDependencies": {
4746
"@jest/globals": "^29.7.0",
4847
"@trivago/prettier-plugin-sort-imports": "4.1.1",
49-
"@types/jest": "^29.5.5",
48+
"@types/jest": "^29.5.10",
5049
"commitizen": "^4.3.0",
5150
"conventional-changelog-conventionalcommits": "^5.0.0",
5251
"cz-conventional-changelog": "^3.3.0",
5352
"husky": "^8.0.3",
5453
"jest": "^29.7.0",
5554
"lint-staged": "^14.0.1",
56-
"prettier": "^3.0.3",
55+
"prettier": "^3.1.0",
5756
"ts-jest": "^29.1.1",
58-
"typescript": "^5.2.2"
59-
},
60-
"peerDependencies": {
61-
"ethers": "^6.0.0"
57+
"typescript": "^5.3.2"
6258
},
6359
"config": {
6460
"commitizen": {

src/comp.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { BigNumberish, toBigInt } from "ethers";
21
import { WAD, WAD_SQUARED } from "./constants";
32

4-
export const compMul = (x: BigNumberish, other: BigNumberish) => {
5-
return (toBigInt(x) * toBigInt(other)) / WAD;
3+
export const compMul = (x: bigint, other: bigint) => {
4+
return (x * other) / WAD;
65
};
76

8-
export const compDiv = (x: BigNumberish, other: BigNumberish) => {
9-
return (toBigInt(x) * WAD_SQUARED) / toBigInt(other) / WAD;
7+
export const compDiv = (x: bigint, other: bigint) => {
8+
return (x * WAD_SQUARED) / other / WAD;
109
};

src/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { pow10 } from "./utils";
22

3-
export const PERCENT = pow10(4);
4-
export const WAD = pow10(18);
5-
export const RAY = pow10(27);
3+
export const PERCENT = pow10(4n);
4+
export const WAD = pow10(18n);
5+
export const RAY = pow10(27n);
66
export const WAD_SQUARED = WAD ** 2n;
77

88
export const HALF_PERCENT = PERCENT / 2n;

src/format.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
import { BigNumberish, formatUnits, toBigInt } from "ethers";
21
import { pow10 } from "./utils";
32

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

7-
let dotIndex = formatted.indexOf(".");
8-
if (dotIndex < 0) dotIndex = formatted.length;
7+
if (decimals === 0) return x.toString();
98

10-
decimals = formatted.length - 1 - dotIndex;
11-
digits ??= decimals;
9+
let negative = "";
10+
if (x < 0n) {
11+
negative = "-";
12+
x *= -1n;
13+
}
14+
15+
const abs = x.toString().padStart(decimals + 1, "0");
16+
17+
const length = abs.length;
18+
const dotIndex = length - decimals;
1219

13-
return digits < decimals
14-
? formatted.slice(0, dotIndex + (digits > 0 ? digits + 1 : 0))
15-
: formatted + "0".repeat(digits - decimals);
20+
let full = negative + abs.substring(0, dotIndex);
21+
if (digits > 0) full += "." + abs.substring(dotIndex, dotIndex + digits).padEnd(digits, "0");
22+
23+
return full;
1624
};
1725

18-
export const toFloat = (x: BigNumberish, decimals?: number) => {
26+
export const toFloat = (x: bigint, decimals?: number) => {
1927
return parseFloat(format(x, decimals));
2028
};
2129

22-
export const toDecimals = (x: BigNumberish, decimals: number, scaleDecimals: number) => {
23-
x = toBigInt(x);
24-
30+
export const toDecimals = (x: bigint, decimals: number, scaleDecimals: number) => {
2531
if (decimals <= scaleDecimals) {
26-
const ratio = pow10(scaleDecimals - decimals);
32+
const ratio = pow10(BigInt(Math.floor(scaleDecimals - decimals)));
2733

2834
return (x + ratio / 2n) / ratio;
2935
}
3036

31-
return x * pow10(decimals - scaleDecimals);
37+
return x * pow10(BigInt(Math.floor(decimals - scaleDecimals)));
3238
};

0 commit comments

Comments
 (0)