@dedot/api 0.0.1-next.cfee875e.21 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/chaintypes/substrate/tx.d.ts +2 -2
  2. package/chaintypes/substrate/types.d.ts +377 -377
  3. package/cjs/client/BaseSubstrateClient.js +21 -0
  4. package/cjs/client/DedotClient.js +5 -4
  5. package/cjs/client/LegacyClient.js +6 -2
  6. package/cjs/executor/ErrorExecutor.js +3 -3
  7. package/cjs/executor/EventExecutor.js +2 -2
  8. package/cjs/executor/StorageQueryExecutor.js +1 -1
  9. package/cjs/executor/TxExecutor.js +3 -3
  10. package/cjs/executor/v2/StorageQueryExecutorV2.js +4 -4
  11. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +2 -2
  12. package/cjs/extrinsic/extensions/known/StorageWeightReclaim.js +10 -0
  13. package/cjs/extrinsic/extensions/known/index.js +2 -0
  14. package/cjs/extrinsic/submittable/BaseSubmittableExtrinsic.js +31 -1
  15. package/cjs/extrinsic/submittable/SubmittableExtrinsic.js +7 -4
  16. package/cjs/extrinsic/submittable/SubmittableExtrinsicV2.js +11 -11
  17. package/cjs/extrinsic/submittable/utils.js +66 -1
  18. package/cjs/storage/QueryableStorage.js +17 -17
  19. package/client/BaseSubstrateClient.d.ts +15 -9
  20. package/client/BaseSubstrateClient.js +22 -1
  21. package/client/DedotClient.d.ts +2 -2
  22. package/client/DedotClient.js +5 -4
  23. package/client/LegacyClient.d.ts +3 -2
  24. package/client/LegacyClient.js +6 -2
  25. package/executor/ErrorExecutor.js +3 -3
  26. package/executor/EventExecutor.js +2 -2
  27. package/executor/Executor.d.ts +12 -12
  28. package/executor/StorageQueryExecutor.js +1 -1
  29. package/executor/TxExecutor.js +3 -3
  30. package/executor/v2/StorageQueryExecutorV2.js +4 -4
  31. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +2 -2
  32. package/extrinsic/extensions/known/StorageWeightReclaim.d.ts +6 -0
  33. package/extrinsic/extensions/known/StorageWeightReclaim.js +6 -0
  34. package/extrinsic/extensions/known/index.js +2 -0
  35. package/extrinsic/submittable/BaseSubmittableExtrinsic.d.ts +2 -0
  36. package/extrinsic/submittable/BaseSubmittableExtrinsic.js +32 -2
  37. package/extrinsic/submittable/SubmittableExtrinsic.js +7 -4
  38. package/extrinsic/submittable/SubmittableExtrinsicV2.js +11 -11
  39. package/extrinsic/submittable/SubmittableResult.d.ts +4 -4
  40. package/extrinsic/submittable/utils.d.ts +10 -1
  41. package/extrinsic/submittable/utils.js +65 -1
  42. package/json-rpc/JsonRpcClient.d.ts +6 -5
  43. package/package.json +11 -11
  44. package/storage/QueryableStorage.js +17 -17
  45. package/types.d.ts +9 -1
@@ -1,4 +1,4 @@
1
- import { blake2AsU8a, hexToU8a, isFunction } from '@dedot/utils';
1
+ import { assert, blake2AsU8a, hexToU8a, isFunction } from '@dedot/utils';
2
2
  export function isKeyringPair(account) {
3
3
  return isFunction(account.sign);
4
4
  }
@@ -13,3 +13,67 @@ export function signRaw(signerPair, raw) {
13
13
  const toSignRaw = u8a.length > 256 ? blake2AsU8a(u8a, 256) : u8a;
14
14
  return signerPair.sign(toSignRaw, { withType: true });
15
15
  }
