Update Darwinia Crab IndividualExposure types (#3719)

* Update Darwininia Crab IndividualExposure types

* own comment

* Charts
This commit is contained in:
Jaco Greeff 2020-09-19 09:56:25 +02:00 committed by GitHub
parent e893eb1a8d
commit 158ab2ee19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 8 deletions

View File

@ -96,8 +96,9 @@ export default {
who: 'AccountId',
ring_balance: 'Compact<Balance>',
kton_balance: 'Compact<Balance>',
power: 'Power',
value: 'Compact<Balance>'
power: 'Power'
// not in https://github.com/darwinia-network/darwinia-common/blob/master/frame/staking/src/lib.rs
// value: 'Compact<Balance>'
},
ElectionResultT: {
elected_stashes: 'Vec<AccountId>',

View File

@ -24,8 +24,9 @@ function extractStake (exposures: DeriveOwnExposure[] = [], divisor: BN): ChartI
let total = 0;
exposures.forEach(({ clipped, era, exposure }): void => {
const cli = balanceToNumber(clipped.total.unwrap(), divisor);
const exp = balanceToNumber(exposure.total.unwrap(), divisor);
// Darwinia Crab doesn't have the total field
const cli = balanceToNumber(clipped.total?.unwrap(), divisor);
const exp = balanceToNumber(exposure.total?.unwrap(), divisor);
total += cli;

View File

@ -2,7 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { BN_ZERO, isBn, isFunction } from '@polkadot/util';
export function balanceToNumber (amount: BN, divisor: BN): number {
return amount.muln(1000).div(divisor).toNumber() / 1000;
interface ToBN {
toBn: () => BN;
}
export function balanceToNumber (amount: BN | ToBN = BN_ZERO, divisor: BN): number {
const value = isBn(amount)
? amount
: isFunction(amount.toBn)
? amount.toBn()
: BN_ZERO;
return value.muln(1000).div(divisor).toNumber() / 1000;
}

View File

@ -65,8 +65,9 @@ function sortValidators (list: ValidatorInfo[]): ValidatorInfo[] {
function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { info }: DeriveStakingElected | DeriveStakingWaiting, favorites: string[], perValidatorReward: BN, isElected: boolean): [ValidatorInfo[], string[]] {
const nominators: Record<string, boolean> = {};
const list = info.map(({ accountId, exposure, stakingLedger, validatorPrefs }): ValidatorInfo => {
let bondOwn = exposure.own.unwrap();
let bondTotal = exposure.total.unwrap();
// some overrides (e.g. Darwinia Crab) does not have the own field in Exposure
let bondOwn = exposure.own?.unwrap() || BN_ZERO;
let bondTotal = exposure.total?.unwrap() || BN_ZERO;
const skipRewards = bondTotal.isZero();
if (bondTotal.isZero()) {