derive.{balances, contracts, staking, session} returns Codec (#953)

* derive.{balances, contracts, staking, session} returns Codec

* Json -> StructAny (as per VectorAny)

* multi returns VectorAny

* VectorAny casts

* Bump common
This commit is contained in:
Jaco Greeff
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 069fddee64
commit 773b8654eb
30 changed files with 265 additions and 352 deletions
+19 -19
View File
@@ -5,46 +5,45 @@
import { combineLatest, of, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@plugnet/api/types';
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Option, VestingSchedule } from '@plugnet/types';
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Index, StructAny, Option, VestingSchedule } from '@plugnet/types';
import { bnMax } from '@plugnet/util';
import { idAndIndex } from '../accounts/idAndIndex';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
type Result = [AccountId | undefined, BlockNumber | undefined, [Balance?, Balance?, Array<BalanceLock>?, Option<VestingSchedule>?]];
type Result = [AccountId | undefined, BlockNumber | undefined, [Balance?, Balance?, Array<BalanceLock>?, Option<VestingSchedule>?, Index?]];
const EMPTY_ACCOUNT = new AccountId();
function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = new BlockNumber(0), [freeBalance = new Balance(0), reservedBalance = new Balance(0), locks = [], vesting = new Option<VestingSchedule>(VestingSchedule, null)]]: Result): DerivedBalances {
function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = new BlockNumber(0), [freeBalance = new Balance(0), reservedBalance = new Balance(0), locks = [], vesting = new Option<VestingSchedule>(VestingSchedule, null), accountNonce = new Index(0)]]: Result): DerivedBalances {
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 = totals[0] ? bnMax(...totals.map(({ amount }) => amount)) as Balance : new Balance(0);
lockedBalance = totals[0]
? bnMax(...totals.map(({ amount }) => amount)) as Balance
: new Balance(0);
}
const { offset, perBlock } = vesting.unwrapOr({
offset: new Balance(0),
perBlock: new Balance(0)
} as VestingSchedule);
const { offset, perBlock } = vesting.unwrapOr(new VestingSchedule());
const vestedBalance = new Balance(perBlock.mul(bestNumber).add(freeBalance.sub(offset)));
const availableBalance = new Balance(vestedBalance.sub(lockedBalance));
return {
return new StructAny({
accountId,
freeBalance,
reservedBalance,
votingBalance: new Balance(
freeBalance.add(reservedBalance)
),
accountNonce,
availableBalance,
freeBalance,
lockedBalance,
vestedBalance: vestedBalance.lt(new Balance(0)) ? new Balance(0) : vestedBalance
};
reservedBalance,
vestedBalance: vestedBalance.lt(new Balance(0))
? new Balance(0)
: vestedBalance,
votingBalance: new Balance(freeBalance.add(reservedBalance))
}) as DerivedBalances;
}
/**
@@ -75,10 +74,11 @@ export function all (api: ApiInterface$Rx) {
[api.query.balances.freeBalance, accountId],
[api.query.balances.reservedBalance, accountId],
[api.query.balances.locks, accountId],
[api.query.balances.vesting, accountId]
[api.query.balances.vesting, accountId],
[api.query.system.accountNonce, accountId]
])
])
: of([undefined, undefined, [undefined, undefined, undefined, undefined]])
: of([undefined, undefined, [undefined, undefined, undefined, undefined, undefined]])
) as any as Observable<Result>
),
map(calcBalances),