Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 3dc0a29

Browse files
committed
use Decimal instead of BigNumber
1 parent d388a3d commit 3dc0a29

File tree

4 files changed

+17
-471
lines changed

4 files changed

+17
-471
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"body-parser": "^1.19.0",
2323
"cors": "^2.8.5",
2424
"chalk": "^4.1.0",
25+
"decimal.js": "^10.2.1",
2526
"dotenv": "^8.2.0",
26-
"ethers": "^5.1.0",
2727
"express": "^4.17.1",
2828
"knex": "^0.21.1",
2929
"objection": "^2.1.3",

src/helpers/numbers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { BigNumber } from 'ethers'
1+
import { Decimal } from 'decimal.js'
22

3-
export type BigNumberish = string | number | BigNumber
4-
5-
export const bn = (x: BigNumberish): BigNumber => BigNumber.from(x)
3+
export const decimal = (x: string | number): Decimal => new Decimal(x)

src/lib/MigratedValueCalculator.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BigNumber } from 'ethers'
1+
import { Decimal } from 'decimal.js'
22

3-
import { bn } from '../helpers/numbers'
3+
import { decimal } from '../helpers/numbers'
44
import Logger from '../helpers/logger'
55

66
import Asset from '../models/Asset'
@@ -25,7 +25,7 @@ class MigratedValueCalculator {
2525
const balances = await organization.balances()
2626
const values = await Promise.all(balances.map(balance => this._calcOrganizationBalanceValue(balance, createdAt)))
2727

28-
const valueUSD = values.reduce((total, value) => total.add(value), bn(0))
28+
const valueUSD = values.reduce((total, value) => total.add(value), decimal(0))
2929
const ant = await this._getAntPrice(createdAt)
3030
const valueANT = valueUSD.mul(ant)
3131
const computedAt = new Date().getTime()
@@ -36,21 +36,21 @@ class MigratedValueCalculator {
3636
}
3737
}
3838

39-
private async _calcOrganizationBalanceValue(balance: OrganizationBalance, createdAt: string): Promise<BigNumber> {
39+
private async _calcOrganizationBalanceValue(balance: OrganizationBalance, createdAt: string): Promise<Decimal> {
4040
const asset = await Asset.findById(balance.assetId)
4141
const price = await this._getAssetPrice(asset!.address, createdAt)
42-
const value = bn(balance.amount).mul(price)
42+
const value = decimal(balance.amount).mul(price)
4343
await balance.$query().update({ price: price.toString(), value: value.toString() })
4444
return value
4545
}
4646

47-
async _getAntPrice(createdAt: string): Promise<BigNumber> {
47+
async _getAntPrice(createdAt: string): Promise<Decimal> {
4848
return this._getAssetPrice(ANT_ADDRESS, createdAt)
4949
}
5050

51-
async _getAssetPrice(address: string, createdAt: string): Promise<BigNumber> {
51+
async _getAssetPrice(address: string, createdAt: string): Promise<Decimal> {
5252
// TODO: fetch price from coin-gecko
53-
return bn(0)
53+
return decimal(0)
5454
}
5555
}
5656

0 commit comments

Comments
 (0)