@polkadot-api/metadata-builders 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,48 +1,34 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
1
+ 'use strict';
2
+
3
+ var scale = require('@polkadot-api/substrate-bindings');
4
+ var utils = require('@polkadot-api/utils');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
17
18
  }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
29
22
 
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- getChecksumBuilder: () => getChecksumBuilder,
34
- getDynamicBuilder: () => getDynamicBuilder,
35
- getLookupFn: () => getLookupFn
36
- });
37
- module.exports = __toCommonJS(src_exports);
23
+ var scale__namespace = /*#__PURE__*/_interopNamespaceDefault(scale);
38
24
 
39
- // src/lookups.ts
40
- var isBytes = (value, nBytes) => value.type === "array" && value.len === nBytes && value.value.type === "primitive" && value.value.value === "u8";
41
- var _void = { type: "void" };
42
- var getLookupFn = (lookupData) => {
25
+ const isBytes = (value, nBytes) => value.type === "array" && value.len === nBytes && value.value.type === "primitive" && value.value.value === "u8";
26
+ const _void = { type: "void" };
27
+ const getLookupFn = (metadata) => {
28
+ const lookupData = metadata.lookup;
43
29
  const lookups = /* @__PURE__ */ new Map();
44
30
  const from = /* @__PURE__ */ new Set();
45
- const withCache2 = (fn) => {
31
+ const withCache = (fn) => {
46
32
  return (id) => {
47
33
  let entry = lookups.get(id);
48
34
  if (entry) return entry;
@@ -71,7 +57,7 @@ var getLookupFn = (lookupData) => {
71
57
  };
72
58
  let isAccountId32SearchOn = true;
73
59
  let isAccountId20SearchOn = true;
74
- const getLookupEntryDef = withCache2((id) => {
60
+ const getLookupEntryDef = withCache((id) => {
75
61
  const { def, path, params } = lookupData[id];
76
62
  if (def.tag === "composite") {
77
63
  if (def.value.length === 0) return _void;
@@ -210,14 +196,10 @@ var getLookupFn = (lookupData) => {
210
196
  innerDocs
211
197
  };
212
198
  };
213
- return getLookupEntryDef;
199
+ return Object.assign(getLookupEntryDef, { metadata });
214
200
  };
215
201
 
216
- // src/dynamic-builder.ts
217
- var scale = __toESM(require("@polkadot-api/substrate-bindings"));
218
-
219
- // src/with-cache.ts
220
- var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, ...rest) => {
202
+ const withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, ...rest) => {
221
203
  const { id } = input;
222
204
  if (cache.has(id)) return cache.get(id);
223
205
  if (stack.has(id)) {
@@ -234,53 +216,51 @@ var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, .
234
216
  return result;
235
217
  };
236
218
 
237
- // src/dynamic-builder.ts
238
- var import_utils = require("@polkadot-api/utils");
239
- var _bytes = scale.Bin();
240
- var bigCompact = scale.createCodec(
241
- scale.compact[0],
242
- scale.enhanceDecoder(scale.compact[1], BigInt)
219
+ const _bytes = scale__namespace.Bin();
220
+ const bigCompact = scale__namespace.createCodec(
221
+ scale__namespace.compact[0],
222
+ scale__namespace.enhanceDecoder(scale__namespace.compact[1], BigInt)
243
223
  );
244
- var _buildCodec = (input, cache, stack, _accountId) => {
245
- if (input.type === "primitive") return scale[input.value];
246
- if (input.type === "void") return scale._void;
224
+ const _buildCodec = (input, cache, stack, _accountId) => {
225
+ if (input.type === "primitive") return scale__namespace[input.value];
226
+ if (input.type === "void") return scale__namespace._void;
247
227
  if (input.type === "AccountId32") return _accountId;
248
- if (input.type === "AccountId20") return scale.ethAccount;
249
- if (input.type === "compact") return input.isBig ? bigCompact : scale.compact;
250
- if (input.type === "bitSequence") return scale.bitSequence;
228
+ if (input.type === "AccountId20") return scale__namespace.ethAccount;
229
+ if (input.type === "compact") return input.isBig ? bigCompact : scale__namespace.compact;
230
+ if (input.type === "bitSequence") return scale__namespace.bitSequence;
251
231
  const buildNextCodec = (nextInput) => buildCodec(nextInput, cache, stack, _accountId);
252
232
  const buildVector = (inner2, len) => {
253
233
  const innerCodec = buildNextCodec(inner2);
254
- return len ? scale.Vector(innerCodec, len) : scale.Vector(innerCodec);
234
+ return len ? scale__namespace.Vector(innerCodec, len) : scale__namespace.Vector(innerCodec);
255
235
  };
256
- const buildTuple = (value) => scale.Tuple(...value.map(buildNextCodec));
236
+ const buildTuple = (value) => scale__namespace.Tuple(...value.map(buildNextCodec));
257
237
  const buildStruct = (value) => {
258
238
  const inner2 = Object.fromEntries(
259
239
  Object.entries(value).map(([key, value2]) => [key, buildNextCodec(value2)])
260
240
  );
261
- return scale.Struct(inner2);
241
+ return scale__namespace.Struct(inner2);
262
242
  };
263
243
  if (input.type === "sequence" && input.value.type === "primitive" && input.value.value === "u8") {
264
244
  return _bytes;
265
245
  }
266
246
  if (input.type === "array") {
267
247
  if (input.value.type === "primitive" && input.value.value === "u8")
268
- return scale.Bin(input.len);
248
+ return scale__namespace.Bin(input.len);
269
249
  return buildVector(input.value, input.len);
270
250
  }
271
251
  if (input.type === "sequence") return buildVector(input.value);
272
252
  if (input.type === "tuple") return buildTuple(input.value);
273
253
  if (input.type === "struct") return buildStruct(input.value);
274
- if (input.type === "option") return scale.Option(buildNextCodec(input.value));
254
+ if (input.type === "option") return scale__namespace.Option(buildNextCodec(input.value));
275
255
  if (input.type === "result")
276
- return scale.Result(
256
+ return scale__namespace.Result(
277
257
  buildNextCodec(input.value.ok),
278
258
  buildNextCodec(input.value.ko)
279
259
  );
280
260
  const dependencies = Object.values(input.value).map((v) => {
281
261
  switch (v.type) {
282
262
  case "void":
283
- return scale._void;
263
+ return scale__namespace._void;
284
264
  case "lookupEntry":
285
265
  return buildNextCodec(v.value);
286
266
  case "tuple":
@@ -298,13 +278,12 @@ var _buildCodec = (input, cache, stack, _accountId) => {
298
278
  );
299
279
  const indexes = Object.values(input.value).map((x) => x.idx);
300
280
  const areIndexesSorted = indexes.every((idx, i) => idx === i);
301
- return areIndexesSorted ? scale.Variant(inner) : scale.Variant(inner, indexes);
281
+ return areIndexesSorted ? scale__namespace.Variant(inner) : scale__namespace.Variant(inner, indexes);
302
282
  };
303
- var buildCodec = withCache(_buildCodec, scale.Self, (res) => res);
304
- var getDynamicBuilder = (metadata) => {
305
- const lookupData = metadata.lookup;
306
- const getLookupEntryDef = getLookupFn(lookupData);
307
- let _accountId = scale.AccountId();
283
+ const buildCodec = withCache(_buildCodec, scale__namespace.Self, (res) => res);
284
+ const getDynamicBuilder = (getLookupEntryDef) => {
285
+ const { metadata } = getLookupEntryDef;
286
+ let _accountId = scale__namespace.AccountId();
308
287
  const cache = /* @__PURE__ */ new Map();
309
288
  const buildDefinition = (id) => buildCodec(getLookupEntryDef(id), cache, /* @__PURE__ */ new Set(), _accountId);
310
289
  const prefix = metadata.pallets.find((x) => x.name === "System")?.constants.find((x) => x.name === "SS58Prefix");
@@ -314,7 +293,7 @@ var getDynamicBuilder = (metadata) => {
314
293
  const prefixVal = buildDefinition(prefix.type).dec(prefix.value);
315
294
  if (typeof prefixVal === "number") {
316
295
  ss58Prefix = prefixVal;
317
- _accountId = scale.AccountId(prefixVal);
296
+ _accountId = scale__namespace.AccountId(prefixVal);
318
297
  }
319
298
  } catch (_) {
320
299
  }
@@ -323,7 +302,7 @@ var getDynamicBuilder = (metadata) => {
323
302
  const buildStorage = (pallet, entry) => {
324
303
  let storagePallet = storagePallets.get(pallet);
325
304
  if (!storagePallet)
326
- storagePallets.set(pallet, storagePallet = scale.Storage(pallet));
305
+ storagePallets.set(pallet, storagePallet = scale__namespace.Storage(pallet));
327
306
  const storageEntry = metadata.pallets.find((x) => x.name === pallet).storage.items.find((s) => s.name === entry);
328
307
  const storageWithFallback = (len, ...args) => {
329
308
  const result = storagePallet(...args);
@@ -341,7 +320,7 @@ var getDynamicBuilder = (metadata) => {
341
320
  );
342
321
  const { key, value, hashers } = storageEntry.type.value;
343
322
  const val = buildDefinition(value);
344
- const hashes = hashers.map((x) => scale[x.tag]);
323
+ const hashes = hashers.map((x) => scale__namespace[x.tag]);
345
324
  const hashArgs = (() => {
346
325
  if (hashes.length === 1) {
347
326
  return [[buildDefinition(key), hashes[0]]];
@@ -364,19 +343,19 @@ var getDynamicBuilder = (metadata) => {
364
343
  const buildEnumEntry = (entry) => {
365
344
  switch (entry.type) {
366
345
  case "void":
367
- return scale._void;
346
+ return scale__namespace._void;
368
347
  case "lookupEntry":
369
348
  return buildDefinition(entry.value.id);
370
349
  case "tuple":
371
- return scale.Tuple(
350
+ return scale__namespace.Tuple(
372
351
  ...Object.values(entry.value).map((l) => buildDefinition(l.id))
373
352
  );
374
353
  case "struct":
375
- return scale.Struct(
376
- (0, import_utils.mapObject)(entry.value, (x) => buildDefinition(x.id))
354
+ return scale__namespace.Struct(
355
+ utils.mapObject(entry.value, (x) => buildDefinition(x.id))
377
356
  );
378
357
  case "array":
379
- return scale.Vector(buildDefinition(entry.value.id), entry.len);
358
+ return scale__namespace.Vector(buildDefinition(entry.value.id), entry.len);
380
359
  }
381
360
  };
382
361
  const buildConstant = (pallet, constantName) => {
@@ -397,7 +376,7 @@ var getDynamicBuilder = (metadata) => {
397
376
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
398
377
  if (!entry) throw null;
399
378
  return {
400
- args: scale.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
379
+ args: scale__namespace.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
401
380
  value: buildDefinition(entry.output)
402
381
  };
403
382
  };
@@ -413,10 +392,6 @@ var getDynamicBuilder = (metadata) => {
413
392
  };
414
393
  };
415
394
 
416
- // src/checksum-builder.ts
417
- var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
418
-
419
- // src/lookup-graph.ts
420
395
  function buildLookupGraph(lookupFn, lookupLength) {
421
396
  const result = /* @__PURE__ */ new Map();
422
397
  const visited = /* @__PURE__ */ new Set();
@@ -484,7 +459,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
484
459
  }
485
460
  return result;
486
461
  }
487
- var subgraphCache = /* @__PURE__ */ new WeakMap();
462
+ const subgraphCache = /* @__PURE__ */ new WeakMap();
488
463
  function _getSubgraph(id, graph, result, cache) {
489
464
  if (result.has(id)) return;
490
465
  const node = graph.get(id);
@@ -576,17 +551,16 @@ function mergeSCCsWithCommonNodes(stronglyConnectedComponents) {
576
551
  return groups;
577
552
  }
578
553
 
579
- // src/checksum-builder.ts
580
- var textEncoder = new TextEncoder();
581
- var encodeText = textEncoder.encode.bind(textEncoder);
582
- var getChecksum = (values) => {
554
+ const textEncoder = new TextEncoder();
555
+ const encodeText = textEncoder.encode.bind(textEncoder);
556
+ const getChecksum = (values) => {
583
557
  const res = new Uint8Array(values.length * 8);
584
558
  const dv = new DataView(res.buffer);
585
559
  for (let i = 0; i < values.length; i++) dv.setBigUint64(i * 8, values[i]);
586
- return (0, import_substrate_bindings.h64)(res);
560
+ return scale.h64(res);
587
561
  };
588
- var getStringChecksum = (values) => getChecksum(values.map((v) => (0, import_substrate_bindings.h64)(encodeText(v))));
589
- var shapeIds = {
562
+ const getStringChecksum = (values) => getChecksum(values.map((v) => scale.h64(encodeText(v))));
563
+ const shapeIds = {
590
564
  primitive: 0n,
591
565
  vector: 1n,
592
566
  tuple: 2n,
@@ -596,7 +570,7 @@ var shapeIds = {
596
570
  enum: 6n,
597
571
  void: 7n
598
572
  };
599
- var runtimePrimitiveIds = {
573
+ const runtimePrimitiveIds = {
600
574
  undefined: 0n,
601
575
  number: 1n,
602
576
  string: 2n,
@@ -611,7 +585,7 @@ var runtimePrimitiveIds = {
611
585
  accountId20: 8n
612
586
  // EthAccount
613
587
  };
614
- var metadataPrimitiveIds = {
588
+ const metadataPrimitiveIds = {
615
589
  bool: runtimePrimitiveIds.boolean,
616
590
  char: runtimePrimitiveIds.string,
617
591
  str: runtimePrimitiveIds.string,
@@ -628,7 +602,7 @@ var metadataPrimitiveIds = {
628
602
  i128: runtimePrimitiveIds.bigint,
629
603
  i256: runtimePrimitiveIds.bigint
630
604
  };
631
- var structLikeBuilder = (shapeId, input, innerChecksum) => {
605
+ const structLikeBuilder = (shapeId, input, innerChecksum) => {
632
606
  const sortedEntries = Object.entries(input).sort(
633
607
  ([a], [b]) => a.localeCompare(b)
634
608
  );
@@ -638,7 +612,7 @@ var structLikeBuilder = (shapeId, input, innerChecksum) => {
638
612
  );
639
613
  return getChecksum([shapeId, keysChecksum, valuesChecksum]);
640
614
  };
641
- var _buildChecksum = (input, buildNextChecksum) => {
615
+ const _buildChecksum = (input, buildNextChecksum) => {
642
616
  if (input.type === "primitive")
643
617
  return getChecksum([shapeIds.primitive, metadataPrimitiveIds[input.value]]);
644
618
  if (input.type === "void") return getChecksum([shapeIds.void]);
@@ -705,7 +679,7 @@ var _buildChecksum = (input, buildNextChecksum) => {
705
679
  }
706
680
  });
707
681
  };
708
- var sortCyclicGroups = (groups, graph) => {
682
+ const sortCyclicGroups = (groups, graph) => {
709
683
  const getReachableNodes = (group) => {
710
684
  const result2 = /* @__PURE__ */ new Set();
711
685
  const toVisit = Array.from(group);
@@ -785,7 +759,7 @@ function getMirroredNodes(cyclicGroups, graph) {
785
759
  });
786
760
  return duplicatesMap;
787
761
  }
788
- var buildChecksum = (entry, cache, graph) => {
762
+ const buildChecksum = (entry, cache, graph) => {
789
763
  if (cache.has(entry.id)) return cache.get(entry.id);
790
764
  const subGraph = getSubgraph(entry.id, graph);
791
765
  const cycles = getStronglyConnectedComponents(subGraph);
@@ -817,10 +791,9 @@ var buildChecksum = (entry, cache, graph) => {
817
791
  };
818
792
  return getChecksum2(entry);
819
793
  };
820
- var getChecksumBuilder = (metadata) => {
821
- const lookupData = metadata.lookup;
822
- const getLookupEntryDef = getLookupFn(lookupData);
823
- const graph = buildLookupGraph(getLookupEntryDef, lookupData.length);
794
+ const getChecksumBuilder = (getLookupEntryDef) => {
795
+ const { metadata } = getLookupEntryDef;
796
+ const graph = buildLookupGraph(getLookupEntryDef, metadata.lookup.length);
824
797
  const cache = /* @__PURE__ */ new Map();
825
798
  const buildDefinition = (id) => buildChecksum(getLookupEntryDef(id), cache, graph);
826
799
  const buildStorage = (pallet, entry) => {
@@ -916,4 +889,8 @@ var getChecksumBuilder = (metadata) => {
916
889
  getAllGeneratedChecksums: () => Array.from(cache.values()).map((v) => v.toString(32))
917
890
  };
918
891
  };
919
- //# sourceMappingURL=index.js.map
892
+
893
+ exports.getChecksumBuilder = getChecksumBuilder;
894
+ exports.getDynamicBuilder = getDynamicBuilder;
895
+ exports.getLookupFn = getLookupFn;
896
+ //# sourceMappingURL=index.js.map