Expose whenReady and extrinsics/storage (#199)
* Expose whenReady and extrinsics/storage * Load genesisHash, expose * Fix import * whenReady -> Observable<boolean> * Check isConnected before making call * Fix import order
This commit is contained in:
@@ -5,8 +5,13 @@
|
||||
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
|
||||
import { Method } from '@polkadot/jsonrpc/types';
|
||||
|
||||
import { Observable, combineLatest } from 'rxjs';
|
||||
import { Observable, combineLatest, from } from 'rxjs';
|
||||
import { defaultIfEmpty, map } from 'rxjs/operators';
|
||||
import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
|
||||
import extrinsicsStatic from '@polkadot/extrinsics/static';
|
||||
import storageFromMeta from '@polkadot/storage/fromMetadata';
|
||||
import storageStatic from '@polkadot/storage/static';
|
||||
import { Hash } from '@polkadot/types/index';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
import assert from '@polkadot/util/assert';
|
||||
import isUndefined from '@polkadot/util/is/undefined';
|
||||
@@ -19,10 +24,47 @@ const defaultMapFn = (result: any): any =>
|
||||
// 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;
|
||||
protected _api: RxApiInterface;
|
||||
protected _genesisHash: Hash;
|
||||
|
||||
// Observable that returns the first time we are connected and loaded
|
||||
whenReady: Observable<boolean>;
|
||||
|
||||
constructor (api: RxApiInterface) {
|
||||
this.api = api;
|
||||
this._api = api;
|
||||
this._genesisHash = new Hash();
|
||||
this.whenReady = from(this.init());
|
||||
}
|
||||
|
||||
static extrinsics = extrinsicsStatic;
|
||||
static storage = storageStatic;
|
||||
|
||||
private init (): Promise<boolean> {
|
||||
let isReady: boolean = false;
|
||||
|
||||
return new Promise((resolveReady) => {
|
||||
// On connection, load data from chain
|
||||
this.isConnected().subscribe(async (isConnected) => {
|
||||
if (!isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const meta = await this._api.state.getMetadata().toPromise();
|
||||
|
||||
this._genesisHash = await this._api.state.getBlockHash(0).toPromise();
|
||||
ApiBase.extrinsics = extrinsicsFromMeta(meta);
|
||||
ApiBase.storage = storageFromMeta(meta);
|
||||
|
||||
if (!isReady) {
|
||||
isReady = true;
|
||||
resolveReady(true);
|
||||
}
|
||||
} catch (error) {
|
||||
// swallow
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected combine = <T, R> (observables: Array<Observable<any>>, mapfn: MapFn<R, T> = defaultMapFn): Observable<T> => {
|
||||
@@ -33,12 +75,16 @@ export default class ApiBase {
|
||||
);
|
||||
}
|
||||
|
||||
get genesisHash (): Hash {
|
||||
return this._genesisHash;
|
||||
}
|
||||
|
||||
isConnected = (): Observable<boolean> => {
|
||||
return this.api.isConnected();
|
||||
return this._api.isConnected();
|
||||
}
|
||||
|
||||
rawCall = <T> ({ method, section }: Method, ...params: Array<any>): Observable<T> => {
|
||||
const apiSection = this.api[section as keyof RxApiInterface] as RxApiInterface$Section;
|
||||
const apiSection = this._api[section as keyof RxApiInterface] as RxApiInterface$Section;
|
||||
|
||||
assert(apiSection, `Unable to find 'api.${section}'`);
|
||||
|
||||
@@ -61,7 +107,7 @@ export default class ApiBase {
|
||||
}
|
||||
|
||||
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
|
||||
return this.api.state
|
||||
return this._api.state
|
||||
.storage(keys)
|
||||
.pipe(
|
||||
map((result?: any) =>
|
||||
|
||||
@@ -23,11 +23,11 @@ export default class ApiCalls extends ApiQueries {
|
||||
}
|
||||
|
||||
chainGetBlock = (hash: Uint8Array): Observable<SignedBlock | undefined> => {
|
||||
return this.api.chain.getBlock(hash);
|
||||
return this._api.chain.getBlock(hash);
|
||||
}
|
||||
|
||||
chainNewHead = (): Observable<Header | undefined> => {
|
||||
return this.api.chain.newHead().pipe(
|
||||
return this._api.chain.newHead().pipe(
|
||||
defaultIfEmpty()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import { RxFees } from './types';
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import storage from '@polkadot/storage/static';
|
||||
import { AccountId, Balance, Bool, BlockNumber, Index, Moment, Perbill, PropIndex, ReferendumIndex, u32 } from '@polkadot/types/index';
|
||||
import { Tuple } from '@polkadot/types/codec';
|
||||
import storage from '@polkadot/storage/testing';
|
||||
|
||||
import ApiBase from './Base';
|
||||
import { RxProposal, RxProposalDeposits, RxReferendum } from './classes';
|
||||
|
||||
@@ -10,8 +10,8 @@ import { ProviderInterface$Emitted } from '../types';
|
||||
import { MockState, MockState$Db, MockState$Subscriptions } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import storage from '@polkadot/storage/static';
|
||||
import Header from '@polkadot/types/Header';
|
||||
import storage from '@polkadot/storage/testing';
|
||||
import bnToU8a from '@polkadot/util/bn/toU8a';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
import randomAsU8a from '@polkadot/util-crypto/random/asU8a';
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import storage from '@polkadot/storage/testing';
|
||||
import Ws from '@polkadot/api-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import RxApi from '../../src';
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import storage from '@polkadot/storage/testing';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import Api from './index';
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import storage from '@polkadot/storage/testing';
|
||||
import Ws from '@polkadot/api-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import Api from '../../src';
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import storage from '@polkadot/storage/testing';
|
||||
import Ws from '@polkadot/api-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import Api from '../../src';
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import fromMetadata from './fromMetadata';
|
||||
|
||||
// Use the pre-generated metadata
|
||||
const metadata = new Metadata().fromJSON(json);
|
||||
const newExtrinsics = fromMetadata({}, metadata);
|
||||
const newExtrinsics = fromMetadata(metadata);
|
||||
|
||||
describe('fromMetadata', () => {
|
||||
it('should throw if an incorrect number of args is supplied', () => {
|
||||
|
||||
@@ -7,6 +7,7 @@ import Metadata, { RuntimeModuleMetadata } from '@polkadot/types/Metadata';
|
||||
|
||||
import createUnchecked from './utils/createUnchecked';
|
||||
import { Extrinsics, ModuleExtrinsics } from './types';
|
||||
import extrinsics from './index';
|
||||
|
||||
/**
|
||||
* Extend a storage object with the storage modules & module functions present
|
||||
@@ -15,7 +16,7 @@ import { Extrinsics, ModuleExtrinsics } from './types';
|
||||
* @param extrinsics - An extrinsics object to be extended.
|
||||
* @param metadata - The metadata to extend the storage object against.
|
||||
*/
|
||||
export default function fromMetadata (extrinsics: Extrinsics, metadata: Metadata) {
|
||||
export default function fromMetadata (metadata: Metadata): Extrinsics {
|
||||
// Dont' clobber the input, create new
|
||||
const result = Object.keys(extrinsics).reduce((result, key) => {
|
||||
result[key] = extrinsics[key];
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
|
||||
import extrinsics from './testing';
|
||||
import extrinsics from './static';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
|
||||
@@ -5,10 +5,8 @@
|
||||
import metadataRpc from '@polkadot/types/Metadata.rpc';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
|
||||
import extrinsics from './index';
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
export default fromMetadata(
|
||||
extrinsics,
|
||||
new Metadata().fromJSON(metadataRpc)
|
||||
);
|
||||
@@ -6,7 +6,7 @@ import Api from '@polkadot/api';
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
import Ws from '@polkadot/api-provider/ws';
|
||||
|
||||
import extrinsics from '../../src/testing';
|
||||
import extrinsics from '../../src/static';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import fromMetadata from './fromMetadata';
|
||||
|
||||
// Use the pre-generated metadata
|
||||
const metadata = new Metadata().fromJSON(json);
|
||||
const newStorage = fromMetadata({}, metadata);
|
||||
const newStorage = fromMetadata(metadata);
|
||||
|
||||
describe('fromMetadata', () => {
|
||||
it('should throw if the storage function expects an argument', () => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import Metadata from '@polkadot/types/Metadata';
|
||||
import { stringLowerFirst } from '@polkadot/util/string';
|
||||
|
||||
import createFunction from './utils/createFunction';
|
||||
import storage from './index';
|
||||
|
||||
/**
|
||||
* Extend a storage object with the storage modules & module functions present
|
||||
@@ -16,7 +17,7 @@ import createFunction from './utils/createFunction';
|
||||
* @param storage - A storage object to be extended.
|
||||
* @param metadata - The metadata to extend the storage object against.
|
||||
*/
|
||||
export default function fromMetadata (storage: Storage, metadata: Metadata) {
|
||||
export default function fromMetadata (metadata: Metadata): Storage {
|
||||
const result = Object.keys(storage).reduce((result, key) => {
|
||||
result[key] = storage[key];
|
||||
|
||||
|
||||
@@ -5,10 +5,8 @@
|
||||
import metadataRpc from '@polkadot/types/Metadata.rpc';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
|
||||
import storage from './index';
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
export default fromMetadata(
|
||||
storage,
|
||||
new Metadata().fromJSON(metadataRpc)
|
||||
);
|
||||
Reference in New Issue
Block a user