Skip to content

Commit

Permalink
Merge pull request #3 from merofinance/bigint
Browse files Browse the repository at this point in the history
馃殌 Upgrade to bigint from BigNumber
  • Loading branch information
chase-manning committed Aug 23, 2023
2 parents 4cd48cb + 368ac3c commit 57b04e3
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 187 deletions.
9 changes: 4 additions & 5 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: "馃悰 Bug Report"
name: '馃悰 Bug Report'
about: Report a reproducible bug or regression.
title: ''
labels: bug
assignees: ''

---

## Current Behavior
Expand All @@ -17,9 +16,9 @@ assignees: ''

## Steps to Reproduce the Problem

1.
2.
3.
1.
2.
3.

## Environment

Expand Down
4 changes: 3 additions & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ about: Suggest an amazing new idea for this project
title: ''
labels: enhancement
assignees: ''

---

## Feature Request

**Is your feature request related to a problem? Please describe.**

<!-- A clear and concise description of what the problem is. Ex. I have an issue when [...] -->

**Describe the solution you'd like**

<!-- A clear and concise description of what you want to happen. Add any considered drawbacks. -->

**Describe alternatives you've considered**

<!-- A clear and concise description of any alternative solutions or features you've considered. -->

## Are you willing to resolve this issue by submitting a Pull Request?
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v2
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: lts/*
node-version: 16
- name: Install dependencies
run: yarn
- name: Test
Expand Down
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ on: [pull_request]

jobs:
build:
name: Build
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- name: Checkout
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
node-version: 16
- run: yarn
- run: yarn run build --if-present
- run: yarn run build
- run: yarn test
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ console.log(scaledNumber.mul(10).toString());

```ts
import { ScaledNumber } from 'scaled-number';
import { BigNumber } from "ethers";
import { getTokenDecimals, getContract, getPrice } from "helpers";

export interface Pool {
Expand Down Expand Up @@ -103,18 +102,16 @@ export const logPoolInfo = async (pool: Pool): void => {

## Creating Scaled Number

### From BigNumber
### From biging

```ts
new ScaledNumber(bigNumber: BigNumber, decimals?: number);
new ScaledNumber(bigInt: bigint, decimals?: number);
```

```ts
import { ScaledNumber } from 'scaled-number';
import { BigNumber } from 'ethers';

const bigNumber = BigNumber.from(123);
const scaledNumber = new ScaledNumber(bigNumber);
const scaledNumber = new ScaledNumber(BigInt(123));
```

### From Unscaled
Expand Down Expand Up @@ -349,7 +346,7 @@ console.log(sn.toPercent()); // 12.34%
### Value

```ts
value: BigNumber;
value: bigint;
```

```ts
Expand Down
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scaled-number",
"version": "1.0.1",
"version": "2.0.0",
"description": "A class for managing large numbers with a decimal scale, useful for web3 development",
"main": "./lib/src/index.js",
"files": [
Expand Down Expand Up @@ -33,9 +33,6 @@
"integer",
"bigint",
"big integer",
"bignumber",
"big-number",
"bignumbers",
"arithmetic",
"operations"
],
Expand Down Expand Up @@ -69,8 +66,5 @@
"branches": [
"main"
]
},
"dependencies": {
"@ethersproject/bignumber": "^5.5.0"
}
}
59 changes: 28 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { BigNumber } from '@ethersproject/bignumber';
import {
bigNumberToString,
bigIntToString,
formatCrypto,
formatCurrency,
formatPercent,
numberToCompactCurrency,
stringToBigNumber,
stringToBigInt,
} from './numeric';

export interface PlainScaledNumber {
Expand All @@ -14,25 +13,25 @@ export interface PlainScaledNumber {
}

export class ScaledNumber {
private _value: BigNumber;
private _value: bigint;

private _decimals: number;

constructor(value: BigNumber | null = BigNumber.from(0), decimals = 18) {
constructor(value: bigint | null = BigInt(0), decimals = 18) {
this._decimals = decimals;
this._value = value || BigNumber.from(0);
this._value = value || BigInt(0);
}

static fromUnscaled(value: number | string = 0, decimals = 18): ScaledNumber {
return new ScaledNumber(
stringToBigNumber(value.toString() || '0', decimals),
stringToBigInt(value.toString() || '0', decimals),
decimals
);
}

static fromPlain(value: PlainScaledNumber): ScaledNumber {
if (!value) return new ScaledNumber();
return new ScaledNumber(BigNumber.from(value.value), value.decimals);
return new ScaledNumber(BigInt(value.value), value.decimals);
}

static isValid(value: number | string, decimals = 18): boolean {
Expand All @@ -44,7 +43,7 @@ export class ScaledNumber {
}
}

get value(): BigNumber {
get value(): bigint {
return this._value;
}

Expand All @@ -53,7 +52,7 @@ export class ScaledNumber {
}

private scale = (decimals: number) => {
return BigNumber.from(10).pow(decimals);
return BigInt(10) ** BigInt(decimals);
};

toPlain = (): PlainScaledNumber => {
Expand All @@ -63,79 +62,77 @@ export class ScaledNumber {
};
};

isZero = (): boolean => this.value.isZero();
isZero = (): boolean => this.value === BigInt(0);

isNegative = (): boolean => this.value.isNegative();
isNegative = (): boolean => this.value < BigInt(0);

standardizeDecimals(other: ScaledNumber): ScaledNumber {
if (this.decimals === other.decimals) return other;
if (this.decimals >= other.decimals) {
return new ScaledNumber(
other.value.mul(
BigNumber.from(10).pow(this._decimals - other.decimals)
),
other.value * BigInt(10) ** BigInt(this._decimals - other.decimals),
this.decimals
);
}
return new ScaledNumber(
other.value.div(BigNumber.from(10).pow(other.decimals - this._decimals)),
other.value / BigInt(10) ** BigInt(other.decimals - this._decimals),
this.decimals
);
}

add(other: ScaledNumber): ScaledNumber {
other = this.standardizeDecimals(other);
return new ScaledNumber(this.value.add(other.value), this.decimals);
return new ScaledNumber(this.value + other.value, this.decimals);
}

sub(other: ScaledNumber): ScaledNumber {
other = this.standardizeDecimals(other);
return new ScaledNumber(this.value.sub(other.value), this.decimals);
return new ScaledNumber(this.value - other.value, this.decimals);
}

eq(other: ScaledNumber): boolean {
return this.value.eq(other.value) && this.decimals === other.decimals;
return this.value === other.value && this.decimals === other.decimals;
}

gt(other: ScaledNumber): boolean {
other = this.standardizeDecimals(other);
return this.value.gt(other.value);
return this.value > other.value;
}

gte(other: ScaledNumber): boolean {
other = this.standardizeDecimals(other);
return this.value.gte(other.value);
return this.value >= other.value;
}

lt(other: ScaledNumber): boolean {
other = this.standardizeDecimals(other);
return this.value.lt(other.value);
return this.value < other.value;
}

lte(other: ScaledNumber): boolean {
other = this.standardizeDecimals(other);
return this.value.lte(other.value);
return this.value <= other.value;
}

max(other: ScaledNumber): ScaledNumber {
other = this.standardizeDecimals(other);
return this.value.gt(other.value) ? this : other;
return this.value > other.value ? this : other;
}

min(other: ScaledNumber): ScaledNumber {
other = this.standardizeDecimals(other);
return this.value.lt(other.value) ? this : other;
return this.value < other.value ? this : other;
}

mul(value: number | string | ScaledNumber): ScaledNumber {
const scaledValue =
value instanceof ScaledNumber
? value.value
: stringToBigNumber(value.toString(), this.decimals);
: stringToBigInt(value.toString(), this.decimals);
const scaledDecimals =
value instanceof ScaledNumber ? value.decimals : this.decimals;
return new ScaledNumber(
this.value.mul(scaledValue).div(this.scale(scaledDecimals)),
(this.value * scaledValue) / this.scale(scaledDecimals),
this.decimals
);
}
Expand All @@ -144,17 +141,17 @@ export class ScaledNumber {
const scaledValue =
value instanceof ScaledNumber
? value.value
: stringToBigNumber(value.toString(), this.decimals);
if (scaledValue.isZero()) return new ScaledNumber();
: stringToBigInt(value.toString(), this.decimals);
if (scaledValue === BigInt(0)) return new ScaledNumber();
const scaledDecimals =
value instanceof ScaledNumber ? value.decimals : this.decimals;
return new ScaledNumber(
this.value.mul(this.scale(scaledDecimals)).div(scaledValue),
(this.value * this.scale(scaledDecimals)) / scaledValue,
this.decimals
);
}

toString = (): string => bigNumberToString(this._value, this._decimals);
toString = (): string => bigIntToString(this._value, this._decimals);

toNumber = (): number => Number(this.toString());

Expand Down
Loading

0 comments on commit 57b04e3

Please sign in to comment.