dedot 0.0.1-alpha.23 → 0.0.1-alpha.25

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.
package/README.md CHANGED
@@ -115,7 +115,7 @@ yarn add -D @dedot/chaintypes
115
115
  npm i -D @dedot/chaintypes
116
116
  ```
117
117
 
118
- Initialize a `DelighfulApi` instance using the `ChainApi` interface for a target chain to enable types & APIs suggestion/autocompletion for that particular chain:
118
+ Initialize a `Dedot` instance using the `ChainApi` interface for a target chain to enable types & APIs suggestion/autocompletion for that particular chain:
119
119
  ```typescript
120
120
  import { Dedot } from 'dedot';
121
121
  import type { PolkadotApi, KusamaApi, MoonbeamApi, AstarApi } from '@dedot/chaintypes';
@@ -239,6 +239,12 @@ await api.query.system.events(async (eventRecords) => {
239
239
 
240
240
  `dedot` take a lot of inspirations from project [@polkadot/api](https://github.com/polkadot-js/api). A big thank to all the maintainers/contributors of this awesome library.
241
241
 
242
+ Proudly supported by Web3 Foundation Grants Program.
243
+ <p align="left">
244
+ <img width="479" src="https://user-images.githubusercontent.com/6867026/227230786-0796214a-3e3f-42af-94e9-d4122c730b62.png">
245
+ </p>
246
+
247
+
242
248
  ### License
243
249
 
244
250
  [Apache-2.0](https://github.com/dedotdev/dedot/blob/main/LICENSE)
@@ -259,7 +259,7 @@ class Dedot {
259
259
  return { endpoint: options };
260
260
  }
261
261
  else {
262
- let { metadata } = options;
262
+ let { metadata } = options || {};
263
263
  if (metadata && typeof metadata === 'string') {
264
264
  metadata = {
265
265
  [exports.CATCH_ALL_METADATA_KEY]: (0, util_1.hexAddPrefix)(metadata),
@@ -10,10 +10,10 @@ exports.FallbackRuntimeApis = [
10
10
  ];
11
11
  class RuntimeApiExecutor extends Executor_1.Executor {
12
12
  execute(runtimeApi, method) {
13
- runtimeApi = (0, util_1.stringPascalCase)(runtimeApi);
14
- method = (0, utils_1.stringSnakeCase)(method);
15
- const callName = `${runtimeApi}_${method}`;
16
- const callSpec = this.#findRuntimeApiMethodSpec(runtimeApi, method);
13
+ const runtimeApiName = (0, util_1.stringPascalCase)(runtimeApi);
14
+ const methodName = (0, utils_1.stringSnakeCase)(method);
15
+ const callName = this.#callName({ runtimeApiName, methodName });
16
+ const callSpec = this.#findRuntimeApiMethodSpec(runtimeApiName, methodName);
17
17
  (0, utils_1.assert)(callSpec, `Runtime Api not found: ${callName}`);
18
18
  const callFn = async (...args) => {
19
19
  const { params } = callSpec;
@@ -30,23 +30,40 @@ class RuntimeApiExecutor extends Executor_1.Executor {
30
30
  return callFn;
31
31
  }
32
32
  tryDecode(callSpec, raw) {
33
- const { type, typeId } = callSpec;
34
- if ((0, util_1.isNumber)(typeId)) {
35
- return this.registry.findPortableCodec(typeId).tryDecode(raw);
36
- }
37
- return this.registry.findCodec(type).tryDecode(raw);
33
+ const $codec = this.#findCodec(callSpec, `Codec not found to decode respond data for ${this.#callName(callSpec)}`);
34
+ return $codec.tryDecode(raw);
38
35
  }
39
36
  tryEncode(paramSpec, value) {
40
- const { type, typeId } = paramSpec;
41
- const $codec = (0, util_1.isNumber)(typeId) ? this.registry.findPortableCodec(typeId) : this.registry.findCodec(type);
37
+ const $codec = this.#findCodec(paramSpec, `Codec not found to encode input for param ${paramSpec.name}`);
42
38
  return $codec.tryEncode(value);
43
39
  }
40
+ #findCodec(spec, error) {
41
+ const { codec, typeId, type } = spec;
42
+ if (codec)
43
+ return codec;
44
+ if ((0, util_1.isNumber)(typeId)) {
45
+ return this.registry.findPortableCodec(typeId);
46
+ }
47
+ if (type) {
48
+ return this.registry.findCodec(type);
49
+ }
50
+ throw new Error(error || 'Codec not found');
51
+ }
52
+ #callName({ runtimeApiName, methodName }) {
53
+ return `${runtimeApiName}_${methodName}`;
54
+ }
44
55
  #findRuntimeApiMethodSpec(runtimeApi, method) {
56
+ const targetVersion = this.#findTargetRuntimeApiVersion(runtimeApi);
57
+ if (!(0, util_1.isNumber)(targetVersion))
58
+ return undefined;
59
+ const userDefinedSpec = this.#findUserDefinedSpec(runtimeApi, method, targetVersion);
60
+ if (userDefinedSpec)
61
+ return userDefinedSpec;
45
62
  const methodDef = this.#findRuntimeApiMethodDef(runtimeApi, method);
46
63
  if (methodDef) {
47
64
  return this.#toMethodSpec(runtimeApi, methodDef);
48
65
  }
49
- return this.#findRuntimeApiMethodExternalSpec(runtimeApi, method);
66
+ return this.#findExternalSpec(runtimeApi, method, targetVersion);
50
67
  }
51
68
  #findRuntimeApiMethodDef(runtimeApi, method) {
52
69
  try {
@@ -62,32 +79,36 @@ class RuntimeApiExecutor extends Executor_1.Executor {
62
79
  catch { }
63
80
  }
64
81
  #toMethodSpec(runtimeApi, methodDef) {
65
- if (!methodDef)
66
- return undefined;
67
82
  const { name, inputs, output, docs } = methodDef;
68
83
  return {
69
84
  docs,
70
85
  runtimeApiName: runtimeApi,
71
86
  methodName: name,
72
- type: '', // TODO generate type name
73
87
  typeId: output,
74
88
  params: inputs.map(({ name, typeId }) => ({
75
89
  name,
76
- type: '', // TODO generate type name
77
90
  typeId,
78
91
  })),
79
92
  };
80
93
  }
81
- #findRuntimeApiMethodExternalSpec(runtimeApi, method) {
82
- const targetRuntimeApiHash = (0, utils_1.calculateRuntimeApiHash)(runtimeApi);
94
+ #findTargetRuntimeApiVersion(runtimeApi) {
95
+ const runtimeApiHash = (0, utils_1.calculateRuntimeApiHash)(runtimeApi);
83
96
  const runtimeApiVersions = this.api.runtimeVersion?.apis || exports.FallbackRuntimeApis;
84
- const targetRuntimeApiVersion = runtimeApiVersions
85
- .find(([supportedRuntimeApiHash]) => targetRuntimeApiHash === supportedRuntimeApiHash)
97
+ const foundVersion = runtimeApiVersions
98
+ .find(([supportedRuntimeApiHash]) => runtimeApiHash === supportedRuntimeApiHash)
86
99
  ?.at(1);
87
- if (!(0, util_1.isNumber)(targetRuntimeApiVersion)) {
100
+ return foundVersion;
101
+ }
102
+ #findExternalSpec(runtimeApiName, methodName, targetRuntimeApiVersion) {
103
+ return (0, specs_1.findRuntimeApiMethodSpec)(this.#callName({ runtimeApiName, methodName }), targetRuntimeApiVersion);
104
+ }
105
+ #findUserDefinedSpec(runtimeApi, method, runtimeApiVersion) {
106
+ const userDefinedSpecs = this.api.options.runtime;
107
+ if (!userDefinedSpecs)
88
108
  return undefined;
89
- }
90
- return (0, specs_1.findRuntimeApiMethodSpec)(`${runtimeApi}_${method}`, targetRuntimeApiVersion);
109
+ const methodSpecs = (0, specs_1.extractRuntimeApisModule)(userDefinedSpecs).map(specs_1.extractRuntimeApiSpec).flat();
110
+ return methodSpecs.find(({ runtimeApiName, methodName, version }) => `${(0, util_1.stringPascalCase)(runtimeApiName)}_${(0, utils_1.stringSnakeCase)(methodName)}` === `${runtimeApi}_${method}` &&
111
+ runtimeApiVersion === version);
91
112
  }
92
113
  }
93
114
  exports.RuntimeApiExecutor = RuntimeApiExecutor;
@@ -2,4 +2,4 @@
2
2
  // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.packageInfo = void 0;
5
- exports.packageInfo = { name: 'dedot', version: '0.0.1-alpha.22' };
5
+ exports.packageInfo = { name: 'dedot', version: '0.0.1-alpha.24' };
package/client/Dedot.js CHANGED
@@ -253,7 +253,7 @@ export class Dedot {
253
253
  return { endpoint: options };
254
254
  }
255
255
  else {
256
- let { metadata } = options;
256
+ let { metadata } = options || {};
257
257
  if (metadata && typeof metadata === 'string') {
258
258
  metadata = {
259
259
  [CATCH_ALL_METADATA_KEY]: hexAddPrefix(metadata),
@@ -1,5 +1,5 @@
1
+ import type { GenericRuntimeApiMethod, GenericSubstrateApi, RuntimeApiMethodSpec, RuntimeApiMethodParamSpec } from '@dedot/types';
1
2
  import { Executor } from './Executor';
2
- import { GenericRuntimeApiMethod, GenericSubstrateApi, RuntimeApiMethodParamSpec, RuntimeApiMethodSpec } from '@dedot/types';
3
3
  export declare const FallbackRuntimeApis: (string | number)[][];
4
4
  export declare class RuntimeApiExecutor<ChainApi extends GenericSubstrateApi = GenericSubstrateApi> extends Executor<ChainApi> {
5
5
  #private;
@@ -1,16 +1,16 @@
1
1
  import { Executor } from './Executor';
2
2
  import { assert, calculateRuntimeApiHash, stringSnakeCase } from '@dedot/utils';
3
3
  import { isNumber, stringPascalCase, u8aConcat, u8aToHex } from '@polkadot/util';
4
- import { findRuntimeApiMethodSpec } from '@dedot/specs';
4
+ import { extractRuntimeApisModule, extractRuntimeApiSpec, findRuntimeApiMethodSpec } from '@dedot/specs';
5
5
  export const FallbackRuntimeApis = [
6
6
  ['0x37e397fc7c91f5e4', 2], // Metadata Api v2
7
7
  ];
8
8
  export class RuntimeApiExecutor extends Executor {
9
9
  execute(runtimeApi, method) {
10
- runtimeApi = stringPascalCase(runtimeApi);
11
- method = stringSnakeCase(method);
12
- const callName = `${runtimeApi}_${method}`;
13
- const callSpec = this.#findRuntimeApiMethodSpec(runtimeApi, method);
10
+ const runtimeApiName = stringPascalCase(runtimeApi);
11
+ const methodName = stringSnakeCase(method);
12
+ const callName = this.#callName({ runtimeApiName, methodName });
13
+ const callSpec = this.#findRuntimeApiMethodSpec(runtimeApiName, methodName);
14
14
  assert(callSpec, `Runtime Api not found: ${callName}`);
15
15
  const callFn = async (...args) => {
16
16
  const { params } = callSpec;
@@ -27,23 +27,40 @@ export class RuntimeApiExecutor extends Executor {
27
27
  return callFn;
28
28
  }
29
29
  tryDecode(callSpec, raw) {
30
- const { type, typeId } = callSpec;
31
- if (isNumber(typeId)) {
32
- return this.registry.findPortableCodec(typeId).tryDecode(raw);
33
- }
34
- return this.registry.findCodec(type).tryDecode(raw);
30
+ const $codec = this.#findCodec(callSpec, `Codec not found to decode respond data for ${this.#callName(callSpec)}`);
31
+ return $codec.tryDecode(raw);
35
32
  }
36
33
  tryEncode(paramSpec, value) {
37
- const { type, typeId } = paramSpec;
38
- const $codec = isNumber(typeId) ? this.registry.findPortableCodec(typeId) : this.registry.findCodec(type);
34
+ const $codec = this.#findCodec(paramSpec, `Codec not found to encode input for param ${paramSpec.name}`);
39
35
  return $codec.tryEncode(value);
40
36
  }
37
+ #findCodec(spec, error) {
38
+ const { codec, typeId, type } = spec;
39
+ if (codec)
40
+ return codec;
41
+ if (isNumber(typeId)) {
42
+ return this.registry.findPortableCodec(typeId);
43
+ }
44
+ if (type) {
45
+ return this.registry.findCodec(type);
46
+ }
47
+ throw new Error(error || 'Codec not found');
48
+ }
49
+ #callName({ runtimeApiName, methodName }) {
50
+ return `${runtimeApiName}_${methodName}`;
51
+ }
41
52
  #findRuntimeApiMethodSpec(runtimeApi, method) {
53
+ const targetVersion = this.#findTargetRuntimeApiVersion(runtimeApi);
54
+ if (!isNumber(targetVersion))
55
+ return undefined;
56
+ const userDefinedSpec = this.#findUserDefinedSpec(runtimeApi, method, targetVersion);
57
+ if (userDefinedSpec)
58
+ return userDefinedSpec;
42
59
  const methodDef = this.#findRuntimeApiMethodDef(runtimeApi, method);
43
60
  if (methodDef) {
44
61
  return this.#toMethodSpec(runtimeApi, methodDef);
45
62
  }
46
- return this.#findRuntimeApiMethodExternalSpec(runtimeApi, method);
63
+ return this.#findExternalSpec(runtimeApi, method, targetVersion);
47
64
  }
48
65
  #findRuntimeApiMethodDef(runtimeApi, method) {
49
66
  try {
@@ -59,31 +76,35 @@ export class RuntimeApiExecutor extends Executor {
59
76
  catch { }
60
77
  }
61
78
  #toMethodSpec(runtimeApi, methodDef) {
62
- if (!methodDef)
63
- return undefined;
64
79
  const { name, inputs, output, docs } = methodDef;
65
80
  return {
66
81
  docs,
67
82
  runtimeApiName: runtimeApi,
68
83
  methodName: name,
69
- type: '', // TODO generate type name
70
84
  typeId: output,
71
85
  params: inputs.map(({ name, typeId }) => ({
72
86
  name,
73
- type: '', // TODO generate type name
74
87
  typeId,
75
88
  })),
76
89
  };
77
90
  }
78
- #findRuntimeApiMethodExternalSpec(runtimeApi, method) {
79
- const targetRuntimeApiHash = calculateRuntimeApiHash(runtimeApi);
91
+ #findTargetRuntimeApiVersion(runtimeApi) {
92
+ const runtimeApiHash = calculateRuntimeApiHash(runtimeApi);
80
93
  const runtimeApiVersions = this.api.runtimeVersion?.apis || FallbackRuntimeApis;
81
- const targetRuntimeApiVersion = runtimeApiVersions
82
- .find(([supportedRuntimeApiHash]) => targetRuntimeApiHash === supportedRuntimeApiHash)
94
+ const foundVersion = runtimeApiVersions
95
+ .find(([supportedRuntimeApiHash]) => runtimeApiHash === supportedRuntimeApiHash)
83
96
  ?.at(1);
84
- if (!isNumber(targetRuntimeApiVersion)) {
97
+ return foundVersion;
98
+ }
99
+ #findExternalSpec(runtimeApiName, methodName, targetRuntimeApiVersion) {
100
+ return findRuntimeApiMethodSpec(this.#callName({ runtimeApiName, methodName }), targetRuntimeApiVersion);
101
+ }
102
+ #findUserDefinedSpec(runtimeApi, method, runtimeApiVersion) {
103
+ const userDefinedSpecs = this.api.options.runtime;
104
+ if (!userDefinedSpecs)
85
105
  return undefined;
86
- }
87
- return findRuntimeApiMethodSpec(`${runtimeApi}_${method}`, targetRuntimeApiVersion);
106
+ const methodSpecs = extractRuntimeApisModule(userDefinedSpecs).map(extractRuntimeApiSpec).flat();
107
+ return methodSpecs.find(({ runtimeApiName, methodName, version }) => `${stringPascalCase(runtimeApiName)}_${stringSnakeCase(methodName)}` === `${runtimeApi}_${method}` &&
108
+ runtimeApiVersion === version);
88
109
  }
89
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dedot",
3
- "version": "0.0.1-alpha.23",
3
+ "version": "0.0.1-alpha.25",
4
4
  "description": "A fast & lightweight JavaScript/TypeScript client for Polkadot & Substrate",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot",
@@ -12,11 +12,11 @@
12
12
  "main": "./cjs/index.js",
13
13
  "type": "module",
14
14
  "dependencies": {
15
- "@dedot/chaintypes": "0.0.1-alpha.23",
16
- "@dedot/codecs": "0.0.1-alpha.23",
17
- "@dedot/shape": "0.0.1-alpha.23",
18
- "@dedot/specs": "0.0.1-alpha.23",
19
- "@dedot/utils": "0.0.1-alpha.23",
15
+ "@dedot/chaintypes": "0.0.1-alpha.25",
16
+ "@dedot/codecs": "0.0.1-alpha.25",
17
+ "@dedot/shape": "0.0.1-alpha.25",
18
+ "@dedot/specs": "0.0.1-alpha.25",
19
+ "@dedot/utils": "0.0.1-alpha.25",
20
20
  "@polkadot/rpc-provider": "^10.11.2",
21
21
  "@polkadot/util": "^12.6.2",
22
22
  "@polkadot/util-crypto": "^12.6.2",
@@ -36,7 +36,7 @@
36
36
  "@polkadot/keyring": "^12.6.2",
37
37
  "@polkadot/types": "^10.11.2"
38
38
  },
39
- "gitHead": "d0aaaf353a6596d5dcd9277b241799bd544a2bd2",
39
+ "gitHead": "44b180e85a3126e45dcf408fcb3d32adc4bedeca",
40
40
  "module": "./index.js",
41
41
  "types": "./index.d.ts",
42
42
  "exports": {
package/packageInfo.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
2
2
 
3
- export const packageInfo = { name: 'dedot', version: '0.0.1-alpha.23' };
3
+ export const packageInfo = { name: 'dedot', version: '0.0.1-alpha.25' };
package/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ProviderInterface } from '@polkadot/rpc-provider/types';
2
2
  import { HexString } from '@dedot/utils';
3
3
  import { AnySignedExtension } from './extrinsic';
4
+ import { RuntimeApiSpec } from '@dedot/types';
4
5
  export type NetworkEndpoint = string;
5
6
  export type MetadataKey = `RAW_META/${string}`;
6
7
  export interface ApiOptions {
@@ -38,6 +39,7 @@ export interface ApiOptions {
38
39
  * @description User-defined chain-specific signed extensions
39
40
  */
40
41
  signedExtensions?: Record<string, AnySignedExtension>;
42
+ runtime?: Record<string, RuntimeApiSpec[]>;
41
43
  }
42
44
  export interface NormalizedApiOptions extends ApiOptions {
43
45
  metadata?: Record<string, HexString>;