@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.js CHANGED
@@ -38,14 +38,14 @@ module.exports = __toCommonJS(src_exports);
38
38
 
39
39
  // src/lookups.ts
40
40
  var isBytes = (value, nBytes) => value.type === "array" && value.len === nBytes && value.value.type === "primitive" && value.value.value === "u8";
41
+ var _void = { type: "void" };
41
42
  var getLookupFn = (lookupData) => {
42
43
  const lookups = /* @__PURE__ */ new Map();
43
44
  const from = /* @__PURE__ */ new Set();
44
45
  const withCache2 = (fn) => {
45
46
  return (id) => {
46
47
  let entry = lookups.get(id);
47
- if (entry)
48
- return entry;
48
+ if (entry) return entry;
49
49
  if (from.has(id)) {
50
50
  const entry2 = {
51
51
  id
@@ -74,8 +74,7 @@ var getLookupFn = (lookupData) => {
74
74
  const getLookupEntryDef = withCache2((id) => {
75
75
  const { def, path, params } = lookupData[id];
76
76
  if (def.tag === "composite") {
77
- if (def.value.length === 0)
78
- return { type: "void" };
77
+ if (def.value.length === 0) return _void;
79
78
  if (def.value.length === 1) {
80
79
  const inner = getLookupEntryDef(def.value[0].type);
81
80
  if (isAccountId32SearchOn && path.at(-1) === "AccountId32" && isBytes(inner, 32)) {
@@ -88,24 +87,7 @@ var getLookupFn = (lookupData) => {
88
87
  }
89
88
  return inner;
90
89
  }
91
- let allKey = true;
92
- const values = {};
93
- const innerDocs = {};
94
- def.value.forEach((x, idx) => {
95
- allKey = allKey && !!x.name;
96
- const key = x.name || idx;
97
- values[key] = getLookupEntryDef(x.type);
98
- innerDocs[key] = x.docs;
99
- });
100
- return allKey ? {
101
- type: "struct",
102
- value: values,
103
- innerDocs
104
- } : {
105
- type: "tuple",
106
- value: Object.values(values),
107
- innerDocs: Object.values(innerDocs)
108
- };
90
+ return getComplexVar(def.value);
109
91
  }
110
92
  if (def.tag === "variant") {
111
93
  if (path.length === 1 && path[0] === "Option" && params.length === 1 && params[0].name === "T") {
@@ -128,15 +110,14 @@ var getLookupFn = (lookupData) => {
128
110
  }
129
111
  };
130
112
  }
131
- if (def.value.length === 0)
132
- return { type: "void" };
113
+ if (def.value.length === 0) return _void;
133
114
  const enumValue = {};
134
115
  const enumDocs = {};
135
116
  def.value.forEach((x) => {
136
117
  const key = x.name;
137
118
  enumDocs[key] = x.docs;
138
119
  if (x.fields.length === 0) {
139
- enumValue[key] = { type: "void", idx: x.index };
120
+ enumValue[key] = { ..._void, idx: x.index };
140
121
  return;
141
122
  }
142
123
  if (x.fields.length === 1 && !x.fields[0].name) {
@@ -147,38 +128,7 @@ var getLookupFn = (lookupData) => {
147
128
  };
148
129
  return;
149
130
  }
150
- let allKey = true;
151
- const values = {};
152
- const innerDocs = {};
153
- x.fields.forEach((x2, idx) => {
154
- allKey = allKey && !!x2.name;
155
- const key2 = x2.name || idx;
156
- values[key2] = getLookupEntryDef(x2.type);
157
- innerDocs[key2] = x2.docs;
158
- });
159
- if (allKey) {
160
- enumValue[key] = {
161
- type: "struct",
162
- value: values,
163
- innerDocs,
164
- idx: x.index
165
- };
166
- } else {
167
- const valuesArr = Object.values(values);
168
- const innerDocsArr = Object.values(innerDocs);
169
- const areAllSame = valuesArr.every((v) => v.id === valuesArr[0].id);
170
- enumValue[key] = areAllSame && innerDocsArr.every((doc) => doc.length === 0) ? {
171
- type: "array",
172
- value: valuesArr[0],
173
- len: valuesArr.length,
174
- idx: x.index
175
- } : {
176
- type: "tuple",
177
- value: valuesArr,
178
- innerDocs: innerDocsArr,
179
- idx: x.index
180
- };
181
- }
131
+ enumValue[key] = { ...getComplexVar(x.fields), idx: x.index };
182
132
  });
183
133
  return {
184
134
  type: "enum",
@@ -186,46 +136,26 @@ var getLookupFn = (lookupData) => {
186
136
  innerDocs: enumDocs
187
137
  };
188
138
  }
189
- if (def.tag === "sequence") {
190
- const value = getLookupEntryDef(def.value);
139
+ if (def.tag === "sequence")
191
140
  return {
192
141
  type: "sequence",
193
- value
142
+ value: getLookupEntryDef(def.value)
194
143
  };
195
- }
196
144
  if (def.tag === "array") {
197
145
  const { len } = def.value;
198
146
  const value = getLookupEntryDef(def.value.type);
199
- if (len === 0)
200
- return { type: "void" };
201
- if (len === 1)
202
- return value;
203
- return {
147
+ return !len || value.type === "void" ? _void : len > 0 ? {
204
148
  type: "array",
205
149
  value,
206
150
  len: def.value.len
207
- };
151
+ } : value;
208
152
  }
209
153
  if (def.tag === "tuple") {
210
- if (def.value.length === 0)
211
- return { type: "void" };
212
- if (def.value.length === 1)
213
- return getLookupEntryDef(def.value[0]);
214
- const value = def.value.map((x) => getLookupEntryDef(x));
215
- const innerDocs = def.value.map((x) => lookupData[x].docs);
216
- const areAllSame = value.every((v) => v.id === value[0].id);
217
- if (areAllSame && innerDocs.every((doc) => doc.length === 0)) {
218
- return {
219
- type: "array",
220
- value: value[0],
221
- len: value.length
222
- };
223
- }
224
- return {
225
- type: "tuple",
226
- value,
227
- innerDocs
228
- };
154
+ if (def.value.length === 0) return _void;
155
+ return def.value.length > 1 ? getArrayOrTuple(
156
+ def.value.map((x) => getLookupEntryDef(x)),
157
+ def.value.map((x) => lookupData[x].docs)
158
+ ) : getLookupEntryDef(def.value[0]);
229
159
  }
230
160
  if (def.tag === "primitive") {
231
161
  return {
@@ -235,8 +165,7 @@ var getLookupFn = (lookupData) => {
235
165
  }
236
166
  if (def.tag === "compact") {
237
167
  const translated = getLookupEntryDef(def.value);
238
- if (translated.type === "void")
239
- return { type: "compact", isBig: null };
168
+ if (translated.type === "void") return { type: "compact", isBig: null };
240
169
  const isBig = Number(translated.value.slice(1)) > 32;
241
170
  return {
242
171
  type: "compact",
@@ -247,6 +176,40 @@ var getLookupFn = (lookupData) => {
247
176
  type: def.tag
248
177
  };
249
178
  });
179
+ const getComplexVar = (input) => {
180
+ let allKey = true;
181
+ const values = {};
182
+ const innerDocs = {};
183
+ input.forEach((x, idx) => {
184
+ allKey = allKey && !!x.name;
185
+ const key = x.name || idx;
186
+ const value = getLookupEntryDef(x.type);
187
+ if (value.type !== "void") {
188
+ values[key] = value;
189
+ innerDocs[key] = x.docs;
190
+ }
191
+ });
192
+ return allKey ? {
193
+ type: "struct",
194
+ value: values,
195
+ innerDocs
196
+ } : getArrayOrTuple(Object.values(values), Object.values(innerDocs));
197
+ };
198
+ const getArrayOrTuple = (values, innerDocs) => {
199
+ if (values.every((v) => v.id === values[0].id) && innerDocs.every((doc) => !doc.length)) {
200
+ const [value] = values;
201
+ return value.type === "void" ? _void : {
202
+ type: "array",
203
+ value: values[0],
204
+ len: values.length
205
+ };
206
+ }
207
+ return {
208
+ type: "tuple",
209
+ value: values,
210
+ innerDocs
211
+ };
212
+ };
250
213
  return getLookupEntryDef;
251
214
  };
252
215
 
@@ -256,8 +219,7 @@ var scale = __toESM(require("@polkadot-api/substrate-bindings"));
256
219
  // src/with-cache.ts
257
220
  var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, ...rest) => {
258
221
  const { id } = input;
259
- if (cache.has(id))
260
- return cache.get(id);
222
+ if (cache.has(id)) return cache.get(id);
261
223
  if (stack.has(id)) {
262
224
  const res = onEnterCircular(() => cache.get(id), input, ...rest);
263
225
  cache.set(id, res);
@@ -275,19 +237,17 @@ var withCache = (fn, onEnterCircular, onExitCircular) => (input, cache, stack, .
275
237
  // src/dynamic-builder.ts
276
238
  var import_utils = require("@polkadot-api/utils");
277
239
  var _bytes = scale.Bin();
240
+ var bigCompact = scale.createCodec(
241
+ scale.compact[0],
242
+ scale.enhanceDecoder(scale.compact[1], BigInt)
243
+ );
278
244
  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;
245
+ if (input.type === "primitive") return scale[input.value];
246
+ if (input.type === "void") return scale._void;
247
+ 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;
291
251
  const buildNextCodec = (nextInput) => buildCodec(nextInput, cache, stack, _accountId);
292
252
  const buildVector = (inner2, len) => {
293
253
  const innerCodec = buildNextCodec(inner2);
@@ -308,14 +268,10 @@ var _buildCodec = (input, cache, stack, _accountId) => {
308
268
  return scale.Bin(input.len);
309
269
  return buildVector(input.value, input.len);
310
270
  }
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));
271
+ if (input.type === "sequence") return buildVector(input.value);
272
+ if (input.type === "tuple") return buildTuple(input.value);
273
+ if (input.type === "struct") return buildStruct(input.value);
274
+ if (input.type === "option") return scale.Option(buildNextCodec(input.value));
319
275
  if (input.type === "result")
320
276
  return scale.Result(
321
277
  buildNextCodec(input.value.ok),
@@ -386,12 +342,23 @@ var getDynamicBuilder = (metadata) => {
386
342
  const { key, value, hashers } = storageEntry.type.value;
387
343
  const val = buildDefinition(value);
388
344
  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
- );
345
+ const hashArgs = (() => {
346
+ if (hashes.length === 1) {
347
+ return [[buildDefinition(key), hashes[0]]];
348
+ }
349
+ const keyDef = getLookupEntryDef(key);
350
+ switch (keyDef.type) {
351
+ case "array":
352
+ return hashes.map((hash) => [buildDefinition(keyDef.value.id), hash]);
353
+ case "tuple":
354
+ return keyDef.value.map((x, idx) => [
355
+ buildDefinition(x.id),
356
+ hashes[idx]
357
+ ]);
358
+ default:
359
+ throw new Error("Invalid key type");
360
+ }
361
+ })();
395
362
  return storageWithFallback(hashes.length, entry, val.dec, ...hashArgs);
396
363
  };
397
364
  const buildEnumEntry = (entry) => {
@@ -419,8 +386,7 @@ var getDynamicBuilder = (metadata) => {
419
386
  const buildVariant = (type) => (pallet, name) => {
420
387
  const palletEntry = metadata.pallets.find((x) => x.name === pallet);
421
388
  const lookup = getLookupEntryDef(palletEntry[type]);
422
- if (lookup.type !== "enum")
423
- throw null;
389
+ if (lookup.type !== "enum") throw null;
424
390
  const entry = lookup.value[name];
425
391
  return {
426
392
  location: [palletEntry.index, entry.idx],
@@ -429,8 +395,7 @@ var getDynamicBuilder = (metadata) => {
429
395
  };
430
396
  const buildRuntimeCall = (api, method) => {
431
397
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
432
- if (!entry)
433
- throw null;
398
+ if (!entry) throw null;
434
399
  return {
435
400
  args: scale.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
436
401
  value: buildDefinition(entry.output)
@@ -476,8 +441,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
476
441
  if (i !== entry.id) {
477
442
  addEdge(i, entry.id);
478
443
  }
479
- if (visited.has(entry.id))
480
- continue;
444
+ if (visited.has(entry.id)) continue;
481
445
  visited.add(entry.id);
482
446
  switch (entry.type) {
483
447
  case "array":
@@ -522,8 +486,7 @@ function buildLookupGraph(lookupFn, lookupLength) {
522
486
  }
523
487
  var subgraphCache = /* @__PURE__ */ new WeakMap();
524
488
  function _getSubgraph(id, graph, result, cache) {
525
- if (result.has(id))
526
- return;
489
+ if (result.has(id)) return;
527
490
  const node = graph.get(id);
528
491
  result.set(id, node);
529
492
  cache.set(id, result);
@@ -535,8 +498,7 @@ function getSubgraph(id, graph) {
535
498
  subgraphCache.set(graph, /* @__PURE__ */ new Map());
536
499
  }
537
500
  const cache = subgraphCache.get(graph);
538
- if (cache.has(id))
539
- return cache.get(id);
501
+ if (cache.has(id)) return cache.get(id);
540
502
  const result = /* @__PURE__ */ new Map();
541
503
  _getSubgraph(id, graph, result, cache);
542
504
  return result;
@@ -573,8 +535,7 @@ function getStronglyConnectedComponents(graph) {
573
535
  tarjanState.get(poppedNode).onStack = false;
574
536
  component.add(poppedNode);
575
537
  } while (poppedNode !== v);
576
- if (component.size > 1)
577
- result.push(component);
538
+ if (component.size > 1) result.push(component);
578
539
  }
579
540
  }
580
541
  for (const node of graph.keys()) {
@@ -604,8 +565,7 @@ function mergeSCCsWithCommonNodes(stronglyConnectedComponents) {
604
565
  const toVisit = [ungroupedCycles.values().next().value];
605
566
  while (toVisit.length) {
606
567
  const idx = toVisit.pop();
607
- if (!ungroupedCycles.has(idx))
608
- continue;
568
+ if (!ungroupedCycles.has(idx)) continue;
609
569
  ungroupedCycles.delete(idx);
610
570
  const cycle = scc[idx];
611
571
  cycle.forEach((v) => group.add(Number(v)));
@@ -622,8 +582,7 @@ var encodeText = textEncoder.encode.bind(textEncoder);
622
582
  var getChecksum = (values) => {
623
583
  const res = new Uint8Array(values.length * 8);
624
584
  const dv = new DataView(res.buffer);
625
- for (let i = 0; i < values.length; i++)
626
- dv.setBigUint64(i * 8, values[i]);
585
+ for (let i = 0; i < values.length; i++) dv.setBigUint64(i * 8, values[i]);
627
586
  return (0, import_substrate_bindings.h64)(res);
628
587
  };
629
588
  var getStringChecksum = (values) => getChecksum(values.map((v) => (0, import_substrate_bindings.h64)(encodeText(v))));
@@ -682,8 +641,7 @@ var structLikeBuilder = (shapeId, input, innerChecksum) => {
682
641
  var _buildChecksum = (input, buildNextChecksum) => {
683
642
  if (input.type === "primitive")
684
643
  return getChecksum([shapeIds.primitive, metadataPrimitiveIds[input.value]]);
685
- if (input.type === "void")
686
- return getChecksum([shapeIds.void]);
644
+ if (input.type === "void") return getChecksum([shapeIds.void]);
687
645
  if (input.type === "compact")
688
646
  return getChecksum([
689
647
  shapeIds.primitive,
@@ -723,10 +681,8 @@ var _buildChecksum = (input, buildNextChecksum) => {
723
681
  }
724
682
  const buildTuple = (entries) => getChecksum([shapeIds.tuple, ...entries.map(buildNextChecksum)]);
725
683
  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);
684
+ if (input.type === "tuple") return buildTuple(input.value);
685
+ if (input.type === "struct") return buildStruct(input.value);
730
686
  if (input.type === "option")
731
687
  return getChecksum([shapeIds.option, buildNextChecksum(input.value)]);
732
688
  if (input.type === "result")
@@ -736,8 +692,7 @@ var _buildChecksum = (input, buildNextChecksum) => {
736
692
  buildNextChecksum(input.value.ko)
737
693
  ]);
738
694
  return structLikeBuilder(shapeIds.enum, input.value, (entry) => {
739
- if (entry.type === "lookupEntry")
740
- return buildNextChecksum(entry.value);
695
+ if (entry.type === "lookupEntry") return buildNextChecksum(entry.value);
741
696
  switch (entry.type) {
742
697
  case "void":
743
698
  return getChecksum([shapeIds.void]);
@@ -756,8 +711,7 @@ var sortCyclicGroups = (groups, graph) => {
756
711
  const toVisit = Array.from(group);
757
712
  while (toVisit.length) {
758
713
  const id = toVisit.pop();
759
- if (result2.has(id))
760
- continue;
714
+ if (result2.has(id)) continue;
761
715
  result2.add(id);
762
716
  graph.get(id)?.refs.forEach((id2) => toVisit.push(id2));
763
717
  }
@@ -765,14 +719,12 @@ var sortCyclicGroups = (groups, graph) => {
765
719
  };
766
720
  const result = new Array();
767
721
  function dependentsFirst(group) {
768
- if (result.includes(group))
769
- return;
722
+ if (result.includes(group)) return;
770
723
  const dependents = groups.filter(
771
724
  (candidate) => candidate !== group && getReachableNodes(group).some((node) => candidate.has(node))
772
725
  );
773
726
  dependents.forEach((group2) => dependentsFirst(group2));
774
- if (result.includes(group))
775
- return;
727
+ if (result.includes(group)) return;
776
728
  result.push(group);
777
729
  }
778
730
  groups.forEach((group) => dependentsFirst(group));
@@ -818,8 +770,7 @@ function getMirroredNodes(cyclicGroups, graph) {
818
770
  const checksumToNodes = /* @__PURE__ */ new Map();
819
771
  for (const id of allEntries) {
820
772
  const checksum = resultingChecksums.get(id);
821
- if (checksum == void 0)
822
- throw new Error("Unreachable");
773
+ if (checksum == void 0) throw new Error("Unreachable");
823
774
  if (!checksumToNodes.has(checksum)) {
824
775
  checksumToNodes.set(checksum, []);
825
776
  }
@@ -835,8 +786,7 @@ function getMirroredNodes(cyclicGroups, graph) {
835
786
  return duplicatesMap;
836
787
  }
837
788
  var buildChecksum = (entry, cache, graph) => {
838
- if (cache.has(entry.id))
839
- return cache.get(entry.id);
789
+ if (cache.has(entry.id)) return cache.get(entry.id);
840
790
  const subGraph = getSubgraph(entry.id, graph);
841
791
  const cycles = getStronglyConnectedComponents(subGraph);
842
792
  const cyclicGroups = mergeSCCsWithCommonNodes(cycles).filter((group) => {
@@ -862,8 +812,7 @@ var buildChecksum = (entry, cache, graph) => {
862
812
  });
863
813
  });
864
814
  const getChecksum2 = (entry2) => {
865
- if (cache.has(entry2.id))
866
- return cache.get(entry2.id);
815
+ if (cache.has(entry2.id)) return cache.get(entry2.id);
867
816
  return _buildChecksum(entry2, getChecksum2);
868
817
  };
869
818
  return getChecksum2(entry);
@@ -890,8 +839,7 @@ var getChecksumBuilder = (metadata) => {
890
839
  const buildRuntimeCall = (api, method) => {
891
840
  try {
892
841
  const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
893
- if (!entry)
894
- throw null;
842
+ if (!entry) throw null;
895
843
  const argNamesChecksum = getStringChecksum(
896
844
  entry.inputs.map((x) => x.name)
897
845
  );
@@ -905,8 +853,7 @@ var getChecksumBuilder = (metadata) => {
905
853
  }
906
854
  };
907
855
  const buildComposite = (input) => {
908
- if (input.type === "void")
909
- return getChecksum([0n]);
856
+ if (input.type === "void") return getChecksum([0n]);
910
857
  if (input.type === "tuple") {
911
858
  const values = Object.values(input.value).map(
912
859
  (entry) => buildDefinition(entry.id)
@@ -940,8 +887,7 @@ var getChecksumBuilder = (metadata) => {
940
887
  palletEntry[variantType]
941
888
  );
942
889
  buildDefinition(enumLookup.id);
943
- if (enumLookup.type !== "enum")
944
- throw null;
890
+ if (enumLookup.type !== "enum") throw null;
945
891
  const entry = enumLookup.value[name];
946
892
  return entry.type === "lookupEntry" ? buildDefinition(entry.value.id) : buildComposite(entry);
947
893
  } catch (_) {