Adjust crowdloan types to lookups (#4065)

* Adjust crowdloan types to lookups

* Remove debug hex...
This commit is contained in:
Jaco
2021-10-15 10:15:02 +03:00
committed by GitHub
parent ed773dd158
commit e305f2c11a
6 changed files with 25 additions and 23 deletions
@@ -4,7 +4,8 @@
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { Option } from '@polkadot/types';
import type { FundInfo, ParaId } from '@polkadot/types/interfaces';
import type { PolkadotRuntimeCommonCrowdloanFundInfo } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import { map } from 'rxjs';
@@ -13,7 +14,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import { memo } from '../util';
function createChildKey ({ trieIndex }: FundInfo): string {
function createChildKey ({ trieIndex }: PolkadotRuntimeCommonCrowdloanFundInfo): string {
return u8aToHex(
u8aConcat(
':child_storage:default:',
@@ -24,9 +25,9 @@ function createChildKey ({ trieIndex }: FundInfo): string {
);
}
export function childKey (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | ParaId) => Observable<string | null> {
return memo(instanceId, (paraId: string | number | ParaId): Observable<string | null> =>
api.query.crowdloan.funds<Option<FundInfo>>(paraId).pipe(
export function childKey (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | BN) => Observable<string | null> {
return memo(instanceId, (paraId: string | number | BN): Observable<string | null> =>
api.query.crowdloan.funds<Option<PolkadotRuntimeCommonCrowdloanFundInfo>>(paraId).pipe(
map((optInfo) =>
optInfo.isSome
? createChildKey(optInfo.unwrap())
@@ -4,7 +4,7 @@
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { StorageKey } from '@polkadot/types';
import type { ParaId } from '@polkadot/types/interfaces';
import type { BN } from '@polkadot/util';
import type { DeriveContributions } from '../types';
import { BehaviorSubject, combineLatest, EMPTY, map, of, startWith, switchMap, tap, toArray } from 'rxjs';
@@ -22,7 +22,7 @@ interface Changes {
const PAGE_SIZE_K = 1000; // limit aligned with the 1k on the node (trie lookups are heavy)
function _getUpdates (api: ApiInterfaceRx, paraId: string | number | ParaId): Observable<Changes> {
function _getUpdates (api: ApiInterfaceRx, paraId: string | number | BN): Observable<Changes> {
let added: string[] = [];
let removed: string[] = [];
@@ -43,7 +43,7 @@ function _getUpdates (api: ApiInterfaceRx, paraId: string | number | ParaId): Ob
);
}
function _eventTriggerAll (api: ApiInterfaceRx, paraId: string | number | ParaId): Observable<string> {
function _eventTriggerAll (api: ApiInterfaceRx, paraId: string | number | BN): Observable<string> {
return api.query.system.events().pipe(
switchMap((events): Observable<string> => {
const items = events.filter(({ event: { data: [eventParaId], method, section } }) =>
@@ -79,12 +79,10 @@ function _getKeysPaged (api: ApiInterfaceRx, childKey: string): Observable<Stora
);
}
function _getAll (api: ApiInterfaceRx, paraId: string | number | ParaId, childKey: string): Observable<string[]> {
function _getAll (api: ApiInterfaceRx, paraId: string | number | BN, childKey: string): Observable<string[]> {
return _eventTriggerAll(api, paraId).pipe(
switchMap(() =>
// FIXME Needs testing and being enabled
// eslint-disable-next-line no-constant-condition
isFunction(api.rpc.childstate.getKeysPaged) && false
isFunction(api.rpc.childstate.getKeysPaged)
? _getKeysPaged(api, childKey)
: api.rpc.childstate.getKeys(childKey, '0x')
),
@@ -94,7 +92,7 @@ function _getAll (api: ApiInterfaceRx, paraId: string | number | ParaId, childKe
);
}
function _contributions (api: ApiInterfaceRx, paraId: string | number | ParaId, childKey: string): Observable<DeriveContributions> {
function _contributions (api: ApiInterfaceRx, paraId: string | number | BN, childKey: string): Observable<DeriveContributions> {
return combineLatest([
_getAll(api, paraId, childKey),
_getUpdates(api, paraId)
@@ -122,8 +120,8 @@ function _contributions (api: ApiInterfaceRx, paraId: string | number | ParaId,
);
}
export function contributions (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | ParaId) => Observable<DeriveContributions> {
return memo(instanceId, (paraId: string | number | ParaId): Observable<DeriveContributions> =>
export function contributions (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | BN) => Observable<DeriveContributions> {
return memo(instanceId, (paraId: string | number | BN): Observable<DeriveContributions> =>
api.derive.crowdloan.childKey(paraId).pipe(
switchMap((childKey) =>
childKey
@@ -3,7 +3,7 @@
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { ParaId } from '@polkadot/types/interfaces';
import type { BN } from '@polkadot/util';
import type { DeriveOwnContributions } from '../types';
import { combineLatest, EMPTY, map, of, startWith, switchMap } from 'rxjs';
@@ -30,7 +30,7 @@ function _getValues (api: ApiInterfaceRx, childKey: string, keys: string[]): Obs
);
}
function _watchOwnChanges (api: ApiInterfaceRx, paraId: string | number | ParaId, childkey: string, keys: string[]): Observable<DeriveOwnContributions> {
function _watchOwnChanges (api: ApiInterfaceRx, paraId: string | number | BN, childkey: string, keys: string[]): Observable<DeriveOwnContributions> {
return api.query.system.events().pipe(
switchMap((events): Observable<DeriveOwnContributions> => {
const changes = extractContributed(paraId, events);
@@ -47,7 +47,7 @@ function _watchOwnChanges (api: ApiInterfaceRx, paraId: string | number | ParaId
);
}
function _contributions (api: ApiInterfaceRx, paraId: string | number | ParaId, childKey: string, keys: string[]): Observable<DeriveOwnContributions> {
function _contributions (api: ApiInterfaceRx, paraId: string | number | BN, childKey: string, keys: string[]): Observable<DeriveOwnContributions> {
return combineLatest([
_getValues(api, childKey, keys),
_watchOwnChanges(api, paraId, childKey, keys)
@@ -59,8 +59,8 @@ function _contributions (api: ApiInterfaceRx, paraId: string | number | ParaId,
);
}
export function ownContributions (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | ParaId, keys: string[]) => Observable<DeriveOwnContributions> {
return memo(instanceId, (paraId: string | number | ParaId, keys: string[]): Observable<DeriveOwnContributions> =>
export function ownContributions (instanceId: string, api: ApiInterfaceRx): (paraId: string | number | BN, keys: string[]) => Observable<DeriveOwnContributions> {
return memo(instanceId, (paraId: string | number | BN, keys: string[]): Observable<DeriveOwnContributions> =>
api.derive.crowdloan.childKey(paraId).pipe(
switchMap((childKey) =>
childKey && keys.length
+3 -2
View File
@@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import type { Vec } from '@polkadot/types';
import type { EventRecord, ParaId } from '@polkadot/types/interfaces';
import type { EventRecord } from '@polkadot/types/interfaces';
import type { BN } from '@polkadot/util';
interface Changes {
added: string[];
@@ -10,7 +11,7 @@ interface Changes {
removed: string[];
}
export function extractContributed (paraId: string | number | ParaId, events: Vec<EventRecord>): Changes {
export function extractContributed (paraId: string | number | BN, events: Vec<EventRecord>): Changes {
const added: string[] = [];
const removed: string[] = [];