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:
@@ -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),
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { DerivedFees } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { DerivedFees } from '../types';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { StructAny } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
@@ -32,13 +34,14 @@ export function fees (api: ApiInterface$Rx) {
|
||||
api.query.balances.transactionByteFee,
|
||||
api.query.balances.transferFee
|
||||
]) as any as Observable<[BN, BN, BN, BN, BN]>).pipe(
|
||||
map(([creationFee, existentialDeposit, transactionBaseFee, transactionByteFee, transferFee]) => ({
|
||||
creationFee,
|
||||
existentialDeposit,
|
||||
transactionBaseFee,
|
||||
transactionByteFee,
|
||||
transferFee
|
||||
})),
|
||||
map(([creationFee, existentialDeposit, transactionBaseFee, transactionByteFee, transferFee]) =>
|
||||
new StructAny({
|
||||
creationFee,
|
||||
existentialDeposit,
|
||||
transactionBaseFee,
|
||||
transactionByteFee,
|
||||
transferFee
|
||||
}) as DerivedFees),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
export * from './all';
|
||||
export * from './fees';
|
||||
export * from './validatingBalance';
|
||||
export * from './validatingBalances';
|
||||
export * from './votingBalance';
|
||||
export * from './votingBalances';
|
||||
export * from './votingBalancesNominatorsFor';
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, AccountIndex, Address, Balance } from '@plugnet/types';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votingBalance } from './votingBalance';
|
||||
import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor';
|
||||
|
||||
export function validatingBalance (api: ApiInterface$Rx) {
|
||||
return (address: AccountId | AccountIndex | Address | string): Observable<DerivedBalances> => {
|
||||
return combineLatest([
|
||||
votingBalance(api)(address),
|
||||
votingBalancesNominatorsFor(api)(address)
|
||||
]).pipe(
|
||||
map(([balance, nominators]) => {
|
||||
const nominatedBalance = nominators.reduce(
|
||||
(total: BN, nominatorBalance: DerivedBalances) =>
|
||||
total.add(nominatorBalance.votingBalance),
|
||||
new BN(0)
|
||||
);
|
||||
|
||||
return {
|
||||
...balance,
|
||||
nominators,
|
||||
nominatedBalance: new Balance(nominatedBalance),
|
||||
stakingBalance: new Balance(
|
||||
nominatedBalance.add(balance.votingBalance)
|
||||
)
|
||||
};
|
||||
}),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// 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, Observable, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, Address } from '@plugnet/types';
|
||||
|
||||
import { DerivedBalancesMap } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { validatingBalance } from './validatingBalance';
|
||||
|
||||
export function validatingBalances (api: ApiInterface$Rx) {
|
||||
return (accountIds: Array<AccountId | Address | string>): Observable<DerivedBalancesMap> => {
|
||||
return !accountIds || !accountIds.length
|
||||
? of({}).pipe(drr())
|
||||
: combineLatest(
|
||||
accountIds.map(validatingBalance(api))
|
||||
).pipe(
|
||||
map((result) => {
|
||||
return result.reduce((balances, balance) => {
|
||||
balances[balance.accountId.toString()] = balance;
|
||||
|
||||
return balances;
|
||||
}, {} as DerivedBalancesMap);
|
||||
}),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -2,46 +2,10 @@
|
||||
// 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 '@plugnet/api/types';
|
||||
import { AccountId, AccountIndex, Address, Balance } from '@plugnet/types';
|
||||
|
||||
import { idAndIndex } from '../accounts/idAndIndex';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32));
|
||||
import { all } from './all';
|
||||
|
||||
export function votingBalance (api: ApiInterface$Rx) {
|
||||
return (address: AccountIndex | AccountId | Address | string): Observable<DerivedBalances> => {
|
||||
return idAndIndex(api)(address).pipe(
|
||||
switchMap(([accountId]) =>
|
||||
(accountId
|
||||
? combineLatest([
|
||||
of(accountId),
|
||||
api.query.balances.freeBalance(accountId),
|
||||
api.query.balances.reservedBalance(accountId)
|
||||
])
|
||||
: of([undefined, undefined, undefined])
|
||||
) as Observable<[AccountId?, Balance?, Balance?]>
|
||||
),
|
||||
map(([accountId = EMPTY_ACCOUNT, freeBalance = new Balance(0), reservedBalance = new Balance(0)]) => {
|
||||
return {
|
||||
accountId,
|
||||
freeBalance,
|
||||
nominatedBalance: new Balance(0),
|
||||
reservedBalance,
|
||||
stakingBalance: new Balance(0),
|
||||
votingBalance: new Balance(
|
||||
freeBalance.add(reservedBalance)
|
||||
),
|
||||
availableBalance: new Balance(0),
|
||||
lockedBalance: new Balance(0),
|
||||
vestedBalance: new Balance(0)
|
||||
};
|
||||
}),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
return all(api);
|
||||
}
|
||||
|
||||
@@ -3,17 +3,25 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, AccountIndex, Address } from '@plugnet/types';
|
||||
import { AccountId, AccountIndex, Address, VectorAny } from '@plugnet/types';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votingBalance } from './votingBalance';
|
||||
|
||||
export function votingBalances (api: ApiInterface$Rx) {
|
||||
return (addresses?: Array<AccountId | AccountIndex | Address | string>): Observable<Array<DerivedBalances>> => {
|
||||
return !addresses || !addresses.length
|
||||
? of([]).pipe(drr())
|
||||
: combineLatest(addresses.map(votingBalance(api))).pipe(drr());
|
||||
return (addresses?: Array<AccountId | AccountIndex | Address | string>): Observable<VectorAny<DerivedBalances>> => {
|
||||
return (
|
||||
!addresses || !addresses.length
|
||||
? of([] as Array<DerivedBalances>)
|
||||
: combineLatest(addresses.map(votingBalance(api)))
|
||||
).pipe(
|
||||
map((balances) =>
|
||||
new VectorAny(...balances)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, AccountIndex, Address, Vector } from '@plugnet/types';
|
||||
import { AccountId, AccountIndex, Address, Vector, VectorAny } from '@plugnet/types';
|
||||
|
||||
import { idAndIndex } from '../accounts/idAndIndex';
|
||||
import { DerivedBalances } from '../types';
|
||||
@@ -13,7 +13,7 @@ import { drr } from '../util/drr';
|
||||
import { votingBalances } from './votingBalances';
|
||||
|
||||
export function votingBalancesNominatorsFor (api: ApiInterface$Rx) {
|
||||
return (address: AccountId | AccountIndex | Address | string): Observable<Array<DerivedBalances>> => {
|
||||
return (address: AccountId | AccountIndex | Address | string): Observable<VectorAny<DerivedBalances>> => {
|
||||
return idAndIndex(api)(address).pipe(
|
||||
switchMap(([accountId]) =>
|
||||
accountId
|
||||
|
||||
@@ -8,6 +8,7 @@ import { DerivedContractFees } from '../types';
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { StructAny } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
@@ -28,8 +29,8 @@ export function fees (api: ApiInterface$Rx) {
|
||||
api.query.contract.transferFee,
|
||||
api.query.contract.tombstoneDeposit
|
||||
]) as any as Observable<Array<BN>>).pipe(
|
||||
map(([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, transactionBaseFee, transactionByteFee, transferFee, tombstoneDeposit]) => {
|
||||
return {
|
||||
map(([callBaseFee, contractFee, createBaseFee, creationFee, rentByteFee, rentDepositOffset, transactionBaseFee, transactionByteFee, transferFee, tombstoneDeposit]) =>
|
||||
new StructAny({
|
||||
callBaseFee,
|
||||
contractFee,
|
||||
createBaseFee,
|
||||
@@ -40,8 +41,8 @@ export function fees (api: ApiInterface$Rx) {
|
||||
transactionByteFee,
|
||||
transferFee,
|
||||
tombstoneDeposit
|
||||
};
|
||||
}),
|
||||
}) as DerivedContractFees
|
||||
),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
@@ -6,45 +6,12 @@ import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { Option, ReferendumInfo, ReferendumIndex } from '@plugnet/types';
|
||||
import { Option, ReferendumInfo } from '@plugnet/types';
|
||||
import { isNull } from '@plugnet/util';
|
||||
|
||||
import { ReferendumInfoExtended } from '../type';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
* @name ReferendumInfoExtended
|
||||
* @description
|
||||
* A [[ReferendumInfo]] with an additional `index` field
|
||||
*/
|
||||
export class ReferendumInfoExtended extends ReferendumInfo {
|
||||
private _index: ReferendumIndex;
|
||||
|
||||
constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
|
||||
super(value);
|
||||
|
||||
this._index = value instanceof ReferendumInfoExtended
|
||||
? value.index
|
||||
: new ReferendumIndex(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience getter, returns the referendumIndex
|
||||
*/
|
||||
get index (): ReferendumIndex {
|
||||
return this._index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates the JSON representation
|
||||
*/
|
||||
toJSON (): any {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
index: this.index.toJSON()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function constructInfo (index: BN | number, optionInfo?: Option<ReferendumInfo>): Option<ReferendumInfoExtended> {
|
||||
const info = optionInfo
|
||||
? optionInfo.unwrapOr(null)
|
||||
|
||||
@@ -6,21 +6,24 @@ import BN from 'bn.js';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { Option, ReferendumInfo } from '@plugnet/types';
|
||||
import { Option, ReferendumInfo, VectorAny } from '@plugnet/types';
|
||||
|
||||
import { ReferendumInfoExtended } from '../type';
|
||||
import { drr } from '../util/drr';
|
||||
import { constructInfo, ReferendumInfoExtended } from './referendumInfo';
|
||||
import { constructInfo } from './referendumInfo';
|
||||
|
||||
export function referendumInfos (api: ApiInterface$Rx) {
|
||||
return (ids: Array<BN | number> = []): Observable<Array<Option<ReferendumInfoExtended>>> => {
|
||||
return (ids: Array<BN | number> = []): Observable<VectorAny<Option<ReferendumInfoExtended>>> => {
|
||||
return (
|
||||
!ids || !ids.length
|
||||
? of([])
|
||||
: api.query.democracy.referendumInfoOf.multi(ids) as any as Observable<Array<Option<ReferendumInfo>>>
|
||||
? of([] as Array<Option<ReferendumInfo>>)
|
||||
: api.query.democracy.referendumInfoOf.multi(ids) as Observable<VectorAny<Option<ReferendumInfo>>>
|
||||
).pipe(
|
||||
map((infos) =>
|
||||
ids.map((id, index) =>
|
||||
constructInfo(id, infos[index])
|
||||
new VectorAny(
|
||||
...ids.map((id, index) =>
|
||||
constructInfo(id, infos[index])
|
||||
)
|
||||
)
|
||||
),
|
||||
drr()
|
||||
|
||||
@@ -6,7 +6,7 @@ import BN from 'bn.js';
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, Balance, Vector, Vote } from '@plugnet/types';
|
||||
import { AccountId, Balance, StructAny, Vector, VectorAny, Vote } from '@plugnet/types';
|
||||
|
||||
import { DerivedReferendumVote } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
@@ -14,7 +14,7 @@ import { votes } from './votes';
|
||||
import { votingBalances } from '../balances/votingBalances';
|
||||
|
||||
export function referendumVotesFor (api: ApiInterface$Rx) {
|
||||
return (referendumId: BN | number): Observable<Array<DerivedReferendumVote>> =>
|
||||
return (referendumId: BN | number): Observable<VectorAny<DerivedReferendumVote>> =>
|
||||
(api.query.democracy.votersFor(referendumId) as Observable<Vector<AccountId>>).pipe(
|
||||
switchMap((votersFor) =>
|
||||
combineLatest([
|
||||
@@ -24,11 +24,15 @@ export function referendumVotesFor (api: ApiInterface$Rx) {
|
||||
])
|
||||
),
|
||||
map(([votersFor, votes, balances]) =>
|
||||
votersFor.map((accountId, index): DerivedReferendumVote => ({
|
||||
accountId,
|
||||
balance: balances[index].votingBalance || new Balance(0),
|
||||
vote: votes[index] || new Vote(0)
|
||||
}))
|
||||
new VectorAny(
|
||||
...votersFor.map((accountId, index): DerivedReferendumVote =>
|
||||
new StructAny({
|
||||
accountId,
|
||||
balance: balances[index].votingBalance || new Balance(0),
|
||||
vote: votes[index] || new Vote(0)
|
||||
}) as DerivedReferendumVote
|
||||
)
|
||||
)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
|
||||
@@ -7,8 +7,8 @@ import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { Option, ReferendumIndex } from '@plugnet/types';
|
||||
|
||||
import { ReferendumInfoExtended } from '../type';
|
||||
import { drr } from '../util/drr';
|
||||
import { ReferendumInfoExtended } from './referendumInfo';
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
|
||||
export function referendums (api: ApiInterface$Rx) {
|
||||
|
||||
@@ -5,22 +5,18 @@
|
||||
import BN from 'bn.js';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, Vote } from '@plugnet/types';
|
||||
import { AccountId, VectorAny, Vote } from '@plugnet/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function votes (api: ApiInterface$Rx) {
|
||||
return (referendumId: BN, accountIds: Array<AccountId> = []): Observable<Array<Vote>> => {
|
||||
return (referendumId: BN, accountIds: Array<AccountId> = []): Observable<VectorAny<Vote>> => {
|
||||
return (
|
||||
!accountIds || !accountIds.length
|
||||
? of([])
|
||||
? of(new VectorAny<Vote>())
|
||||
: api.query.democracy.voteOf.multi(
|
||||
accountIds.map(accountId =>
|
||||
[referendumId, accountId]
|
||||
)
|
||||
) as any as Observable<Array<Vote>>
|
||||
).pipe(
|
||||
drr()
|
||||
);
|
||||
accountIds.map(accountId => [referendumId, accountId])
|
||||
) as Observable<VectorAny<Vote>>
|
||||
).pipe(drr());
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,8 +41,6 @@ describe('derive', () => {
|
||||
|
||||
testFunction(api)('balances', 'all', []);
|
||||
testFunction(api)('balances', 'fees', []);
|
||||
testFunction(api)('balances', 'validatingBalance', []);
|
||||
testFunction(api)('balances', 'validatingBalances', []);
|
||||
testFunction(api)('balances', 'votingBalance', []);
|
||||
testFunction(api)('balances', 'votingBalances', []);
|
||||
testFunction(api)('balances', 'votingBalancesNominatorsFor', []);
|
||||
|
||||
@@ -8,7 +8,7 @@ import BN from 'bn.js';
|
||||
import { Observable, combineLatest } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { BlockNumber, Option } from '@plugnet/types';
|
||||
import { BlockNumber, Option, StructAny } from '@plugnet/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
import { bestNumber } from '../chain';
|
||||
@@ -33,7 +33,7 @@ function createDerived ([bestNumber, [currentIndex, _lastLengthChange, sessionLe
|
||||
.mul(sessionLength)
|
||||
.add(sessionProgress);
|
||||
|
||||
return {
|
||||
return new StructAny({
|
||||
currentIndex,
|
||||
eraLength,
|
||||
eraProgress,
|
||||
@@ -42,7 +42,7 @@ function createDerived ([bestNumber, [currentIndex, _lastLengthChange, sessionLe
|
||||
sessionLength,
|
||||
sessionsPerEra,
|
||||
sessionProgress
|
||||
};
|
||||
}) as DerivedSessionInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,4 +4,3 @@
|
||||
|
||||
export * from './controllers';
|
||||
export * from './info';
|
||||
export * from './intentionsBalances';
|
||||
|
||||
@@ -7,7 +7,7 @@ import { DerivedStaking } from '../types';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, Exposure, Option, StakingLedger, ValidatorPrefs } from '@plugnet/types';
|
||||
import { AccountId, Exposure, Option, StakingLedger, StructAny, ValidatorPrefs } from '@plugnet/types';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
@@ -23,20 +23,21 @@ function withStashController (api: ApiInterface$Rx, accountId: AccountId, contro
|
||||
[api.query.staking.validators, stashId]
|
||||
]) as any as Observable<[Option<AccountId>, Option<StakingLedger>, [Array<AccountId>], Exposure, [ValidatorPrefs]]>
|
||||
).pipe(
|
||||
map(([nextKeyFor, stakingLedger, [nominators], stakers, [validatorPrefs]]) => ({
|
||||
accountId,
|
||||
controllerId,
|
||||
nextSessionId: nextKeyFor.isSome
|
||||
? nextKeyFor.unwrap()
|
||||
: undefined,
|
||||
nominators,
|
||||
stakers,
|
||||
stakingLedger: stakingLedger.isSome
|
||||
? stakingLedger.unwrap()
|
||||
: undefined,
|
||||
stashId,
|
||||
validatorPrefs
|
||||
})),
|
||||
map(([nextKeyFor, stakingLedger, [nominators], stakers, [validatorPrefs]]) =>
|
||||
new StructAny({
|
||||
accountId,
|
||||
controllerId,
|
||||
nextSessionId: nextKeyFor.isSome
|
||||
? nextKeyFor.unwrap()
|
||||
: undefined,
|
||||
nominators,
|
||||
stakers,
|
||||
stakingLedger: stakingLedger.isSome
|
||||
? stakingLedger.unwrap()
|
||||
: undefined,
|
||||
stashId,
|
||||
validatorPrefs
|
||||
}) as DerivedStaking),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -53,18 +54,19 @@ function withControllerLedger (api: ApiInterface$Rx, accountId: AccountId, staki
|
||||
[api.query.staking.validators, stashId]
|
||||
]) as any as Observable<[Option<AccountId>, [Array<AccountId>], Exposure, [ValidatorPrefs]]>
|
||||
).pipe(
|
||||
map(([nextKeyFor, [nominators], stakers, [validatorPrefs]]) => ({
|
||||
accountId,
|
||||
controllerId,
|
||||
nextSessionId: nextKeyFor.isSome
|
||||
? nextKeyFor.unwrap()
|
||||
: undefined,
|
||||
nominators,
|
||||
stakers,
|
||||
stakingLedger,
|
||||
stashId,
|
||||
validatorPrefs
|
||||
})),
|
||||
map(([nextKeyFor, [nominators], stakers, [validatorPrefs]]) =>
|
||||
new StructAny({
|
||||
accountId,
|
||||
controllerId,
|
||||
nextSessionId: nextKeyFor.isSome
|
||||
? nextKeyFor.unwrap()
|
||||
: undefined,
|
||||
nominators,
|
||||
stakers,
|
||||
stakingLedger,
|
||||
stashId,
|
||||
validatorPrefs
|
||||
}) as DerivedStaking),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -90,7 +92,7 @@ export function info (api: ApiInterface$Rx) {
|
||||
stakingLedger.isSome
|
||||
? withControllerLedger(api, accountId, stakingLedger.unwrap())
|
||||
// dangit, this is something else, ok, we are done
|
||||
: of({ accountId })
|
||||
: of(new StructAny({ accountId }) as DerivedStaking)
|
||||
)
|
||||
),
|
||||
drr()
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// 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 { Observable } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@plugnet/api/types';
|
||||
import { AccountId, Vector } from '@plugnet/types';
|
||||
|
||||
import { DerivedBalancesMap } from '../types';
|
||||
import { validatingBalances } from '../balances';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
* Get the balances for all intentions and their nominators
|
||||
*/
|
||||
export function intentionsBalances (api: ApiInterface$Rx) {
|
||||
return (): Observable<DerivedBalancesMap> =>
|
||||
// tslint:disable-next-line
|
||||
(api.query.staking.intentions() as Observable<Vector<AccountId>>)
|
||||
.pipe(
|
||||
switchMap(validatingBalances(api)),
|
||||
drr()
|
||||
) as Observable<DerivedBalancesMap>;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types';
|
||||
|
||||
/**
|
||||
* @name ReferendumInfoExtended
|
||||
* @description
|
||||
* A [[ReferendumInfo]] with an additional `index` field
|
||||
*/
|
||||
export default class ReferendumInfoExtended extends ReferendumInfo {
|
||||
private _index: ReferendumIndex;
|
||||
|
||||
constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
|
||||
super(value);
|
||||
|
||||
this._index = value instanceof ReferendumInfoExtended
|
||||
? value.index
|
||||
: new ReferendumIndex(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience getter, returns the referendumIndex
|
||||
*/
|
||||
get index (): ReferendumIndex {
|
||||
return this._index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates the JSON representation
|
||||
*/
|
||||
toJSON (): any {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
index: this.index.toJSON()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export { default as HeaderExtended } from './HeaderExtended';
|
||||
export { default as ReferendumInfoExtended } from './ReferendumInfoExtended';
|
||||
|
||||
@@ -3,67 +3,68 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { AccountId, Balance, BlockNumber, Exposure, StakingLedger, ValidatorPrefs, Vote } from '@plugnet/types';
|
||||
import { AccountId, Balance, BlockNumber, Exposure, Index, StakingLedger, StructAny, ValidatorPrefs, Vote } from '@plugnet/types';
|
||||
|
||||
export type DerivedBalances = {
|
||||
accountId: AccountId,
|
||||
freeBalance: Balance,
|
||||
lockedBalance: Balance,
|
||||
availableBalance: Balance,
|
||||
reservedBalance: Balance,
|
||||
votingBalance: Balance,
|
||||
vestedBalance: Balance
|
||||
};
|
||||
export interface DerivedBalances extends StructAny {
|
||||
accountId: AccountId;
|
||||
accountNonce: Index;
|
||||
freeBalance: Balance;
|
||||
lockedBalance: Balance;
|
||||
availableBalance: Balance;
|
||||
reservedBalance: Balance;
|
||||
votingBalance: Balance;
|
||||
vestedBalance: Balance;
|
||||
}
|
||||
|
||||
export type DerivedBalancesMap = {
|
||||
[index: string]: DerivedBalances
|
||||
};
|
||||
|
||||
export type DerivedContractFees = {
|
||||
callBaseFee: BN,
|
||||
contractFee: BN,
|
||||
createBaseFee: BN,
|
||||
creationFee: BN,
|
||||
rentByteFee: BN,
|
||||
rentDepositOffset: BN,
|
||||
transactionBaseFee: BN,
|
||||
transactionByteFee: BN,
|
||||
transferFee: BN,
|
||||
tombstoneDeposit: BN
|
||||
};
|
||||
export interface DerivedContractFees extends StructAny {
|
||||
callBaseFee: BN;
|
||||
contractFee: BN;
|
||||
createBaseFee: BN;
|
||||
creationFee: BN;
|
||||
rentByteFee: BN;
|
||||
rentDepositOffset: BN;
|
||||
transactionBaseFee: BN;
|
||||
transactionByteFee: BN;
|
||||
transferFee: BN;
|
||||
tombstoneDeposit: BN;
|
||||
}
|
||||
|
||||
export type DerivedFees = {
|
||||
creationFee: BN,
|
||||
existentialDeposit: BN,
|
||||
transactionBaseFee: BN,
|
||||
transactionByteFee: BN,
|
||||
transferFee: BN
|
||||
};
|
||||
export interface DerivedFees extends StructAny {
|
||||
creationFee: BN;
|
||||
existentialDeposit: BN;
|
||||
transactionBaseFee: BN;
|
||||
transactionByteFee: BN;
|
||||
transferFee: BN;
|
||||
}
|
||||
|
||||
export type DerivedReferendumVote = {
|
||||
accountId: AccountId,
|
||||
balance: Balance,
|
||||
vote: Vote
|
||||
};
|
||||
export interface DerivedReferendumVote extends StructAny {
|
||||
accountId: AccountId;
|
||||
balance: Balance;
|
||||
vote: Vote;
|
||||
}
|
||||
|
||||
export type DerivedSessionInfo = {
|
||||
currentIndex: BlockNumber,
|
||||
eraLength: BN,
|
||||
eraProgress: BN,
|
||||
lastEraLengthChange: BlockNumber,
|
||||
lastLengthChange: BN,
|
||||
sessionLength: BlockNumber,
|
||||
sessionsPerEra: BlockNumber,
|
||||
sessionProgress: BN
|
||||
};
|
||||
export interface DerivedSessionInfo extends StructAny {
|
||||
currentIndex: BlockNumber;
|
||||
eraLength: BN;
|
||||
eraProgress: BN;
|
||||
lastEraLengthChange: BlockNumber;
|
||||
lastLengthChange: BN;
|
||||
sessionLength: BlockNumber;
|
||||
sessionsPerEra: BlockNumber;
|
||||
sessionProgress: BN;
|
||||
}
|
||||
|
||||
export type DerivedStaking = {
|
||||
accountId: AccountId,
|
||||
controllerId?: AccountId,
|
||||
nextSessionId?: AccountId,
|
||||
nominators?: Array<AccountId>,
|
||||
stakers?: Exposure,
|
||||
stakingLedger?: StakingLedger,
|
||||
stashId?: AccountId,
|
||||
validatorPrefs?: ValidatorPrefs
|
||||
};
|
||||
export interface DerivedStaking extends StructAny {
|
||||
accountId: AccountId;
|
||||
controllerId?: AccountId;
|
||||
nextSessionId?: AccountId;
|
||||
nominators?: Array<AccountId>;
|
||||
stakers?: Exposure;
|
||||
stakingLedger?: StakingLedger;
|
||||
stashId?: AccountId;
|
||||
validatorPrefs?: ValidatorPrefs;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import extrinsicsFromMeta from '@plugnet/extrinsics/fromMetadata';
|
||||
import RpcBase from '@plugnet/rpc-core';
|
||||
import RpcRx from '@plugnet/rpc-rx';
|
||||
import storageFromMeta from '@plugnet/storage/fromMetadata';
|
||||
import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, Null } from '@plugnet/types';
|
||||
import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, Null, VectorAny } from '@plugnet/types';
|
||||
import Linkage, { LinkageResult } from '@plugnet/types/codec/Linkage';
|
||||
import { MethodFunction, ModulesWithMethods } from '@plugnet/types/primitive/Method';
|
||||
import { StorageFunction } from '@plugnet/types/primitive/StorageKey';
|
||||
@@ -506,7 +506,10 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
);
|
||||
|
||||
return onCall(
|
||||
() => this._rpcRx.state.subscribeStorage(mapped),
|
||||
() =>
|
||||
this._rpcRx.state
|
||||
.subscribeStorage(mapped)
|
||||
.pipe(map((results) => new VectorAny(...results))),
|
||||
[],
|
||||
callback
|
||||
) as S;
|
||||
@@ -605,7 +608,10 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
|
||||
decorated.multi = (args: Array<CodecArg>, callback?: CodecCallback): S =>
|
||||
onCall(
|
||||
() => this._rpcRx.state.subscribeStorage(args.map((arg) => [creator, arg])),
|
||||
() =>
|
||||
this._rpcRx.state
|
||||
.subscribeStorage(args.map((arg) => [creator, arg]))
|
||||
.pipe(map((results) => new VectorAny(...results))),
|
||||
[],
|
||||
callback
|
||||
) as unknown as S;
|
||||
|
||||
@@ -18,6 +18,7 @@ These are the base types of the codec. They are typically not used directly, but
|
||||
| [[Option]] | An Option is an optional field. The first byte indicates that there is is value to follow |
|
||||
| [[Set]] | An Set is an array of string values, represented an an encoded type by a bitwise representation of the values |
|
||||
| [[Struct]] | A Struct defines an Object with key-value pairs - where the values are Codec values. |
|
||||
| [[StructAny]] | Wraps the a JSON structure retrieve via RPC. It extends the standard JS Map |
|
||||
| [[Tuple]] | A Tuple defines an anonymous fixed-length array, where each element has its own type |
|
||||
| [[U8a]] | A basic wrapper around Uint8Array. It will consume the full Uint8Array as passed to it |
|
||||
| [[U8aFixed]] | A U8a that manages a a sequence of bytes up to the specified bitLength |
|
||||
@@ -150,7 +151,6 @@ These types are not used in the runtime, but are rather used in RPC results:
|
||||
| [[ExtrinsicStatus]] | An EnumType that indicates the status of the Extrinsic as been submitted |
|
||||
| [[Header]] | A [[Block]] header |
|
||||
| [[Health]] | A system health indicator, reported back over RPC |
|
||||
| [[Json]] | Wraps the a JSON structure retrieve via RPC. It extends the standard JS Map |
|
||||
| [[NetworkState]] | Wraps the properties retrieved from the chain via the `system.network_state` RPC call |
|
||||
| [[Metadata]] | The versioned runtime metadata as a decoded structure |
|
||||
| [[PeerInfo]] | A system peer info indicator, reported back over RPC |
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Codec } from '../types';
|
||||
import { compareMap } from '../codec/utils';
|
||||
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
import { compareMap } from './utils';
|
||||
|
||||
/**
|
||||
* @name Json
|
||||
@@ -13,9 +16,24 @@ import { compareMap } from '../codec/utils';
|
||||
* i.e. no hex decoding. Unlike a struct, this waps a JSON object with unknown keys
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export default class Json extends Map<string, any> implements Codec {
|
||||
export default class StructAny extends Map<string, any> implements Codec {
|
||||
constructor (value?: { [index: string]: any } | null) {
|
||||
super(Json.decodeJson(value));
|
||||
const decoded = StructAny.decodeJson(value);
|
||||
|
||||
super(decoded);
|
||||
|
||||
// like we are doing with structs, add the keys as getters
|
||||
decoded.forEach(([key]) => {
|
||||
// do not clobber existing properties on the object
|
||||
if (!isUndefined((this as any)[key])) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object.defineProperty(this, key, {
|
||||
enumerable: true,
|
||||
get: () => this.get(key)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static decodeJson (value?: { [index: string]: any } | null): Array<[string, any]> {
|
||||
@@ -14,8 +14,10 @@ export { default as EnumType } from './EnumType';
|
||||
export { default as Option } from './Option';
|
||||
export { default as Set } from './Set';
|
||||
export { default as Struct } from './Struct';
|
||||
export { default as StructAny } from './StructAny';
|
||||
export { default as Tuple } from './Tuple';
|
||||
export { default as Vector } from './Vector';
|
||||
export { default as VectorAny } from './VectorAny';
|
||||
|
||||
// Convenience base classes, used as "anything of this type" bases
|
||||
export { default as U8a } from './U8a';
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Json from './Json';
|
||||
import StructAny from '../codec/StructAny';
|
||||
|
||||
/**
|
||||
* @name ChainProperties
|
||||
* @description
|
||||
* Wraps the properties retrieved from the chain via the `system.properties` RPC call.
|
||||
*/
|
||||
export default class ChainProperties extends Json {
|
||||
export default class ChainProperties extends StructAny {
|
||||
/**
|
||||
* @description The token decimals, if defined (de-facto standard only)
|
||||
*/
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Json from './Json';
|
||||
import StructAny from '../codec/StructAny';
|
||||
|
||||
/**
|
||||
* @name NetworkState
|
||||
* @description
|
||||
* Wraps the properties retrieved from the chain via the `system.network_state` RPC call.
|
||||
*/
|
||||
export default class NetworkState extends Json {
|
||||
export default class NetworkState extends StructAny {
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ export { default as Digest, DigestItem } from './Digest';
|
||||
export { default as ExtrinsicStatus } from './ExtrinsicStatus';
|
||||
export { default as Header } from './Header';
|
||||
export { default as Health } from './Health';
|
||||
export { default as Json } from './Json';
|
||||
export { default as NetworkState } from './NetworkState';
|
||||
export { default as PeerInfo } from './PeerInfo';
|
||||
export { default as PendingExtrinsics } from './PendingExtrinsics';
|
||||
|
||||
Reference in New Issue
Block a user