Skip to content

Commit

Permalink
feat(refactor): Use stringToBigNumber from @w3ux/utils (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat authored Apr 17, 2024
1 parent ebcc9c6 commit 4425227
Show file tree
Hide file tree
Showing 4 changed files with 696 additions and 944 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"@substrate/connect": "0.7.35",
"@w3ux/extension-assets": "0.2.6",
"@w3ux/hooks": "^0.0.3",
"@w3ux/react-connect-kit": "^0.1.15",
"@w3ux/react-connect-kit": "^0.1.16",
"@w3ux/react-odometer": "^0.0.3",
"@w3ux/react-polkicon": "^0.0.2",
"@w3ux/utils": "^0.1.0",
"@w3ux/react-polkicon": "^0.0.3",
"@w3ux/utils": "0.1.1",
"@w3ux/validator-assets": "^0.0.4",
"@zondax/ledger-substrate": "^0.41.4",
"bignumber.js": "^9.1.2",
Expand Down
20 changes: 8 additions & 12 deletions src/controllers/BalancesController/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { rmCommas } from '@w3ux/utils';
import { rmCommas, stringToBigNumber } from '@w3ux/utils';
import BigNumber from 'bignumber.js';
import type { AnyApi, MaybeAddress } from 'types';
import type {
Expand Down Expand Up @@ -171,11 +171,11 @@ export class BalancesController {

this.ledgers[address] = {
stash: stash.toString(),
active: this.stringToBigNumber(active.toString()),
total: this.stringToBigNumber(total.toString()),
active: stringToBigNumber(active.toString()),
total: stringToBigNumber(total.toString()),
unlocking: unlocking.toHuman().map(({ era, value }: UnlockChunkRaw) => ({
era: Number(rmCommas(era)),
value: this.stringToBigNumber(value),
value: stringToBigNumber(value),
})),
};
};
Expand All @@ -189,16 +189,16 @@ export class BalancesController {
this.balances[address] = {
nonce: nonce.toNumber(),
balance: {
free: this.stringToBigNumber(accountData.free.toString()),
reserved: this.stringToBigNumber(accountData.reserved.toString()),
frozen: this.stringToBigNumber(accountData.frozen.toString()),
free: stringToBigNumber(accountData.free.toString()),
reserved: stringToBigNumber(accountData.reserved.toString()),
frozen: stringToBigNumber(accountData.frozen.toString()),
},
locks: locksResult
.toHuman()
.map((lock: { id: string; amount: string }) => ({
...lock,
id: lock.id.trim(),
amount: this.stringToBigNumber(lock.amount),
amount: stringToBigNumber(lock.amount),
})),
};
};
Expand Down Expand Up @@ -338,10 +338,6 @@ export class BalancesController {
// Class helpers.
// ------------------------------------------------------

// Converts a balance string into a `BigNumber`.
static stringToBigNumber = (value: string): BigNumber =>
new BigNumber(rmCommas(value));

// Checks if event detailis a valid `new-account-balance` event. Note that `ledger` may not exist
// and therefore cannot be tested.
static isValidNewAccountBalanceEvent = (
Expand Down
90 changes: 39 additions & 51 deletions src/model/Api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import { SyncController } from 'controllers/SyncController';
import type { AnyApi, NetworkName } from 'types';
import { NetworkList, NetworksWithPagedRewards } from 'config/networks';
import { makeCancelable, rmCommas } from '@w3ux/utils';
import { makeCancelable, rmCommas, stringToBigNumber } from '@w3ux/utils';
import { WellKnownChain } from '@substrate/connect';
import type { BlockNumber } from '@polkadot/types/interfaces';
import type {
Expand Down Expand Up @@ -324,35 +324,35 @@ export class Api {
return {
consts: {
bondDuration: consts[0]
? this.stringToBigNumber(consts[0].toString())
? stringToBigNumber(consts[0].toString())
: this.FALLBACK.BONDING_DURATION,
maxNominations: consts[1]
? this.stringToBigNumber(consts[1].toString())
? stringToBigNumber(consts[1].toString())
: this.FALLBACK.MAX_NOMINATIONS,
sessionsPerEra: consts[2]
? this.stringToBigNumber(consts[2].toString())
? stringToBigNumber(consts[2].toString())
: this.FALLBACK.SESSIONS_PER_ERA,
maxElectingVoters: consts[3]
? this.stringToBigNumber(consts[3].toString())
? stringToBigNumber(consts[3].toString())
: this.FALLBACK.MAX_ELECTING_VOTERS,
expectedBlockTime: consts[4]
? this.stringToBigNumber(consts[4].toString())
? stringToBigNumber(consts[4].toString())
: this.FALLBACK.EXPECTED_BLOCK_TIME,
epochDuration: consts[5]
? this.stringToBigNumber(consts[5].toString())
? stringToBigNumber(consts[5].toString())
: this.FALLBACK.EPOCH_DURATION,
existentialDeposit: consts[6]
? this.stringToBigNumber(consts[6].toString())
? stringToBigNumber(consts[6].toString())
: new BigNumber(0),
historyDepth: consts[7]
? this.stringToBigNumber(consts[7].toString())
? stringToBigNumber(consts[7].toString())
: new BigNumber(0),
fastUnstakeDeposit: consts[8]
? this.stringToBigNumber(consts[8].toString())
? stringToBigNumber(consts[8].toString())
: new BigNumber(0),
poolsPalletId: consts[9] ? consts[9].toU8a() : new Uint8Array(0),
maxExposurePageSize: consts[10]
? this.stringToBigNumber(consts[10].toString())
? stringToBigNumber(consts[10].toString())
: NetworkList[this.network].maxExposurePageSize,
},
networkMetrics: {
Expand All @@ -366,36 +366,28 @@ export class Api {
},
activeEra,
poolsConfig: {
counterForPoolMembers: this.stringToBigNumber(
networkMetrics[5].toString()
),
counterForBondedPools: this.stringToBigNumber(
networkMetrics[6].toString()
),
counterForRewardPools: this.stringToBigNumber(
networkMetrics[7].toString()
),
lastPoolId: this.stringToBigNumber(networkMetrics[8].toString()),
counterForPoolMembers: stringToBigNumber(networkMetrics[5].toString()),
counterForBondedPools: stringToBigNumber(networkMetrics[6].toString()),
counterForRewardPools: stringToBigNumber(networkMetrics[7].toString()),
lastPoolId: stringToBigNumber(networkMetrics[8].toString()),
maxPoolMembers,
maxPoolMembersPerPool,
maxPools,
minCreateBond: this.stringToBigNumber(networkMetrics[12].toString()),
minJoinBond: this.stringToBigNumber(networkMetrics[13].toString()),
minCreateBond: stringToBigNumber(networkMetrics[12].toString()),
minJoinBond: stringToBigNumber(networkMetrics[13].toString()),
globalMaxCommission: Number(
String(networkMetrics[14]?.toHuman() || '100%').slice(0, -1)
),
},
stakingMetrics: {
totalNominators: this.stringToBigNumber(networkMetrics[15].toString()),
totalValidators: this.stringToBigNumber(networkMetrics[16].toString()),
maxValidatorsCount: this.stringToBigNumber(
networkMetrics[17].toString()
),
validatorCount: this.stringToBigNumber(networkMetrics[18].toString()),
lastReward: this.stringToBigNumber(networkMetrics[19].toString()),
lastTotalStake: this.stringToBigNumber(networkMetrics[20].toString()),
minNominatorBond: this.stringToBigNumber(networkMetrics[21].toString()),
totalStaked: this.stringToBigNumber(networkMetrics[22].toString()),
totalNominators: stringToBigNumber(networkMetrics[15].toString()),
totalValidators: stringToBigNumber(networkMetrics[16].toString()),
maxValidatorsCount: stringToBigNumber(networkMetrics[17].toString()),
validatorCount: stringToBigNumber(networkMetrics[18].toString()),
lastReward: stringToBigNumber(networkMetrics[19].toString()),
lastTotalStake: stringToBigNumber(networkMetrics[20].toString()),
minNominatorBond: stringToBigNumber(networkMetrics[21].toString()),
totalStaked: stringToBigNumber(networkMetrics[22].toString()),
},
};
};
Expand Down Expand Up @@ -500,15 +492,15 @@ export class Api {
: null;

const poolsConfig = {
counterForPoolMembers: this.stringToBigNumber(result[0].toString()),
counterForBondedPools: this.stringToBigNumber(result[1].toString()),
counterForRewardPools: this.stringToBigNumber(result[2].toString()),
lastPoolId: this.stringToBigNumber(result[3].toString()),
counterForPoolMembers: stringToBigNumber(result[0].toString()),
counterForBondedPools: stringToBigNumber(result[1].toString()),
counterForRewardPools: stringToBigNumber(result[2].toString()),
lastPoolId: stringToBigNumber(result[3].toString()),
maxPoolMembers,
maxPoolMembersPerPool,
maxPools,
minCreateBond: this.stringToBigNumber(result[7].toString()),
minJoinBond: this.stringToBigNumber(result[8].toString()),
minCreateBond: stringToBigNumber(result[7].toString()),
minJoinBond: stringToBigNumber(result[8].toString()),
globalMaxCommission: Number(
String(result[9]?.toHuman() || '100%').slice(0, -1)
),
Expand Down Expand Up @@ -549,14 +541,14 @@ export class Api {
],
(result) => {
const stakingMetrics = {
totalNominators: this.stringToBigNumber(result[0].toString()),
totalValidators: this.stringToBigNumber(result[1].toString()),
maxValidatorsCount: this.stringToBigNumber(result[2].toString()),
validatorCount: this.stringToBigNumber(result[3].toString()),
lastReward: this.stringToBigNumber(result[4].toString()),
lastTotalStake: this.stringToBigNumber(result[5].toString()),
minNominatorBond: this.stringToBigNumber(result[6].toString()),
totalStaked: this.stringToBigNumber(result[7].toString()),
totalNominators: stringToBigNumber(result[0].toString()),
totalValidators: stringToBigNumber(result[1].toString()),
maxValidatorsCount: stringToBigNumber(result[2].toString()),
validatorCount: stringToBigNumber(result[3].toString()),
lastReward: stringToBigNumber(result[4].toString()),
lastTotalStake: stringToBigNumber(result[5].toString()),
minNominatorBond: stringToBigNumber(result[6].toString()),
totalStaked: stringToBigNumber(result[7].toString()),
};

document.dispatchEvent(
Expand Down Expand Up @@ -598,10 +590,6 @@ export class Api {
this.#unsubs = {};
};

// Converts a balance string into a `BigNumber`.
stringToBigNumber = (value: string): BigNumber =>
new BigNumber(rmCommas(value));

// ------------------------------------------------------
// Disconnect.
// ------------------------------------------------------
Expand Down
Loading

0 comments on commit 4425227

Please sign in to comment.