Replace votingBalance -> derive.balances.all (#883)
* Updated with derive.balances.all * add new keys to votingBalance * remove multi test * vestedBalance > 0
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { combineLatest, of, Observable } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Option, VestingSchedule } from '@polkadot/types';
|
||||
import { bnMax } from '@polkadot/util';
|
||||
|
||||
import { idAndIndex } from '../accounts/idAndIndex';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
const EMPTY_ACCOUNT = new AccountId();
|
||||
|
||||
export function all (api: ApiInterface$Rx) {
|
||||
return (address: AccountIndex | AccountId | Address | string): Observable<DerivedBalances> => {
|
||||
return idAndIndex(api)(address).pipe(
|
||||
switchMap(([accountId]) =>
|
||||
(accountId
|
||||
? combineLatest([
|
||||
of(accountId),
|
||||
api.query.chain.bestNumber(),
|
||||
api.query.balances.freeBalance(accountId),
|
||||
api.query.balances.reservedBalance(accountId),
|
||||
api.query.balances.locks(accountId),
|
||||
api.query.balances.vesting(accountId)
|
||||
])
|
||||
: of([undefined, undefined, undefined, undefined, undefined, undefined])
|
||||
) as Observable<[AccountId?, BlockNumber?, Balance?, Balance?, Array<BalanceLock>?, Option<VestingSchedule>?]>
|
||||
),
|
||||
map(([accountId = EMPTY_ACCOUNT, bestNumber = new BlockNumber(0), freeBalance = new Balance(0), reservedBalance = new Balance(0), locks = [], vesting = new Option<VestingSchedule>(VestingSchedule, null)]) => {
|
||||
let lockedBalance = new Balance(0);
|
||||
|
||||
if (Array.isArray(locks)) {
|
||||
// only get the locks that are valid until passed the current block
|
||||
const totals = locks.filter((value) => bestNumber && value.until.gt(bestNumber));
|
||||
// get the maximum of the locks according to https://github.com/paritytech/substrate/blob/master/srml/balances/src/lib.rs#L699
|
||||
lockedBalance = bnMax(...totals.map(({ amount }) => amount)) as Balance;
|
||||
}
|
||||
|
||||
const { offset, perBlock } = vesting.unwrapOr({
|
||||
offset: new Balance(0),
|
||||
perBlock: new Balance(0)
|
||||
} as VestingSchedule);
|
||||
|
||||
const vestedBalance = new Balance(perBlock.mul(bestNumber).add(freeBalance.sub(offset)));
|
||||
|
||||
// const availableBalance = lockedBalance.eq(new BlockNumber('0xffffffffffffffffffffffffffffffff')) ? new Balance(0) : freeBalance && new Balance(freeBalance.sub(lockedBalance));
|
||||
const availableBalance = new Balance(vestedBalance.sub(lockedBalance));
|
||||
|
||||
return {
|
||||
accountId,
|
||||
freeBalance,
|
||||
reservedBalance,
|
||||
votingBalance: new Balance(
|
||||
freeBalance.add(reservedBalance)
|
||||
),
|
||||
availableBalance,
|
||||
lockedBalance,
|
||||
vestedBalance: vestedBalance.lt(new Balance(0)) ? new Balance(0) : vestedBalance
|
||||
};
|
||||
}),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './all';
|
||||
export * from './fees';
|
||||
export * from './validatingBalance';
|
||||
export * from './validatingBalances';
|
||||
|
||||
@@ -35,7 +35,10 @@ export function votingBalance (api: ApiInterface$Rx) {
|
||||
stakingBalance: new Balance(0),
|
||||
votingBalance: new Balance(
|
||||
freeBalance.add(reservedBalance)
|
||||
)
|
||||
),
|
||||
availableBalance: new Balance(0),
|
||||
lockedBalance: new Balance(0),
|
||||
vestedBalance: new Balance(0)
|
||||
};
|
||||
}),
|
||||
drr()
|
||||
|
||||
@@ -39,6 +39,7 @@ describe('derive', () => {
|
||||
testFunction(api)('accounts', 'indexes', []);
|
||||
testFunction(api)('accounts', 'indexToId', []);
|
||||
|
||||
testFunction(api)('balances', 'all', []);
|
||||
testFunction(api)('balances', 'fees', []);
|
||||
testFunction(api)('balances', 'validatingBalance', []);
|
||||
testFunction(api)('balances', 'validatingBalances', []);
|
||||
|
||||
@@ -8,11 +8,11 @@ import { AccountId, Balance, Vote } from '@polkadot/types';
|
||||
export type DerivedBalances = {
|
||||
accountId: AccountId,
|
||||
freeBalance: Balance,
|
||||
nominatedBalance: Balance,
|
||||
lockedBalance: Balance,
|
||||
availableBalance: Balance,
|
||||
reservedBalance: Balance,
|
||||
votingBalance: Balance,
|
||||
stakingBalance: Balance,
|
||||
nominators?: Array<DerivedBalances>
|
||||
vestedBalance: Balance
|
||||
};
|
||||
|
||||
export type DerivedFees = {
|
||||
|
||||
Reference in New Issue
Block a user