Pull in observable API from UI (#175)

* Pull in observable api...

* Progress changing stuff...

* votingBalance

* Update resul types

* Expose toBn on Moment

* Rework... WIP

* ... More type cleanups

* Linting passes

* BN helpers for UInt

* Update api-observable

* Add api-observable package.json

* Allow Struct/Tuple to create from hex string

* Future-proof Length with bitLength (u64/u128 compant encoding PR out)

* Make Compact number encoding shareable

* Split Api into functional sections

* Cleanup types

* Additional per-use comments

* Remove params/primitives from README

* api-format  -> api-codec

* Whitespace.
This commit is contained in:
Jaco Greeff
2018-10-01 11:22:15 +02:00
committed by GitHub
parent 465d986de1
commit 782bb8063c
24 changed files with 1010 additions and 68 deletions
+74
View File
@@ -0,0 +1,74 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
import { Method } from '@polkadot/jsonrpc/types';
import { Observable, combineLatest } from 'rxjs';
import { defaultIfEmpty, map } from 'rxjs/operators';
import { StorageFunction } from '@polkadot/api-codec/StorageKey';
import assert from '@polkadot/util/assert';
import isUndefined from '@polkadot/util/is/undefined';
type MapFn<R, T> = (combined: R) => T;
const defaultMapFn = (result: any): any =>
result;
// Raw base implementation for the observable API. It simply provides access to raw calls, allowing
// decendants to make direct queries to either API methods or actual storage
export default class ApiBase {
protected api: RxApiInterface;
constructor (api: RxApiInterface) {
this.api = api;
}
protected combine = <T, R> (observables: Array<Observable<any>>, mapfn: MapFn<R, T> = defaultMapFn): Observable<T> => {
return combineLatest(...observables).pipe(
// FIXME There are a couple of places now where this casting happens after rxjs 6.3.2
defaultIfEmpty([] as any),
map(mapfn)
);
}
isConnected = (): Observable<boolean> => {
return this.api.isConnected();
}
rawCall = <T> ({ name, section }: Method, ...params: Array<any>): Observable<T> => {
const apiSection = this.api[section as keyof RxApiInterface] as RxApiInterface$Section;
assert(apiSection, `Unable to find 'api.${section}'`);
const fn: RxApiInterface$Method = apiSection[name];
assert(fn, `Unable to find 'api.${section}.${name}'`);
return fn.apply(null, params);
}
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T> => {
return this
.rawStorageMulti([key, ...params] as [StorageFunction, any])
.pipe(
// @ts-ignore After upgrade to 6.3.2
map(([result]: Array<T>): T =>
result
)
);
}
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
return this.api.state
.storage(keys)
.pipe(
map((result?: any) =>
isUndefined(result)
? []
: result
)
);
}
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { defaultIfEmpty, map } from 'rxjs/operators';
import { BlockNumber, Header, SignedBlock } from '@polkadot/api-codec/index';
import ApiQueries from './Queries';
// Implementation of calls to API endpoints.
export default class ApiCalls extends ApiQueries {
bestNumber = (): Observable<BlockNumber | undefined> => {
return this
.chainNewHead()
.pipe(
map((header?: Header): BlockNumber | undefined =>
header && header.blockNumber
? header.blockNumber
: undefined
)
);
}
chainGetBlock = (hash: Uint8Array): Observable<SignedBlock | undefined> => {
return this.api.chain.getBlock(hash);
}
chainNewHead = (): Observable<Header | undefined> => {
return this.api.chain.newHead().pipe(
defaultIfEmpty()
);
}
}
+329
View File
@@ -0,0 +1,329 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { RxBalance, RxBalanceMap, RxReferendumVote } from './types';
import BN from 'bn.js';
import { EMPTY, Observable } from 'rxjs';
import { switchMap, defaultIfEmpty, map } from 'rxjs/operators';
import { AccountId, Balance, Bool, BlockNumber, Moment, ReferendumIndex } from '@polkadot/api-codec/index';
import isString from '@polkadot/util/is/string';
import ApiCalls from './Calls';
import { RxProposal, RxReferendum } from './classes';
// Combines API calls and queries into single results. This allos for the exposed API to have
// useful extensions, i.e. queries can be made that returns the results from multiple observables,
// make the noise for the API users significantly less
export default class ApiCombined extends ApiCalls {
democracyProposalCount = (): Observable<number> => {
return this
.democracyPublicProposals()
.pipe(
map((proposals: Array<RxProposal>) =>
proposals.length
)
);
}
democracyReferendumInfos = (referendumIds: Array<ReferendumIndex | BN | number>): Observable<Array<RxReferendum>> => {
return this.combine(
referendumIds.map((referendumId) =>
this.democracyReferendumInfoOf(referendumId)
),
(referendums: Array<RxReferendum> = []): Array<RxReferendum> =>
referendums.filter((referendum) =>
referendum
)
);
}
democracyReferendumVoters = (referendumId: ReferendumIndex | BN | number): Observable<Array<RxReferendumVote>> => {
return this.combine(
[
this.democacyVotersFor(referendumId),
this.democracyVotersBalancesOf(referendumId),
this.democracyVotersVotesOf(referendumId)
],
([voters, balances, votes]: [Array<AccountId>, Array<Balance>, Array<Bool>]): Array<RxReferendumVote> =>
voters.map((address, index): RxReferendumVote => ({
address,
balance: balances[index] || new Balance(0),
vote: votes[index] || new Bool(false)
}))
);
}
democracyReferendums = (): Observable<Array<RxReferendum>> => {
return this.combine(
[
this.democracyReferendumCount(),
this.democracyNextTally()
]
).pipe(
// @ts-ignore After upgrade to 6.3.2
switchMap(([referendumCount, nextTally]: [ReferendumIndex | undefined, ReferendumIndex | undefined]): Observable<Array<RxReferendum>> =>
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gt(0)
? this.democracyReferendumInfos(
[...Array(referendumCount.toBn().sub(nextTally.toBn()).toNumber())].map((_, i) =>
nextTally.add(i).toNumber()
)
)
: EMPTY
),
defaultIfEmpty([])
);
}
democracyVotesOf = (index: ReferendumIndex | BN | number, addresses: Array<AccountId | string>): Observable<boolean> => {
return this.combine(
addresses.map((address) =>
this.democacyVoteOf(index, address)
)
);
}
democracyVotersBalancesOf = (referendumId: ReferendumIndex | BN | number): Observable<Array<Balance>> => {
return this
.democacyVotersFor(referendumId)
.pipe(
switchMap((voters: Array<AccountId> = []) =>
this.votingBalances(...voters)
),
defaultIfEmpty([] as any),
map((balances: Array<RxBalance>) =>
balances.map(({ votingBalance }) =>
votingBalance
)
)
);
}
democracyVotersVotesOf = (referendumId: ReferendumIndex | BN | number): Observable<Array<Bool>> => {
return this
.democacyVotersFor(referendumId)
.pipe(
switchMap((voters: Array<AccountId> = []) =>
this.democracyVotesOf(referendumId, voters)
),
defaultIfEmpty([] as any)
);
}
eraBlockLength = (): Observable<BlockNumber | undefined> => {
return this.combine(
[
this.sessionLength(),
this.sessionsPerEra()
],
([sessionLength, sessionsPerEra]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
sessionLength && sessionsPerEra
? new BlockNumber(
sessionLength.mul(sessionsPerEra)
)
: undefined
);
}
eraBlockProgress = (): Observable<BlockNumber | undefined> => {
return this.combine(
[
this.sessionBlockProgress(),
this.sessionLength(),
this.sessionCurrentIndex(),
this.sessionsPerEra(),
this.eraLastLengthChange()
],
([sessionBlockProgress, sessionLength, sessionCurrentIndex, sessionsPerEra, eraLastLengthChange = new BlockNumber(0)]: [BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
sessionsPerEra && sessionCurrentIndex && sessionLength && sessionBlockProgress && eraLastLengthChange
? new BlockNumber(
sessionCurrentIndex
.sub(eraLastLengthChange)
.mod(sessionsPerEra.toBn())
.mul(sessionLength.toBn())
.add(sessionBlockProgress.toBn())
)
: undefined
);
}
eraBlockRemaining = (): Observable<BlockNumber | undefined> => {
return this.combine(
[
this.eraBlockLength(),
this.eraBlockProgress()
],
([eraBlockLength, eraBlockProgress]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
eraBlockLength && eraBlockProgress
? new BlockNumber(
eraBlockLength.sub(eraBlockProgress)
)
: undefined
);
}
sessionBlockProgress = (): Observable<BlockNumber | undefined> => {
return this.combine(
[
this.bestNumber(),
this.sessionLength(),
this.sessionLastLengthChange()
],
([bestNumber, sessionLength, lastSessionLengthChange]: [BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
bestNumber && sessionLength && lastSessionLengthChange
? new BlockNumber(
bestNumber
.sub(lastSessionLengthChange)
.add(sessionLength.toBn())
.mod(sessionLength.toBn())
)
: undefined
);
}
sessionBlockRemaining = (): Observable<BlockNumber | undefined> => {
return this.combine(
[
this.sessionBlockProgress(),
this.sessionLength()
],
([sessionBlockProgress, sessionLength]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
sessionBlockProgress && sessionLength
? new BlockNumber(
sessionLength.sub(sessionBlockProgress)
)
: undefined
);
}
sessionBrokenValue = (): Observable<Moment | undefined> => {
return this.combine(
[
this.timestampNow(),
this.sessionTimeExpected(),
this.sessionTimeRemaining(),
this.sessionCurrentStart()
],
([now, sessionTimeExpected, sessionTimeRemaining, sessionCurrentStart]: [Moment | undefined, Moment | undefined, Moment | undefined, Moment | undefined]): Moment | undefined =>
sessionTimeExpected && sessionTimeRemaining && sessionCurrentStart && now
? new Moment(
Math.round(
100 * (now.getTime() + sessionTimeRemaining.toNumber() - sessionCurrentStart.getTime()) / sessionTimeExpected.toNumber() - 100
)
)
: undefined
);
}
sessionTimeExpected = (): Observable<Moment | undefined> => {
return this.combine(
[
this.sessionLength(),
this.timestampBlockPeriod()
],
([sessionLength, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
sessionLength && blockPeriod
? new Moment(
sessionLength.mul(blockPeriod.toBn()).muln(1000)
)
: undefined
);
}
sessionTimeRemaining = (): Observable<Moment | undefined> => {
return this.combine(
[
this.sessionBlockRemaining(),
this.timestampBlockPeriod()
],
([sessionBlockRemaining, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
blockPeriod && sessionBlockRemaining
? new Moment(
sessionBlockRemaining.mul(blockPeriod.toBn()).muln(1000)
)
: undefined
);
}
validatingBalance = (address: AccountId | string): Observable<RxBalance> => {
return this.combine(
[
this.votingBalance(address),
this.votingBalancesNominatorsFor(address)
],
([balance, nominators = []]: [RxBalance, Array<RxBalance>]): RxBalance => {
const nominatedBalance = nominators.reduce((total, nominator: RxBalance) => {
return total.add(nominator.votingBalance.toBn());
}, new BN(0));
const result = {
...balance,
nominators,
nominatedBalance: new Balance(nominatedBalance),
stakingBalance: new Balance(
nominatedBalance.add(balance.votingBalance.toBn())
)
};
return result;
}
);
}
validatingBalances = (...addresses: Array<AccountId | string>): Observable<RxBalanceMap> => {
return this.combine(
addresses.map((address) =>
this.validatingBalance(address)
),
(result: Array<RxBalance>): RxBalanceMap =>
result.reduce((balances, balance) => {
balances[balance.address.toString()] = balance;
return balances;
}, {} as RxBalanceMap)
);
}
votingBalance = (_address: AccountId | string): Observable<RxBalance> => {
const address = isString(_address)
? new AccountId(_address)
: _address;
return this.combine(
[
this.stakingFreeBalanceOf(address),
this.stakingReservedBalanceOf(address)
],
([freeBalance = new Balance(0), reservedBalance = new Balance(0)]: [Balance | undefined, Balance | undefined]): RxBalance => ({
address,
freeBalance,
nominatedBalance: new Balance(0),
reservedBalance,
stakingBalance: new Balance(0),
votingBalance: new Balance(
freeBalance.add(reservedBalance)
)
})
);
}
votingBalancesNominatorsFor = (address: AccountId | string) => {
return this
.stakingNominatorsFor(address)
.pipe(
switchMap((nominators: Array<AccountId>) =>
this.votingBalances(...nominators)
),
defaultIfEmpty([] as any)
);
}
votingBalances = (...addresses: Array<AccountId | string>): Observable<RxBalance[]> => {
return this.combine(
addresses.map((address) =>
this.votingBalance(address)
)
);
}
}
+210
View File
@@ -0,0 +1,210 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { RxFees } from './types';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AccountId, Balance, Bool, BlockNumber, Index, Moment, Perbill, PropIndex, ReferendumIndex, u32 } from '@polkadot/api-codec/index';
import { Tuple } from '@polkadot/api-codec/codec';
import storage from '@polkadot/storage/testing';
import ApiBase from './Base';
import { RxProposal, RxProposalDeposits, RxReferendum } from './classes';
// Perform storage queries to the API endpoints.
export default class ApiQueries extends ApiBase {
democracyLaunchPeriod = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.democracy.launchPeriod);
}
democracyNextTally = (): Observable<ReferendumIndex | undefined> => {
return this.rawStorage(storage.democracy.nextTally);
}
democracyProposalDeposits = (proposalId: PropIndex | BN | number): Observable<RxProposalDeposits | undefined> => {
return this
.rawStorage(storage.democracy.depositOf, proposalId)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((result: Tuple): RxProposalDeposits | undefined =>
result
? new RxProposalDeposits(result)
: undefined
)
);
}
democracyPublicProposals = (): Observable<Array<RxProposal>> => {
return this
.rawStorage(storage.democracy.publicProps)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((proposals: Array<Tuple> = []) =>
proposals
.map((result: Tuple): RxProposal | undefined =>
result
? new RxProposal(result)
: undefined
)
.filter((proposal) =>
proposal
)
)
);
}
democracyReferendumCount = (): Observable<ReferendumIndex | undefined> => {
return this.rawStorage(storage.democracy.referendumCount);
}
democracyReferendumInfoOf = (referendumId: ReferendumIndex | BN | number): Observable<RxReferendum | undefined> => {
return this
.rawStorage(storage.democracy.referendumInfoOf, referendumId)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((result: Tuple): RxReferendum | undefined =>
result
? new RxReferendum(result, referendumId)
: undefined
)
);
}
democacyVoteOf = (index: ReferendumIndex | BN | number, address: AccountId | string): Observable<Bool> => {
return this.rawStorage(storage.democracy.voteOf, index, address);
}
democacyVotersFor = (index: ReferendumIndex | BN | number): Observable<Array<AccountId>> => {
return this
.rawStorage(storage.democracy.votersFor, index)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((voters: Array<AccountId> = []) =>
voters
)
);
}
democracyVotingPeriod = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.democracy.votingPeriod);
}
fees = (): Observable<RxFees> => {
return this
.rawStorageMulti(
[storage.balances.transactionBaseFee],
[storage.balances.transactionByteFee],
[storage.balances.creationFee],
[storage.balances.existentialDeposit],
[storage.balances.transferFee]
)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map(([baseFee = new Balance(0), byteFee = new Balance(0), creationFee = new Balance(0), existentialDeposit = new Balance(0), transferFee = new Balance(0)]: [Balance | undefined, Balance | undefined, Balance | undefined, Balance | undefined, Balance | undefined]) => ({
baseFee,
byteFee,
creationFee,
existentialDeposit,
transferFee
}))
);
}
eraLastLengthChange = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.staking.lastEraLengthChange);
}
sessionReward = (): Observable<Perbill | undefined> => {
return this.rawStorage(storage.staking.sessionReward);
}
sessionRewardCurrent = (): Observable<Balance | undefined> => {
return this.rawStorage(storage.staking.currentSessionReward);
}
sessionCurrentIndex = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.session.currentIndex);
}
sessionCurrentStart = (): Observable<Moment | undefined> => {
return this.rawStorage(storage.session.currentStart);
}
sessionLastLengthChange = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.session.lastLengthChange);
}
sessionLength = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.session.sessionLength);
}
sessionsPerEra = (): Observable<BlockNumber | undefined> => {
return this.rawStorage(storage.staking.sessionsPerEra);
}
sessionValidators = (): Observable<Array<AccountId>> => {
return this
.rawStorage(storage.session.validators)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((validators: Array<AccountId> = []) =>
validators
)
);
}
stakingIntentions = (): Observable<Array<AccountId>> => {
return this
.rawStorage(storage.staking.intentions)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((intentions: Array<AccountId> = []) =>
intentions
)
);
}
// FIXME -> freeBalance
stakingFreeBalanceOf = (address: AccountId | string): Observable<Balance | undefined> => {
return this.rawStorage(storage.balances.freeBalance, address);
}
stakingNominatorsFor = (address: AccountId | string): Observable<Array<AccountId>> => {
return this
.rawStorage(storage.staking.nominatorsFor, address)
.pipe(
// @ts-ignore After upgrade to 6.3.2
map((nominators: Array<AccountId> = []) =>
nominators
)
);
}
stakingNominating = (address: AccountId | string): Observable<AccountId | undefined> => {
return this.rawStorage(storage.staking.nominating, address);
}
// FIXME -> reservedBalance
stakingReservedBalanceOf = (address: AccountId | string): Observable<Balance | undefined> => {
return this.rawStorage(storage.balances.reservedBalance, address);
}
timestampBlockPeriod = (): Observable<Moment | undefined> => {
return this.rawStorage(storage.timestamp.blockPeriod);
}
timestampNow = (): Observable<Moment | undefined> => {
return this.rawStorage(storage.timestamp.now);
}
systemAccountIndexOf = (address: AccountId | string): Observable<Index | undefined> => {
return this.rawStorage(storage.system.accountNonce, address);
}
validatorCount = (): Observable<u32> => {
return this.rawStorage(storage.staking.validatorCount);
}
}
+73
View File
@@ -0,0 +1,73 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import BN from 'bn.js';
import { AccountId, Balance, BlockNumber, PropIndex, Proposal, ReferendumIndex, VoteThreshold } from '@polkadot/api-codec/index';
import { Struct, Tuple, Vector } from '@polkadot/api-codec/codec';
export class RxProposal extends Struct.with({ id: PropIndex, proposal: Proposal, address: AccountId }) {
constructor (value: Tuple) {
super({
id: value.get(0),
proposal: value.get(1),
address: value.get(2)
});
}
get address (): AccountId {
return this.raw.address as AccountId;
}
get id (): PropIndex {
return this.raw.id as PropIndex;
}
get proposal (): Proposal {
return this.raw.proposal as Proposal;
}
}
export class RxProposalDeposits extends Struct.with({ balance: Balance, addresses: Vector.with(AccountId) }) {
constructor (value: Tuple) {
super({
balance: value.get(0),
addresses: value.get(1)
});
}
get addresses (): Vector<AccountId> {
return this.raw.addresses as Vector<AccountId>;
}
get balance (): Balance {
return this.raw.balance as Balance;
}
}
export class RxReferendum extends Struct.with({ blockNumber: BlockNumber, proposal: Proposal, voteThreshold: VoteThreshold, id: ReferendumIndex }) {
constructor (value: Tuple, id: ReferendumIndex | BN | number) {
super({
blockNumber: value.get(0),
proposal: value.get(1),
voteThreshold: value.get(2),
id
});
}
get blockNumber (): BlockNumber {
return this.raw.blockNumber as BlockNumber;
}
get id (): ReferendumIndex {
return this.raw.id as ReferendumIndex;
}
get proposal (): Proposal {
return this.raw.proposal as Proposal;
}
get voteThreshold (): VoteThreshold {
return this.raw.voteThreshold as VoteThreshold;
}
}
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import ApiCombined from './Combined';
export default class Api extends ApiCombined {
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { AccountId, Balance, BlockNumber, Bool, Header, PropIndex, Proposal, ReferendumIndex, u32, VoteThreshold } from '@polkadot/api-codec/index';
import { Tuple } from '@polkadot/api-codec/codec';
import ApiBase from './Base';
import ApiCalls from './Calls';
import ApiCombined from './Combined';
import ApiQueries from './Queries';
export type ApiFunctions = keyof ApiBase | keyof ApiCalls | keyof ApiCombined | keyof ApiQueries;
export type RxBalance = {
address: AccountId,
freeBalance: Balance,
nominatedBalance: Balance,
reservedBalance: Balance,
votingBalance: Balance,
stakingBalance: Balance,
nominators?: Array<RxBalance>
}
export type RxFees = {
baseFee: Balance,
byteFee: Balance,
creationFee: Balance,
existentialDeposit: Balance,
transferFee: Balance,
};
export type RxReferendumVote = {
address: AccountId,
balance: Balance,
vote: Bool
};
export type RxBalanceMap = {
[index: string]: RxBalance
}