dedot 0.0.1-alpha.11

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 (103) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +251 -0
  3. package/cjs/client/Dedot.js +447 -0
  4. package/cjs/client/index.js +17 -0
  5. package/cjs/executor/ConstantExecutor.js +22 -0
  6. package/cjs/executor/ErrorExecutor.js +47 -0
  7. package/cjs/executor/EventExecutor.js +53 -0
  8. package/cjs/executor/Executor.js +39 -0
  9. package/cjs/executor/RpcExecutor.js +109 -0
  10. package/cjs/executor/RuntimeApiExecutor.js +93 -0
  11. package/cjs/executor/StorageQueryExecutor.js +33 -0
  12. package/cjs/executor/TxExecutor.js +153 -0
  13. package/cjs/executor/index.js +24 -0
  14. package/cjs/extrinsic/SubmittableResult.js +33 -0
  15. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +88 -0
  16. package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
  17. package/cjs/extrinsic/extensions/index.js +19 -0
  18. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +44 -0
  19. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
  20. package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
  21. package/cjs/extrinsic/extensions/known/CheckMortality.js +71 -0
  22. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
  23. package/cjs/extrinsic/extensions/known/CheckNonce.js +34 -0
  24. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
  25. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
  26. package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
  27. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
  28. package/cjs/extrinsic/extensions/known/index.js +25 -0
  29. package/cjs/extrinsic/index.js +18 -0
  30. package/cjs/index.js +25 -0
  31. package/cjs/package.json +1 -0
  32. package/cjs/packageInfo.js +5 -0
  33. package/cjs/proxychain.js +20 -0
  34. package/cjs/storage/QueryableStorage.js +99 -0
  35. package/cjs/test.js +5 -0
  36. package/cjs/types.js +2 -0
  37. package/client/Dedot.d.ts +181 -0
  38. package/client/Dedot.js +440 -0
  39. package/client/index.d.ts +1 -0
  40. package/client/index.js +1 -0
  41. package/executor/ConstantExecutor.d.ts +11 -0
  42. package/executor/ConstantExecutor.js +18 -0
  43. package/executor/ErrorExecutor.d.ts +11 -0
  44. package/executor/ErrorExecutor.js +43 -0
  45. package/executor/EventExecutor.d.ts +11 -0
  46. package/executor/EventExecutor.js +49 -0
  47. package/executor/Executor.d.ts +150 -0
  48. package/executor/Executor.js +35 -0
  49. package/executor/RpcExecutor.d.ts +14 -0
  50. package/executor/RpcExecutor.js +105 -0
  51. package/executor/RuntimeApiExecutor.d.ts +9 -0
  52. package/executor/RuntimeApiExecutor.js +89 -0
  53. package/executor/StorageQueryExecutor.d.ts +10 -0
  54. package/executor/StorageQueryExecutor.js +29 -0
  55. package/executor/TxExecutor.d.ts +47 -0
  56. package/executor/TxExecutor.js +147 -0
  57. package/executor/index.d.ts +8 -0
  58. package/executor/index.js +8 -0
  59. package/extrinsic/SubmittableResult.d.ts +19 -0
  60. package/extrinsic/SubmittableResult.js +29 -0
  61. package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
  62. package/extrinsic/extensions/ExtraSignedExtension.js +61 -0
  63. package/extrinsic/extensions/SignedExtension.d.ts +40 -0
  64. package/extrinsic/extensions/SignedExtension.js +37 -0
  65. package/extrinsic/extensions/index.d.ts +3 -0
  66. package/extrinsic/extensions/index.js +3 -0
  67. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
  68. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +40 -0
  69. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
  70. package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
  71. package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
  72. package/extrinsic/extensions/known/CheckGenesis.js +15 -0
  73. package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
  74. package/extrinsic/extensions/known/CheckMortality.js +67 -0
  75. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
  76. package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
  77. package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
  78. package/extrinsic/extensions/known/CheckNonce.js +30 -0
  79. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
  80. package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
  81. package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
  82. package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
  83. package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
  84. package/extrinsic/extensions/known/CheckWeight.js +6 -0
  85. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
  86. package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
  87. package/extrinsic/extensions/known/index.d.ts +3 -0
  88. package/extrinsic/extensions/known/index.js +22 -0
  89. package/extrinsic/index.d.ts +2 -0
  90. package/extrinsic/index.js +2 -0
  91. package/index.d.ts +6 -0
  92. package/index.js +5 -0
  93. package/package.json +54 -0
  94. package/packageInfo.d.ts +4 -0
  95. package/packageInfo.js +2 -0
  96. package/proxychain.d.ts +7 -0
  97. package/proxychain.js +16 -0
  98. package/storage/QueryableStorage.d.ts +16 -0
  99. package/storage/QueryableStorage.js +95 -0
  100. package/test.d.ts +1 -0
  101. package/test.js +5 -0
  102. package/types.d.ts +44 -0
  103. package/types.js +1 -0
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RpcExecutor = void 0;
4
+ const util_1 = require("@polkadot/util");
5
+ const specs_1 = require("@dedot/specs");
6
+ const utils_1 = require("@dedot/utils");
7
+ const Executor_1 = require("./Executor");
8
+ const isOptionalParam = (param) => {
9
+ return param.isOptional || param.name.startsWith('Option<');
10
+ };
11
+ /**
12
+ * @name RpcExecutor
13
+ * @description Execute a rpc based from an RPC spec,
14
+ * if a spec is not found we execute an arbitrary rpc method
15
+ */
16
+ class RpcExecutor extends Executor_1.Executor {
17
+ execute(section, method) {
18
+ const maybeRpcName = `${section}_${method}`;
19
+ const callSpec = (0, specs_1.findRpcSpec)(maybeRpcName) || (0, specs_1.findAliasRpcSpec)(maybeRpcName);
20
+ const rpcName = callSpec?.name || `${section}_${method}`;
21
+ const isSubscription = !!callSpec?.pubsub;
22
+ const fnRpc = async (...args) => {
23
+ (0, utils_1.assert)(callSpec, 'Rpc spec not found');
24
+ this.checkRpcInputs(callSpec, args);
25
+ const { params } = callSpec;
26
+ const formattedInputs = args.map((input, index) => this.tryEncode(params[index], input));
27
+ const result = await this.provider.send(rpcName, formattedInputs);
28
+ return this.tryDecode(callSpec, result);
29
+ };
30
+ const fnSubRpc = async (...args) => {
31
+ (0, utils_1.assert)(callSpec, 'Rpc spec not found');
32
+ const inArgs = args.slice();
33
+ const callback = inArgs.pop();
34
+ (0, utils_1.assert)((0, util_1.isFunction)(callback), 'A callback is required for subscription');
35
+ this.checkRpcInputs(callSpec, inArgs);
36
+ const onNewMessage = (error, result) => {
37
+ if (error) {
38
+ console.error(error);
39
+ return;
40
+ }
41
+ callback(this.tryDecode(callSpec, result));
42
+ };
43
+ const { params, pubsub } = callSpec;
44
+ const formattedInputs = inArgs.map((input, index) => this.tryEncode(params[index], input));
45
+ const [subname, subscribe, unsubcribe] = pubsub;
46
+ const subscription = this.provider.subscribe(subname, subscribe, formattedInputs, onNewMessage);
47
+ return async () => {
48
+ return subscription
49
+ .then((subscriptionId) => this.provider.unsubscribe(subname, unsubcribe, subscriptionId))
50
+ .catch((err) => {
51
+ console.error(err);
52
+ return false;
53
+ });
54
+ };
55
+ };
56
+ if (!callSpec) {
57
+ // Call arbitrary rpc method
58
+ return async (...args) => {
59
+ return await this.provider.send(rpcName, args);
60
+ };
61
+ }
62
+ const oneFnRpc = isSubscription ? fnSubRpc : fnRpc;
63
+ oneFnRpc.meta = callSpec;
64
+ return oneFnRpc;
65
+ }
66
+ tryDecode(callSpec, raw) {
67
+ if (raw === null) {
68
+ // We use `undefined` to represent Option::None in the client
69
+ return undefined;
70
+ }
71
+ const { type, isScale } = callSpec;
72
+ if (isScale) {
73
+ return this.registry.findCodec(type).tryDecode(raw);
74
+ }
75
+ if ((0, utils_1.isNativeType)(type)) {
76
+ return raw;
77
+ }
78
+ return raw;
79
+ }
80
+ tryEncode(paramSpec, value) {
81
+ const { type, isScale, isOptional } = paramSpec;
82
+ if (isScale) {
83
+ if (isOptional && (value === undefined || value === null)) {
84
+ return null;
85
+ }
86
+ const $codec = this.registry.findCodec(type);
87
+ return (0, util_1.u8aToHex)($codec.tryEncode(value));
88
+ }
89
+ // TODO generalize this!
90
+ if (value && value['toJSON']) {
91
+ return value.toJSON();
92
+ }
93
+ return value;
94
+ }
95
+ checkRpcInputs(callSpec, actualArgs) {
96
+ const params = callSpec.params;
97
+ if (params.length === actualArgs.length) {
98
+ return;
99
+ }
100
+ params.forEach((param, idx) => {
101
+ const isRequiredParam = !isOptionalParam(param);
102
+ if (isRequiredParam && actualArgs[idx] === undefined) {
103
+ const requiredParamCount = params.filter((p) => !isOptionalParam(p)).length;
104
+ throw new Error(`Miss match RPC params, required ${requiredParamCount} params, current inputs length: ${actualArgs.length}`);
105
+ }
106
+ });
107
+ }
108
+ }
109
+ exports.RpcExecutor = RpcExecutor;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RuntimeApiExecutor = exports.FallbackRuntimeApis = void 0;
4
+ const Executor_1 = require("./Executor");
5
+ const utils_1 = require("@dedot/utils");
6
+ const util_1 = require("@polkadot/util");
7
+ const specs_1 = require("@dedot/specs");
8
+ exports.FallbackRuntimeApis = [
9
+ ['0x37e397fc7c91f5e4', 2], // Metadata Api v2
10
+ ];
11
+ class RuntimeApiExecutor extends Executor_1.Executor {
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);
17
+ (0, utils_1.assert)(callSpec, `Runtime Api not found: ${callName}`);
18
+ const callFn = async (...args) => {
19
+ const { params } = callSpec;
20
+ const formattedInputs = params.map((param, index) => this.tryEncode(param, args[index]));
21
+ const bytes = (0, util_1.u8aToHex)((0, util_1.u8aConcat)(...formattedInputs));
22
+ const callArgs = [callName, bytes];
23
+ if (this.atBlockHash) {
24
+ callArgs.push(this.atBlockHash);
25
+ }
26
+ const result = await this.api.rpc.state.call(...callArgs);
27
+ return this.tryDecode(callSpec, result);
28
+ };
29
+ callFn.meta = callSpec;
30
+ return callFn;
31
+ }
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);
38
+ }
39
+ tryEncode(paramSpec, value) {
40
+ const { type, typeId } = paramSpec;
41
+ const $codec = (0, util_1.isNumber)(typeId) ? this.registry.findPortableCodec(typeId) : this.registry.findCodec(type);
42
+ return $codec.tryEncode(value);
43
+ }
44
+ #findRuntimeApiMethodSpec(runtimeApi, method) {
45
+ const methodDef = this.#findRuntimeApiMethodDef(runtimeApi, method);
46
+ if (methodDef) {
47
+ return this.#toMethodSpec(runtimeApi, methodDef);
48
+ }
49
+ return this.#findRuntimeApiMethodExternalSpec(runtimeApi, method);
50
+ }
51
+ #findRuntimeApiMethodDef(runtimeApi, method) {
52
+ try {
53
+ for (const api of this.metadata.apis) {
54
+ if (api.name !== runtimeApi)
55
+ continue;
56
+ for (const apiMethod of api.methods) {
57
+ if (apiMethod.name === method)
58
+ return apiMethod;
59
+ }
60
+ }
61
+ }
62
+ catch { }
63
+ }
64
+ #toMethodSpec(runtimeApi, methodDef) {
65
+ if (!methodDef)
66
+ return undefined;
67
+ const { name, inputs, output, docs } = methodDef;
68
+ return {
69
+ docs,
70
+ runtimeApiName: runtimeApi,
71
+ methodName: name,
72
+ type: '', // TODO generate type name
73
+ typeId: output,
74
+ params: inputs.map(({ name, typeId }) => ({
75
+ name,
76
+ type: '', // TODO generate type name
77
+ typeId,
78
+ })),
79
+ };
80
+ }
81
+ #findRuntimeApiMethodExternalSpec(runtimeApi, method) {
82
+ const targetRuntimeApiHash = (0, utils_1.calculateRuntimeApiHash)(runtimeApi);
83
+ const runtimeApiVersions = this.api.runtimeVersion?.apis || exports.FallbackRuntimeApis;
84
+ const targetRuntimeApiVersion = runtimeApiVersions
85
+ .find(([supportedRuntimeApiHash]) => targetRuntimeApiHash === supportedRuntimeApiHash)
86
+ ?.at(1);
87
+ if (!(0, util_1.isNumber)(targetRuntimeApiVersion)) {
88
+ return undefined;
89
+ }
90
+ return (0, specs_1.findRuntimeApiMethodSpec)(`${runtimeApi}_${method}`, targetRuntimeApiVersion);
91
+ }
92
+ }
93
+ exports.RuntimeApiExecutor = RuntimeApiExecutor;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageQueryExecutor = void 0;
4
+ const Executor_1 = require("./Executor");
5
+ const QueryableStorage_1 = require("../storage/QueryableStorage");
6
+ const util_1 = require("@polkadot/util");
7
+ /**
8
+ * @name StorageQueryExecutor
9
+ * @description Execute a query to on-chain storage
10
+ */
11
+ class StorageQueryExecutor extends Executor_1.Executor {
12
+ execute(pallet, storage) {
13
+ return async (...args) => {
14
+ const entry = new QueryableStorage_1.QueryableStorage(this.registry, pallet, storage);
15
+ const inArgs = args.slice();
16
+ const lastArg = args.at(-1);
17
+ const callback = (0, util_1.isFunction)(lastArg) ? inArgs.pop() : undefined;
18
+ const encodedKey = entry.encodeKey(inArgs.at(0));
19
+ // if a callback is passed, make a storage subscription and return an unsub function
20
+ if (callback) {
21
+ return await this.api.rpc.state.subscribeStorage([encodedKey], (changeSet) => {
22
+ const targetChange = changeSet.changes.find((change) => change[0] === encodedKey);
23
+ targetChange && callback(entry.decodeValue(targetChange[1]));
24
+ });
25
+ }
26
+ else {
27
+ const result = await this.api.rpc.state.getStorage(encodedKey, this.atBlockHash);
28
+ return entry.decodeValue(result);
29
+ }
30
+ };
31
+ }
32
+ }
33
+ exports.StorageQueryExecutor = StorageQueryExecutor;
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TxExecutor = exports.signRaw = exports.isKeyringPair = void 0;
4
+ const Executor_1 = require("./Executor");
5
+ const utils_1 = require("@dedot/utils");
6
+ const util_1 = require("@polkadot/util");
7
+ const codecs_1 = require("@dedot/codecs");
8
+ const extrinsic_1 = require("../extrinsic");
9
+ const util_crypto_1 = require("@polkadot/util-crypto");
10
+ function isKeyringPair(account) {
11
+ return (0, util_1.isFunction)(account.sign);
12
+ }
13
+ exports.isKeyringPair = isKeyringPair;
14
+ /**
15
+ * Sign a raw message
16
+ * @param signerPair
17
+ * @param raw
18
+ * @param options
19
+ */
20
+ function signRaw(signerPair, raw, options) {
21
+ const u8a = (0, util_1.hexToU8a)(raw);
22
+ // Ref: https://github.com/paritytech/polkadot-sdk/blob/943697fa693a4da6ef481ef93df522accb7d0583/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs#L234-L238
23
+ const toSignRaw = u8a.length > 256 ? (0, util_crypto_1.blake2AsU8a)(u8a, 256) : u8a;
24
+ return signerPair.sign(toSignRaw, options);
25
+ }
26
+ exports.signRaw = signRaw;
27
+ /**
28
+ * @name TxExecutor
29
+ * @description Execute a transaction instruction, returns a submittable extrinsic
30
+ */
31
+ class TxExecutor extends Executor_1.Executor {
32
+ execute(pallet, functionName) {
33
+ const targetPallet = this.getPallet(pallet);
34
+ (0, utils_1.assert)(targetPallet.calls, 'Tx call type not found');
35
+ const txType = this.metadata.types[targetPallet.calls];
36
+ (0, utils_1.assert)(txType.type.tag === 'Enum', 'Tx type should be enum');
37
+ const isFlatEnum = txType.type.value.members.every((m) => m.fields.length === 0);
38
+ const txCallDef = txType.type.value.members.find((m) => (0, util_1.stringCamelCase)(m.name) === functionName);
39
+ (0, utils_1.assert)(txCallDef, 'Tx call not found');
40
+ const txCallFn = (...args) => {
41
+ let call;
42
+ if (isFlatEnum) {
43
+ call = {
44
+ pallet: (0, util_1.stringPascalCase)(targetPallet.name),
45
+ palletCall: (0, util_1.stringPascalCase)(txCallDef.name),
46
+ };
47
+ }
48
+ else {
49
+ const callParams = txCallDef.fields.reduce((o, { name }, idx) => {
50
+ o[(0, util_1.stringCamelCase)(name)] = args[idx];
51
+ return o;
52
+ }, {});
53
+ call = {
54
+ pallet: (0, util_1.stringPascalCase)(targetPallet.name),
55
+ palletCall: {
56
+ name: (0, util_1.stringPascalCase)(txCallDef.name),
57
+ params: callParams,
58
+ },
59
+ };
60
+ }
61
+ return this.createExtrinsic(call);
62
+ };
63
+ txCallFn.meta = {
64
+ ...txCallDef,
65
+ fieldCodecs: txCallDef.fields.map(({ typeId }) => this.registry.findPortableCodec(typeId)),
66
+ pallet: targetPallet.name,
67
+ palletIndex: targetPallet.index,
68
+ };
69
+ return txCallFn;
70
+ }
71
+ createExtrinsic(call) {
72
+ const api = this.api;
73
+ class SubmittableExtrinsic extends codecs_1.Extrinsic {
74
+ async sign(fromAccount, options) {
75
+ const address = isKeyringPair(fromAccount) ? fromAccount.address : fromAccount.toString();
76
+ const extra = new extrinsic_1.ExtraSignedExtension(api, {
77
+ signerAddress: address,
78
+ payloadOptions: options,
79
+ });
80
+ await extra.init();
81
+ const { signer } = options || {};
82
+ let signature;
83
+ if (isKeyringPair(fromAccount)) {
84
+ signature = (0, util_1.u8aToHex)(signRaw(fromAccount, extra.toRawPayload(this.callHex).data, { withType: true }));
85
+ }
86
+ else if (signer?.signPayload) {
87
+ const result = await signer.signPayload(extra.toPayload(this.callHex));
88
+ signature = result.signature;
89
+ }
90
+ else {
91
+ throw new Error('Signer not found. Cannot sign the extrinsic!');
92
+ }
93
+ const { signatureTypeId } = this.registry.metadata.extrinsic;
94
+ const $Signature = this.registry.findPortableCodec(signatureTypeId);
95
+ this.attachSignature({
96
+ address: address,
97
+ signature: $Signature.tryDecode(signature),
98
+ extra: extra.data,
99
+ });
100
+ return this;
101
+ }
102
+ async signAndSend(fromAccount, partialOptions, maybeCallback) {
103
+ const [options, callback] = this.#normalizeOptions(partialOptions, maybeCallback);
104
+ await this.sign(fromAccount, options);
105
+ return this.send(callback);
106
+ }
107
+ #normalizeOptions(partialOptions, callback) {
108
+ if ((0, util_1.isFunction)(partialOptions)) {
109
+ return [{}, partialOptions];
110
+ }
111
+ else {
112
+ return [(0, util_1.objectSpread)({}, partialOptions), callback];
113
+ }
114
+ }
115
+ async dryRun(account, optionsOrHash) {
116
+ const dryRunFn = api.rpc.system.dryRun;
117
+ (0, utils_1.assert)(dryRunFn.meta, 'system_dryRun RPC does not supported!');
118
+ if ((0, util_1.isHex)(optionsOrHash)) {
119
+ return dryRunFn(this.toHex(), optionsOrHash);
120
+ }
121
+ await this.sign(account, optionsOrHash);
122
+ return dryRunFn(this.toHex());
123
+ }
124
+ async send(callback) {
125
+ const isSubscription = !!callback;
126
+ const txHash = this.hash;
127
+ if (isSubscription) {
128
+ return api.rpc.author.submitAndWatchExtrinsic(this.toHex(), async (status) => {
129
+ if (status.tag === 'InBlock' || status.tag === 'Finalized') {
130
+ const blockHash = status.value;
131
+ const [signedBlock, blockEvents] = await Promise.all([
132
+ api.rpc.chain.getBlock(blockHash),
133
+ api.queryAt(blockHash).system.events(),
134
+ ]);
135
+ const txIndex = signedBlock.block.extrinsics.findIndex((tx) => (0, util_crypto_1.blake2AsHex)((0, util_1.hexToU8a)(tx)) === txHash);
136
+ (0, utils_1.assert)(txIndex >= 0, 'Extrinsic not found!');
137
+ const events = blockEvents.filter(({ phase }) => phase.tag === 'ApplyExtrinsic' && phase.value === txIndex);
138
+ return callback(new extrinsic_1.SubmittableResult({ status, txHash, events, txIndex }));
139
+ }
140
+ else {
141
+ return callback(new extrinsic_1.SubmittableResult({ status, txHash }));
142
+ }
143
+ });
144
+ }
145
+ else {
146
+ return api.rpc.author.submitExtrinsic(this.toHex());
147
+ }
148
+ }
149
+ }
150
+ return new SubmittableExtrinsic(api.registry, call);
151
+ }
152
+ }
153
+ exports.TxExecutor = TxExecutor;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Executor"), exports);
18
+ __exportStar(require("./RpcExecutor"), exports);
19
+ __exportStar(require("./ConstantExecutor"), exports);
20
+ __exportStar(require("./StorageQueryExecutor"), exports);
21
+ __exportStar(require("./ErrorExecutor"), exports);
22
+ __exportStar(require("./EventExecutor"), exports);
23
+ __exportStar(require("./RuntimeApiExecutor"), exports);
24
+ __exportStar(require("./TxExecutor"), exports);
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SubmittableResult = void 0;
4
+ class SubmittableResult {
5
+ dispatchInfo;
6
+ dispatchError;
7
+ events;
8
+ status;
9
+ txHash;
10
+ txIndex;
11
+ constructor({ events, status, txHash, txIndex }) {
12
+ this.events = events || [];
13
+ this.status = status;
14
+ this.txHash = txHash;
15
+ this.txIndex = txIndex;
16
+ [this.dispatchInfo, this.dispatchError] = this.#extractInfo();
17
+ }
18
+ #extractInfo() {
19
+ for (const { event } of this.events) {
20
+ const { pallet, palletEvent } = event;
21
+ if (pallet === 'System' && palletEvent.name === 'ExtrinsicFailed') {
22
+ const { dispatchInfo, dispatchError } = palletEvent.data;
23
+ return [dispatchInfo, dispatchError];
24
+ }
25
+ else if (pallet === 'System' && palletEvent.name === 'ExtrinsicSuccess') {
26
+ const { dispatchInfo } = palletEvent.data;
27
+ return [dispatchInfo, undefined];
28
+ }
29
+ }
30
+ return [undefined, undefined];
31
+ }
32
+ }
33
+ exports.SubmittableResult = SubmittableResult;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ExtraSignedExtension = void 0;
27
+ const SignedExtension_1 = require("./SignedExtension");
28
+ const utils_1 = require("@dedot/utils");
29
+ const $ = __importStar(require("@dedot/shape"));
30
+ const known_1 = require("./known");
31
+ const util_1 = require("@polkadot/util");
32
+ class ExtraSignedExtension extends SignedExtension_1.SignedExtension {
33
+ #signedExtensions;
34
+ async init() {
35
+ this.#signedExtensions = this.#getSignedExtensions();
36
+ await Promise.all(this.#signedExtensions.map((se) => se.init()));
37
+ this.data = this.#signedExtensions.map((se) => se.data);
38
+ this.additionalSigned = this.#signedExtensions.map((se) => se.additionalSigned);
39
+ }
40
+ get identifier() {
41
+ return 'ExtraSignedExtension';
42
+ }
43
+ get dataCodec() {
44
+ const { extraTypeId } = this.api.metadataLatest.extrinsic;
45
+ return (0, utils_1.ensurePresence)(this.registry.findPortableCodec(extraTypeId));
46
+ }
47
+ get additionalSignedCodec() {
48
+ const $AdditionalSignedCodecs = this.#signedExtensionDefs.map((se) => this.registry.findPortableCodec(se.additionalSigned));
49
+ return $.Tuple(...$AdditionalSignedCodecs);
50
+ }
51
+ get payloadCodec() {
52
+ const { callTypeId } = this.api.metadataLatest.extrinsic;
53
+ const $Call = this.registry.findPortableCodec(callTypeId);
54
+ return $.Tuple($Call, this.dataCodec, this.additionalSignedCodec);
55
+ }
56
+ get #signedExtensionDefs() {
57
+ return this.api.metadataLatest.extrinsic.signedExtensions;
58
+ }
59
+ #getSignedExtensions() {
60
+ return this.#signedExtensionDefs.map((extDef) => {
61
+ const { signedExtensions: userSignedExtensions = {} } = this.api.options;
62
+ const Extension = userSignedExtensions[extDef.ident] ||
63
+ known_1.knownSignedExtensions[extDef.ident];
64
+ (0, utils_1.assert)(Extension, `SignedExtension for ${extDef.ident} not found`);
65
+ return new Extension(this.api, {
66
+ ...(0, utils_1.ensurePresence)(this.options),
67
+ def: extDef,
68
+ });
69
+ });
70
+ }
71
+ toPayload(call = '0x') {
72
+ const signedExtensions = this.#signedExtensions.map((se) => se.identifier);
73
+ const { version } = this.api.registry.metadata.extrinsic;
74
+ return (0, util_1.objectSpread)({ address: this.options.signerAddress, signedExtensions, version, method: call }, ...this.#signedExtensions.map((se) => se.toPayload()));
75
+ }
76
+ toRawPayload(call = '0x') {
77
+ const payload = this.toPayload(call);
78
+ const $ToSignPayload = $.Tuple($.RawHex, this.dataCodec, this.additionalSignedCodec);
79
+ const toSignPayload = [call, this.data, this.additionalSigned];
80
+ const rawPayload = $ToSignPayload.tryEncode(toSignPayload);
81
+ return {
82
+ address: payload.address,
83
+ data: (0, util_1.u8aToHex)(rawPayload),
84
+ type: 'payload',
85
+ };
86
+ }
87
+ }
88
+ exports.ExtraSignedExtension = ExtraSignedExtension;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SignedExtension = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ class SignedExtension {
6
+ api;
7
+ options;
8
+ data;
9
+ additionalSigned;
10
+ constructor(api, options) {
11
+ this.api = api;
12
+ this.options = options;
13
+ this.data = {};
14
+ this.additionalSigned = [];
15
+ }
16
+ async init() {
17
+ // TODO implement this method
18
+ }
19
+ get identifier() {
20
+ return this.signedExtensionDef.ident;
21
+ }
22
+ get dataCodec() {
23
+ return (0, utils_1.ensurePresence)(this.api.registry.findPortableCodec(this.signedExtensionDef.typeId));
24
+ }
25
+ get additionalSignedCodec() {
26
+ return (0, utils_1.ensurePresence)(this.api.registry.findPortableCodec(this.signedExtensionDef.additionalSigned));
27
+ }
28
+ get registry() {
29
+ return this.api.registry;
30
+ }
31
+ get signedExtensionDef() {
32
+ return (0, utils_1.ensurePresence)(this.options.def);
33
+ }
34
+ get payloadOptions() {
35
+ return this.options?.payloadOptions || {};
36
+ }
37
+ toPayload(...args) {
38
+ return {};
39
+ }
40
+ }
41
+ exports.SignedExtension = SignedExtension;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./SignedExtension"), exports);
18
+ __exportStar(require("./known"), exports);
19
+ __exportStar(require("./ExtraSignedExtension"), exports);
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChargeAssetTxPayment = void 0;
4
+ const SignedExtension_1 = require("../SignedExtension");
5
+ const util_1 = require("@polkadot/util");
6
+ const utils_1 = require("@dedot/utils");
7
+ /**
8
+ * @name ChargeAssetTxPayment
9
+ */
10
+ class ChargeAssetTxPayment extends SignedExtension_1.SignedExtension {
11
+ async init() {
12
+ const { tip, assetId } = this.payloadOptions;
13
+ this.data = {
14
+ tip: tip || 0n,
15
+ assetId,
16
+ };
17
+ }
18
+ toPayload() {
19
+ const { tip, assetId } = this.data;
20
+ return {
21
+ tip: (0, util_1.bnToHex)(tip, { isLe: false }),
22
+ // @ts-ignore
23
+ assetId: this.#encodeAssetId(assetId),
24
+ };
25
+ }
26
+ #encodeAssetId(assetId) {
27
+ if (assetId === null || assetId === undefined) {
28
+ return undefined;
29
+ }
30
+ return (0, util_1.u8aToHex)(this.$AssetId().tryEncode(assetId));
31
+ }
32
+ $AssetId() {
33
+ const extensionTypeDef = this.registry.findPortableType(this.signedExtensionDef.typeId);
34
+ (0, utils_1.assert)(extensionTypeDef.type.tag === 'Struct');
35
+ const assetIdTypeDef = extensionTypeDef.type.value.fields.find((f) => f.name === 'asset_id');
36
+ const $codec = this.registry.findPortableCodec(assetIdTypeDef.typeId);
37
+ const codecMetadata = $codec.metadata[0];
38
+ if (codecMetadata.name === '$.option') {
39
+ return codecMetadata.args[0]; // inner shape
40
+ }
41
+ return $codec;
42
+ }
43
+ }
44
+ exports.ChargeAssetTxPayment = ChargeAssetTxPayment;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChargeTransactionPayment = void 0;
4
+ const SignedExtension_1 = require("../SignedExtension");
5
+ const util_1 = require("@polkadot/util");
6
+ class ChargeTransactionPayment extends SignedExtension_1.SignedExtension {
7
+ async init() {
8
+ this.data = this.payloadOptions.tip || 0n;
9
+ }
10
+ toPayload() {
11
+ return {
12
+ tip: (0, util_1.bnToHex)(this.data, { isLe: false }),
13
+ };
14
+ }
15
+ }
16
+ exports.ChargeTransactionPayment = ChargeTransactionPayment;