Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
36 changes: 25 additions & 11 deletions packages/api/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
const thisProvider = options.source
? options.source._rpcBase._provider.clone()
: options.provider;

Comment thread
KarishmaBothara marked this conversation as resolved.
const genesisSpecToMetaDataMap = options.GenesisSpecToMetaDataMap ? options.GenesisSpecToMetaDataMap : {};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use deconstructing to assign a default value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenesisSpecToMetaDataMap first G should be lowercase

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a better name, something like prebundles

this._options = options;
this._type = type;
this._eventemitter = new EventEmitter();
Expand All @@ -125,7 +125,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
this.registerTypes(options.types);
}

this.init();
this.init(genesisSpecToMetaDataMap);
}

/**
Expand Down Expand Up @@ -331,7 +331,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
this._eventemitter.emit(type, ...args);
}

private init (): void {
private init (genesisSpecToMetaDataMap: {[key: string]: string}): void {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change signature of init(), you can use this.options to get visit the value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for loadMeta()

let healthTimer: NodeJS.Timeout | null = null;

this._rpcBase._provider.on('disconnected', () => {
Expand All @@ -352,7 +352,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement

try {
const [hasMeta, cryptoReady] = await Promise.all([
this.loadMeta(),
this.loadMeta(genesisSpecToMetaDataMap),
cryptoWaitReady()
]);

Expand All @@ -361,7 +361,6 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement

this.emit('ready', this);
}

healthTimer = setInterval(() => {
this._rpcRx.system.health().toPromise().catch(() => {
// ignore
Expand All @@ -373,15 +372,30 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
});
}

private async loadMeta (): Promise<boolean> {
private async loadMeta (genesisSpecToMetaDataMap: {[key: string]: string}): Promise<boolean> {
// 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);

// get unique types & validate
[this._genesisHash, this._runtimeVersion] = await Promise.all([
this._rpcBase.chain.getBlockHash(0),
this._rpcBase.chain.getRuntimeVersion()
]);
let key = '';
Comment thread
KarishmaBothara marked this conversation as resolved.
Outdated
if (this._runtimeVersion) {
key = `${this._genesisHash}${this._runtimeVersion.specVersion}`;
}
if (key in genesisSpecToMetaDataMap) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use a for loop? i think you have a key already, so either it exists or not

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no for loop used

try {
const rpcData = genesisSpecToMetaDataMap[key];
const metadata = new Metadata(rpcData);
this._runtimeMetadata = metadata;
this.runtimeMetadata.getUniqTypes(false);
} catch (e) {
this._runtimeMetadata = await this._rpcBase.state.getMetadata();
}
} else {
this._runtimeMetadata = await this._rpcBase.state.getMetadata();
}
this.runtimeMetadata.getUniqTypes(false);
} else {
this._runtimeMetadata = this._options.source.runtimeMetadata;
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export interface ApiOptions {
* uses types not available in the base Substrate runtime.
*/
types?: RegistryTypes;

GenesisSpecToMetaDataMap?: {
[key: string]: string
Comment thread
KarishmaBothara marked this conversation as resolved.
Outdated
};
}

export interface ApiInterface$Decorated<CodecResult, SubscriptionResult> {
Expand Down
71 changes: 71 additions & 0 deletions packages/api/test/e2e/promise-metadata.spec.js

Large diffs are not rendered by default.