Skip to content

Commit f4115c5

Browse files
authored
feat(client): add chain spec api (#683)
Unified chain spec api for both v2 and legacy client, we removed the method to fetch genesis hash since it's already fetched and available within the client during initialization. ```ts const client = await DedotClient.new({ ... }); console.log('chain genesisHash', client.genesisHash); console.log('chain name', await client.chainSpec.chainName()); console.log('chain props', await client.chainSpec.properties()); ```
1 parent 46eb8c4 commit f4115c5

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

examples/scripts/chain-spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DedotClient, WsProvider } from 'dedot';
2+
import { RpcVersion } from 'dedot/types';
3+
4+
const fetchChainSpec = async (rpcVersion: RpcVersion) => {
5+
console.log(`Fetching chain spec via ${rpcVersion} client`);
6+
const client = await DedotClient.new({
7+
provider: new WsProvider('wss://westend-rpc.polkadot.io'),
8+
rpcVersion,
9+
});
10+
11+
console.log('chain name', await client.chainSpec.chainName());
12+
console.log('chain props', await client.chainSpec.properties());
13+
console.log('chain genesisHash', client.genesisHash);
14+
15+
await client.disconnect();
16+
};
17+
18+
await fetchChainSpec('v2');
19+
await fetchChainSpec('legacy');

packages/api/src/client/BaseSubstrateClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import type {
3939
ApiEvent,
4040
ApiOptions,
4141
BlockExplorer,
42+
IChainSpec,
4243
ISubstrateClient,
4344
ISubstrateClientAt,
4445
JsonRpcClientOptions,
@@ -532,4 +533,8 @@ export abstract class BaseSubstrateClient<
532533
sendTx(tx: HexString | Extrinsic, callback?: Callback): TxUnsub {
533534
throw new Error('Unimplemented!');
534535
}
536+
537+
get chainSpec(): IChainSpec {
538+
throw new Error('Unimplemented!');
539+
}
535540
}

packages/api/src/client/DedotClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ISubstrateClient,
2222
ISubstrateClientAt,
2323
SubstrateRuntimeVersion,
24+
IChainSpec,
2425
} from '../types.js';
2526
import { LegacyClient } from './LegacyClient.js';
2627
import { V2Client } from './V2Client.js';
@@ -144,6 +145,10 @@ export class DedotClient<
144145
return this.#client.block;
145146
}
146147

148+
get chainSpec(): IChainSpec {
149+
return this.#client.chainSpec;
150+
}
151+
147152
async connect(): Promise<this> {
148153
await this.#client.connect();
149154

packages/api/src/client/LegacyClient.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BlockHash, type Extrinsic, Hash, Header, PortableRegistry, RuntimeVersion } from '@dedot/codecs';
22
import type { JsonRpcProvider } from '@dedot/providers';
33
import { Callback, GenericSubstrateApi, TxUnsub, Unsub } from '@dedot/types';
4+
import { ChainProperties } from '@dedot/types/json-rpc';
45
import { assert, HexString } from '@dedot/utils';
56
import type { SubstrateApi } from '../chaintypes/index.js';
67
import {
@@ -15,7 +16,7 @@ import {
1516
import { SubmittableExtrinsic } from '../extrinsic/submittable/SubmittableExtrinsic.js';
1617
import { newProxyChain } from '../proxychain.js';
1718
import { BaseStorageQuery, LegacyStorageQuery } from '../storage/index.js';
18-
import type { ApiOptions, BlockExplorer, ISubstrateClientAt, SubstrateRuntimeVersion } from '../types.js';
19+
import type { ApiOptions, BlockExplorer, IChainSpec, ISubstrateClientAt, SubstrateRuntimeVersion } from '../types.js';
1920
import { BaseSubstrateClient, ensurePresence } from './BaseSubstrateClient.js';
2021
import { LegacyBlockExplorer } from './explorer/index.js';
2122

@@ -283,6 +284,17 @@ export class LegacyClient<ChainApi extends GenericSubstrateApi = SubstrateApi> /
283284
return ensurePresence(this._blockExplorer);
284285
}
285286

287+
get chainSpec(): IChainSpec {
288+
return {
289+
chainName: (): Promise<string> => {
290+
return this.rpc.system_chain();
291+
},
292+
properties: (): Promise<ChainProperties> => {
293+
return this.rpc.system_properties();
294+
},
295+
};
296+
}
297+
286298
/**
287299
* Create a new API instance at a specific block hash
288300
* This is useful when we want to inspect the state of the chain at a specific block hash

packages/api/src/json-rpc/group/ChainSpec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Properties } from '@dedot/types/json-rpc';
22
import { HexString } from '@dedot/utils';
3-
import { IJsonRpcClient } from '../../types.js';
3+
import { IJsonRpcClient, IChainSpec } from '../../types.js';
44
import { JsonRpcGroup, JsonRpcGroupOptions } from './JsonRpcGroup.js';
55

6-
export class ChainSpec extends JsonRpcGroup {
6+
export class ChainSpec extends JsonRpcGroup implements IChainSpec {
77
constructor(client: IJsonRpcClient<any>, options?: Partial<JsonRpcGroupOptions>) {
88
super(client, { prefix: 'chainSpec', supportedVersions: ['unstable', 'v1'], ...options });
99
}

packages/api/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
TxUnsub,
1616
Unsub,
1717
} from '@dedot/types';
18+
import { Properties } from '@dedot/types/json-rpc';
1819
import type { HashFn, HexString, IEventEmitter } from '@dedot/utils';
1920
import type { SubstrateApi } from './chaintypes/index.js';
2021
import type { AnySignedExtension } from './extrinsic/index.js';
@@ -184,6 +185,11 @@ export interface BlockExplorer {
184185
body(numberOrHash: number | BlockHash): Promise<HexString[]>;
185186
}
186187

188+
export interface IChainSpec {
189+
chainName(): Promise<string>;
190+
properties(): Promise<Properties>;
191+
}
192+
187193
/**
188194
* A generic interface for Substrate clients
189195
*/
@@ -195,6 +201,7 @@ export interface ISubstrateClient<
195201
options: ApiOptions;
196202
tx: ChainApi['tx'];
197203

204+
chainSpec: IChainSpec;
198205
block: BlockExplorer;
199206

200207
at<ChainApiAt extends GenericSubstrateApi = ChainApi>(hash: BlockHash): Promise<ISubstrateClientAt<ChainApiAt>>;

0 commit comments

Comments
 (0)