@talismn/scale 0.0.0-pr2277-20251211070508 → 0.0.0-pr2295-20260110031023

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.
@@ -1,410 +0,0 @@
1
- import { getLookupFn, getDynamicBuilder } from '@polkadot-api/metadata-builders';
2
- export { getDynamicBuilder, getLookupFn } from '@polkadot-api/metadata-builders';
3
- import { metadata, Binary, decAnyMetadata, unifyMetadata, Twox128 } from '@polkadot-api/substrate-bindings';
4
- export { Binary, Blake2128, Blake2128Concat, Blake2256, Blake3256, Blake3256Concat, FixedSizeBinary, Twox128, Twox256, Twox64Concat, decAnyMetadata, metadata, unifyMetadata } from '@polkadot-api/substrate-bindings';
5
- import { toHex } from '@polkadot-api/utils';
6
- export { fromHex, mergeUint8, toHex } from '@polkadot-api/utils';
7
- import anylogger from 'anylogger';
8
- import { Struct, u8, u32 } from 'scale-ts';
9
-
10
- /** Constant: https://docs.substrate.io/build/application-development/#metadata-format */
11
- const magicNumber = 1635018093;
12
-
13
- var packageJson = {
14
- name: "@talismn/scale"};
15
-
16
- var log = anylogger(packageJson.name);
17
-
18
- /**
19
- * Converts a `Metadata` into a `MiniMetadata`.
20
- *
21
- * A `MiniMetadata` only contains the types inside of its `lookup` which are relevant for
22
- * the storage queries specified in `palletsAndItems`.
23
- *
24
- * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
25
- * types used in the `System.Account` storage query will remain inside of metadata.lookups.
26
- */
27
- const compactMetadata = (anyMetadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
28
- if (!isCompactableMetadata(anyMetadata)) throw new Error(`Metadata version ${anyMetadata.metadata.tag} not supported in compactMetadata`);
29
- const metadata = anyMetadata.metadata.value;
30
-
31
- // remove pallets we don't care about
32
- metadata.pallets = metadata.pallets.filter(pallet =>
33
- // keep this pallet if it's listed in `palletsAndItems`
34
- palletsAndItems.some(({
35
- pallet: palletName
36
- }) => pallet.name === palletName));
37
-
38
- // remove fields we don't care about from each pallet, and extract types for each storage item we care about
39
- const palletsKeepTypes = palletsAndItems.flatMap(({
40
- pallet: palletName,
41
- constants: constantNames,
42
- items: itemNames
43
- }) => {
44
- const pallet = metadata.pallets.find(pallet => pallet.name === palletName);
45
- if (!pallet) return [];
46
-
47
- // remove pallet fields we don't care about
48
- pallet.calls = undefined;
49
- pallet.constants = constantNames ? pallet.constants.filter(constant => constantNames.includes(constant.name)) : [];
50
- // v15 (NOT v14) has docs
51
- if ("docs" in pallet) pallet.docs = [];
52
- pallet.errors = undefined;
53
- pallet.events = undefined;
54
- if (!pallet.storage) return [];
55
-
56
- // filter and extract storage items we care about
57
- pallet.storage.items = pallet.storage.items.filter(item => itemNames.some(itemName => item.name === itemName));
58
- return [...pallet.storage.items.flatMap(item => [
59
- // each type can be either "Plain" or "Map"
60
- // if it's "Plain" we only need to get the value type
61
- // if it's a "Map" we want to keep both the key AND the value types
62
- item.type.tag === "plain" && item.type.value, item.type.tag === "map" && item.type.value.key, item.type.tag === "map" && item.type.value.value]).filter(type => typeof type === "number"), ...pallet.constants.flatMap(constant => constant.type)];
63
- });
64
-
65
- // remove runtime apis we don't care about
66
- let runtimeApisKeepTypes = [];
67
- if ("apis" in metadata) {
68
- // metadata is v15 (NOT v14)
69
-
70
- // keep this api if it's listed in `runtimeApisAndMethods`
71
- metadata.apis = metadata.apis.filter(runtimeApi => runtimeApisAndMethods.some(({
72
- runtimeApi: runtimeApiName
73
- }) => runtimeApi.name === runtimeApiName));
74
-
75
- // remove methods we don't care about from each runtime api, and extract types for each call's params and result
76
- runtimeApisKeepTypes = runtimeApisAndMethods.flatMap(({
77
- runtimeApi: runtimeApiName,
78
- methods: methodNames
79
- }) => {
80
- const runtimeApi = metadata.apis.find(runtimeApi => runtimeApi.name === runtimeApiName);
81
- if (!runtimeApi) return [];
82
-
83
- // remove runtime fields we don't care about
84
- runtimeApi.docs = [];
85
- if (!runtimeApi.methods) return [];
86
-
87
- // filter and extract methods we care about
88
- runtimeApi.methods = runtimeApi.methods.filter(method => methodNames.some(methodName => method.name === methodName));
89
- return runtimeApi.methods.flatMap(method => [
90
- // each method has an array of input types (for the params)
91
- ...method.inputs.map(input => input.type),
92
- // and one output type (for the result)
93
- method.output]);
94
- });
95
- }
96
-
97
- // this is a set of type ids which we plan to keep in our compacted metadata
98
- // anything not in this set will be deleted
99
- // we start off with just the types of the state calls we plan to make,
100
- // then we run those types through a function (addDependentTypes) which will also include
101
- // all of the types which those types depend on - recursively
102
- const keepTypes = new Set([...palletsKeepTypes, ...runtimeApisKeepTypes]);
103
- extraKeepTypes?.forEach(type => keepTypes.add(type));
104
-
105
- // recursively find all the types which our keepTypes depend on and add them to the keepTypes set
106
- const metadataTysMap = new Map(metadata.lookup.map(ty => [ty.id, ty]));
107
- addDependentTypes(metadataTysMap, keepTypes, [...keepTypes]);
108
-
109
- // ditch the types we aren't keeping
110
- metadata.lookup = metadata.lookup.filter(type => keepTypes.has(type.id));
111
-
112
- // update all type ids to be sequential (fill the gaps left by the deleted types)
113
- const newTypeIds = new Map();
114
- metadata.lookup.forEach((type, index) => newTypeIds.set(type.id, index));
115
- const getNewTypeId = oldTypeId => {
116
- const newTypeId = newTypeIds.get(oldTypeId);
117
- if (typeof newTypeId !== "number") log.error(`Failed to find newTypeId for type ${oldTypeId}`);
118
- return newTypeId ?? 0;
119
- };
120
- remapTypeIds(metadata, getNewTypeId);
121
- if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
122
- if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
123
- if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
124
- if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
125
- if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
126
- if ("outerEnums" in metadata) {
127
- // metadata is v15 (NOT v14)
128
- metadata.outerEnums.call = 0;
129
- metadata.outerEnums.error = 0;
130
- metadata.outerEnums.event = 0;
131
- }
132
- };
133
- const isCompactableMetadata = metadata => {
134
- switch (metadata.metadata.tag) {
135
- case "v14":
136
- case "v15":
137
- case "v16":
138
- return true;
139
- default:
140
- return false;
141
- }
142
- };
143
- const addDependentTypes = (metadataTysMap, keepTypes, types,
144
- // Prevent stack overflow when a type references itself
145
- addedTypes = new Set()) => {
146
- const addDependentSubTypes = subTypes => addDependentTypes(metadataTysMap, keepTypes, subTypes, addedTypes);
147
- for (const typeId of types) {
148
- const type = metadataTysMap.get(typeId);
149
- if (!type) {
150
- log.warn(`Unable to find type with id ${typeId}`);
151
- continue;
152
- }
153
- if (addedTypes.has(type.id)) continue;
154
- keepTypes.add(type.id);
155
- addedTypes.add(type.id);
156
- const paramTypes = type.params.map(param => param.type).filter(type => typeof type === "number");
157
- addDependentSubTypes(paramTypes);
158
- switch (type.def.tag) {
159
- case "array":
160
- addDependentSubTypes([type.def.value.type]);
161
- break;
162
- case "bitSequence":
163
- addDependentSubTypes([type.def.value.bitOrderType, type.def.value.bitStoreType]);
164
- break;
165
- case "compact":
166
- addDependentSubTypes([type.def.value]);
167
- break;
168
- case "composite":
169
- addDependentSubTypes(type.def.value.map(field => field.type).filter(type => typeof type === "number"));
170
- break;
171
- case "primitive":
172
- break;
173
- case "sequence":
174
- addDependentSubTypes([type.def.value]);
175
- break;
176
- case "tuple":
177
- addDependentSubTypes(type.def.value.filter(type => typeof type === "number"));
178
- break;
179
- case "variant":
180
- addDependentSubTypes(type.def.value.flatMap(member => member.fields.map(field => field.type)).filter(type => typeof type === "number"));
181
- break;
182
- default:
183
- {
184
- // force compilation error if any types don't have a case
185
- const exhaustiveCheck = type.def;
186
- log.error(`Unhandled V15Type type ${exhaustiveCheck}`);
187
- }
188
- }
189
- }
190
- };
191
- const remapTypeIds = (metadata, getNewTypeId) => {
192
- remapLookupTypeIds(metadata, getNewTypeId);
193
- remapStorageTypeIds(metadata, getNewTypeId);
194
- remapRuntimeApisTypeIds(metadata, getNewTypeId);
195
- };
196
- const remapLookupTypeIds = (metadata, getNewTypeId) => {
197
- for (const type of metadata.lookup) {
198
- type.id = getNewTypeId(type.id);
199
- for (const param of type.params) {
200
- if (typeof param.type !== "number") continue;
201
- param.type = getNewTypeId(param.type);
202
- }
203
- switch (type.def.tag) {
204
- case "array":
205
- type.def.value.type = getNewTypeId(type.def.value.type);
206
- break;
207
- case "bitSequence":
208
- type.def.value.bitOrderType = getNewTypeId(type.def.value.bitOrderType);
209
- type.def.value.bitStoreType = getNewTypeId(type.def.value.bitStoreType);
210
- break;
211
- case "compact":
212
- type.def.value = getNewTypeId(type.def.value);
213
- break;
214
- case "composite":
215
- for (const field of type.def.value) {
216
- if (typeof field.type !== "number") continue;
217
- field.type = getNewTypeId(field.type);
218
- }
219
- break;
220
- case "primitive":
221
- break;
222
- case "sequence":
223
- type.def.value = getNewTypeId(type.def.value);
224
- break;
225
- case "tuple":
226
- type.def.value = type.def.value.map(type => {
227
- if (typeof type !== "number") return type;
228
- return getNewTypeId(type);
229
- });
230
- break;
231
- case "variant":
232
- for (const member of type.def.value) {
233
- for (const field of member.fields) {
234
- if (typeof field.type !== "number") continue;
235
- field.type = getNewTypeId(field.type);
236
- }
237
- }
238
- break;
239
- default:
240
- {
241
- // force compilation error if any types don't have a case
242
- const exhaustiveCheck = type.def;
243
- log.error(`Unhandled V15Type type ${exhaustiveCheck}`);
244
- }
245
- }
246
- }
247
- };
248
- const remapStorageTypeIds = (metadata, getNewTypeId) => {
249
- for (const pallet of metadata.pallets) {
250
- for (const item of pallet.storage?.items ?? []) {
251
- if (item.type.tag === "plain") item.type.value = getNewTypeId(item.type.value);
252
- if (item.type.tag === "map") {
253
- item.type.value.key = getNewTypeId(item.type.value.key);
254
- item.type.value.value = getNewTypeId(item.type.value.value);
255
- }
256
- }
257
- for (const constant of pallet.constants ?? []) {
258
- constant.type = getNewTypeId(constant.type);
259
- }
260
- }
261
- };
262
- const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
263
- for (const runtimeApi of metadata.apis) {
264
- for (const method of runtimeApi.methods ?? []) {
265
- for (const input of method.inputs) {
266
- input.type = getNewTypeId(input.type);
267
- }
268
- method.output = getNewTypeId(method.output);
269
- }
270
- }
271
- };
272
-
273
- const decodeScale = (scaleCoder, change, error) => {
274
- if (change === null) return null;
275
- try {
276
- return scaleCoder?.value?.dec(change) ?? null;
277
- } catch (cause) {
278
- log.warn(error ?? `Failed to decode ${change}`, cause);
279
- return null;
280
- }
281
- };
282
-
283
- const encodeMetadata = metadata$1 => toHex(metadata.enc(metadata$1));
284
-
285
- const encodeStateKey = (scaleCoder, error, ...args) => {
286
- try {
287
- return scaleCoder?.keys?.enc(...args);
288
- } catch (cause) {
289
- log.warn(error ?? `Failed to encode stateKey ${JSON.stringify(args)}`, cause);
290
- return;
291
- }
292
- };
293
-
294
- /**
295
- * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
296
- *
297
- * Only reads the first 40 bytes of the blob.
298
- */
299
- const getMetadataVersion = metadataRpc => {
300
- try {
301
- return Struct({
302
- magicNumber: u32,
303
- version: u8
304
- }).dec(metadataRpc).version;
305
- } catch {
306
- return 0;
307
- }
308
- };
309
-
310
- /**
311
- * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
312
- * These queries are made to the tokens pallet.
313
- * E.g. api.query.Tokens.Account(accountAddress, papiParse(onChainId))
314
- *
315
- * The `onChainId` field on chaindata must be a JSON-parseable string, but for some SCALE types (especially the Binary type) we must
316
- * use specific `polkadot-api` classes to handle SCALE-encoding the statekey.
317
- *
318
- * Some examples:
319
- * Input: `5`
320
- * Output: `5`
321
- *
322
- * Input: `{ type: "DexShare", value: [ { type: "Token", value: { type: "ACA" } }, { type: "Token", value: { type: "AUSD" } } ] }`
323
- * Output: `Enum("DexShare", [Enum("Token", Enum("ACA")), Enum("Token", Enum("AUSD"))])`
324
- *
325
- * Input: `{ type: "LiquidCrowdloan", value: 13 }`
326
- * Output: `Enum("LiquidCrowdloan", 13)`
327
- *
328
- * Input: `{ type: "NativeToken", value: "bigint:2" }`
329
- * Output: `Enum("NativeToken", 2n)`
330
- *
331
- * Input: `{ type: "Erc20", value: "hex:0x07df96d1341a7d16ba1ad431e2c847d978bc2bce" }`
332
- * Output: `Enum("Erc20", Binary.fromHex("0x07df96d1341a7d16ba1ad431e2c847d978bc2bce"))`
333
- *
334
- * Input: `{ type: "Stellar", value: { code: "bin:TZS", issuer: "hex:0x34c94b2a4ba9e8b57b22547dcbb30f443c4cb02da3829a89aa1bd4780e4466ba" } }`
335
- * Output: `Enum("Stellar", { code: Binary.fromText("TZS"), issuer: Binary.fromHex("0x34c94b2a4ba9e8b57b22547dcbb30f443c4cb02da3829a89aa1bd4780e4466ba") })`
336
- */
337
- const papiParse = text => {
338
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
339
- const reviver = (_key, value) => {
340
- if (typeof value !== "string") return value;
341
- if (value.startsWith("bigint:")) return BigInt(value.slice("bigint:".length));
342
- if (value.startsWith("hex:")) return Binary.fromHex(value.slice("hex:".length));
343
- if (value.startsWith("bin:")) return Binary.fromText(value.slice("bin:".length));
344
- return value;
345
- };
346
- if (typeof text !== "string") return text;
347
- return JSON.parse(text, reviver);
348
- };
349
- const papiStringify = (value, space) => {
350
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
351
- const replacer = (_key, value) => {
352
- if (typeof value === "bigint") return `bigint:${String(value)}`;
353
- if (value instanceof Binary) return `hex:${value.asHex()}`;
354
- return value;
355
- };
356
- return JSON.stringify(value, replacer, space);
357
- };
358
-
359
- const parseMetadataRpc = metadataRpc => {
360
- const metadata = decAnyMetadata(metadataRpc);
361
- const unifiedMetadata = unifyMetadata(metadata);
362
- const lookupFn = getLookupFn(unifiedMetadata);
363
- const builder = getDynamicBuilder(lookupFn);
364
- return {
365
- metadata,
366
- unifiedMetadata,
367
- lookupFn,
368
- builder
369
- };
370
- };
371
-
372
- const getStorageKeyPrefix = (palletName, storageName) => {
373
- const palletHash = Twox128(new TextEncoder().encode(palletName));
374
- const storageHash = Twox128(new TextEncoder().encode(storageName));
375
-
376
- // Concatenate and convert to hex
377
- const combined = new Uint8Array(palletHash.length + storageHash.length);
378
- combined.set(palletHash, 0);
379
- combined.set(storageHash, palletHash.length);
380
- return toHex(combined);
381
- };
382
-
383
- const getConstantValueFromMetadata = (metadata, pallet, constant) => {
384
- const {
385
- builder,
386
- unifiedMetadata
387
- } = typeof metadata === "string" ? parseMetadataRpc(metadata) : metadata;
388
- return getConstantValueInner(builder, unifiedMetadata, pallet, constant);
389
- };
390
- const getConstantValueInner = (builder, unifiedMetadata, pallet, constant) => {
391
- try {
392
- const storageCodec = builder.buildConstant(pallet, constant);
393
- const encodedValue = unifiedMetadata.pallets.find(({
394
- name
395
- }) => name === pallet)?.constants.find(({
396
- name
397
- }) => name === constant)?.value;
398
- if (!encodedValue) throw new Error(`Constant ${pallet}.${constant} not found`);
399
- return storageCodec.dec(encodedValue);
400
- } catch (err) {
401
- log.error("Failed to get constant value from metadata", {
402
- err,
403
- pallet,
404
- constant
405
- });
406
- throw err;
407
- }
408
- };
409
-
410
- export { compactMetadata, decodeScale, encodeMetadata, encodeStateKey, getConstantValueFromMetadata, getMetadataVersion, getStorageKeyPrefix, magicNumber, papiParse, papiStringify, parseMetadataRpc };