16
+ /**
17
+ * Convert transaction status to transaction event
18
+ *
19
+ * Ref: https://github.com/paritytech/polkadot-sdk/blob/98a364fe6e7abf10819f5fddd3de0588f7c38700/substrate/client/rpc-spec-v2/src/transaction/transaction.rs#L132-L159
20
+ * @param txStatus
21
+ * @param txIndex
22
+ */
23
+ export function toTxStatus(txStatus, txIndex) {
24
+ switch (txStatus.type) {
25
+ case 'Ready':
26
+ case 'Future':
27
+ return { type: 'Validated' };
28
+ case 'Broadcast':
29
+ return { type: 'Broadcasting' };
30
+ case 'Retracted':
31
+ return { type: 'NoLongerInBestChain' };
32
+ case 'InBlock':
33
+ assert(txIndex, 'TxIndex is required');
34
+ return {
35
+ type: 'BestChainBlockIncluded',
36
+ value: {
37
+ blockHash: txStatus.value,
38
+ txIndex,
39
+ },
40
+ };
41
+ case 'Finalized':
42
+ assert(txIndex, 'TxIndex is required');
43
+ return {
44
+ type: 'Finalized',
45
+ value: {
46
+ blockHash: txStatus.value,
47
+ txIndex,
48
+ },
49
+ };
50
+ case 'FinalityTimeout':
51
+ return {
52
+ type: 'Drop',
53
+ value: {
54
+ error: 'Maximum number of finality watchers has been reached',
55
+ },
56
+ };
57
+ case 'Dropped':
58
+ return {
59
+ type: 'Drop',
60
+ value: {
61
+ error: 'Extrinsic dropped from the pool due to exceeding limits',
62
+ },
63
+ };
64
+ case 'Usurped':
65
+ return {
66
+ type: 'Invalid',
67
+ value: {
68
+ error: 'Extrinsic was rendered invalid by another extrinsic',
69
+ },
70
+ };
71
+ case 'Invalid':
72
+ return {
73
+ type: 'Invalid',
74
+ value: {
75
+ error: 'Extrinsic marked as invalid',
76
+ },
77
+ };
78
+ }
79
+ }
@@ -1,10 +1,11 @@
1
1
  import type { ConnectionStatus, JsonRpcProvider, ProviderEvent } from '@dedot/providers';
2
- import type { GenericSubstrateApi, RpcVersion } from '@dedot/types';
2
+ import type { RpcVersion, VersionedGenericSubstrateApi } from '@dedot/types';
3
3
  import { EventEmitter } from '@dedot/utils';
4
4
  import type { SubstrateApi } from '../chaintypes/index.js';
5
5
  import type { IJsonRpcClient, JsonRpcClientOptions } from '../types.js';
6
6
  export declare const isJsonRpcProvider: (provider: any) => provider is JsonRpcProvider;
