Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/api/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,21 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
}

private async loadMeta (): Promise<boolean> {
const { metadata = {} } = this._options;

// only load from on-chain if we are not a clone (default path), alternatively
// just use the values from the source instance provided
if (!this._options.source || !this._options.source._isReady) {
this._runtimeMetadata = await this._rpcBase.state.getMetadata();
this._runtimeVersion = await this._rpcBase.chain.getRuntimeVersion();
this._genesisHash = await this._rpcBase.chain.getBlockHash(0);
[this._genesisHash, this._runtimeVersion] = await Promise.all([
this._rpcBase.chain.getBlockHash(0),
this._rpcBase.chain.getRuntimeVersion()
]);
const metadataKey = `${this._genesisHash}-${(this._runtimeVersion as RuntimeVersion).specVersion}`;
if (metadataKey in metadata) {
this._runtimeMetadata = new Metadata(metadata[metadataKey]);
} else {
this._runtimeMetadata = await this._rpcBase.state.getMetadata();
}

// get unique types & validate
this.runtimeMetadata.getUniqTypes(false);
Expand Down
52 changes: 52 additions & 0 deletions packages/api/src/promise/Api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2017-2019 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { ApiPromise } from '@polkadot/api';
import { ApiOptions } from '@polkadot/api/types';
import { Hash, Metadata } from '@polkadot/types';

import Mock from '@polkadot/rpc-provider/mock/index';

describe.skip('Metadata queries', () => {
let mock: Mock;

beforeEach(() => {
jest.setTimeout(3000000);
mock = new Mock();
});

it('Create API instance with metadata map and makes the runtime, rpc, state & extrinsics available', async () => {
const rpcData = await mock.send('state_getMetadata',[]);
const genesisHash = new Hash(await mock.send('chain_getBlockHash',[])).toHex();
const specVersion = 0;
const metadata: any = {};
const key = `${genesisHash}-${specVersion}`;
metadata[key] = rpcData;
const api = await ApiPromise.create({ provider: mock, metadata } as ApiOptions);

expect(api.genesisHash).toBeDefined();
expect(api.runtimeMetadata.toJSON()).toEqual(new Metadata(rpcData).toJSON());
expect(api.runtimeVersion).toBeDefined();
expect(api.rpc).toBeDefined();
expect(api.query).toBeDefined();
expect(api.tx).toBeDefined();
expect(api.derive).toBeDefined();
});

it('Create API instance without metadata and makes the runtime, rpc, state & extrinsics available', async () => {
const metadata = {};
const api = await ApiPromise.create({
provider: mock, metadata
});

expect(api.genesisHash).toBeDefined();
expect(api.runtimeMetadata).toBeDefined();
expect(api.runtimeVersion).toBeDefined();
expect(api.rpc).toBeDefined();
expect(api.query).toBeDefined();
expect(api.tx).toBeDefined();
expect(api.derive).toBeDefined();
});

});
7 changes: 7 additions & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ export interface ApiOptions {
* @description Add custom derives to be injected
*/
derives?: DeriveCustom;
/**
* @description prebundles is a map of 'genesis hash and runtime spec version' as key to metadata's hex string
* if genesis hash and runtime spec version matches, then use metadata, else fetch it from chain
*/
metadata?: {
[key: string]: string
};
/**
* @description Transport Provider from rpc-provider. If not specified, it will default to
* connecting to a WsProvider connecting localhost with the default port, i.e. `ws://127.0.0.1:9944`
Expand Down