@polkadot-api/metadata-builders 0.3.0 → 0.3.1

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
@@ -44,8 +44,7 @@ var getLookupFn = (lookupData) => {
44
44
  const withCache2 = (fn) => {
45
45
  return (id) => {
46
46
  let entry = lookups.get(id);
47
- if (entry)
48
- return entry;
47
+ if (entry) return entry;
49
48
  if (from.has(id)) {
50
49
  const entry2 = {
51
50
  id
@@ -74,8 +73,7 @@ var getLookupFn = (lookupData) => {
74
73
  const getLookupEntryDef = withCache2((id) => {
75
74
  const { def, path, params } = lookupData[id];
76
75
  if (def.tag === "composite") {
77
- if (def.value.length === 0)
78
- return { type: "void" };
76
+ if (def.value.length === 0) return { type: "void" };
79
77
  if (def.value.length === 1) {
80
78
  const inner = getLookupEntryDef(def.value[0].type);
81
79
  if (isAccountId32SearchOn && path.at(-1) === "AccountId32" && isBytes(inner, 32)) {
@@ -128,8 +126,7 @@ var getLookupFn = (lookupData) => {
128
126
  }
129
127
  };
130
128
  }
131
- if (def.value.length === 0)
132
- return { type: "void" };
129
+ if (def.value.length === 0) return { type: "void" };
133
130
  const enumValue = {};
134
131
  const enumDocs = {};
135
132
  def.value.forEach((x) => {
@@ -196,10 +193,8 @@ var getLookupFn = (lookupData) => {
196
193
  if (def.tag === "array") {
197
194
  const { len } = def.value;
198
195
  const value = getLookupEntryDef(def.value.type);
199
- if (len === 0)
200
- return { type: "void" };
201
- if (len === 1)
202
- return value;
196
+ if (len === 0) return { type: "void" };
197
+ if (len === 1) return value;
203
198
  return {
204
199
  type: "array",
205
200
  value,
@@ -207,8 +202,7 @@ var getLookupFn = (lookupData) => {
207
202
  };
208
203
  }
209
204
  if (def.tag === "tuple") {
210
- if (def.value.length === 0)
211
- return { type: "void" };
205
+ if (def.value.length === 0) return { type: "void" };
212
206
  if (def.value.length === 1)
213
207
  return getLookupEntryDef(def.value[0]);
214
208
  const value = def.value.map((x) => getLookupEntryDef(x));
@@ -235,8 +229,7 @@ var getLookupFn = (lookupData) => {
235
229
  }
236
230
  if (def.tag === "compact") {
237
231
  const translated = getLookupEntryDef(def.value);
238
- if (translated.type === "void")
239
- return { type: "compact", isBig: null };
232
+ if (translated.type === "void") return { type: "compact", isBig: null };
240
233
  const isBig = Number(translated.value.slice(1)) > 32;
241
234
  return {
242
235
  type: "compact",
@@ -256,8 +249,7 @@ var scale = __toESM(require("@polkadot-api/substrate-bindings"));
256
249
  // src/with-cache.ts
257
250
  var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, ...rest) => {
258
251
  const { id } = input;
259
- if (cache.has(id))
260
- return cache.get(id);
252
+ if (cache.has(id)) return cache.get(id);
261
253
  if (stack.has(id)) {
262
254
  const res = onEnterCircular(() => cache.get(id), input, ...rest);
263
255
  cache.set(id, res);
@@ -276,18 +268,12 @@ var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, .
276
268
  var import_utils = require("@polkadot-api/utils");
277
269
  var _bytes = scale.Bin();
278
270
  var _buildCodec = (input, cache, stack, _accountId) => {
279
- if (input.type === "primitive")
280
- return scale[input.value];
281
- if (input.type === "void")
282
- return scale._void;
283
- if (input.type === "AccountId32")
284
- return _accountId;
285
- if (input.type === "AccountId20")
286
- return scale.ethAccount;
287
- if (input.type === "compact")
288
- return scale.compact;
289
- if (input.type === "bitSequence")
290
- return scale.bitSequence;
271
+ if (input.type === "primitive") return scale[input.value];
272
+ if (input.type === "void") return scale._void;
273
+ if (input.type === "AccountId32") return _accountId;
274
+ if (input.type === "AccountId20") return scale.ethAccount;
275
+ if (input.type === "compact") return scale.compact;
276
+ if (input.type === "bitSequence") return scale.bitSequence;
291
277
  const buildNextCodec = (nextInput) => buildCodec(nextInput, cache, stack, _accountId);
292
278
  const buildVector = (inner2, len) => {
293
279
  const innerCodec = buildNextCodec(inner2);
@@ -308,14 +294,10 @@ var _buildCodec = (input, cache, stack, _accountId) => {
308
294
  return scale.Bin(input.len);
309
295
  return buildVector(input.value, input.len);
310
296
  }
311
- if (input.type === "sequence")
312
- return buildVector(input.value);
313
- if (input.type === "tuple")
314
- return buildTuple(input.value);
315
- if (input.type === "struct")
316
- return buildStruct(input.value);
317
- if (input.type === "option")
318
- return scale.Option(buildNextCodec(input.value));
297
+ if (input.type === "sequence") return buildVector(input.value);
298
+ if (input.type === "tuple") return buildTuple(input.value);
299
+ if (input.type === "struct") return buildStruct(input.value);
300
+ if (input.type === "option") return scale.Option(buildNextCodec(input.value));
319
301
  if (input.type === "result")
320
302
  return scale.Result(
321
303
  buildNextCodec(input.value.ok),
@@ -386,12 +368,23 @@ var getDynamicBuilder = (metadata) => {
386
368
  const { key, value, hashers } = storageEntry.type.value;
387
369
  const val = buildDefinition(value);
388
370
  const hashes = hashers.map((x) => scale[x.tag]);
389
- const hashArgs = hashes.length === 1 ? [[buildDefinition(key), hashes[0]]] : getLookupEntryDef(key).value.map(
390
- (x, idx) => [
391
- buildDefinition(x.id),
392
- hashes[idx]
393
- ]
394
- );
371
+ const hashArgs = (() => {
372
+ if (hashes.length === 1) {
373
+ return [[buildDefinition(key), hashes[0]]];
374
+ }
375
+ const keyDef = getLookupEntryDef(key);
376
+ switch (keyDef.type) {
377
+ case "array":
378
+ return hashes.map((hash) => [buildDefinition(keyDef.value.id), hash]);
379
+ case "tuple":
380
+ return keyDef.value.map((x, idx) => [
381
+ buildDefinition(x.id),
382
+ hashes[idx]
383
+ ]);
384
+ default:
385
+ throw new Error("Invalid key type");
386
+ }
387
+ })();
395
388
  return storageWithFallback(hashes.length, entry, val.dec, ...hashArgs);
396
389
  };
397
390
  const buildEnumEntry = (entry) => {
@@ -419,8 +412,7 @@ var getDynamicBuilder = (metadata) => {
419
412
  const buildVariant = (type) => (pallet, name) => {
420
413
  const palletEntry = metadata.pallets.find((x) => x.name === pallet);
421
414
  const lookup = getLookupEntryDef(palletEntry[type]);
422
- if (lookup.type !== "enum")
423
- throw null;
415
+ if (lookup.type !== "enum") throw null;
424
416
  const entry = lookup.value[name];
425
417
  return {
426
418
  location: [palletEntry.index, entry.idx],
@@ -429,8 +421,7 @@ var getDynamicBuilder = (metadata) => {
429
421
  };
430
422
  const buildRuntimeCall = (api, method) => {
431
423
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
432
- if (!entry)
433
- throw null;
424
+ if (!entry) throw null;
434
425
  return {
435
426
  args: scale.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
436
427
  value: buildDefinition(entry.output)
@@ -476,8 +467,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
476
467
  if (i !== entry.id) {
477
468
  addEdge(i, entry.id);
478
469
  }
479
- if (visited.has(entry.id))
480
- continue;
470
+ if (visited.has(entry.id)) continue;
481
471
  visited.add(entry.id);
482
472
  switch (entry.type) {
483
473
  case "array":
@@ -522,8 +512,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
522
512
  }
523
513
  var subgraphCache = /* @__PURE__ */ new WeakMap();
524
514
  function _getSubgraph(id, graph, result, cache) {
525
- if (result.has(id))
526
- return;
515
+ if (result.has(id)) return;
527
516
  const node = graph.get(id);
528
517
  result.set(id, node);
529
518
  cache.set(id, result);
@@ -535,8 +524,7 @@ function getSubgraph(id, graph) {
535
524
  subgraphCache.set(graph, /* @__PURE__ */ new Map());
536
525
  }
537
526
  const cache = subgraphCache.get(graph);
538
- if (cache.has(id))
539
- return cache.get(id);
527
+ if (cache.has(id)) return cache.get(id);
540
528
  const result = /* @__PURE__ */ new Map();
541
529
  _getSubgraph(id, graph, result, cache);
542
530
  return result;
@@ -573,8 +561,7 @@ function getStronglyConnectedComponents(graph) {
573
561
  tarjanState.get(poppedNode).onStack = false;
574
562
  component.add(poppedNode);
575
563
  } while (poppedNode !== v);
576
- if (component.size > 1)
577
- result.push(component);
564
+ if (component.size > 1) result.push(component);
578
565
  }
579
566
  }
580
567
  for (const node of graph.keys()) {
@@ -604,8 +591,7 @@ function mergeSCCsWithCommonNodes(stronglyConnectedComponents) {
604
591
  const toVisit = [ungroupedCycles.values().next().value];
605
592
  while (toVisit.length) {
606
593
  const idx = toVisit.pop();
607
- if (!ungroupedCycles.has(idx))
608
- continue;
594
+ if (!ungroupedCycles.has(idx)) continue;
609
595
  ungroupedCycles.delete(idx);
610
596
  const cycle = scc[idx];
611
597
  cycle.forEach((v) => group.add(Number(v)));
@@ -622,8 +608,7 @@ var encodeText = textEncoder.encode.bind(textEncoder);
622
608
  var getChecksum = (values) => {
623
609
  const res = new Uint8Array(values.length * 8);
624
610
  const dv = new DataView(res.buffer);
625
- for (let i = 0; i < values.length; i++)
626
- dv.setBigUint64(i * 8, values[i]);
611
+ for (let i = 0; i < values.length; i++) dv.setBigUint64(i * 8, values[i]);
627
612
  return (0, import_substrate_bindings.h64)(res);
628
613
  };
629
614
  var getStringChecksum = (values) => getChecksum(values.map((v) => (0, import_substrate_bindings.h64)(encodeText(v))));
@@ -682,8 +667,7 @@ var structLikeBuilder = (shapeId, input, innerChecksum) => {
682
667
  var _buildChecksum = (input, buildNextChecksum) => {
683
668
  if (input.type === "primitive")
684
669
  return getChecksum([shapeIds.primitive, metadataPrimitiveIds[input.value]]);
685
- if (input.type === "void")
686
- return getChecksum([shapeIds.void]);
670
+ if (input.type === "void") return getChecksum([shapeIds.void]);
687
671
  if (input.type === "compact")
688
672
  return getChecksum([
689
673
  shapeIds.primitive,
@@ -723,10 +707,8 @@ var _buildChecksum = (input, buildNextChecksum) => {
723
707
  }
724
708
  const buildTuple = (entries) => getChecksum([shapeIds.tuple, ...entries.map(buildNextChecksum)]);
725
709
  const buildStruct = (entries) => structLikeBuilder(shapeIds.struct, entries, buildNextChecksum);
726
- if (input.type === "tuple")
727
- return buildTuple(input.value);
728
- if (input.type === "struct")
729
- return buildStruct(input.value);
710
+ if (input.type === "tuple") return buildTuple(input.value);
711
+ if (input.type === "struct") return buildStruct(input.value);
730
712
  if (input.type === "option")
731
713
  return getChecksum([shapeIds.option, buildNextChecksum(input.value)]);
732
714
  if (input.type === "result")
@@ -736,8 +718,7 @@ var _buildChecksum = (input, buildNextChecksum) => {
736
718
  buildNextChecksum(input.value.ko)
737
719
  ]);
738
720
  return structLikeBuilder(shapeIds.enum, input.value, (entry) => {
739
- if (entry.type === "lookupEntry")
740
- return buildNextChecksum(entry.value);
721
+ if (entry.type === "lookupEntry") return buildNextChecksum(entry.value);
741
722
  switch (entry.type) {
742
723
  case "void":
743
724
  return getChecksum([shapeIds.void]);
@@ -756,8 +737,7 @@ var sortCyclicGroups = (groups, graph) => {
756
737
  const toVisit = Array.from(group);
757
738
  while (toVisit.length) {
758
739
  const id = toVisit.pop();
759
- if (result2.has(id))
760
- continue;
740
+ if (result2.has(id)) continue;
761
741
  result2.add(id);
762
742
  graph.get(id)?.refs.forEach((id2) => toVisit.push(id2));
763
743
  }
@@ -765,14 +745,12 @@ var sortCyclicGroups = (groups, graph) => {
765
745
  };
766
746
  const result = new Array();
767
747
  function dependentsFirst(group) {
768
- if (result.includes(group))
769
- return;
748
+ if (result.includes(group)) return;
770
749
  const dependents = groups.filter(
771
750
  (candidate) => candidate !== group && getReachableNodes(group).some((node) => candidate.has(node))
772
751
  );
773
752
  dependents.forEach((group2) => dependentsFirst(group2));
774
- if (result.includes(group))
775
- return;
753
+ if (result.includes(group)) return;
776
754
  result.push(group);
777
755
  }
778
756
  groups.forEach((group) => dependentsFirst(group));
@@ -818,8 +796,7 @@ function getMirroredNodes(cyclicGroups, graph) {
818
796
  const checksumToNodes = /* @__PURE__ */ new Map();
819
797
  for (const id of allEntries) {
820
798
  const checksum = resultingChecksums.get(id);
821
- if (checksum == void 0)
822
- throw new Error("Unreachable");
799
+ if (checksum == void 0) throw new Error("Unreachable");
823
800
  if (!checksumToNodes.has(checksum)) {
824
801
  checksumToNodes.set(checksum, []);
825
802
  }
@@ -835,8 +812,7 @@ function getMirroredNodes(cyclicGroups, graph) {
835
812
  return duplicatesMap;
836
813
  }
837
814
  var buildChecksum = (entry, cache, graph) => {
838
- if (cache.has(entry.id))
839
- return cache.get(entry.id);
815
+ if (cache.has(entry.id)) return cache.get(entry.id);
840
816
  const subGraph = getSubgraph(entry.id, graph);
841
817
  const cycles = getStronglyConnectedComponents(subGraph);
842
818
  const cyclicGroups = mergeSCCsWithCommonNodes(cycles).filter((group) => {
@@ -862,8 +838,7 @@ var buildChecksum = (entry, cache, graph) => {
862
838
  });
863
839
  });
864
840
  const getChecksum2 = (entry2) => {
865
- if (cache.has(entry2.id))
866
- return cache.get(entry2.id);
841
+ if (cache.has(entry2.id)) return cache.get(entry2.id);
867
842
  return _buildChecksum(entry2, getChecksum2);
868
843
  };
869
844
  return getChecksum2(entry);
@@ -890,8 +865,7 @@ var getChecksumBuilder = (metadata) => {
890
865
  const buildRuntimeCall = (api, method) => {
891
866
  try {
892
867
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
893
- if (!entry)
894
- throw null;
868
+ if (!entry) throw null;
895
869
  const argNamesChecksum = getStringChecksum(
896
870
  entry.inputs.map((x) => x.name)
897
871
  );
@@ -905,8 +879,7 @@ var getChecksumBuilder = (metadata) => {
905
879
  }
906
880
  };
907
881
  const buildComposite = (input) => {
908
- if (input.type === "void")
909
- return getChecksum([0n]);
882
+ if (input.type === "void") return getChecksum([0n]);
910
883
  if (input.type === "tuple") {
911
884
  const values = Object.values(input.value).map(
912
885
  (entry) => buildDefinition(entry.id)
@@ -940,8 +913,7 @@ var getChecksumBuilder = (metadata) => {
940
913
  palletEntry[variantType]
941
914
  );
942
915
  buildDefinition(enumLookup.id);
943
- if (enumLookup.type !== "enum")
944
- throw null;
916
+ if (enumLookup.type !== "enum") throw null;
945
917
  const entry = enumLookup.value[name];
946
918
  return entry.type === "lookupEntry" ? buildDefinition(entry.value.id) : buildComposite(entry);
947
919
  } catch (_) {