Remove onCall, use decorateMethod (#987)
* update signature of doublemap storage function * add test cases * update doublemap implementation * update implementation * remove duplicate test case * update test cases * Add hkt * Fix derive types * Make derive work * Fix some bugs * Fix some bugs * More stuff * Make some more stuff work * Fix types in derive * Fix all tests in derive * Change back ApiType * Remove vscode * Fix Submittables * Fix some errors * Fix some bugs * Change some stuff * Fix merge conflict * Fix types * Add console.log * Add console.log * Re-add decorateMethod * Fix tests * Skip e2e * Small fixes * Fix signAndSend * Fix typings in test * Fix lint * Add comments * One line * Remove useless * URI->ApiType * Indentation
This commit is contained in:
@@ -30,7 +30,7 @@ export function indexToId (api: ApiInterface$Rx) {
|
||||
? _accountIndex
|
||||
: new AccountIndex(_accountIndex);
|
||||
|
||||
return (querySection.enumSet(accountIndex.div(ENUMSET_SIZE)) as Observable<Vector<AccountId>>)
|
||||
return (querySection.enumSet<Vector<AccountId>>(accountIndex.div(ENUMSET_SIZE)))
|
||||
.pipe(
|
||||
startWith([]),
|
||||
map((accounts) =>
|
||||
|
||||
@@ -31,7 +31,7 @@ const enumsetSize = ENUMSET_SIZE.toNumber();
|
||||
*/
|
||||
export function indexes (api: ApiInterface$Rx) {
|
||||
return (): Observable<AccountIndexes> => {
|
||||
return (api.query.indices.nextEnumSet() as Observable<AccountIndex>)
|
||||
return (api.query.indices.nextEnumSet<AccountIndex>())
|
||||
.pipe(
|
||||
// use the nextEnumSet (which is a counter of the number of sets) to construct
|
||||
// a range of values to query [0, 1, 2, ...]. Retrieve the full enum set for the
|
||||
|
||||
@@ -9,6 +9,7 @@ import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, In
|
||||
import { bnMax } from '@polkadot/util';
|
||||
|
||||
import { idAndIndex } from '../accounts/idAndIndex';
|
||||
import { bestNumber } from '../chain/bestNumber';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
@@ -78,7 +79,7 @@ export function all (api: ApiInterface$Rx) {
|
||||
(accountId
|
||||
? combineLatest([
|
||||
of(accountId),
|
||||
api.derive.chain.bestNumber(),
|
||||
bestNumber(api)(),
|
||||
api.queryMulti([
|
||||
[api.query.balances.freeBalance, accountId],
|
||||
[api.query.balances.reservedBalance, accountId],
|
||||
|
||||
@@ -17,7 +17,7 @@ export function votingBalancesNominatorsFor (api: ApiInterface$Rx) {
|
||||
return idAndIndex(api)(address).pipe(
|
||||
switchMap(([accountId]) =>
|
||||
accountId
|
||||
? (api.query.staking.nominatorsFor(accountId) as Observable<Vector<AccountId>>)
|
||||
? (api.query.staking.nominatorsFor<Vector<AccountId>>(accountId))
|
||||
: of([] as Array<AccountId>)
|
||||
),
|
||||
switchMap(votingBalances(api)),
|
||||
|
||||
@@ -27,7 +27,7 @@ export function constructInfo (index: BN | number, optionInfo?: Option<Referendu
|
||||
|
||||
export function referendumInfo (api: ApiInterface$Rx) {
|
||||
return (index: BN | number): Observable<Option<ReferendumInfoExtended>> => {
|
||||
return (api.query.democracy.referendumInfoOf(index) as Observable<Option<ReferendumInfo>>)
|
||||
return (api.query.democracy.referendumInfoOf<Option<ReferendumInfo>>(index))
|
||||
.pipe(
|
||||
map((optionInfo) =>
|
||||
constructInfo(index, optionInfo)
|
||||
|
||||
@@ -15,7 +15,7 @@ import { votingBalances } from '../balances/votingBalances';
|
||||
|
||||
export function referendumVotesFor (api: ApiInterface$Rx) {
|
||||
return (referendumId: BN | number): Observable<VectorAny<DerivedReferendumVote>> =>
|
||||
(api.query.democracy.votersFor(referendumId) as Observable<Vector<AccountId>>).pipe(
|
||||
(api.query.democracy.votersFor<Vector<AccountId>>(referendumId)).pipe(
|
||||
switchMap((votersFor) =>
|
||||
combineLatest([
|
||||
of(votersFor),
|
||||
|
||||
@@ -3,14 +3,12 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable, from } from 'rxjs';
|
||||
|
||||
import ApiRx from '@polkadot/api/rx/Api';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import MockProvider from '@polkadot/rpc-provider/mock';
|
||||
|
||||
import { Derive } from '.';
|
||||
|
||||
const testFunction = (api: ApiInterface$Rx) => {
|
||||
const testFunction = (api: ApiRx) => {
|
||||
return <
|
||||
Section extends keyof Derive,
|
||||
Method extends keyof (typeof api.derive[Section])
|
||||
|
||||
@@ -2,8 +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 { Observable } from 'rxjs';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { AnyFunction } from '@polkadot/types/types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import * as accounts from './accounts';
|
||||
import * as balances from './balances';
|
||||
@@ -15,65 +17,54 @@ import * as staking from './staking';
|
||||
|
||||
export * from './type';
|
||||
|
||||
// Put all derived functions in an object, for easier Object.keys()-ing.
|
||||
const functions = { accounts, balances, chain, democracy, session, staking };
|
||||
|
||||
/**
|
||||
* T represents the section here (chain, balances...), and P represents
|
||||
* the function name (bestNumber, sessionProgress...).
|
||||
*/
|
||||
type ReturnTypes<T extends Record<keyof T, (...args: any[]) => any>> = {
|
||||
[P in keyof T]: ReturnType<T[P]>
|
||||
export type ReturnTypes<Section> = {
|
||||
[Method in keyof Section]: Section[Method] extends AnyFunction
|
||||
? ReturnType<Section[Method]> // ReturnType<Section[Method]> will be the inner function, i.e. without (api) argument
|
||||
: never;
|
||||
};
|
||||
|
||||
export interface DeriveCustomMethod {
|
||||
(api: ApiInterface$Rx): (...args: Array<any>) => Observable<any>;
|
||||
}
|
||||
export type DeriveSections<AllSections> = {
|
||||
[Section in keyof AllSections]: ReturnTypes<AllSections[Section]>
|
||||
};
|
||||
|
||||
export interface DeriveCustom {
|
||||
[index: string]: {
|
||||
[index: string]: DeriveCustomMethod
|
||||
[index: string]: (api: ApiInterface$Rx) => (...args: Array<any>) => Observable<any>
|
||||
};
|
||||
}
|
||||
|
||||
export interface Derive {
|
||||
accounts: ReturnTypes<typeof accounts>;
|
||||
balances: ReturnTypes<typeof balances>;
|
||||
chain: ReturnTypes<typeof chain>;
|
||||
contract: ReturnTypes<typeof contract>;
|
||||
democracy: ReturnTypes<typeof democracy>;
|
||||
session: ReturnTypes<typeof session>;
|
||||
staking: ReturnTypes<typeof staking>;
|
||||
/**
|
||||
* Returns an object that will inject `api` into all the functions inside
|
||||
* `allSections`, and keep the object architecture of `allSections`.
|
||||
*/
|
||||
function injectFunctions<AllSections> (api: ApiInterface$Rx, allSections: AllSections) {
|
||||
return Object
|
||||
.keys(allSections)
|
||||
.reduce((deriveAcc, sectionName) => {
|
||||
const section = allSections[sectionName as keyof AllSections];
|
||||
|
||||
deriveAcc[sectionName as keyof AllSections] = Object
|
||||
.keys(section)
|
||||
.reduce((sectionAcc, _methodName) => {
|
||||
const methodName = _methodName as keyof typeof section;
|
||||
// Not sure what to do here, casting as any. Though the final types are good
|
||||
const method = (section[methodName] as any)(api);
|
||||
// idem
|
||||
(sectionAcc as any)[methodName] = method;
|
||||
|
||||
return sectionAcc;
|
||||
}, {} as ReturnTypes<typeof section>);
|
||||
|
||||
return deriveAcc;
|
||||
}, {} as DeriveSections<AllSections>);
|
||||
}
|
||||
|
||||
function injectFunctions (api: ApiInterface$Rx, derive: Derive, functions: DeriveCustom): Derive {
|
||||
Object
|
||||
.entries(functions)
|
||||
.forEach(([sectionName, section]) => {
|
||||
const sectionKey = sectionName as keyof Derive;
|
||||
export const derive = { accounts, balances, chain, contract, democracy, session, staking };
|
||||
export type Derive = typeof derive;
|
||||
|
||||
if (!derive[sectionKey]) {
|
||||
derive[sectionKey] = {} as any;
|
||||
}
|
||||
|
||||
const result = derive[sectionKey];
|
||||
|
||||
Object
|
||||
.entries(section)
|
||||
.forEach(([methodName, method]) => {
|
||||
// @ts-ignore No idea how to make this work...
|
||||
result[methodName as keyof section] = method(api);
|
||||
});
|
||||
});
|
||||
|
||||
return derive;
|
||||
}
|
||||
|
||||
export default function decorateDerive (api: ApiInterface$Rx, custom: DeriveCustom = {}): Derive {
|
||||
const derive = {} as Derive;
|
||||
|
||||
injectFunctions(api, derive, functions);
|
||||
injectFunctions(api, derive, custom);
|
||||
|
||||
return derive;
|
||||
export default function decorateDerive (api: ApiInterface$Rx, custom: DeriveCustom = {}) {
|
||||
return {
|
||||
...injectFunctions(api, derive),
|
||||
...injectFunctions(api, custom)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user