@dedot/api 0.0.1-alpha.32

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 (202) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/chaintypes/index.d.ts +1 -0
  4. package/chaintypes/index.js +1 -0
  5. package/chaintypes/substrate/consts.d.ts +2132 -0
  6. package/chaintypes/substrate/consts.js +2 -0
  7. package/chaintypes/substrate/errors.d.ts +3490 -0
  8. package/chaintypes/substrate/errors.js +2 -0
  9. package/chaintypes/substrate/events.d.ts +5187 -0
  10. package/chaintypes/substrate/events.js +2 -0
  11. package/chaintypes/substrate/index.d.ts +22 -0
  12. package/chaintypes/substrate/index.js +2 -0
  13. package/chaintypes/substrate/json-rpc.d.ts +3 -0
  14. package/chaintypes/substrate/json-rpc.js +2 -0
  15. package/chaintypes/substrate/query.d.ts +3829 -0
  16. package/chaintypes/substrate/query.js +2 -0
  17. package/chaintypes/substrate/runtime.d.ts +850 -0
  18. package/chaintypes/substrate/runtime.js +2 -0
  19. package/chaintypes/substrate/tx.d.ts +14269 -0
  20. package/chaintypes/substrate/tx.js +2 -0
  21. package/chaintypes/substrate/types.d.ts +30457 -0
  22. package/chaintypes/substrate/types.js +2 -0
  23. package/cjs/chaintypes/index.js +17 -0
  24. package/cjs/chaintypes/substrate/consts.js +3 -0
  25. package/cjs/chaintypes/substrate/errors.js +3 -0
  26. package/cjs/chaintypes/substrate/events.js +3 -0
  27. package/cjs/chaintypes/substrate/index.js +18 -0
  28. package/cjs/chaintypes/substrate/json-rpc.js +3 -0
  29. package/cjs/chaintypes/substrate/query.js +3 -0
  30. package/cjs/chaintypes/substrate/runtime.js +3 -0
  31. package/cjs/chaintypes/substrate/tx.js +3 -0
  32. package/cjs/chaintypes/substrate/types.js +3 -0
  33. package/cjs/client/BaseSubstrateClient.js +257 -0
  34. package/cjs/client/Dedot.js +250 -0
  35. package/cjs/client/DedotClient.js +186 -0
  36. package/cjs/client/index.js +18 -0
  37. package/cjs/executor/ConstantExecutor.js +20 -0
  38. package/cjs/executor/ErrorExecutor.js +46 -0
  39. package/cjs/executor/EventExecutor.js +52 -0
  40. package/cjs/executor/Executor.js +42 -0
  41. package/cjs/executor/RuntimeApiExecutor.js +121 -0
  42. package/cjs/executor/StorageQueryExecutor.js +115 -0
  43. package/cjs/executor/TxExecutor.js +55 -0
  44. package/cjs/executor/index.js +26 -0
  45. package/cjs/executor/v2/RuntimeApiExecutorV2.js +21 -0
  46. package/cjs/executor/v2/StorageQueryExecutorV2.js +65 -0
  47. package/cjs/executor/v2/TxExecutorV2.js +20 -0
  48. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +87 -0
  49. package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
  50. package/cjs/extrinsic/extensions/index.js +19 -0
  51. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +43 -0
  52. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
  53. package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
  54. package/cjs/extrinsic/extensions/known/CheckMortality.js +90 -0
  55. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
  56. package/cjs/extrinsic/extensions/known/CheckNonce.js +33 -0
  57. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
  58. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
  59. package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
  60. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
  61. package/cjs/extrinsic/extensions/known/index.js +25 -0
  62. package/cjs/extrinsic/index.js +18 -0
  63. package/cjs/extrinsic/submittable/BaseSubmittableExtrinsic.js +70 -0
  64. package/cjs/extrinsic/submittable/SubmittableExtrinsic.js +47 -0
  65. package/cjs/extrinsic/submittable/SubmittableExtrinsicV2.js +204 -0
  66. package/cjs/extrinsic/submittable/SubmittableResult.js +33 -0
  67. package/cjs/extrinsic/submittable/errors.js +16 -0
  68. package/cjs/extrinsic/submittable/fakeSigner.js +13 -0
  69. package/cjs/extrinsic/submittable/index.js +20 -0
  70. package/cjs/extrinsic/submittable/utils.js +20 -0
  71. package/cjs/index.js +21 -0
  72. package/cjs/json-rpc/JsonRpcClient.js +165 -0
  73. package/cjs/json-rpc/group/ChainHead/BlockUsage.js +24 -0
  74. package/cjs/json-rpc/group/ChainHead/ChainHead.js +603 -0
  75. package/cjs/json-rpc/group/ChainHead/error.js +59 -0
  76. package/cjs/json-rpc/group/ChainHead/index.js +18 -0
  77. package/cjs/json-rpc/group/ChainSpec.js +19 -0
  78. package/cjs/json-rpc/group/JsonRpcGroup.js +89 -0
  79. package/cjs/json-rpc/group/Transaction.js +25 -0
  80. package/cjs/json-rpc/group/TransactionWatch.js +17 -0
  81. package/cjs/json-rpc/group/index.js +21 -0
  82. package/cjs/json-rpc/index.js +20 -0
  83. package/cjs/json-rpc/scaledResponses.js +15 -0
  84. package/cjs/json-rpc/subscriptionsInfo.js +20 -0
  85. package/cjs/package.json +1 -0
  86. package/cjs/proxychain.js +27 -0
  87. package/cjs/storage/QueryableStorage.js +161 -0
  88. package/cjs/storage/index.js +17 -0
  89. package/cjs/types.js +2 -0
  90. package/client/BaseSubstrateClient.d.ts +65 -0
  91. package/client/BaseSubstrateClient.js +252 -0
  92. package/client/Dedot.d.ts +131 -0
  93. package/client/Dedot.js +246 -0
  94. package/client/DedotClient.d.ts +61 -0
  95. package/client/DedotClient.js +182 -0
  96. package/client/index.d.ts +2 -0
  97. package/client/index.js +2 -0
  98. package/executor/ConstantExecutor.d.ts +10 -0
  99. package/executor/ConstantExecutor.js +16 -0
  100. package/executor/ErrorExecutor.d.ts +10 -0
  101. package/executor/ErrorExecutor.js +42 -0
  102. package/executor/EventExecutor.d.ts +10 -0
  103. package/executor/EventExecutor.js +48 -0
  104. package/executor/Executor.d.ts +149 -0
  105. package/executor/Executor.js +38 -0
  106. package/executor/RuntimeApiExecutor.d.ts +27 -0
  107. package/executor/RuntimeApiExecutor.js +117 -0
  108. package/executor/StorageQueryExecutor.d.ts +14 -0
  109. package/executor/StorageQueryExecutor.js +111 -0
  110. package/executor/TxExecutor.d.ts +10 -0
  111. package/executor/TxExecutor.js +51 -0
  112. package/executor/index.d.ts +10 -0
  113. package/executor/index.js +10 -0
  114. package/executor/v2/RuntimeApiExecutorV2.d.ts +14 -0
  115. package/executor/v2/RuntimeApiExecutorV2.js +17 -0
  116. package/executor/v2/StorageQueryExecutorV2.d.ts +17 -0
  117. package/executor/v2/StorageQueryExecutorV2.js +61 -0
  118. package/executor/v2/TxExecutorV2.d.ts +11 -0
  119. package/executor/v2/TxExecutorV2.js +16 -0
  120. package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
  121. package/extrinsic/extensions/ExtraSignedExtension.js +60 -0
  122. package/extrinsic/extensions/SignedExtension.d.ts +40 -0
  123. package/extrinsic/extensions/SignedExtension.js +37 -0
  124. package/extrinsic/extensions/index.d.ts +3 -0
  125. package/extrinsic/extensions/index.js +3 -0
  126. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
  127. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +39 -0
  128. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
  129. package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
  130. package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
  131. package/extrinsic/extensions/known/CheckGenesis.js +15 -0
  132. package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
  133. package/extrinsic/extensions/known/CheckMortality.js +86 -0
  134. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
  135. package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
  136. package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
  137. package/extrinsic/extensions/known/CheckNonce.js +29 -0
  138. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
  139. package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
  140. package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
  141. package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
  142. package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
  143. package/extrinsic/extensions/known/CheckWeight.js +6 -0
  144. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
  145. package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
  146. package/extrinsic/extensions/known/index.d.ts +3 -0
  147. package/extrinsic/extensions/known/index.js +22 -0
  148. package/extrinsic/index.d.ts +2 -0
  149. package/extrinsic/index.js +2 -0
  150. package/extrinsic/submittable/BaseSubmittableExtrinsic.d.ts +17 -0
  151. package/extrinsic/submittable/BaseSubmittableExtrinsic.js +66 -0
  152. package/extrinsic/submittable/SubmittableExtrinsic.d.ts +12 -0
  153. package/extrinsic/submittable/SubmittableExtrinsic.js +43 -0
  154. package/extrinsic/submittable/SubmittableExtrinsicV2.d.ts +15 -0
  155. package/extrinsic/submittable/SubmittableExtrinsicV2.js +200 -0
  156. package/extrinsic/submittable/SubmittableResult.d.ts +19 -0
  157. package/extrinsic/submittable/SubmittableResult.js +29 -0
  158. package/extrinsic/submittable/errors.d.ts +11 -0
  159. package/extrinsic/submittable/errors.js +12 -0
  160. package/extrinsic/submittable/fakeSigner.d.ts +2 -0
  161. package/extrinsic/submittable/fakeSigner.js +10 -0
  162. package/extrinsic/submittable/index.d.ts +4 -0
  163. package/extrinsic/submittable/index.js +4 -0
  164. package/extrinsic/submittable/utils.d.ts +10 -0
  165. package/extrinsic/submittable/utils.js +15 -0
  166. package/index.d.ts +6 -0
  167. package/index.js +5 -0
  168. package/json-rpc/JsonRpcClient.d.ts +50 -0
  169. package/json-rpc/JsonRpcClient.js +160 -0
  170. package/json-rpc/group/ChainHead/BlockUsage.d.ts +11 -0
  171. package/json-rpc/group/ChainHead/BlockUsage.js +20 -0
  172. package/json-rpc/group/ChainHead/ChainHead.d.ts +70 -0
  173. package/json-rpc/group/ChainHead/ChainHead.js +599 -0
  174. package/json-rpc/group/ChainHead/error.d.ts +47 -0
  175. package/json-rpc/group/ChainHead/error.js +48 -0
  176. package/json-rpc/group/ChainHead/index.d.ts +2 -0
  177. package/json-rpc/group/ChainHead/index.js +2 -0
  178. package/json-rpc/group/ChainSpec.d.ts +10 -0
  179. package/json-rpc/group/ChainSpec.js +15 -0
  180. package/json-rpc/group/JsonRpcGroup.d.ts +74 -0
  181. package/json-rpc/group/JsonRpcGroup.js +85 -0
  182. package/json-rpc/group/Transaction.d.ts +11 -0
  183. package/json-rpc/group/Transaction.js +21 -0
  184. package/json-rpc/group/TransactionWatch.d.ts +10 -0
  185. package/json-rpc/group/TransactionWatch.js +13 -0
  186. package/json-rpc/group/index.d.ts +5 -0
  187. package/json-rpc/group/index.js +5 -0
  188. package/json-rpc/index.d.ts +4 -0
  189. package/json-rpc/index.js +4 -0
  190. package/json-rpc/scaledResponses.d.ts +2 -0
  191. package/json-rpc/scaledResponses.js +12 -0
  192. package/json-rpc/subscriptionsInfo.d.ts +2 -0
  193. package/json-rpc/subscriptionsInfo.js +17 -0
  194. package/package.json +54 -0
  195. package/proxychain.d.ts +14 -0
  196. package/proxychain.js +23 -0
  197. package/storage/QueryableStorage.d.ts +35 -0
  198. package/storage/QueryableStorage.js +157 -0
  199. package/storage/index.d.ts +1 -0
  200. package/storage/index.js +1 -0
  201. package/types.d.ts +120 -0
  202. package/types.js +1 -0
