Skip to content

Commit ad89d52

Browse files
committed
feat(lib): extract utils to scale-specific domains
BREAKING CHANGE: function names changes
1 parent 7b50d72 commit ad89d52

8 files changed

Lines changed: 424 additions & 113 deletions

File tree

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ BigInt.from(1).wadMul(WAD); // 1
3535
BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
3636
```
3737

38+
If you choose to avoid prototype pollution, you can always import specific utilities:
39+
40+
```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";
44+
```
45+
3846
---
3947

4048
## Book
@@ -125,7 +133,7 @@ Returns whether the BigNumber is approximately close to the given BigNumber, wit
125133

126134
```typescript
127135
// only if you want to avoid BigNumber prototype pollution
128-
import { approxEqAbs } from "ethers-maths/utils";
136+
import { approxEqAbs } from "ethers-maths/lib/utils";
129137

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

140148
```typescript
141149
// only if you want to avoid BigInt prototype pollution
142-
import { min } from "ethers-maths/utils";
150+
import { min } from "ethers-maths/lib/utils";
143151

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

154162
```typescript
155163
// only if you want to avoid BigInt prototype pollution
156-
import { max } from "ethers-maths/utils";
164+
import { max } from "ethers-maths/lib/utils";
157165

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

168176
```typescript
169177
// only if you want to avoid BigInt prototype pollution
170-
import { sum } from "ethers-maths/utils";
178+
import { sum } from "ethers-maths/lib/utils";
171179

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

205213
```typescript
206214
// only if you want to avoid BigInt prototype pollution
207-
import { pow10 } from "ethers-maths/utils";
215+
import { pow10 } from "ethers-maths/lib/utils";
208216

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

218226
```typescript
219227
// only if you want to avoid BigInt prototype pollution
220-
import { mulDivHalfUp } from "ethers-maths/utils";
228+
import { mulDivHalfUp } from "ethers-maths/lib/utils";
221229

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

231239
```typescript
232240
// only if you want to avoid BigInt prototype pollution
233-
import { mulDivUp } from "ethers-maths/utils";
241+
import { mulDivUp } from "ethers-maths/lib/utils";
234242

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

244252
```typescript
245253
// only if you want to avoid BigInt prototype pollution
246-
import { mulDivDown } from "ethers-maths/utils";
254+
import { mulDivDown } from "ethers-maths/lib/utils";
247255

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

263271
```typescript
264272
// only if you want to avoid BigInt prototype pollution
265-
import { WAD } from "ethers-maths/constants";
273+
import { WAD } from "ethers-maths/lib/constants";
266274

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

278286
```typescript
279287
// only if you want to avoid BigInt prototype pollution
280-
import { RAY } from "ethers-maths/constants";
288+
import { RAY } from "ethers-maths/lib/constants";
281289

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

293301
```typescript
294302
// only if you want to avoid BigInt prototype pollution
295-
import { PERCENT } from "ethers-maths/constants";
303+
import { PERCENT } from "ethers-maths/lib/constants";
296304

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

306314
```typescript
307315
// only if you want to avoid BigInt prototype pollution
308-
import { HALF_WAD } from "ethers-maths/constants";
316+
import { HALF_WAD } from "ethers-maths/lib/constants";
309317

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

319327
```typescript
320328
// only if you want to avoid BigInt prototype pollution
321-
import { HALF_RAY } from "ethers-maths/constants";
329+
import { HALF_RAY } from "ethers-maths/lib/constants";
322330

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

334342
```typescript
335343
// only if you want to avoid BigInt prototype pollution
336-
import { HALF_PERCENT } from "ethers-maths/constants";
344+
import { HALF_PERCENT } from "ethers-maths/lib/constants";
337345

338346
// Returns a 1 followed by 4 zeros: 10000
339347
HALF_PERCENT;

src/comp.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BigNumberish, toBigInt } from "ethers";
2+
import { WAD, WAD_SQUARED } from "./constants";
3+
4+
export const compMul = (x: BigNumberish, other: BigNumberish) => {
5+
return (toBigInt(x) * toBigInt(other)) / WAD;
6+
};
7+
8+
export const compDiv = (x: BigNumberish, other: BigNumberish) => {
9+
return (toBigInt(x) * WAD_SQUARED) / toBigInt(other) / WAD;
10+
};

src/format.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { BigNumberish, formatUnits, toBigInt } from "ethers";
2+
import { pow10 } from "./utils";
3+
4+
export const format = (x: BigNumberish, decimals?: number, digits?: number) => {
5+
const formatted = formatUnits(x, decimals);
6+
7+
let dotIndex = formatted.indexOf(".");
8+
if (dotIndex < 0) dotIndex = formatted.length;
9+
10+
decimals = formatted.length - 1 - dotIndex;
11+
digits ??= decimals;
12+
13+
return digits < decimals
14+
? formatted.slice(0, dotIndex + (digits > 0 ? digits + 1 : 0))
15+
: formatted + "0".repeat(digits - decimals);
16+
};
17+
18+
export const toFloat = (x: BigNumberish, decimals?: number) => {
19+
return parseFloat(format(x, decimals));
20+
};
21+
22+
export const toDecimals = (x: BigNumberish, decimals: number, scaleDecimals: number) => {
23+
x = toBigInt(x);
24+
25+
if (decimals <= scaleDecimals) {
26+
const ratio = pow10(scaleDecimals - decimals);
27+
28+
return (x + ratio / 2n) / ratio;
29+
}
30+
31+
return x * pow10(decimals - scaleDecimals);
32+
};

0 commit comments

Comments
 (0)