* fix: Remove docs/api since replaced with docs/rpc-core * fix: Remove docs/api-provider since replaced with docs/rpc-provider * fix: Remove docs/api-rx since replaced with docs/rpc-rx * fix: Remove the guide file that serves no purpose * fix: Change license from ui-observable to api-observable * fix: Remove disused mockHttp and mockWs that are duplicates of those in rpc-provider * fix: Clarifies difference between PropIndex and ProposalIndex from Issue #226 * fix: Update api-observable Readme to refer to rpc-core, previously called api * misc: Update Readme of rpc-core. See commit message details. * Update name to be @polkadot/rpc-core instead of @polkadot/api * Use Yarn instead of NPM for instructions adding dependencies * Refer to @polkadot/rpc-core as `Rpc` * Highlight that given block header hash should be a 0x-prefixed hex string with length of 64 (not only length of 10 as shown in the example) * Showcase more information that you can obtain from the header object (i.e. best block number, parentHash, stateRoot, extrinsicsRoot, and digest) * Use `header.raw` instead of just `header` to access properties of the header object * Update the example to console.log the subscription ID * Add missing semicolon * fix: Fix name in license header and rename Api to Rpc for rpc-core e2e tests * fix: Update rpc-provider Readme to use Yarn instead of NPM install * fix: Fix minor typo * fix: Rename Api to Rpc for rpc-provider ws * fix: Refer to rpc-core instead of api in the rpc-rx Readme * fix: Update rpc-rx files to refer to it as RpcRx instead of RxApi or just Api * fix: Refer to rpc-core as Rpc instead of Api * fix: Update type-jsonrpc Readme. Fix typo. Use Yarn not NPM. Refer to TSLint not Flow * refactor: Move non-type dependency import with non-type dependencies * docs: Add link to parity-codec repo * fix: Avoid use of .raw. Fix listen to blocks example * fix: Add missing error to newHead in listen to blocks example
332 lines
11 KiB
TypeScript
332 lines
11 KiB
TypeScript
// Copyright 2017-2018 @polkadot/api-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 as Bool, BlockNumber, Moment, ReferendumIndex } from '@polkadot/types/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 {
|
|
publicProposalCount = (): Observable<number> => {
|
|
return this
|
|
.publicProposals()
|
|
.pipe(
|
|
map((proposals: Array<RxProposal>) =>
|
|
proposals.length
|
|
)
|
|
);
|
|
}
|
|
|
|
referendumsInfo = (referendumIds: Array<ReferendumIndex | BN | number>): Observable<Array<RxReferendum>> => {
|
|
return this.combine(
|
|
referendumIds.map((referendumId) =>
|
|
this.referendumInfo(referendumId)
|
|
),
|
|
(referendums: Array<RxReferendum> = []): Array<RxReferendum> =>
|
|
referendums.filter((referendum) =>
|
|
referendum
|
|
)
|
|
);
|
|
}
|
|
|
|
democracyReferendumVoters = (referendumId: ReferendumIndex | BN | number): Observable<Array<RxReferendumVote>> => {
|
|
return this.combine(
|
|
[
|
|
this.referendumVoters(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)
|
|
}))
|
|
);
|
|
}
|
|
|
|
referendums = (): Observable<Array<RxReferendum>> => {
|
|
return this.combine(
|
|
[
|
|
this.referendumCount(),
|
|
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.referendumsInfo(
|
|
[...Array(referendumCount.toBn().sub(nextTally.toBn()).toNumber())].map((_, i) =>
|
|
nextTally.add(i).toNumber()
|
|
)
|
|
)
|
|
: EMPTY
|
|
),
|
|
defaultIfEmpty([])
|
|
);
|
|
}
|
|
|
|
referendumVotes = (index: ReferendumIndex | BN | number, addresses: Array<AccountId | string>): Observable<boolean> => {
|
|
return this.combine(
|
|
addresses.map((address) =>
|
|
this.referendumVote(index, address)
|
|
)
|
|
);
|
|
}
|
|
|
|
democracyVotersBalancesOf = (referendumId: ReferendumIndex | BN | number): Observable<Array<Balance>> => {
|
|
return this
|
|
.referendumVoters(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
|
|
.referendumVoters(referendumId)
|
|
.pipe(
|
|
switchMap((voters: Array<AccountId> = []) =>
|
|
this.referendumVotes(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]: [BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
|
sessionsPerEra && sessionCurrentIndex && sessionLength && sessionBlockProgress
|
|
? new BlockNumber(
|
|
sessionCurrentIndex
|
|
// last era can be null (i.e. new chain, valid key, no value set yet)
|
|
.sub(eraLastLengthChange || new BlockNumber(0))
|
|
.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
|
|
? new BlockNumber(
|
|
bestNumber
|
|
// last change can be null (i.e. new chain, valid key, no value set yet)
|
|
.sub(lastSessionLengthChange || new BlockNumber(0))
|
|
.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.blockNow(),
|
|
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.blockPeriod()
|
|
],
|
|
([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.blockPeriod()
|
|
],
|
|
([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.balanceFree(address),
|
|
this.balanceReserved(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)
|
|
)
|
|
);
|
|
}
|
|
}
|