@@ -0,0 +1,111 @@
1
+ import { assert, isFunction } from '@dedot/utils';
2
+ import { QueryableStorage } from '../storage/QueryableStorage.js';
3
+ import { Executor } from './Executor.js';
4
+ const DEFAULT_KEYS_PAGE_SIZE = 1000;
5
+ const DEFAULT_ENTRIES_PAGE_SIZE = 250;
6
+ /**
7
+ * @name StorageQueryExecutor
8
+ * @description Execute a query to on-chain storage
9
+ */
10
+ export class StorageQueryExecutor extends Executor {
11
+ doExecute(pallet, storage) {
12
+ const entry = new QueryableStorage(this.registry, pallet, storage);
13
+ const extractArgs = (args) => {
14
+ const inArgs = args.slice();
15
+ const lastArg = args.at(-1);
16
+ const callback = isFunction(lastArg) ? inArgs.pop() : undefined;
17
+ return [inArgs, callback];
18
+ };
19
+ const getStorageKey = (...args) => {
20
+ const [inArgs] = extractArgs(args);
21
+ return entry.encodeKey(inArgs.at(0));
22
+ };
23
+ const getStorage = async (keys) => {
24
+ const results = await this.queryStorage(keys, this.atBlockHash);
25
+ return keys.reduce((o, key) => {
26
+ o[key] = entry.decodeValue(results[key]);
27
+ return o;
28
+ }, {});
29
+ };
30
+ const queryFn = async (...args) => {
31
+ const [inArgs, callback] = extractArgs(args);
32
+ const encodedKey = entry.encodeKey(inArgs.at(0));
33
+ // if a callback is passed, make a storage subscription and return an unsub function
34
+ if (callback) {
35
+ return await this.subscribeStorage([encodedKey], (changes) => {
36
+ if (changes.length === 0)
37
+ return;
38
+ callback(entry.decodeValue(changes[0]));
39
+ });
40
+ }
41
+ else {
42
+ const results = await getStorage([encodedKey]);
43
+ return results[encodedKey];
44
+ }
45
+ };
46
+ queryFn.rawKey = getStorageKey;
47
+ queryFn.meta = {
48
+ pallet: entry.pallet.name,
49
+ palletIndex: entry.pallet.index,
50
+ ...entry.storageEntry,
51
+ };
52
+ const isMap = entry.storageEntry.type.tag === 'Map';
53
+ if (isMap) {
54
+ const queryMultiFn = async (...args) => {
55
+ const [inArgs, callback] = extractArgs(args);
56
+ const multiArgs = inArgs.at(0);
57
+ assert(Array.isArray(multiArgs), 'First param for multi query should be an array');
58
+ const encodedKeys = multiArgs.map((arg) => entry.encodeKey(arg));
59
+ // if a callback is passed, make a storage subscription and return an unsub function
60
+ if (callback) {
61
+ return await this.subscribeStorage(encodedKeys, (changes) => {
62
+ callback(changes.map((change) => entry.decodeValue(change)));
63
+ });
64
+ }
65
+ else {
66
+ const result = await getStorage(encodedKeys);
67
+ return encodedKeys.map((key) => result[key]);
68
+ }
69
+ };
70
+ // @ts-ignore
71
+ queryFn.multi = queryMultiFn;
72
+ Object.assign(queryFn, this.exposeStorageMapMethods(entry));
73
+ }
74
+ return queryFn;
75
+ }
76
+ exposeStorageMapMethods(entry) {
77
+ const rawKeys = async (pagination) => {
78
+ const pageSize = pagination?.pageSize || DEFAULT_KEYS_PAGE_SIZE;
79
+ const startKey = pagination?.startKey || entry.prefixKey;
80
+ return await this.api.rpc.state_getKeysPaged(entry.prefixKey, pageSize, startKey, this.atBlockHash);
81
+ };
82
+ const pagedKeys = async (pagination) => {
83
+ const storageKeys = await rawKeys({ pageSize: DEFAULT_KEYS_PAGE_SIZE, ...pagination });
84
+ return storageKeys.map((key) => entry.decodeKey(key));
85
+ };
86
+ const pagedEntries = async (pagination) => {
87
+ const storageKeys = await rawKeys({ pageSize: DEFAULT_ENTRIES_PAGE_SIZE, ...pagination });
88
+ const storageMap = await this.queryStorage(storageKeys, this.atBlockHash);
89
+ return storageKeys.map((key) => [entry.decodeKey(key), entry.decodeValue(storageMap[key])]);
90
+ };
91
+ return { pagedKeys, pagedEntries };
92
+ }
93
+ async queryStorage(keys, hash) {
94
+ const changeSets = await this.api.rpc.state_queryStorageAt(keys, hash);
95
+ return changeSets[0].changes.reduce((o, [key, value]) => {
96
+ o[key] = value ?? undefined;
97
+ return o;
98
+ }, {});
99
+ }
100
+ subscribeStorage(keys, callback) {
101
+ const lastChanges = {};
102
+ return this.api.rpc.state_subscribeStorage(keys, (changeSet) => {
103
+ changeSet.changes.forEach(([key, value]) => {
104
+ if (lastChanges[key] !== value) {
105
+ lastChanges[key] = value ?? undefined;
106
+ }
107
+ });
108
+ return callback(keys.map((key) => lastChanges[key]));
109
+ });
110
+ }
111
+ }
@@ -0,0 +1,10 @@
1
+ import type { GenericSubstrateApi, GenericTxCall, IRuntimeTxCall } from '@dedot/types';
2
+ import { Executor } from './Executor.js';
3
+ /**
4
+ * @name TxExecutor
5
+ * @description Execute a transaction instruction, returns a submittable extrinsic
6
+ */
7
+ export declare class TxExecutor<ChainApi extends GenericSubstrateApi = GenericSubstrateApi> extends Executor<ChainApi> {
8
+ doExecute(pallet: string, functionName: string): GenericTxCall;
9
+ protected createExtrinsic(call: IRuntimeTxCall): any;
10
+ }
@@ -0,0 +1,51 @@
1
+ import { assert, stringCamelCase, stringPascalCase, UnknownApiError } from '@dedot/utils';
2
+ import { SubmittableExtrinsic } from '../extrinsic/index.js';
3
+ import { Executor } from './Executor.js';
4
+ /**
5
+ * @name TxExecutor
6
+ * @description Execute a transaction instruction, returns a submittable extrinsic
7
+ */
8
+ export class TxExecutor extends Executor {
9
+ doExecute(pallet, functionName) {
10
+ const targetPallet = this.getPallet(pallet);
11
+ assert(targetPallet.calls, new UnknownApiError(`Tx calls are not available for pallet ${targetPallet.name}`));
12
+ const txType = this.metadata.types[targetPallet.calls];
13
+ assert(txType.type.tag === 'Enum', new UnknownApiError('Tx type defs should be enum'));
14
+ const isFlatEnum = txType.type.value.members.every((m) => m.fields.length === 0);
15
+ const txCallDef = txType.type.value.members.find((m) => stringCamelCase(m.name) === functionName);
16
+ assert(txCallDef, new UnknownApiError(`Tx call spec not found for ${pallet}.${functionName}`));
17
+ const txCallFn = (...args) => {
18
+ let call;
19
+ if (isFlatEnum) {
20
+ call = {
21
+ pallet: stringPascalCase(targetPallet.name),
22
+ palletCall: stringPascalCase(txCallDef.name),
23
+ };
24
+ }
25
+ else {
26
+ const callParams = txCallDef.fields.reduce((o, { name }, idx) => {
27
+ o[stringCamelCase(name)] = args[idx];
28
+ return o;
29
+ }, {});
30
+ call = {
31
+ pallet: stringPascalCase(targetPallet.name),
32
+ palletCall: {
33
+ name: stringPascalCase(txCallDef.name),
34
+ params: callParams,
35
+ },
36
+ };
37
+ }
38
+ return this.createExtrinsic(call);
39
+ };
40
+ txCallFn.meta = {
41
+ ...txCallDef,
42
+ fieldCodecs: txCallDef.fields.map(({ typeId }) => this.registry.findCodec(typeId)),
43
+ pallet: targetPallet.name,
44
+ palletIndex: targetPallet.index,
45
+ };
46
+ return txCallFn;
47
+ }
48
+ createExtrinsic(call) {
49
+ return new SubmittableExtrinsic(this.api, call);
50
+ }
51
+ }
@@ -0,0 +1,10 @@
1
+ export * from './Executor.js';
2
+ export * from './ConstantExecutor.js';
3
+ export * from './StorageQueryExecutor.js';
4
+ export * from './ErrorExecutor.js';
5
+ export * from './EventExecutor.js';
6
+ export * from './RuntimeApiExecutor.js';
7
+ export * from './TxExecutor.js';
8
+ export * from './v2/StorageQueryExecutorV2.js';
9
+ export * from './v2/RuntimeApiExecutorV2.js';
10
+ export * from './v2/TxExecutorV2.js';
@@ -0,0 +1,10 @@
1
+ export * from './Executor.js';
2
+ export * from './ConstantExecutor.js';
3
+ export * from './StorageQueryExecutor.js';
4
+ export * from './ErrorExecutor.js';
5
+ export * from './EventExecutor.js';
6
+ export * from './RuntimeApiExecutor.js';
7
+ export * from './TxExecutor.js';
8
+ export * from './v2/StorageQueryExecutorV2.js';
9
+ export * from './v2/RuntimeApiExecutorV2.js';
10
+ export * from './v2/TxExecutorV2.js';
@@ -0,0 +1,14 @@
1
+ import type { BlockHash } from '@dedot/codecs';
2
+ import type { GenericSubstrateApi } from '@dedot/types';
3
+ import { HexString } from '@dedot/utils';
4
+ import { ChainHead } from '../../json-rpc/index.js';
5
+ import { ISubstrateClientAt } from '../../types.js';
6
+ import { RuntimeApiExecutor, StateCallParams } from '../RuntimeApiExecutor.js';
7
+ /**
8
+ * @name RuntimeApiExecutorV2
9
+ */
10
+ export declare class RuntimeApiExecutorV2<ChainApi extends GenericSubstrateApi = GenericSubstrateApi> extends RuntimeApiExecutor<ChainApi> {
11
+ chainHead: ChainHead;
12
+ constructor(api: ISubstrateClientAt<ChainApi>, chainHead: ChainHead, atBlockHash?: BlockHash);
13
+ protected stateCall(callParams: StateCallParams): Promise<HexString>;
14
+ }
@@ -0,0 +1,17 @@
1
+ import { assert } from '@dedot/utils';
2
+ import { RuntimeApiExecutor } from '../RuntimeApiExecutor.js';
3
+ /**
4
+ * @name RuntimeApiExecutorV2
5
+ */
6
+ export class RuntimeApiExecutorV2 extends RuntimeApiExecutor {
7
+ chainHead;
8
+ constructor(api, chainHead, atBlockHash) {
9
+ assert(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
10
+ super(api, atBlockHash);
11
+ this.chainHead = chainHead;
12
+ }
13
+ stateCall(callParams) {
14
+ const { func, params, at } = callParams;
15
+ return this.chainHead.call(func, params, at);
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ import { BlockHash, Option, StorageData, StorageKey } from '@dedot/codecs';
2
+ import type { AsyncMethod, Callback, GenericSubstrateApi } from '@dedot/types';
3
+ import { HexString } from '@dedot/utils';
4
+ import { ChainHead } from '../../json-rpc/index.js';
5
+ import { QueryableStorage } from '../../storage/QueryableStorage.js';
6
+ import { ISubstrateClientAt } from '../../types.js';
7
+ import { StorageQueryExecutor } from '../StorageQueryExecutor.js';
8
+ /**
9
+ * @name StorageQueryExecutorV2
10
+ */
11
+ export declare class StorageQueryExecutorV2<ChainApi extends GenericSubstrateApi = GenericSubstrateApi> extends StorageQueryExecutor<ChainApi> {
12
+ chainHead: ChainHead;
13
+ constructor(api: ISubstrateClientAt<ChainApi>, chainHead: ChainHead, atBlockHash?: BlockHash);
14
+ protected exposeStorageMapMethods(entry: QueryableStorage): Record<string, AsyncMethod>;
15
+ protected queryStorage(keys: StorageKey[], at?: BlockHash): Promise<Record<StorageKey, Option<StorageData>>>;
16
+ protected subscribeStorage(keys: HexString[], callback: Callback<Array<StorageData | undefined>>): Promise<() => Promise<void>>;
17
+ }
@@ -0,0 +1,61 @@
1
+ import { assert } from '@dedot/utils';
2
+ import { StorageQueryExecutor } from '../StorageQueryExecutor.js';
3
+ /**
4
+ * @name StorageQueryExecutorV2
5
+ */
6
+ export class StorageQueryExecutorV2 extends StorageQueryExecutor {
7
+ chainHead;
8
+ constructor(api, chainHead, atBlockHash) {
9
+ assert(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
10
+ super(api, atBlockHash);
11
+ this.chainHead = chainHead;
12
+ }
13
+ exposeStorageMapMethods(entry) {
14
+ // chainHead_storage does not support pagination
15
+ // so for now we're trying to pull all entries from storage
16
+ // this might take a while for large storage
17
+ // TODO improve this, fallback to use `archive`-prefixed if available?
18
+ const entries = async () => {
19
+ const results = await this.chainHead.storage([{ type: 'descendantsValues', key: entry.prefixKey }]);
20
+ return results.map(({ key, value }) => [
21
+ entry.decodeKey(key),
22
+ entry.decodeValue(value),
23
+ ]);
24
+ };
25
+ return { entries };
26
+ }
27
+ async queryStorage(keys, at) {
28
+ const results = await this.chainHead.storage(keys.map((key) => ({ type: 'value', key })), undefined, at);
29
+ return results.reduce((o, r) => {
30
+ o[r.key] = (r.value ?? undefined);
31
+ return o;
32
+ }, {});
33
+ }
34
+ async subscribeStorage(keys, callback) {
35
+ let initialHash = await this.chainHead.bestHash();
36
+ let eventToListen = 'bestBlock';
37
+ // TODO subscribe to finalized data source
38
+ // initialHash = this.chainHead.finalizedHash;
39
+ // eventToListen = 'finalizedBlock';
40
+ const latestChanges = {};
41
+ const pull = async (hash) => {
42
+ const results = await this.queryStorage(keys, hash);
43
+ let changed = false;
44
+ keys.forEach((key) => {
45
+ const newValue = results[key];
46
+ if (latestChanges[key] === newValue)
47
+ return;
48
+ changed = true;
49
+ latestChanges[key] = newValue;
50
+ });
51
+ if (!changed)
52
+ return;
53
+ callback(keys.map((key) => latestChanges[key]));
54
+ };
55
+ await pull(initialHash);
56
+ const unsub = this.chainHead.on(eventToListen, pull);
57
+ return async () => {
58
+ unsub();
59
+ };
60
+ }
61
+ }
@@ -0,0 +1,11 @@
1
+ import { IRuntimeTxCall, RpcVersion, VersionedGenericSubstrateApi } from '@dedot/types';
2
+ import { DedotClient } from '../../client/index.js';
3
+ import { TxExecutor } from '../TxExecutor.js';
4
+ /**
5
+ * @name TxExecutor
6
+ * @description Execute a transaction instruction, returns a submittable extrinsic
7
+ */
8
+ export declare class TxExecutorV2<ChainApi extends VersionedGenericSubstrateApi = VersionedGenericSubstrateApi> extends TxExecutor<ChainApi[RpcVersion]> {
9
+ constructor(api: DedotClient<ChainApi>);
10
+ protected createExtrinsic(call: IRuntimeTxCall): any;
11
+ }
@@ -0,0 +1,16 @@
1
+ import { assert } from '@dedot/utils';
2
+ import { SubmittableExtrinsicV2 } from '../../extrinsic/index.js';
3
+ import { TxExecutor } from '../TxExecutor.js';
4
+ /**
5
+ * @name TxExecutor
6
+ * @description Execute a transaction instruction, returns a submittable extrinsic
7
+ */
8
+ export class TxExecutorV2 extends TxExecutor {
9
+ constructor(api) {
10
+ assert(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
11
+ super(api);
12
+ }
13
+ createExtrinsic(call) {
14
+ return new SubmittableExtrinsicV2(this.api, call);
15
+ }
16
+ }
@@ -0,0 +1,14 @@
1
+ import { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
2
+ import * as $ from '@dedot/shape';
3
+ import { HexString } from '@dedot/utils';
4
+ import { SignedExtension } from './SignedExtension.js';
5
+ export declare class ExtraSignedExtension extends SignedExtension<any[], any[]> {
6
+ #private;
7
+ init(): Promise<void>;
8
+ get identifier(): string;
9
+ get $Data(): $.AnyShape;
10
+ get $AdditionalSigned(): $.AnyShape;
11
+ get $Payload(): $.AnyShape;
12
+ toPayload(call?: HexString): SignerPayloadJSON;
13
+ toRawPayload(call?: HexString): SignerPayloadRaw;
14
+ }
@@ -0,0 +1,60 @@
1
+ import * as $ from '@dedot/shape';
2
+ import { assert, ensurePresence, u8aToHex } from '@dedot/utils';
3
+ import { SignedExtension } from './SignedExtension.js';
4
+ import { knownSignedExtensions } from './known/index.js';
5
+ export class ExtraSignedExtension extends SignedExtension {
6
+ #signedExtensions;
7
+ async init() {
8
+ this.#signedExtensions = this.#getSignedExtensions();
9
+ await Promise.all(this.#signedExtensions.map((se) => se.init()));
10
+ this.data = this.#signedExtensions.map((se) => se.data);
11
+ this.additionalSigned = this.#signedExtensions.map((se) => se.additionalSigned);
12
+ }
13
+ get identifier() {
14
+ return 'ExtraSignedExtension';
15
+ }
16
+ get $Data() {
17
+ const { extraTypeId } = this.registry.metadata.extrinsic;
18
+ return ensurePresence(this.registry.findCodec(extraTypeId));
19
+ }
20
+ get $AdditionalSigned() {
21
+ const $AdditionalSignedCodecs = this.#signedExtensionDefs.map((se) => this.registry.findCodec(se.additionalSigned));
22
+ return $.Tuple(...$AdditionalSignedCodecs);
23
+ }
24
+ get $Payload() {
25
+ const { callTypeId } = this.registry.metadata.extrinsic;
26
+ const $Call = this.registry.findCodec(callTypeId);
27
+ return $.Tuple($Call, this.$Data, this.$AdditionalSigned);
28
+ }
29
+ get #signedExtensionDefs() {
30
+ return this.registry.metadata.extrinsic.signedExtensions;
31
+ }
32
+ #getSignedExtensions() {
33
+ return this.#signedExtensionDefs.map((extDef) => {
34
+ const { signedExtensions: userSignedExtensions = {} } = this.api.options;
35
+ const Extension = userSignedExtensions[extDef.ident] ||
36
+ knownSignedExtensions[extDef.ident];
37
+ assert(Extension, `SignedExtension for ${extDef.ident} not found`);
38
+ return new Extension(this.api, {
39
+ ...ensurePresence(this.options),
40
+ def: extDef,
41
+ });
42
+ });
43
+ }
44
+ toPayload(call = '0x') {
45
+ const signedExtensions = this.#signedExtensions.map((se) => se.identifier);
46
+ const { version } = this.registry.metadata.extrinsic;
47
+ return Object.assign({ address: this.options.signerAddress, signedExtensions, version, method: call }, ...this.#signedExtensions.map((se) => se.toPayload()));
48
+ }
49
+ toRawPayload(call = '0x') {
50
+ const payload = this.toPayload(call);
51
+ const $ToSignPayload = $.Tuple($.RawHex, this.$Data, this.$AdditionalSigned);
52
+ const toSignPayload = [call, this.data, this.additionalSigned];
53
+ const rawPayload = $ToSignPayload.tryEncode(toSignPayload);
54
+ return {
55
+ address: payload.address,
56
+ data: u8aToHex(rawPayload),
57
+ type: 'payload',
58
+ };
59
+ }
60
+ }
@@ -0,0 +1,40 @@
1
+ import { SignerPayloadJSON } from '@polkadot/types/types';
2
+ import { PortableRegistry, SignedExtensionDefLatest } from '@dedot/codecs';
3
+ import * as $ from '@dedot/shape';
4
+ import { PayloadOptions } from '@dedot/types';
5
+ import { ISubstrateClient } from '../../types.js';
6
+ export interface ISignedExtension {
7
+ identifier: string;
8
+ $Data: $.AnyShape;
9
+ $AdditionalSigned: $.AnyShape;
10
+ data: any;
11
+ additionalSigned: any;
12
+ init(): Promise<void>;
13
+ registry: PortableRegistry;
14
+ toPayload(...additional: any[]): Partial<SignerPayloadJSON>;
15
+ }
16
+ interface SignedExtensionOptions {
17
+ def?: SignedExtensionDefLatest;
18
+ signerAddress?: string;
19
+ payloadOptions?: Partial<PayloadOptions>;
20
+ }
21
+ export declare abstract class SignedExtension<Data extends any = {}, AdditionalSigned extends any = []> implements ISignedExtension {
22
+ api: ISubstrateClient;
23
+ options?: SignedExtensionOptions | undefined;
24
+ data: Data;
25
+ additionalSigned: AdditionalSigned;
26
+ constructor(api: ISubstrateClient, options?: SignedExtensionOptions | undefined);
27
+ init(): Promise<void>;
28
+ get identifier(): string;
29
+ get $Data(): $.AnyShape;
30
+ get $AdditionalSigned(): $.AnyShape;
31
+ get registry(): PortableRegistry;
32
+ get signedExtensionDef(): {
33
+ ident: string;
34
+ typeId: number;
35
+ additionalSigned: number;
36
+ };
37
+ get payloadOptions(): Partial<PayloadOptions>;
38
+ toPayload(...args: any[]): Partial<SignerPayloadJSON>;
39
+ }
40
+ export {};
@@ -0,0 +1,37 @@
1
+ import { ensurePresence } from '@dedot/utils';
2
+ export class SignedExtension {
3
+ api;
4
+ options;
5
+ data;
6
+ additionalSigned;
7
+ constructor(api, options) {
8
+ this.api = api;
9
+ this.options = options;
10
+ this.data = {};
11
+ this.additionalSigned = [];
12
+ }
13
+ async init() {
14
+ // TODO implement this method
15
+ }
16
+ get identifier() {
17
+ return this.signedExtensionDef.ident;
18
+ }
19
+ get $Data() {
20
+ return ensurePresence(this.registry.findCodec(this.signedExtensionDef.typeId));
21
+ }
22
+ get $AdditionalSigned() {
23
+ return ensurePresence(this.registry.findCodec(this.signedExtensionDef.additionalSigned));
24
+ }
25
+ get registry() {
26
+ return this.api.registry;
27
+ }
28
+ get signedExtensionDef() {
29
+ return ensurePresence(this.options.def);
30
+ }
31
+ get payloadOptions() {
32
+ return this.options?.payloadOptions || {};
33
+ }
34
+ toPayload(...args) {
35
+ return {};
36
+ }
37
+ }
@@ -0,0 +1,3 @@
1
+ export * from './SignedExtension.js';
2
+ export * from './known/index.js';
3
+ export * from './ExtraSignedExtension.js';
@@ -0,0 +1,3 @@
1
+ export * from './SignedExtension.js';
2
+ export * from './known/index.js';
3
+ export * from './ExtraSignedExtension.js';
@@ -0,0 +1,14 @@
1
+ import { SignerPayloadJSON } from '@polkadot/types/types';
2
+ import { SignedExtension } from '../SignedExtension.js';
3
+ /**
4
+ * @name ChargeAssetTxPayment
5
+ */
6
+ export declare class ChargeAssetTxPayment extends SignedExtension<{
7
+ tip: bigint;
8
+ assetId?: number | object;
9
+ }> {
10
+ #private;
11
+ init(): Promise<void>;
12
+ toPayload(): Partial<SignerPayloadJSON>;
13
+ $AssetId(): any;
14
+ }
@@ -0,0 +1,39 @@
1
+ import { assert, bnToHex, u8aToHex } from '@dedot/utils';
2
+ import { SignedExtension } from '../SignedExtension.js';
3
+ /**
4
+ * @name ChargeAssetTxPayment
5
+ */
6
+ export class ChargeAssetTxPayment extends SignedExtension {
7
+ async init() {
8
+ const { tip, assetId } = this.payloadOptions;
9
+ this.data = {
10
+ tip: tip || 0n,
11
+ assetId,
12
+ };
13
+ }
14
+ toPayload() {
15
+ const { tip, assetId } = this.data;
16
+ return {
17
+ tip: bnToHex(tip),
18
+ // @ts-ignore
19
+ assetId: this.#encodeAssetId(assetId),
20
+ };
21
+ }
22
+ #encodeAssetId(assetId) {
23
+ if (assetId === null || assetId === undefined) {
24
+ return undefined;
25
+ }
26
+ return u8aToHex(this.$AssetId().tryEncode(assetId));
27
+ }
28
+ $AssetId() {
29
+ const extensionTypeDef = this.registry.findType(this.signedExtensionDef.typeId);
30
+ assert(extensionTypeDef.type.tag === 'Struct');
31
+ const assetIdTypeDef = extensionTypeDef.type.value.fields.find((f) => f.name === 'asset_id');
32
+ const $codec = this.registry.findCodec(assetIdTypeDef.typeId);
33
+ const codecMetadata = $codec.metadata[0];
34
+ if (codecMetadata.name === '$.option') {
35
+ return codecMetadata.args[0]; // inner shape
36
+ }
37
+ return $codec;
38
+ }
39
+ }
@@ -0,0 +1,6 @@
1
+ import { SignerPayloadJSON } from '@polkadot/types/types';
2
+ import { SignedExtension } from '../SignedExtension.js';
3
+ export declare class ChargeTransactionPayment extends SignedExtension<bigint> {
4
+ init(): Promise<void>;
5
+ toPayload(): Partial<SignerPayloadJSON>;
6
+ }
@@ -0,0 +1,12 @@
1
+ import { bnToHex } from '@dedot/utils';
2
+ import { SignedExtension } from '../SignedExtension.js';
3
+ export class ChargeTransactionPayment extends SignedExtension {
4
+ async init() {
5
+ this.data = this.payloadOptions.tip || 0n;
6
+ }
7
+ toPayload() {
8
+ return {
9
+ tip: bnToHex(this.data),
10
+ };
11
+ }
12
+ }
@@ -0,0 +1,10 @@
1
+ import { SignerPayloadJSON } from '@polkadot/types/types';
2
+ import { Hash } from '@dedot/codecs';
3
+ import { SignedExtension } from '../SignedExtension.js';
4
+ /**
5
+ * @description Genesis hash check to provide replay protection between different networks.
6
+ */
7
+ export declare class CheckGenesis extends SignedExtension<{}, Hash> {
8
+ init(): Promise<void>;
9
+ toPayload(): Partial<SignerPayloadJSON>;
10
+ }
@@ -0,0 +1,15 @@
1
+ import { ensurePresence } from '@dedot/utils';
2
+ import { SignedExtension } from '../SignedExtension.js';
3
+ /**
4
+ * @description Genesis hash check to provide replay protection between different networks.
5
+ */
6
+ export class CheckGenesis extends SignedExtension {
7
+ async init() {
8
+ this.additionalSigned = ensurePresence(this.api.genesisHash);
9
+ }
10
+ toPayload() {
11
+ return {
12
+ genesisHash: this.additionalSigned,
13
+ };
14
+ }
15
+ }
@@ -0,0 +1,15 @@
1
+ import { SignerPayloadJSON } from '@polkadot/types/types';
2
+ import { EraLike, Hash } from '@dedot/codecs';
3
+ import { SignedExtension } from '../SignedExtension.js';
4
+ export declare const MAX_FINALITY_LAG: number;
5
+ export declare const FALLBACK_MAX_HASH_COUNT: number;
6
+ export declare const FALLBACK_PERIOD: number;
7
+ export declare const MORTAL_PERIOD: number;
8
+ /**
9
+ * @description Check for transaction mortality.
10
+ */
11
+ export declare class CheckMortality extends SignedExtension<EraLike, Hash> {
12
+ #private;
13
+ init(): Promise<void>;
14
+ toPayload(): Partial<SignerPayloadJSON>;
15
+ }