7
- export declare class JsonRpcClient<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion], Events extends string = ProviderEvent> extends EventEmitter<Events> implements IJsonRpcClient<ChainApi, Events> {
7
+ export declare class JsonRpcClient<ChainApi extends VersionedGenericSubstrateApi = SubstrateApi, // prettier-end-here
8
+ Events extends string = ProviderEvent> extends EventEmitter<Events> implements IJsonRpcClient<ChainApi[RpcVersion], Events> {
8
9
  #private;
9
10
  constructor(options: JsonRpcClientOptions | JsonRpcProvider);
10
11
  /**
@@ -12,13 +13,13 @@ export declare class JsonRpcClient<ChainApi extends GenericSubstrateApi = Substr
12
13
  *
13
14
  * @param options
14
15
  */
15
- static create<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
16
+ static create<ChainApi extends VersionedGenericSubstrateApi = SubstrateApi>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
16
17
  /**
17
18
  * Alias for __JsonRpcClient.create__
18
19
  *
19
20
  * @param options
20
21
  */
21
- static new<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
22
+ static new<ChainApi extends VersionedGenericSubstrateApi = SubstrateApi>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
22
23
  get options(): JsonRpcClientOptions;
23
24
  /**
24
25
  * @description Check connection status of the api instance
@@ -46,5 +47,5 @@ export declare class JsonRpcClient<ChainApi extends GenericSubstrateApi = Substr
46
47
  * const result = await client.rpc.module_rpc_name();
47
48
  * ```
48
49
  */
49
- get rpc(): ChainApi['rpc'];
50
+ get rpc(): ChainApi[RpcVersion]['rpc'];
50
51
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@dedot/api",
3
- "version": "0.0.1-next.cfee875e.21+cfee875",
3
+ "version": "0.1.0",
4
4
  "description": "A delightful JavaScript/TypeScript client for Polkadot & Substrate",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
- "homepage": "https://github.com/dedotdev/dedot",
6
+ "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/api",
7
7
  "repository": {
8
8
  "directory": "packages/api",
9
9
  "type": "git",
@@ -13,13 +13,13 @@
13
13
  "type": "module",
14
14
  "sideEffects": false,
15
15
  "dependencies": {
16
- "@dedot/codecs": "0.0.1-next.cfee875e.21+cfee875",
17
- "@dedot/providers": "0.0.1-next.cfee875e.21+cfee875",
18
- "@dedot/runtime-specs": "0.0.1-next.cfee875e.21+cfee875",
19
- "@dedot/shape": "0.0.1-next.cfee875e.21+cfee875",
20
- "@dedot/storage": "0.0.1-next.cfee875e.21+cfee875",
21
- "@dedot/types": "0.0.1-next.cfee875e.21+cfee875",
22
- "@dedot/utils": "0.0.1-next.cfee875e.21+cfee875"
16
+ "@dedot/codecs": "0.1.0",
17
+ "@dedot/providers": "0.1.0",
18
+ "@dedot/runtime-specs": "0.1.0",
19
+ "@dedot/shape": "0.1.0",
20
+ "@dedot/storage": "0.1.0",
21
+ "@dedot/types": "0.1.0",
22
+ "@dedot/utils": "0.1.0"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
@@ -46,9 +46,9 @@
46
46
  },
47
47
  "license": "Apache-2.0",
48
48
  "devDependencies": {
49
- "@polkadot/types": "^11.2.1"
49
+ "@polkadot/types": "^12.2.1"
50
50
  },
51
- "gitHead": "cfee875eafba07e0fbb8a72556ca549a62ddceff",
51
+ "gitHead": "c8220a4ab4b171fe04adb49759318bde865d7c17",
52
52
  "module": "./index.js",
53
53
  "types": "./index.d.ts"
54
54
  }
@@ -36,14 +36,14 @@ export class QueryableStorage {
36
36
  const storageItemHash = xxhashAsU8a(this.storageEntry.name, 128);
37
37
  return concatU8a(palletNameHash, storageItemHash);
38
38
  }
39
- #getStorageMapInfo(type) {
40
- assert(type.tag === 'Map');
41
- const { hashers, keyTypeId } = type.value;
39
+ #getStorageMapInfo(storageType) {
40
+ assert(storageType.type === 'Map');
41
+ const { hashers, keyTypeId } = storageType.value;
42
42
  let keyTypeIds = [keyTypeId];
43
43
  if (hashers.length > 1) {
44
- const { type } = this.registry.findType(keyTypeId);
45
- assert(type.tag === 'Tuple', 'Key type should be a tuple!');
46
- keyTypeIds = type.value.fields;
44
+ const { typeDef } = this.registry.findType(keyTypeId);
45
+ assert(typeDef.type === 'Tuple', 'Key type should be a tuple!');
46
+ keyTypeIds = typeDef.value.fields;
47
47
  }
48
48
  return { hashers, keyTypeIds };
49
49
  }
@@ -53,12 +53,12 @@ export class QueryableStorage {
53
53
  * @param keyInput
54
54
  */
55
55
  encodeKey(keyInput) {
56
- const { type } = this.storageEntry;
57
- if (type.tag === 'Plain') {
56
+ const { storageType } = this.storageEntry;
57
+ if (storageType.type === 'Plain') {
58
58
  return this.prefixKey;
59
59
  }
60
- else if (type.tag === 'Map') {
61
- const { hashers, keyTypeIds } = this.#getStorageMapInfo(type);
60
+ else if (storageType.type === 'Map') {
61
+ const { hashers, keyTypeIds } = this.#getStorageMapInfo(storageType);
62
62
  const extractedInputs = this.#extractRequiredKeyInputs(keyInput, hashers.length);
63
63
  const keyParts = keyTypeIds.map((keyId, index) => {
64
64
  const input = extractedInputs[index];
@@ -68,7 +68,7 @@ export class QueryableStorage {
68
68
  });
69
69
  return u8aToHex(concatU8a(this.prefixKeyAsU8a, ...keyParts));
70
70
  }
71
- throw Error(`Invalid storage entry type: ${type}`);
71
+ throw Error(`Invalid storage entry type: ${JSON.stringify(storageType)}`);
72
72
  }
73
73
  /**
74
74
  * Decode storage key to plain key input
@@ -77,16 +77,16 @@ export class QueryableStorage {
77
77
  * @param key
78
78
  */
79
79
  decodeKey(key) {
80
- const { type } = this.storageEntry;
81
- if (type.tag === 'Plain') {
80
+ const { storageType } = this.storageEntry;
81
+ if (storageType.type === 'Plain') {
82
82
  return;
83
83
  }
84
- else if (type.tag === 'Map') {
84
+ else if (storageType.type === 'Map') {
85
85
  const prefix = this.prefixKey;
86
86
  if (!key.startsWith(prefix)) {
87
87
  throw new Error(`Storage key does not match this storage entry (${this.palletName}.${this.storageItem})`);
88
88
  }
89
- const { hashers, keyTypeIds } = this.#getStorageMapInfo(type);
89
+ const { hashers, keyTypeIds } = this.#getStorageMapInfo(storageType);
90
90
  let keyData = hexToU8a(hexAddPrefix(key.slice(prefix.length)));
91
91
  const results = keyTypeIds.map((keyId, index) => {
92
92
  const [hashLen, canDecode] = HASHER_INFO[hashers[index]];
@@ -101,7 +101,7 @@ export class QueryableStorage {
101
101
  });
102
102
  return hashers.length > 1 ? results : results[0];
103
103
  }
104
- throw Error(`Invalid storage entry type: ${type}`);
104
+ throw Error(`Invalid storage entry type: ${JSON.stringify(storageType)}`);
105
105
  }
106
106
  /**
107
107
  * Decode raw/bytes storage data to plain value
@@ -109,7 +109,7 @@ export class QueryableStorage {
109
109
  * @param raw
110
110
  */
111
111
  decodeValue(raw) {
112
- const { modifier, type: { value: { valueTypeId }, }, default: defaultValue, } = this.storageEntry;
112
+ const { modifier, storageType: { value: { valueTypeId }, }, default: defaultValue, } = this.storageEntry;
113
113
  if (raw === null || raw === undefined) {
114
114
  if (modifier === 'Optional') {
115
115
  return undefined;
package/types.d.ts CHANGED
@@ -56,7 +56,7 @@ export interface ApiOptions extends JsonRpcClientOptions {
56
56
  */
57
57
  hasher?: HashFn;
58
58
  }
59
- export type ApiEvent = ProviderEvent | 'ready';
59
+ export type ApiEvent = ProviderEvent | 'ready' | 'runtimeUpgraded';
60
60
  export interface SubstrateRuntimeVersion {
61
61
  specName: string;
62
62
  implName: string;
@@ -101,6 +101,14 @@ export interface ISubstrateClient<ChainApi extends GenericSubstrateApi = Generic
101
101
  options: ApiOptions;
102
102
  tx: ChainApi['tx'];
103
103
  at<ChainApiAt extends GenericSubstrateApi = ChainApi>(hash: BlockHash): Promise<ISubstrateClientAt<ChainApiAt>>;
104
+ /**
105
+ * Get current version of the runtime
106
+ * This is similar to `.runtimeVersion` but also ensure
107
+ * the corresponding metadata of this runtime version is downloaded & setup.
108
+ *
109
+ * This is helpful when you want to check runtime version to prepare for runtime upgrade
110
+ */
111
+ getRuntimeVersion(): Promise<SubstrateRuntimeVersion>;
104
112
  }
105
113
  /**
106
114
  * A generic interface for broadcasting transactions