@polkadot-api/metadata-builders 0.3.0 → 0.3.2

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.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  // src/lookups.ts
2
2
  var isBytes = (value, nBytes) => value.type === "array" && value.len === nBytes && value.value.type === "primitive" && value.value.value === "u8";
3
+ var _void = { type: "void" };
3
4
  var getLookupFn = (lookupData) => {
4
5
  const lookups = /* @__PURE__ */ new Map();
5
6
  const from = /* @__PURE__ */ new Set();
6
7
  const withCache2 = (fn) => {
7
8
  return (id) => {
8
9
  let entry = lookups.get(id);
9
- if (entry)
10
- return entry;
10
+ if (entry) return entry;
11
11
  if (from.has(id)) {
12
12
  const entry2 = {
13
13
  id
@@ -36,8 +36,7 @@ var getLookupFn = (lookupData) => {
36
36
  const getLookupEntryDef = withCache2((id) => {
37
37
  const { def, path, params } = lookupData[id];
38
38
  if (def.tag === "composite") {
39
- if (def.value.length === 0)
40
- return { type: "void" };
39
+ if (def.value.length === 0) return _void;
41
40
  if (def.value.length === 1) {
42
41
  const inner = getLookupEntryDef(def.value[0].type);
43
42
  if (isAccountId32SearchOn && path.at(-1) === "AccountId32" && isBytes(inner, 32)) {
@@ -50,24 +49,7 @@ var getLookupFn = (lookupData) => {
50
49
  }
51
50
  return inner;
52
51
  }
53
- let allKey = true;
54
- const values = {};
55
- const innerDocs = {};
56
- def.value.forEach((x, idx) => {
57
- allKey = allKey && !!x.name;
58
- const key = x.name || idx;
59
- values[key] = getLookupEntryDef(x.type);
60
- innerDocs[key] = x.docs;
61
- });
62
- return allKey ? {
63
- type: "struct",
64
- value: values,
65
- innerDocs
66
- } : {
67
- type: "tuple",
68
- value: Object.values(values),
69
- innerDocs: Object.values(innerDocs)
70
- };
52
+ return getComplexVar(def.value);
71
53
  }
72
54
  if (def.tag === "variant") {
73
55
  if (path.length === 1 && path[0] === "Option" && params.length === 1 && params[0].name === "T") {
@@ -90,15 +72,14 @@ var getLookupFn = (lookupData) => {
90
72
  }
91
73
  };
92
74
  }
93
- if (def.value.length === 0)
94
- return { type: "void" };
75
+ if (def.value.length === 0) return _void;
95
76
  const enumValue = {};
96
77
  const enumDocs = {};
97
78
  def.value.forEach((x) => {
98
79
  const key = x.name;
99
80
  enumDocs[key] = x.docs;
100
81
  if (x.fields.length === 0) {
101
- enumValue[key] = { type: "void", idx: x.index };
82
+ enumValue[key] = { ..._void, idx: x.index };
102
83
  return;
103
84
  }
104
85
  if (x.fields.length === 1 && !x.fields[0].name) {
@@ -109,38 +90,7 @@ var getLookupFn = (lookupData) => {
109
90
  };
110
91
  return;
111
92
  }
112
- let allKey = true;
113
- const values = {};
114
- const innerDocs = {};
115
- x.fields.forEach((x2, idx) => {
116
- allKey = allKey && !!x2.name;
117
- const key2 = x2.name || idx;
118
- values[key2] = getLookupEntryDef(x2.type);
119
- innerDocs[key2] = x2.docs;
120
- });
121
- if (allKey) {
122
- enumValue[key] = {
123
- type: "struct",
124
- value: values,
125
- innerDocs,
126
- idx: x.index
127
- };
128
- } else {
129
- const valuesArr = Object.values(values);
130
- const innerDocsArr = Object.values(innerDocs);
131
- const areAllSame = valuesArr.every((v) => v.id === valuesArr[0].id);
132
- enumValue[key] = areAllSame && innerDocsArr.every((doc) => doc.length === 0) ? {
133
- type: "array",
134
- value: valuesArr[0],
135
- len: valuesArr.length,
136
- idx: x.index
137
- } : {
138
- type: "tuple",
139
- value: valuesArr,
140
- innerDocs: innerDocsArr,
141
- idx: x.index
142
- };
143
- }
93
+ enumValue[key] = { ...getComplexVar(x.fields), idx: x.index };
144
94
  });
145
95
  return {
146
96
  type: "enum",
@@ -148,46 +98,26 @@ var getLookupFn = (lookupData) => {
148
98
  innerDocs: enumDocs
149
99
  };
150
100
  }
151
- if (def.tag === "sequence") {
152
- const value = getLookupEntryDef(def.value);
101
+ if (def.tag === "sequence")
153
102
  return {
154
103
  type: "sequence",
155
- value
104
+ value: getLookupEntryDef(def.value)
156
105
  };
157
- }
158
106
  if (def.tag === "array") {
159
107
  const { len } = def.value;
160
108
  const value = getLookupEntryDef(def.value.type);
161
- if (len === 0)
162
- return { type: "void" };
163
- if (len === 1)
164
- return value;
165
- return {
109
+ return !len || value.type === "void" ? _void : len > 0 ? {
166
110
  type: "array",
167
111
  value,
168
112
  len: def.value.len
169
- };
113
+ } : value;
170
114
  }
171
115
  if (def.tag === "tuple") {
172
- if (def.value.length === 0)
173
- return { type: "void" };
174
- if (def.value.length === 1)
175
- return getLookupEntryDef(def.value[0]);
176
- const value = def.value.map((x) => getLookupEntryDef(x));
177
- const innerDocs = def.value.map((x) => lookupData[x].docs);
178
- const areAllSame = value.every((v) => v.id === value[0].id);
179
- if (areAllSame && innerDocs.every((doc) => doc.length === 0)) {
180
- return {
181
- type: "array",
182
- value: value[0],
183
- len: value.length
184
- };
185
- }
186
- return {
187
- type: "tuple",
188
- value,
189
- innerDocs
190
- };
116
+ if (def.value.length === 0) return _void;
117
+ return def.value.length > 1 ? getArrayOrTuple(
118
+ def.value.map((x) => getLookupEntryDef(x)),
119
+ def.value.map((x) => lookupData[x].docs)
120
+ ) : getLookupEntryDef(def.value[0]);
191
121
  }
192
122
  if (def.tag === "primitive") {
193
123
  return {
@@ -197,8 +127,7 @@ var getLookupFn = (lookupData) => {
197
127
  }
198
128
  if (def.tag === "compact") {
199
129
  const translated = getLookupEntryDef(def.value);
200
- if (translated.type === "void")
201
- return { type: "compact", isBig: null };
130
+ if (translated.type === "void") return { type: "compact", isBig: null };
202
131
  const isBig = Number(translated.value.slice(1)) > 32;
203
132
  return {
204
133
  type: "compact",
@@ -209,6 +138,40 @@ var getLookupFn = (lookupData) => {
209
138
  type: def.tag
210
139
  };
211
140
  });
141
+ const getComplexVar = (input) => {
142
+ let allKey = true;
143
+ const values = {};
144
+ const innerDocs = {};
145
+ input.forEach((x, idx) => {
146
+ allKey = allKey && !!x.name;
147
+ const key = x.name || idx;
148
+ const value = getLookupEntryDef(x.type);
149
+ if (value.type !== "void") {
150
+ values[key] = value;
151
+ innerDocs[key] = x.docs;
152
+ }
153
+ });
154
+ return allKey ? {
155
+ type: "struct",
156
+ value: values,
157
+ innerDocs
158
+ } : getArrayOrTuple(Object.values(values), Object.values(innerDocs));
159
+ };
160
+ const getArrayOrTuple = (values, innerDocs) => {
161
+ if (values.every((v) => v.id === values[0].id) && innerDocs.every((doc) => !doc.length)) {
162
+ const [value] = values;
163
+ return value.type === "void" ? _void : {
164
+ type: "array",
165
+ value: values[0],
166
+ len: values.length
167
+ };
168
+ }
169
+ return {
170
+ type: "tuple",
171
+ value: values,
172
+ innerDocs
173
+ };
174
+ };
212
175
  return getLookupEntryDef;
213
176
  };
214
177
 
@@ -218,8 +181,7 @@ import * as scale from "@polkadot-api/substrate-bindings";
218
181
  // src/with-cache.ts
219
182
  var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, ...rest) => {
220
183
  const { id } = input;
221
- if (cache.has(id))
222
- return cache.get(id);
184
+ if (cache.has(id)) return cache.get(id);
223
185
  if (stack.has(id)) {
224
186
  const res = onEnterCircular(() => cache.get(id), input, ...rest);
225
187
  cache.set(id, res);
@@ -237,19 +199,17 @@ var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, .
237
199
  // src/dynamic-builder.ts
238
200
  import { mapObject } from "@polkadot-api/utils";
239
201
  var _bytes = scale.Bin();
202
+ var bigCompact = scale.createCodec(
203
+ scale.compact[0],
204
+ scale.enhanceDecoder(scale.compact[1], BigInt)
205
+ );
240
206
  var _buildCodec = (input, cache, stack, _accountId) => {
241
- if (input.type === "primitive")
242
- return scale[input.value];
243
- if (input.type === "void")
244
- return scale._void;
245
- if (input.type === "AccountId32")
246
- return _accountId;
247
- if (input.type === "AccountId20")
248
- return scale.ethAccount;
249
- if (input.type === "compact")
250
- return scale.compact;
251
- if (input.type === "bitSequence")
252
- return scale.bitSequence;
207
+ if (input.type === "primitive") return scale[input.value];
208
+ if (input.type === "void") return scale._void;
209
+ if (input.type === "AccountId32") return _accountId;
210
+ if (input.type === "AccountId20") return scale.ethAccount;
211
+ if (input.type === "compact") return input.isBig ? bigCompact : scale.compact;
212
+ if (input.type === "bitSequence") return scale.bitSequence;
253
213
  const buildNextCodec = (nextInput) => buildCodec(nextInput, cache, stack, _accountId);
254
214
  const buildVector = (inner2, len) => {
255
215
  const innerCodec = buildNextCodec(inner2);
@@ -270,14 +230,10 @@ var _buildCodec = (input, cache, stack, _accountId) => {
270
230
  return scale.Bin(input.len);
271
231
  return buildVector(input.value, input.len);
272
232
  }
273
- if (input.type === "sequence")
274
- return buildVector(input.value);
275
- if (input.type === "tuple")
276
- return buildTuple(input.value);
277
- if (input.type === "struct")
278
- return buildStruct(input.value);
279
- if (input.type === "option")
280
- return scale.Option(buildNextCodec(input.value));
233
+ if (input.type === "sequence") return buildVector(input.value);
234
+ if (input.type === "tuple") return buildTuple(input.value);
235
+ if (input.type === "struct") return buildStruct(input.value);
236
+ if (input.type === "option") return scale.Option(buildNextCodec(input.value));
281
237
  if (input.type === "result")
282
238
  return scale.Result(
283
239
  buildNextCodec(input.value.ok),
@@ -348,12 +304,23 @@ var getDynamicBuilder = (metadata) => {
348
304
  const { key, value, hashers } = storageEntry.type.value;
349
305
  const val = buildDefinition(value);
350
306
  const hashes = hashers.map((x) => scale[x.tag]);
351
- const hashArgs = hashes.length === 1 ? [[buildDefinition(key), hashes[0]]] : getLookupEntryDef(key).value.map(
352
- (x, idx) => [
353
- buildDefinition(x.id),
354
- hashes[idx]
355
- ]
356
- );
307
+ const hashArgs = (() => {
308
+ if (hashes.length === 1) {
309
+ return [[buildDefinition(key), hashes[0]]];
310
+ }
311
+ const keyDef = getLookupEntryDef(key);
312
+ switch (keyDef.type) {
313
+ case "array":
314
+ return hashes.map((hash) => [buildDefinition(keyDef.value.id), hash]);
315
+ case "tuple":
316
+ return keyDef.value.map((x, idx) => [
317
+ buildDefinition(x.id),
318
+ hashes[idx]
319
+ ]);
320
+ default:
321
+ throw new Error("Invalid key type");
322
+ }
323
+ })();
357
324
  return storageWithFallback(hashes.length, entry, val.dec, ...hashArgs);
358
325
  };
359
326
  const buildEnumEntry = (entry) => {
@@ -381,8 +348,7 @@ var getDynamicBuilder = (metadata) => {
381
348
  const buildVariant = (type) => (pallet, name) => {
382
349
  const palletEntry = metadata.pallets.find((x) => x.name === pallet);
383
350
  const lookup = getLookupEntryDef(palletEntry[type]);
384
- if (lookup.type !== "enum")
385
- throw null;
351
+ if (lookup.type !== "enum") throw null;
386
352
  const entry = lookup.value[name];
387
353
  return {
388
354
  location: [palletEntry.index, entry.idx],
@@ -391,8 +357,7 @@ var getDynamicBuilder = (metadata) => {
391
357
  };
392
358
  const buildRuntimeCall = (api, method) => {
393
359
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
394
- if (!entry)
395
- throw null;
360
+ if (!entry) throw null;
396
361
  return {
397
362
  args: scale.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
398
363
  value: buildDefinition(entry.output)
@@ -438,8 +403,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
438
403
  if (i !== entry.id) {
439
404
  addEdge(i, entry.id);
440
405
  }
441
- if (visited.has(entry.id))
442
- continue;
406
+ if (visited.has(entry.id)) continue;
443
407
  visited.add(entry.id);
444
408
  switch (entry.type) {
445
409
  case "array":
@@ -484,8 +448,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
484
448
  }
485
449
  var subgraphCache = /* @__PURE__ */ new WeakMap();
486
450
  function _getSubgraph(id, graph, result, cache) {
487
- if (result.has(id))
488
- return;
451
+ if (result.has(id)) return;
489
452
  const node = graph.get(id);
490
453
  result.set(id, node);
491
454
  cache.set(id, result);
@@ -497,8 +460,7 @@ function getSubgraph(id, graph) {
497
460
  subgraphCache.set(graph, /* @__PURE__ */ new Map());
498
461
  }
499
462
  const cache = subgraphCache.get(graph);
500
- if (cache.has(id))
501
- return cache.get(id);
463
+ if (cache.has(id)) return cache.get(id);
502
464
  const result = /* @__PURE__ */ new Map();
503
465
  _getSubgraph(id, graph, result, cache);
504
466
  return result;
@@ -535,8 +497,7 @@ function getStronglyConnectedComponents(graph) {
535
497
  tarjanState.get(poppedNode).onStack = false;
536
498
  component.add(poppedNode);
537
499
  } while (poppedNode !== v);
538
- if (component.size > 1)
539
- result.push(component);
500
+ if (component.size > 1) result.push(component);
540
501
  }
541
502
  }
542
503
  for (const node of graph.keys()) {
@@ -566,8 +527,7 @@ function mergeSCCsWithCommonNodes(stronglyConnectedComponents) {
566
527
  const toVisit = [ungroupedCycles.values().next().value];
567
528
  while (toVisit.length) {
568
529
  const idx = toVisit.pop();
569
- if (!ungroupedCycles.has(idx))
570
- continue;
530
+ if (!ungroupedCycles.has(idx)) continue;
571
531
  ungroupedCycles.delete(idx);
572
532
  const cycle = scc[idx];
573
533
  cycle.forEach((v) => group.add(Number(v)));
@@ -584,8 +544,7 @@ var encodeText = textEncoder.encode.bind(textEncoder);
584
544
  var getChecksum = (values) => {
585
545
  const res = new Uint8Array(values.length * 8);
586
546
  const dv = new DataView(res.buffer);
587
- for (let i = 0; i < values.length; i++)
588
- dv.setBigUint64(i * 8, values[i]);
547
+ for (let i = 0; i < values.length; i++) dv.setBigUint64(i * 8, values[i]);
589
548
  return h64(res);
590
549
  };
591
550
  var getStringChecksum = (values) => getChecksum(values.map((v) => h64(encodeText(v))));
@@ -644,8 +603,7 @@ var structLikeBuilder = (shapeId, input, innerChecksum) => {
644
603
  var _buildChecksum = (input, buildNextChecksum) => {
645
604
  if (input.type === "primitive")
646
605
  return getChecksum([shapeIds.primitive, metadataPrimitiveIds[input.value]]);
647
- if (input.type === "void")
648
- return getChecksum([shapeIds.void]);
606
+ if (input.type === "void") return getChecksum([shapeIds.void]);
649
607
  if (input.type === "compact")
650
608
  return getChecksum([
651
609
  shapeIds.primitive,
@@ -685,10 +643,8 @@ var _buildChecksum = (input, buildNextChecksum) => {
685
643
  }
686
644
  const buildTuple = (entries) => getChecksum([shapeIds.tuple, ...entries.map(buildNextChecksum)]);
687
645
  const buildStruct = (entries) => structLikeBuilder(shapeIds.struct, entries, buildNextChecksum);
688
- if (input.type === "tuple")
689
- return buildTuple(input.value);
690
- if (input.type === "struct")
691
- return buildStruct(input.value);
646
+ if (input.type === "tuple") return buildTuple(input.value);
647
+ if (input.type === "struct") return buildStruct(input.value);
692
648
  if (input.type === "option")
693
649
  return getChecksum([shapeIds.option, buildNextChecksum(input.value)]);
694
650
  if (input.type === "result")
@@ -698,8 +654,7 @@ var _buildChecksum = (input, buildNextChecksum) => {
698
654
  buildNextChecksum(input.value.ko)
699
655
  ]);
700
656
  return structLikeBuilder(shapeIds.enum, input.value, (entry) => {
701
- if (entry.type === "lookupEntry")
702
- return buildNextChecksum(entry.value);
657
+ if (entry.type === "lookupEntry") return buildNextChecksum(entry.value);
703
658
  switch (entry.type) {
704
659
  case "void":
705
660
  return getChecksum([shapeIds.void]);
@@ -718,8 +673,7 @@ var sortCyclicGroups = (groups, graph) => {
718
673
  const toVisit = Array.from(group);
719
674
  while (toVisit.length) {
720
675
  const id = toVisit.pop();
721
- if (result2.has(id))
722
- continue;
676
+ if (result2.has(id)) continue;
723
677
  result2.add(id);
724
678
  graph.get(id)?.refs.forEach((id2) => toVisit.push(id2));
725
679
  }
@@ -727,14 +681,12 @@ var sortCyclicGroups = (groups, graph) => {
727
681
  };
728
682
  const result = new Array();
729
683
  function dependentsFirst(group) {
730
- if (result.includes(group))
731
- return;
684
+ if (result.includes(group)) return;
732
685
  const dependents = groups.filter(
733
686
  (candidate) => candidate !== group && getReachableNodes(group).some((node) => candidate.has(node))
734
687
  );
735
688
  dependents.forEach((group2) => dependentsFirst(group2));
736
- if (result.includes(group))
737
- return;
689
+ if (result.includes(group)) return;
738
690
  result.push(group);
739
691
  }
740
692
  groups.forEach((group) => dependentsFirst(group));
@@ -780,8 +732,7 @@ function getMirroredNodes(cyclicGroups, graph) {
780
732
  const checksumToNodes = /* @__PURE__ */ new Map();
781
733
  for (const id of allEntries) {
782
734
  const checksum = resultingChecksums.get(id);
783
- if (checksum == void 0)
784
- throw new Error("Unreachable");
735
+ if (checksum == void 0) throw new Error("Unreachable");
785
736
  if (!checksumToNodes.has(checksum)) {
786
737
  checksumToNodes.set(checksum, []);
787
738
  }
@@ -797,8 +748,7 @@ function getMirroredNodes(cyclicGroups, graph) {
797
748
  return duplicatesMap;
798
749
  }
799
750
  var buildChecksum = (entry, cache, graph) => {
800
- if (cache.has(entry.id))
801
- return cache.get(entry.id);
751
+ if (cache.has(entry.id)) return cache.get(entry.id);
802
752
  const subGraph = getSubgraph(entry.id, graph);
803
753
  const cycles = getStronglyConnectedComponents(subGraph);
804
754
  const cyclicGroups = mergeSCCsWithCommonNodes(cycles).filter((group) => {
@@ -824,8 +774,7 @@ var buildChecksum = (entry, cache, graph) => {
824
774
  });
825
775
  });
826
776
  const getChecksum2 = (entry2) => {
827
- if (cache.has(entry2.id))
828
- return cache.get(entry2.id);
777
+ if (cache.has(entry2.id)) return cache.get(entry2.id);
829
778
  return _buildChecksum(entry2, getChecksum2);
830
779
  };
831
780
  return getChecksum2(entry);
@@ -852,8 +801,7 @@ var getChecksumBuilder = (metadata) => {
852
801
  const buildRuntimeCall = (api, method) => {
853
802
  try {
854
803
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
855
- if (!entry)
856
- throw null;
804
+ if (!entry) throw null;
857
805
  const argNamesChecksum = getStringChecksum(
858
806
  entry.inputs.map((x) => x.name)
859
807
  );
@@ -867,8 +815,7 @@ var getChecksumBuilder = (metadata) => {
867
815
  }
868
816
  };
869
817
  const buildComposite = (input) => {
870
- if (input.type === "void")
871
- return getChecksum([0n]);
818
+ if (input.type === "void") return getChecksum([0n]);
872
819
  if (input.type === "tuple") {
873
820
  const values = Object.values(input.value).map(
874
821
  (entry) => buildDefinition(entry.id)
@@ -902,8 +849,7 @@ var getChecksumBuilder = (metadata) => {
902
849
  palletEntry[variantType]
903
850
  );
904
851
  buildDefinition(enumLookup.id);
905
- if (enumLookup.type !== "enum")
906
- throw null;
852
+ if (enumLookup.type !== "enum") throw null;
907
853
  const entry = enumLookup.value[name];
908
854
  return entry.type === "lookupEntry" ? buildDefinition(entry.value.id) : buildComposite(entry);
909
855
  } catch (_) {