Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@polkadot-api/metadata-builders

Package Overview
Dependencies
Maintainers
2
Versions
201
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot-api/metadata-builders - npm Package Compare versions

Comparing version 0.0.1-56b4ad0a42cc57d8ef99401f196fcbe4691f8e41.1.0 to 0.0.1-5c907acc11c645d42dfa0b29ff92fff89832d986.1.0

86

dist/index.d.ts
import * as scale from '@polkadot-api/substrate-bindings';
import { StringRecord, V14, Codec, Decoder, HexString } from '@polkadot-api/substrate-bindings';
export { Decoder, HexString, StringRecord, V14 } from '@polkadot-api/substrate-bindings';
import { StringRecord, V15, Codec, Decoder, HexString } from '@polkadot-api/substrate-bindings';
export { Decoder, HexString, StringRecord, V15 } from '@polkadot-api/substrate-bindings';

@@ -39,2 +39,13 @@ type VoidVar = {

};
type OptionVar = {
type: "option";
value: LookupEntry;
};
type ResultVar = {
type: "result";
value: {
ok: LookupEntry;
ko: LookupEntry;
};
};
type SequenceVar = {

@@ -49,3 +60,3 @@ type: "sequence";

};
type ComposedVar = TupleVar | StructVar | SequenceVar | ArrayVar | EnumVar;
type ComposedVar = TupleVar | StructVar | SequenceVar | ArrayVar | OptionVar | ResultVar | EnumVar;
type Var = TerminalVar | ComposedVar;

@@ -170,5 +181,6 @@ type LookupEntry = {

typeImports: Set<string>;
enums: Map<string, string>;
variables: Map<string, Variable>;
}
declare const getStaticBuilder: (metadata: V14) => {
declare const getStaticBuilder: (metadata: V15, namespace: string) => {
buildDefinition: (id: number) => string;

@@ -181,9 +193,14 @@ buildStorage: (pallet: string, entry: string) => {

buildError: (pallet: string, name: string) => string;
buildCall: (pallet: string, callName: string) => string;
buildCall: (pallet: string, name: string) => string;
buildRuntimeCall: (api: string, method: string) => {
args: string;
value: string;
};
buildConstant: (pallet: string, constantName: string) => string;
getTypeFromVarName: (varName: string) => string;
getCode: () => string;
getEnums: () => string[];
};
declare const getDynamicBuilder: (metadata: V14) => {
declare const getDynamicBuilder: (metadata: V15) => {
buildDefinition: (id: number) => Codec<any>;

@@ -211,3 +228,8 @@ buildStorage: (pallet: string, entry: string) => {

};
buildCall: (pallet: string, callName: string) => {
buildRuntimeCall: (api: string, method: string) => {
args: Codec<any[]>;
value: Codec<any>;
};
buildCall: (pallet: string, name: string) => {
args: Codec<any>;
location: [

@@ -217,3 +239,2 @@ number,

];
args: Codec<any>;
};

@@ -224,6 +245,7 @@ buildConstant: (pallet: string, constantName: string) => Codec<any>;

declare const getChecksumBuilder: (metadata: V14) => {
declare const getChecksumBuilder: (metadata: V15) => {
buildDefinition: (id: number) => string | null;
buildRuntimeCall: (api: string, method: string) => string | null;
buildStorage: (pallet: string, entry: string) => string | null;
buildCall: (pallet: string, callName: string) => string | null;
buildCall: (pallet: string, name: string) => string | null;
buildEvent: (pallet: string, name: string) => string | null;

@@ -234,3 +256,11 @@ buildError: (pallet: string, name: string) => string | null;

type GetViewBuilder = (metadata: V14) => {
type UnshapedDecoder = {
shape: Shape;
decoder: Decoder<Decoded>;
};
type VariantBasedBuild = (pallet: string, name: string) => {
view: UnshapedDecoder;
location: [number, number];
};
type GetViewBuilder = (metadata: V15) => {
buildDefinition: (idx: number) => {

@@ -241,2 +271,6 @@ shape: Shape;

callDecoder: Decoder<DecodedCall>;
buildEvent: VariantBasedBuild;
buildError: VariantBasedBuild;
buildCall: VariantBasedBuild;
buildConstant: (pallet: string, name: string) => UnshapedDecoder;
};

@@ -320,2 +354,13 @@ interface DecodedCall {

}>;
type OptionDecoded = WithInputAndPath<{
codec: "Option";
value: Decoded;
}>;
type ResultDecoded = WithInputAndPath<{
codec: "Result";
value: {
ok: Decoded;
ko: Decoded;
};
}>;
type TupleDecoded = WithInputAndPath<{

@@ -334,3 +379,3 @@ codec: "Tuple";

value: {
tag: string;
type: string;
value: Decoded;

@@ -340,3 +385,3 @@ };

}>;
type ComplexDecoded = SequenceDecoded | ArrayDecoded | TupleDecoded | StructDecoded | EnumDecoded;
type ComplexDecoded = SequenceDecoded | ArrayDecoded | TupleDecoded | StructDecoded | OptionDecoded | ResultDecoded | EnumDecoded;
type Decoded = PrimitiveDecoded | ComplexDecoded;

@@ -360,2 +405,13 @@ interface SequenceShape {

}
interface OptionShape {
codec: "Option";
shape: Shape;
}
interface ResultShape {
codec: "Result";
shape: {
ok: Shape;
ko: Shape;
};
}
interface EnumShape {

@@ -365,3 +421,3 @@ codec: "Enum";

}
type ComplexShape = SequenceShape | ArrayShape | TupleShape | StructShape | EnumShape;
type ComplexShape = SequenceShape | ArrayShape | TupleShape | StructShape | OptionShape | ResultShape | EnumShape;
type Shape = {

@@ -373,2 +429,2 @@ codec: PrimitiveDecoded["codec"];

export { type AccountIdDecoded, type ArrayDecoded, type ArrayShape, type ArrayVar, type BigNumberDecoded, type BitSequenceDecoded, type BitSequenceVar, type BoolDecoded, type BytesArrayDecoded, type BytesSequenceDecoded, type CodeDeclarations, type CompactVar, type ComplexDecoded, type ComplexShape, type ComposedVar, type Decoded, type DecodedCall, type EnumDecoded, type EnumShape, type EnumVar, type GetViewBuilder, type LookupEntry, type MetadataPrimitives$1 as MetadataPrimitives, type NumberDecoded, type PrimitiveDecoded, type PrimitiveVar, type SequenceDecoded, type SequenceShape, type SequenceVar, type Shape, type StringDecoded, type StructDecoded, type StructShape, type StructVar, type TerminalVar, type TupleDecoded, type TupleShape, type TupleVar, type Var, type Variable, type VoidDecoded, type VoidVar, getChecksumBuilder, getDynamicBuilder, getLookupFn, getStaticBuilder, getViewBuilder, primitiveTypes };
export { type AccountIdDecoded, type ArrayDecoded, type ArrayShape, type ArrayVar, type BigNumberDecoded, type BitSequenceDecoded, type BitSequenceVar, type BoolDecoded, type BytesArrayDecoded, type BytesSequenceDecoded, type CodeDeclarations, type CompactVar, type ComplexDecoded, type ComplexShape, type ComposedVar, type Decoded, type DecodedCall, type EnumDecoded, type EnumShape, type EnumVar, type GetViewBuilder, type LookupEntry, type MetadataPrimitives$1 as MetadataPrimitives, type NumberDecoded, type OptionDecoded, type OptionShape, type OptionVar, type PrimitiveDecoded, type PrimitiveVar, type ResultDecoded, type ResultShape, type ResultVar, type SequenceDecoded, type SequenceShape, type SequenceVar, type Shape, type StringDecoded, type StructDecoded, type StructShape, type StructVar, type TerminalVar, type TupleDecoded, type TupleShape, type TupleVar, type UnshapedDecoder, type Var, type Variable, type VoidDecoded, type VoidVar, getChecksumBuilder, getDynamicBuilder, getLookupFn, getStaticBuilder, getViewBuilder, primitiveTypes };

354

dist/index.js

@@ -76,3 +76,3 @@ "use strict";

const getLookupEntryDef = withCache2((id) => {
const { def } = lookupData[id];
const { def, path, params } = lookupData[id];
if (def.tag === "composite") {

@@ -103,2 +103,17 @@ if (def.value.length === 0)

if (def.tag === "variant") {
if (path.length === 1 && path[0] === "Option" && params.length === 1 && params[0].name === "T") {
return {
type: "option",
value: getLookupEntryDef(params[0].type)
};
}
if (path.length === 1 && path[0] === "Result" && params.length === 2 && params[0].name === "T" && params[1].name === "E") {
return {
type: "result",
value: {
ok: getLookupEntryDef(params[0].type),
ko: getLookupEntryDef(params[1].type)
}
};
}
if (def.value.length === 0)

@@ -240,3 +255,3 @@ return voidVar;

var isBytes = (input) => input.type === "primitive" && input.value === "u8";
var getTypes = (varName) => primitiveTypes[varName] ?? `I${varName}`;
var getTypes = (varName) => primitiveTypes[varName] ?? varName;
var _buildSyntax = (input, cache, stack, declarations, getVarName) => {

@@ -257,8 +272,9 @@ if (input.type === "primitive") {

if (input.type === "sequence" && input.value.type === "primitive" && input.value.value === "u8") {
declarations.imports.add("Hex");
declarations.imports.add("Bin");
declarations.typeImports.add("HexString");
declarations.typeImports.add("Binary");
const variable = {
id: "_bytesSeq",
value: "Hex()",
types: "HexString",
value: "Bin()",
types: "Binary",
directDependencies: /* @__PURE__ */ new Set()

@@ -328,10 +344,11 @@ };

}
declarations.imports.add("Hex");
declarations.imports.add("Bin");
declarations.variables.set(varId, {
id: varId,
value: `Hex(${input.len})`,
types: "HexString",
value: `Bin(${input.len})`,
types: "Binary",
directDependencies: /* @__PURE__ */ new Set()
});
declarations.typeImports.add("HexString");
declarations.typeImports.add("Binary");
return varId;

@@ -347,3 +364,32 @@ }

return buildStruct(varId, input.value);
declarations.imports.add("Enum");
if (input.type === "option") {
declarations.imports.add("Option");
const inner = buildNextSyntax(input.value);
const id = `_Option_${inner}`;
const variable = {
id,
value: `Option(${inner})`,
types: `${getTypes(inner)} | undefined`,
directDependencies: /* @__PURE__ */ new Set([inner])
};
declarations.variables.set(id, variable);
return id;
}
if (input.type === "result") {
declarations.imports.add("Result");
declarations.typeImports.add("ResultPayload");
const ok = buildNextSyntax(input.value.ok);
const ko = buildNextSyntax(input.value.ko);
const id = `_Result_${ok}_${ko}`;
const variable = {
id,
value: `Result(${ok}, ${ko})`,
types: `ResultPayload<${getTypes(ok)}, ${getTypes(ko)}>`,
directDependencies: /* @__PURE__ */ new Set([ok, ko])
};
declarations.variables.set(id, variable);
return id;
}
declarations.imports.add("Variant");
declarations.typeImports.add("Enum");
const dependencies = Object.entries(input.value).map(([key, value]) => {

@@ -357,19 +403,3 @@ if (value.type === "primitive") {

if (value.value.length === 1) {
let result;
const innerVal = value.value[0];
if (key.startsWith("Raw") && innerVal.type === "array" && isBytes(innerVal.value)) {
const id = `_fixedStr${innerVal.len}`;
result = id;
if (!declarations.variables.has(id)) {
declarations.imports.add("fixedStr");
declarations.variables.set(id, {
id,
value: `fixedStr(${innerVal.len})`,
types: "string",
directDependencies: /* @__PURE__ */ new Set()
});
}
} else {
result = buildNextSyntax(value.value[0]);
}
let result = buildNextSyntax(value.value[0]);
if (!declarations.variables.has(varName)) {

@@ -396,8 +426,12 @@ declarations.variables.set(varName, {

)}}${areIndexesSorted ? "" : `, [${indexes.join(", ")}]`}`;
declarations.enums.set(
varId,
Object.keys(input.value).map(
(key, idx) => `{type: '${key}', value: ${getTypes(dependencies[idx])}}`
).join(" | ")
);
declarations.variables.set(varId, {
id: varId,
value: `Enum(${innerEnum})`,
types: Object.keys(input.value).map(
(key, idx) => `{tag: '${key}', value: ${getTypes(dependencies[idx])}}`
).join(" | "),
value: `Variant(${innerEnum})`,
types: `Enum<E${varId}>`,
directDependencies: new Set(dependencies)

@@ -414,3 +448,3 @@ });

id: getVarName(entry.id, "circular"),
types: `I${nonCircular}`,
types: nonCircular,
value: `Self(() => ${nonCircular})`,

@@ -424,7 +458,8 @@ directDependencies: /* @__PURE__ */ new Set([nonCircular])

);
var getStaticBuilder = (metadata) => {
var getStaticBuilder = (metadata, namespace) => {
const declarations = {
imports: /* @__PURE__ */ new Set(),
typeImports: /* @__PURE__ */ new Set(["Codec"]),
variables: /* @__PURE__ */ new Map()
variables: /* @__PURE__ */ new Map(),
enums: /* @__PURE__ */ new Map()
};

@@ -435,3 +470,3 @@ const lookupData = metadata.lookup;

const { path } = lookupData[idx];
const parts = path.length === 0 ? ["cdc" + idx] : ["c", ...path];
const parts = path.length === 0 ? ["cdc" + idx] : path[0] === "sp_runtime" ? [namespace, path.slice(-1)[0]] : [namespace, ...path];
parts.push(...post);

@@ -493,10 +528,19 @@ return toCamelCase(...parts);

};
const buildRuntimeCall = (api, method) => {
const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
if (!entry)
throw null;
return {
args: buildNamedTuple(entry.inputs, `${api}${method}Args`),
value: buildDefinition(entry.output)
};
};
const buildVariant = (type) => (pallet, name) => {
const eventsLookup = getLookupEntryDef(
const lookupEntry = getLookupEntryDef(
metadata.pallets.find((x) => x.name === pallet)[type]
);
if (eventsLookup.type !== "enum")
if (lookupEntry.type !== "enum")
throw null;
const returnVar = toCamelCase(buildDefinition(eventsLookup.id), name);
if (!declarations.variables.has(returnVar) && eventsLookup.value[name].type === "primitive") {
const returnVar = toCamelCase(buildDefinition(lookupEntry.id), name);
if (!declarations.variables.has(returnVar) && lookupEntry.value[name].type === "primitive") {
declarations.variables.set(returnVar, {

@@ -511,22 +555,2 @@ id: returnVar,

};
const buildCall = (pallet, callName) => {
const callsLookup = getLookupEntryDef(
metadata.pallets.find((x) => x.name === pallet).calls
);
if (callsLookup.type !== "enum")
throw null;
const callEntry = callsLookup.value[callName];
if (callEntry.type === "primitive")
return getEmptyTuple();
if (callEntry.type === "tuple")
return toCamelCase(buildDefinition(callsLookup.id), callName);
const params = Object.entries(callEntry.value).map(([name, val]) => ({
name,
type: val.id
}));
return buildNamedTuple(
params,
getVarName(callsLookup.id, callName, "Tupled")
);
};
const buildConstant = (pallet, constantName) => {

@@ -539,3 +563,3 @@ const storageEntry = metadata.pallets.find((x) => x.name === pallet).constants.find((s) => s.name === constantName);

", "
)}} from "@polkadot-api/substrate-bindings";
)}} from "@polkadot-api/client";
`;

@@ -548,8 +572,13 @@ const varImports = `import {${[...declarations.imports].join(

const code = [...declarations.variables.values()].map((variable) => {
return `type I${variable.id} = ${variable.types};
const ${variable.id}: Codec<I${variable.id}> = ${variable.value};`;
const ePrefix = declarations.enums.has(variable.id) ? `type E${variable.id} = ${declarations.enums.get(
variable.id
)};
export ` : "";
return `${ePrefix}type ${variable.id} = ${variable.types};
const ${variable.id}: Codec<${variable.id}> = ${variable.value};`;
}).join("\n\n");
return `${typeImports}${varImports}${code}`;
};
const getTypeFromVarName = (varName) => primitiveTypes[varName] ?? declarations.variables.get(varName)?.types ?? `I${varName}`;
const getEnums = () => [...declarations.enums.keys()];
const getTypeFromVarName = (varName) => primitiveTypes[varName] ?? declarations.variables.get(varName)?.types ?? varName;
return {

@@ -560,6 +589,8 @@ buildDefinition,

buildError: buildVariant("errors"),
buildCall,
buildCall: buildVariant("calls"),
buildRuntimeCall,
buildConstant,
getTypeFromVarName,
getCode
getCode,
getEnums
};

@@ -570,3 +601,4 @@ };

var scale = __toESM(require("@polkadot-api/substrate-bindings"));
var _bytes = scale.Hex();
var import_utils = require("@polkadot-api/utils");
var _bytes = scale.Bin();
var isBytes2 = (input) => input.type === "primitive" && input.value === "u8";

@@ -597,3 +629,3 @@ var _buildCodec = (input, cache, stack, _accountId) => {

if (isBytes2(input.value)) {
return input.len === 32 && (input.id === 0 || input.id === 1) ? _accountId : scale.Hex(input.len);
return input.len === 32 && (input.id === 0 || input.id === 1) ? _accountId : scale.Bin(input.len);
}

@@ -608,9 +640,14 @@ return buildVector(input.value, input.len);

return buildStruct(input.value);
const dependencies = Object.entries(input.value).map(([k, v]) => {
if (input.type === "option")
return scale.Option(buildNextCodec(input.value));
if (input.type === "result")
return scale.Result(
buildNextCodec(input.value.ok),
buildNextCodec(input.value.ko)
);
const dependencies = Object.values(input.value).map((v) => {
if (v.type === "primitive")
return scale._void;
if (v.type === "tuple" && v.value.length === 1) {
const innerVal = v.value[0];
return k.startsWith("Raw") && innerVal.type === "array" && isBytes2(innerVal.value) ? scale.fixedStr(innerVal.len) : buildNextCodec(innerVal);
}
if (v.type === "tuple" && v.value.length === 1)
return buildNextCodec(v.value[0]);
return v.type === "tuple" ? buildTuple(v.value) : buildStruct(v.value);

@@ -625,6 +662,5 @@ });

const areIndexesSorted = indexes.every((idx, i) => idx === i);
return areIndexesSorted ? scale.Enum(inner) : scale.Enum(inner, indexes);
return areIndexesSorted ? scale.Variant(inner) : scale.Variant(inner, indexes);
};
var buildCodec = withCache(_buildCodec, scale.Self, (res) => res);
var emptyTuple = scale.Tuple();
var getDynamicBuilder = (metadata) => {

@@ -679,16 +715,10 @@ const lookupData = metadata.lookup;

};
const buildCall = (pallet, callName) => {
const palletEntry = metadata.pallets.find((x) => x.name === pallet);
const callsLookup = getLookupEntryDef(palletEntry.calls);
if (callsLookup.type !== "enum")
throw null;
const callEntry = callsLookup.value[callName];
return {
location: [palletEntry.index, callEntry.idx],
args: callEntry.type === "primitive" ? emptyTuple : scale.Tuple(
...Object.values(callEntry.value).map(
(l) => buildDefinition(l.id)
)
)
};
const buildEnumEntry = (entry) => {
if (entry.type === "primitive")
return scale._void;
return entry.type === "tuple" ? scale.Tuple(
...Object.values(entry.value).map((l) => buildDefinition(l.id))
) : scale.Struct(
(0, import_utils.mapObject)(entry.value, (x) => buildDefinition(x.id))
);
};

@@ -704,8 +734,28 @@ const buildConstant = (pallet, constantName) => {

throw null;
const event = lookup.value[name];
const entry = lookup.value[name];
return {
location: [palletEntry.index, event.idx],
codec: event.type === "primitive" ? scale._void : buildDefinition(lookup.id)
location: [palletEntry.index, entry.idx],
codec: buildEnumEntry(lookup.value[name])
};
};
const buildCall = (pallet, name) => {
const palletEntry = metadata.pallets.find((x) => x.name === pallet);
const lookup = getLookupEntryDef(palletEntry.calls);
if (lookup.type !== "enum")
throw null;
const entry = lookup.value[name];
return {
location: [palletEntry.index, entry.idx],
args: buildEnumEntry(lookup.value[name])
};
};
const buildRuntimeCall = (api, method) => {
const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
if (!entry)
throw null;
return {
args: scale.Tuple(...entry.inputs.map((x) => buildDefinition(x.type))),
value: buildDefinition(entry.output)
};
};
return {

@@ -716,2 +766,3 @@ buildDefinition,

buildError: buildVariant("errors"),
buildRuntimeCall,
buildCall,

@@ -795,2 +846,9 @@ buildConstant,

return buildStruct(input.value);
if (input.type === "option")
return getChecksum([buildNextChecksum(input.value)], "Option()");
if (input.type === "result")
return getChecksum(
[input.value.ok, input.value.ko].map(buildNextChecksum),
"Result()"
);
const dependencies = Object.values(input.value).map((v) => {

@@ -828,2 +886,16 @@ if (v.type === "primitive")

};
const buildRuntimeCall = (api, method) => {
try {
const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
if (!entry)
throw null;
const args = getChecksum(
entry.inputs.map((x) => buildDefinition(x.type)),
`(${entry.inputs.map((x) => x.name).join(",")})`
);
return getChecksum([args, buildDefinition(entry.output)]);
} catch (_) {
return null;
}
};
const buildEnumEntry = (entry) => {

@@ -835,13 +907,2 @@ if (entry.type === "primitive")

};
const buildCall = (pallet, callName) => {
try {
const palletEntry = metadata.pallets.find((x) => x.name === pallet);
const callsLookup = getLookupEntryDef(palletEntry.calls);
if (callsLookup.type !== "enum")
throw null;
return buildEnumEntry(callsLookup.value[callName]);
} catch (_) {
return null;
}
};
const buildVariant = (variantType) => (pallet, name) => {

@@ -871,4 +932,5 @@ try {

buildDefinition: toStringEnhancer(buildDefinition),
buildRuntimeCall: toStringEnhancer(buildRuntimeCall),
buildStorage: toStringEnhancer(buildStorage),
buildCall: toStringEnhancer(buildCall),
buildCall: toStringEnhancer(buildVariant("calls")),
buildEvent: toStringEnhancer(buildVariant("events")),

@@ -881,9 +943,9 @@ buildError: toStringEnhancer(buildVariant("errors")),

// src/view-builder/view-builder.ts
var import_utils2 = require("@polkadot-api/utils");
var import_utils3 = require("@polkadot-api/utils");
var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
// src/view-builder/shaped-decoders.ts
var import_utils = require("@polkadot-api/utils");
var import_utils2 = require("@polkadot-api/utils");
var scale2 = __toESM(require("@polkadot-api/substrate-bindings"));
var toHex = import_utils.toHex;
var toHex = import_utils2.toHex;
var createInputValueDecoder = (dec, rest) => scale2.createDecoder((_bytes2) => {

@@ -962,9 +1024,17 @@ const bytes = _bytes2;

var StructDec = (input) => complexShapedDecoder(
{ codec: "Struct", shape: (0, import_utils.mapStringRecord)(input, (x) => x.shape) },
{ codec: "Struct", shape: (0, import_utils2.mapStringRecord)(input, (x) => x.shape) },
scale2.Struct.dec(input)
);
var EnumDec = (input, args) => complexShapedDecoder(
{ codec: "Enum", shape: (0, import_utils.mapStringRecord)(input, (x) => x.shape) },
scale2.Enum.dec(input, args)
{ codec: "Enum", shape: (0, import_utils2.mapStringRecord)(input, (x) => x.shape) },
scale2.Variant.dec(input, args)
);
var OptionDec = (input) => complexShapedDecoder(
{ codec: "Option", shape: input.shape },
scale2.Option.dec(input)
);
var ResultDec = (ok, ko) => complexShapedDecoder(
{ codec: "Result", shape: { ok: ok.shape, ko: ko.shape } },
scale2.Result.dec(ok, ko)
);
var selfDecoder = (value) => {

@@ -986,6 +1056,16 @@ let cache = (x) => {

Struct: StructDec,
Enum: EnumDec
Enum: EnumDec,
Option: OptionDec,
Result: ResultDec
};
// src/view-builder/view-builder.ts
var emptyTuple = complex.Tuple();
var toUnshapedDecoder = (fn) => (...args) => {
const value = fn(...args);
return {
shape: value.shape,
decoder: value
};
};
var withProp = (input, propName, propValue) => {

@@ -1021,3 +1101,3 @@ const decoder = (0, import_substrate_bindings2.enhanceDecoder)(input, (x) => ({

const buildStruct = (value, innerDocs) => withProp(
complex.Struct((0, import_utils2.mapStringRecord)(value, buildNext)),
complex.Struct((0, import_utils3.mapStringRecord)(value, buildNext)),
"innerDocs",

@@ -1038,2 +1118,6 @@ innerDocs

return buildStruct(input.value, input.innerDocs);
if (input.type === "option")
return complex.Option(buildNext(input.value));
if (input.type === "result")
return complex.Result(buildNext(input.value.ok), buildNext(input.value.ko));
const dependencies = Object.values(input.value).map((v) => {

@@ -1054,3 +1138,3 @@ if (v.type === "primitive")

const withDocs = (0, import_substrate_bindings2.enhanceDecoder)(withoutDocs, (val) => {
const docs = input.innerDocs[val.value.tag];
const docs = input.innerDocs[val.value.type];
return {

@@ -1091,9 +1175,3 @@ ...val,

}
const buildDefinition = (id) => {
const shapedDecoder = getDecoder(id);
return {
shape: shapedDecoder.shape,
decoder: shapedDecoder
};
};
const buildDefinition = toUnshapedDecoder(getDecoder);
const callDecoder = (0, import_substrate_bindings2.createDecoder)((bytes) => {

@@ -1117,3 +1195,3 @@ const palletIdx = import_substrate_bindings2.u8.dec(bytes);

value: {
name: decoded.value.tag,
name: decoded.value.type,
idx: bytes[1]

@@ -1130,4 +1208,40 @@ },

});
return { buildDefinition, callDecoder };
const buildEnumEntry = toUnshapedDecoder(
(entry, forceTuple = false) => {
if (entry.type === "primitive")
return forceTuple ? emptyTuple : primitives._void;
return entry.type === "tuple" ? complex.Tuple(
...Object.values(entry.value).map((l) => getDecoder(l.id))
) : complex.Struct(
(0, import_utils3.mapObject)(
entry.value,
(x) => getDecoder(x.id)
)
);
}
);
const buildVariant = (type) => (pallet, name) => {
const palletEntry = metadata.pallets.find((x) => x.name === pallet);
const lookup = getLookupEntryDef(palletEntry[type]);
if (lookup.type !== "enum")
throw null;
const event = lookup.value[name];
return {
location: [palletEntry.index, event.idx],
view: buildEnumEntry(event, type === "calls")
};
};
const buildConstant = (pallet, constantName) => {
const storageEntry = metadata.pallets.find((x) => x.name === pallet).constants.find((s) => s.name === constantName);
return buildDefinition(storageEntry.type);
};
return {
buildDefinition,
callDecoder,
buildEvent: buildVariant("events"),
buildError: buildVariant("errors"),
buildCall: buildVariant("calls"),
buildConstant
};
};
//# sourceMappingURL=index.js.map
import * as scale from '@polkadot-api/substrate-bindings';
import { StringRecord, V14, Codec, Decoder, HexString } from '@polkadot-api/substrate-bindings';
export { Decoder, HexString, StringRecord, V14 } from '@polkadot-api/substrate-bindings';
import { StringRecord, V15, Codec, Decoder, HexString } from '@polkadot-api/substrate-bindings';
export { Decoder, HexString, StringRecord, V15 } from '@polkadot-api/substrate-bindings';

@@ -39,2 +39,13 @@ type VoidVar = {

};
type OptionVar = {
type: "option";
value: LookupEntry;
};
type ResultVar = {
type: "result";
value: {
ok: LookupEntry;
ko: LookupEntry;
};
};
type SequenceVar = {

@@ -49,3 +60,3 @@ type: "sequence";

};
type ComposedVar = TupleVar | StructVar | SequenceVar | ArrayVar | EnumVar;
type ComposedVar = TupleVar | StructVar | SequenceVar | ArrayVar | OptionVar | ResultVar | EnumVar;
type Var = TerminalVar | ComposedVar;

@@ -170,5 +181,6 @@ type LookupEntry = {

typeImports: Set<string>;
enums: Map<string, string>;
variables: Map<string, Variable>;
}
declare const getStaticBuilder: (metadata: V14) => {
declare const getStaticBuilder: (metadata: V15, namespace: string) => {
buildDefinition: (id: number) => string;

@@ -181,9 +193,14 @@ buildStorage: (pallet: string, entry: string) => {

buildError: (pallet: string, name: string) => string;
buildCall: (pallet: string, callName: string) => string;
buildCall: (pallet: string, name: string) => string;
buildRuntimeCall: (api: string, method: string) => {
args: string;
value: string;
};
buildConstant: (pallet: string, constantName: string) => string;
getTypeFromVarName: (varName: string) => string;
getCode: () => string;
getEnums: () => string[];
};
declare const getDynamicBuilder: (metadata: V14) => {
declare const getDynamicBuilder: (metadata: V15) => {
buildDefinition: (id: number) => Codec<any>;

@@ -211,3 +228,8 @@ buildStorage: (pallet: string, entry: string) => {

};
buildCall: (pallet: string, callName: string) => {
buildRuntimeCall: (api: string, method: string) => {
args: Codec<any[]>;
value: Codec<any>;
};
buildCall: (pallet: string, name: string) => {
args: Codec<any>;
location: [

@@ -217,3 +239,2 @@ number,

];
args: Codec<any>;
};

@@ -224,6 +245,7 @@ buildConstant: (pallet: string, constantName: string) => Codec<any>;

declare const getChecksumBuilder: (metadata: V14) => {
declare const getChecksumBuilder: (metadata: V15) => {
buildDefinition: (id: number) => string | null;
buildRuntimeCall: (api: string, method: string) => string | null;
buildStorage: (pallet: string, entry: string) => string | null;
buildCall: (pallet: string, callName: string) => string | null;
buildCall: (pallet: string, name: string) => string | null;
buildEvent: (pallet: string, name: string) => string | null;

@@ -234,3 +256,11 @@ buildError: (pallet: string, name: string) => string | null;

type GetViewBuilder = (metadata: V14) => {
type UnshapedDecoder = {
shape: Shape;
decoder: Decoder<Decoded>;
};
type VariantBasedBuild = (pallet: string, name: string) => {
view: UnshapedDecoder;
location: [number, number];
};
type GetViewBuilder = (metadata: V15) => {
buildDefinition: (idx: number) => {

@@ -241,2 +271,6 @@ shape: Shape;

callDecoder: Decoder<DecodedCall>;
buildEvent: VariantBasedBuild;
buildError: VariantBasedBuild;
buildCall: VariantBasedBuild;
buildConstant: (pallet: string, name: string) => UnshapedDecoder;
};

@@ -320,2 +354,13 @@ interface DecodedCall {

}>;
type OptionDecoded = WithInputAndPath<{
codec: "Option";
value: Decoded;
}>;
type ResultDecoded = WithInputAndPath<{
codec: "Result";
value: {
ok: Decoded;
ko: Decoded;
};
}>;
type TupleDecoded = WithInputAndPath<{

@@ -334,3 +379,3 @@ codec: "Tuple";

value: {
tag: string;
type: string;
value: Decoded;

@@ -340,3 +385,3 @@ };

}>;
type ComplexDecoded = SequenceDecoded | ArrayDecoded | TupleDecoded | StructDecoded | EnumDecoded;
type ComplexDecoded = SequenceDecoded | ArrayDecoded | TupleDecoded | StructDecoded | OptionDecoded | ResultDecoded | EnumDecoded;
type Decoded = PrimitiveDecoded | ComplexDecoded;

@@ -360,2 +405,13 @@ interface SequenceShape {

}
interface OptionShape {
codec: "Option";
shape: Shape;
}
interface ResultShape {
codec: "Result";
shape: {
ok: Shape;
ko: Shape;
};
}
interface EnumShape {

@@ -365,3 +421,3 @@ codec: "Enum";

}
type ComplexShape = SequenceShape | ArrayShape | TupleShape | StructShape | EnumShape;
type ComplexShape = SequenceShape | ArrayShape | TupleShape | StructShape | OptionShape | ResultShape | EnumShape;
type Shape = {

@@ -373,2 +429,2 @@ codec: PrimitiveDecoded["codec"];

export { type AccountIdDecoded, type ArrayDecoded, type ArrayShape, type ArrayVar, type BigNumberDecoded, type BitSequenceDecoded, type BitSequenceVar, type BoolDecoded, type BytesArrayDecoded, type BytesSequenceDecoded, type CodeDeclarations, type CompactVar, type ComplexDecoded, type ComplexShape, type ComposedVar, type Decoded, type DecodedCall, type EnumDecoded, type EnumShape, type EnumVar, type GetViewBuilder, type LookupEntry, type MetadataPrimitives$1 as MetadataPrimitives, type NumberDecoded, type PrimitiveDecoded, type PrimitiveVar, type SequenceDecoded, type SequenceShape, type SequenceVar, type Shape, type StringDecoded, type StructDecoded, type StructShape, type StructVar, type TerminalVar, type TupleDecoded, type TupleShape, type TupleVar, type Var, type Variable, type VoidDecoded, type VoidVar, getChecksumBuilder, getDynamicBuilder, getLookupFn, getStaticBuilder, getViewBuilder, primitiveTypes };
export { type AccountIdDecoded, type ArrayDecoded, type ArrayShape, type ArrayVar, type BigNumberDecoded, type BitSequenceDecoded, type BitSequenceVar, type BoolDecoded, type BytesArrayDecoded, type BytesSequenceDecoded, type CodeDeclarations, type CompactVar, type ComplexDecoded, type ComplexShape, type ComposedVar, type Decoded, type DecodedCall, type EnumDecoded, type EnumShape, type EnumVar, type GetViewBuilder, type LookupEntry, type MetadataPrimitives$1 as MetadataPrimitives, type NumberDecoded, type OptionDecoded, type OptionShape, type OptionVar, type PrimitiveDecoded, type PrimitiveVar, type ResultDecoded, type ResultShape, type ResultVar, type SequenceDecoded, type SequenceShape, type SequenceVar, type Shape, type StringDecoded, type StructDecoded, type StructShape, type StructVar, type TerminalVar, type TupleDecoded, type TupleShape, type TupleVar, type UnshapedDecoder, type Var, type Variable, type VoidDecoded, type VoidVar, getChecksumBuilder, getDynamicBuilder, getLookupFn, getStaticBuilder, getViewBuilder, primitiveTypes };

@@ -1,8 +0,9 @@

"use strict";var se=Object.create;var W=Object.defineProperty;var ce=Object.getOwnPropertyDescriptor;var ue=Object.getOwnPropertyNames;var pe=Object.getPrototypeOf,de=Object.prototype.hasOwnProperty;var le=(e,t)=>{for(var p in t)W(e,p,{get:t[p],enumerable:!0})},G=(e,t,p,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of ue(t))!de.call(e,s)&&s!==p&&W(e,s,{get:()=>t[s],enumerable:!(r=ce(t,s))||r.enumerable});return e};var J=(e,t,p)=>(p=e!=null?se(pe(e)):{},G(t||!e||!e.__esModule?W(p,"default",{value:e,enumerable:!0}):p,e)),me=e=>G(W({},"__esModule",{value:!0}),e);var We={};le(We,{getChecksumBuilder:()=>Ee,getDynamicBuilder:()=>Se,getLookupFn:()=>T,getStaticBuilder:()=>ve,getViewBuilder:()=>ae,primitiveTypes:()=>M});module.exports=me(We);var $={type:"primitive",value:"_void"},T=e=>{let t=new Map,p=new Set,s=(d=>o=>{let h=t.get(o);if(h)return h;if(p.has(o)){let c={id:o};return t.set(o,c),c}p.add(o);let y=d(o);return h=t.get(o),h?Object.assign(h,y):(h={id:o,...y},t.set(o,h)),p.delete(o),h})(d=>{let{def:o}=e[d];if(o.tag==="composite"){if(o.value.length===0)return $;if(o.value.length===1)return s(o.value[0].type);let y=!0,c={},v={};return o.value.forEach((b,g)=>{y=y&&!!b.name;let u=b.name||g;c[u]=s(b.type),v[u]=b.docs}),y?{type:"struct",value:c,innerDocs:v}:{type:"tuple",value:Object.values(c),innerDocs:Object.values(v)}}if(o.tag==="variant"){if(o.value.length===0)return $;let y={},c={};return o.value.forEach(v=>{let b=v.name;if(c[b]=v.docs,v.fields.length===0){y[b]={...$,idx:v.index};return}let g=!0,u={},n={};v.fields.forEach((i,a)=>{g=g&&!!i.name;let l=i.name||a;u[l]=s(i.type),n[l]=i.docs}),y[b]=g?{type:"struct",value:u,innerDocs:n,idx:v.index}:{type:"tuple",value:Object.values(u),innerDocs:Object.values(n),idx:v.index}}),{type:"enum",value:y,innerDocs:c}}if(o.tag==="sequence")return{type:"sequence",value:s(o.value)};if(o.tag==="array")return{type:"array",value:s(o.value.type),len:o.value.len};if(o.tag==="tuple"){if(o.value.length===0)return $;if(o.value.length===1)return s(o.value[0]);let y=o.value.map(v=>s(v)),c=o.value.map(v=>e[v].docs);return{type:"tuple",value:y,innerDocs:c}}if(o.tag==="primitive")return{type:"primitive",value:o.value.tag};if(o.tag==="compact"){let y=s(o.value);return{type:"compact",isBig:Number(y.value.slice(1))>32}}return o.tag==="bitSequence"?{type:"bitSequence"}:{type:"primitive",value:o.value}});return s};var L=(e,t,p)=>(r,s,d,...o)=>{let{id:h}=r;if(s.has(h))return s.get(h);if(d.has(h)){let c=t(()=>s.get(h),r,...o);return s.set(h,c),c}d.add(h);let y=e(r,s,d,...o);return d.delete(h),s.has(h)&&(y=p(y,s.get(h),r,...o)),s.set(h,y),y};var M={_void:"undefined",bool:"boolean",char:"string",str:"string",u8:"number",u16:"number",u32:"number",u64:"bigint",u128:"bigint",u256:"bigint",i8:"number",i16:"number",i32:"number",i64:"bigint",i128:"bigint",i256:"bigint",compactNumber:"number",compactBn:"bigint",bitSequence:"{bitsLen: number, bytes: Uint8Array}",historicMetaCompat:"any"},I=(...e)=>e[0]+e.slice(1).map(t=>t[0].toUpperCase()+t.slice(1)).join(""),Y=e=>e.type==="primitive"&&e.value==="u8",O=e=>M[e]??`I${e}`,ye=(e,t,p,r,s)=>{if(e.type==="primitive")return r.imports.add(e.value),e.value;if(e.type==="compact"){let n=e.isBig?"compactBn":"compactNumber";return r.imports.add(n),n}if(e.type==="bitSequence")return r.imports.add(e.type),e.type;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8"){r.imports.add("Hex"),r.typeImports.add("HexString");let n={id:"_bytesSeq",value:"Hex()",types:"HexString",directDependencies:new Set};return r.variables.has(n.id)||r.variables.set(n.id,n),n.id}if(r.variables.has(s(e.id,"circular")))return s(e.id,"circular");if(r.variables.has(s(e.id)))return s(e.id);let d=n=>z(n,t,p,r,s),o=(n,i,a)=>{r.imports.add("Vector");let l=d(i),S={id:n,value:`Vector(${(a?[l,a]:[l]).join(", ")})`,types:`Array<${O(l)}>`,directDependencies:new Set([l])};return r.variables.set(n,S),n},h=(n,i)=>{r.imports.add("Tuple");let a=i.map(d),l={id:n,value:`Tuple(${a.join(", ")})`,types:`[${a.map(O).join(", ")}]`,directDependencies:new Set(a)};return r.variables.set(n,l),n},y=(n,i)=>{r.imports.add("Struct");let a=Object.values(i).map(d),l={id:n,value:`Struct({${Object.keys(i).map((m,S)=>`${m}: ${a[S]}`).join(", ")}})`,types:`{${Object.keys(i).map((m,S)=>`${m}: ${O(a[S])}`).join(", ")}}`,directDependencies:new Set(a)};return r.variables.set(n,l),n},c=s(e.id);if(e.type==="array"){if(Y(e.value)){if(e.len===32&&(e.id===0||e.id===1)){r.imports.add("AccountId");let n="_accountId";return r.variables.set(n,{id:n,value:"AccountId()",types:"SS58String",directDependencies:new Set}),r.typeImports.add("SS58String"),n}return r.imports.add("Hex"),r.variables.set(c,{id:c,value:`Hex(${e.len})`,types:"HexString",directDependencies:new Set}),r.typeImports.add("HexString"),c}return o(c,e.value,e.len)}if(e.type==="sequence")return o(c,e.value);if(e.type==="tuple")return h(c,e.value);if(e.type==="struct")return y(c,e.value);r.imports.add("Enum");let v=Object.entries(e.value).map(([n,i])=>{if(i.type==="primitive")return r.imports.add(i.value),i.value;let a=I(c,n);if(i.type==="tuple"){if(i.value.length===1){let l,m=i.value[0];if(n.startsWith("Raw")&&m.type==="array"&&Y(m.value)){let S=`_fixedStr${m.len}`;l=S,r.variables.has(S)||(r.imports.add("fixedStr"),r.variables.set(S,{id:S,value:`fixedStr(${m.len})`,types:"string",directDependencies:new Set}))}else l=d(i.value[0]);return r.variables.has(a)||r.variables.set(a,{id:a,value:l,types:O(l),directDependencies:new Set([l])}),a}return h(a,i.value)}else y(a,i.value);return a}),b=Object.values(e.value).map(n=>n.idx),g=b.every((n,i)=>n===i),u=`{${Object.keys(e.value).map((n,i)=>`${n}: ${v[i]}`)}}${g?"":`, [${b.join(", ")}]`}`;return r.variables.set(c,{id:c,value:`Enum(${u})`,types:Object.keys(e.value).map((n,i)=>`{tag: '${n}', value: ${O(v[i])}}`).join(" | "),directDependencies:new Set(v)}),c},z=L(ye,(e,t,p,r)=>{p.imports.add("Self");let s=r(t.id),d={id:r(t.id,"circular"),types:`I${s}`,value:`Self(() => ${s})`,directDependencies:new Set([s])};return p.variables.set(d.id,d),d.id},e=>e),ve=e=>{let t={imports:new Set,typeImports:new Set(["Codec"]),variables:new Map},p=e.lookup,r=T(p),s=(a,...l)=>{let{path:m}=p[a],S=m.length===0?["cdc"+a]:["c",...m];return S.push(...l),I(...S)},d=new Map,o=a=>z(r(a),d,new Set,t,s),h=(a,l)=>{if(t.variables.has(l))return l;let m=a.map(x=>x.type).map(o),S=a.map(x=>x.name);t.imports.add("Tuple");let E={id:l,types:`[${S.map((x,V)=>`${x[0].toUpperCase()+x.slice(1)}: ${O(m[V])}`).join(", ")}]`,value:`Tuple(${m.join(", ")})`,directDependencies:new Set(m)};return t.variables.set(l,E),l},y="_emptyTuple",c=()=>(t.variables.has(y)||(t.imports.add("Tuple"),t.variables.set(y,{id:y,types:"[]",value:"Tuple()",directDependencies:new Set})),y),v=(a,l)=>{let m=e.pallets.find(j=>j.name===a).storage.items.find(j=>j.name===l);if(m.type.tag==="plain")return{key:c(),val:o(m.type.value)};let{key:S,value:E}=m.type.value,x=o(E);return{key:m.type.value.hashers.length===1?h([{name:"key",type:S}],s(S,"Tupled")):o(S),val:x}},b=a=>(l,m)=>{let S=r(e.pallets.find(x=>x.name===l)[a]);if(S.type!=="enum")throw null;let E=I(o(S.id),m);return!t.variables.has(E)&&S.value[m].type==="primitive"&&t.variables.set(E,{id:E,value:"_void",types:"undefined",directDependencies:new Set}),E},g=(a,l)=>{let m=r(e.pallets.find(x=>x.name===a).calls);if(m.type!=="enum")throw null;let S=m.value[l];if(S.type==="primitive")return c();if(S.type==="tuple")return I(o(m.id),l);let E=Object.entries(S.value).map(([x,V])=>({name:x,type:V.id}));return h(E,s(m.id,l,"Tupled"))},u=(a,l)=>{let m=e.pallets.find(S=>S.name===a).constants.find(S=>S.name===l);return o(m.type)},n=()=>{let a=`import type {${[...t.typeImports].join(", ")}} from "@polkadot-api/substrate-bindings";
`,l=`import {${[...t.imports].join(", ")}} from "@polkadot-api/substrate-bindings";
"use strict";var ue=Object.create;var _=Object.defineProperty;var pe=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var le=Object.getPrototypeOf,me=Object.prototype.hasOwnProperty;var ye=(e,s)=>{for(var i in s)_(e,i,{get:s[i],enumerable:!0})},z=(e,s,i,r)=>{if(s&&typeof s=="object"||typeof s=="function")for(let a of de(s))!me.call(e,a)&&a!==i&&_(e,a,{get:()=>s[a],enumerable:!(r=pe(s,a))||r.enumerable});return e};var Q=(e,s,i)=>(i=e!=null?ue(le(e)):{},z(s||!e||!e.__esModule?_(i,"default",{value:e,enumerable:!0}):i,e)),ve=e=>z(_({},"__esModule",{value:!0}),e);var He={};ye(He,{getChecksumBuilder:()=>Ce,getDynamicBuilder:()=>Ee,getLookupFn:()=>P,getStaticBuilder:()=>ge,getViewBuilder:()=>ce,primitiveTypes:()=>F});module.exports=ve(He);var W={type:"primitive",value:"_void"},P=e=>{let s=new Map,i=new Set,a=(p=>c=>{let m=s.get(c);if(m)return m;if(i.has(c)){let y={id:c};return s.set(c,y),y}i.add(c);let g=p(c);return m=s.get(c),m?Object.assign(m,g):(m={id:c,...g},s.set(c,m)),i.delete(c),m})(p=>{let{def:c,path:m,params:g}=e[p];if(c.tag==="composite"){if(c.value.length===0)return W;if(c.value.length===1)return a(c.value[0].type);let h=!0,b={},o={};return c.value.forEach((l,n)=>{h=h&&!!l.name;let t=l.name||n;b[t]=a(l.type),o[t]=l.docs}),h?{type:"struct",value:b,innerDocs:o}:{type:"tuple",value:Object.values(b),innerDocs:Object.values(o)}}if(c.tag==="variant"){if(m.length===1&&m[0]==="Option"&&g.length===1&&g[0].name==="T")return{type:"option",value:a(g[0].type)};if(m.length===1&&m[0]==="Result"&&g.length===2&&g[0].name==="T"&&g[1].name==="E")return{type:"result",value:{ok:a(g[0].type),ko:a(g[1].type)}};if(c.value.length===0)return W;let h={},b={};return c.value.forEach(o=>{let l=o.name;if(b[l]=o.docs,o.fields.length===0){h[l]={...W,idx:o.index};return}let n=!0,t={},u={};o.fields.forEach((v,d)=>{n=n&&!!v.name;let S=v.name||d;t[S]=a(v.type),u[S]=v.docs}),h[l]=n?{type:"struct",value:t,innerDocs:u,idx:o.index}:{type:"tuple",value:Object.values(t),innerDocs:Object.values(u),idx:o.index}}),{type:"enum",value:h,innerDocs:b}}if(c.tag==="sequence")return{type:"sequence",value:a(c.value)};if(c.tag==="array")return{type:"array",value:a(c.value.type),len:c.value.len};if(c.tag==="tuple"){if(c.value.length===0)return W;if(c.value.length===1)return a(c.value[0]);let h=c.value.map(o=>a(o)),b=c.value.map(o=>e[o].docs);return{type:"tuple",value:h,innerDocs:b}}if(c.tag==="primitive")return{type:"primitive",value:c.value.tag};if(c.tag==="compact"){let h=a(c.value);return{type:"compact",isBig:Number(h.value.slice(1))>32}}return c.tag==="bitSequence"?{type:"bitSequence"}:{type:"primitive",value:c.value}});return a};var w=(e,s,i)=>(r,a,p,...c)=>{let{id:m}=r;if(a.has(m))return a.get(m);if(p.has(m)){let y=s(()=>a.get(m),r,...c);return a.set(m,y),y}p.add(m);let g=e(r,a,p,...c);return p.delete(m),a.has(m)&&(g=i(g,a.get(m),r,...c)),a.set(m,g),g};var F={_void:"undefined",bool:"boolean",char:"string",str:"string",u8:"number",u16:"number",u32:"number",u64:"bigint",u128:"bigint",u256:"bigint",i8:"number",i16:"number",i32:"number",i64:"bigint",i128:"bigint",i256:"bigint",compactNumber:"number",compactBn:"bigint",bitSequence:"{bitsLen: number, bytes: Uint8Array}",historicMetaCompat:"any"},U=(...e)=>e[0]+e.slice(1).map(s=>s[0].toUpperCase()+s.slice(1)).join(""),he=e=>e.type==="primitive"&&e.value==="u8",L=e=>F[e]??e,be=(e,s,i,r,a)=>{if(e.type==="primitive")return r.imports.add(e.value),e.value;if(e.type==="compact"){let n=e.isBig?"compactBn":"compactNumber";return r.imports.add(n),n}if(e.type==="bitSequence")return r.imports.add(e.type),e.type;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8"){r.imports.add("Bin"),r.typeImports.add("HexString"),r.typeImports.add("Binary");let n={id:"_bytesSeq",value:"Bin()",types:"Binary",directDependencies:new Set};return r.variables.has(n.id)||r.variables.set(n.id,n),n.id}if(r.variables.has(a(e.id,"circular")))return a(e.id,"circular");if(r.variables.has(a(e.id)))return a(e.id);let p=n=>X(n,s,i,r,a),c=(n,t,u)=>{r.imports.add("Vector");let v=p(t),S={id:n,value:`Vector(${(u?[v,u]:[v]).join(", ")})`,types:`Array<${L(v)}>`,directDependencies:new Set([v])};return r.variables.set(n,S),n},m=(n,t)=>{r.imports.add("Tuple");let u=t.map(p),v={id:n,value:`Tuple(${u.join(", ")})`,types:`[${u.map(L).join(", ")}]`,directDependencies:new Set(u)};return r.variables.set(n,v),n},g=(n,t)=>{r.imports.add("Struct");let u=Object.values(t).map(p),v={id:n,value:`Struct({${Object.keys(t).map((d,S)=>`${d}: ${u[S]}`).join(", ")}})`,types:`{${Object.keys(t).map((d,S)=>`${d}: ${L(u[S])}`).join(", ")}}`,directDependencies:new Set(u)};return r.variables.set(n,v),n},y=a(e.id);if(e.type==="array"){if(he(e.value)){if(e.len===32&&(e.id===0||e.id===1)){r.imports.add("AccountId");let n="_accountId";return r.variables.set(n,{id:n,value:"AccountId()",types:"SS58String",directDependencies:new Set}),r.typeImports.add("SS58String"),n}return r.imports.add("Bin"),r.variables.set(y,{id:y,value:`Bin(${e.len})`,types:"Binary",directDependencies:new Set}),r.typeImports.add("HexString"),r.typeImports.add("Binary"),y}return c(y,e.value,e.len)}if(e.type==="sequence")return c(y,e.value);if(e.type==="tuple")return m(y,e.value);if(e.type==="struct")return g(y,e.value);if(e.type==="option"){r.imports.add("Option");let n=p(e.value),t=`_Option_${n}`,u={id:t,value:`Option(${n})`,types:`${L(n)} | undefined`,directDependencies:new Set([n])};return r.variables.set(t,u),t}if(e.type==="result"){r.imports.add("Result"),r.typeImports.add("ResultPayload");let n=p(e.value.ok),t=p(e.value.ko),u=`_Result_${n}_${t}`,v={id:u,value:`Result(${n}, ${t})`,types:`ResultPayload<${L(n)}, ${L(t)}>`,directDependencies:new Set([n,t])};return r.variables.set(u,v),u}r.imports.add("Variant"),r.typeImports.add("Enum");let h=Object.entries(e.value).map(([n,t])=>{if(t.type==="primitive")return r.imports.add(t.value),t.value;let u=U(y,n);if(t.type==="tuple"){if(t.value.length===1){let v=p(t.value[0]);return r.variables.has(u)||r.variables.set(u,{id:u,value:v,types:L(v),directDependencies:new Set([v])}),u}return m(u,t.value)}else g(u,t.value);return u}),b=Object.values(e.value).map(n=>n.idx),o=b.every((n,t)=>n===t),l=`{${Object.keys(e.value).map((n,t)=>`${n}: ${h[t]}`)}}${o?"":`, [${b.join(", ")}]`}`;return r.enums.set(y,Object.keys(e.value).map((n,t)=>`{type: '${n}', value: ${L(h[t])}}`).join(" | ")),r.variables.set(y,{id:y,value:`Variant(${l})`,types:`Enum<E${y}>`,directDependencies:new Set(h)}),y},X=w(be,(e,s,i,r)=>{i.imports.add("Self");let a=r(s.id),p={id:r(s.id,"circular"),types:a,value:`Self(() => ${a})`,directDependencies:new Set([a])};return i.variables.set(p.id,p),p.id},e=>e),ge=(e,s)=>{let i={imports:new Set,typeImports:new Set(["Codec"]),variables:new Map,enums:new Map},r=e.lookup,a=P(r),p=(d,...S)=>{let{path:E}=r[d],D=E.length===0?["cdc"+d]:E[0]==="sp_runtime"?[s,E.slice(-1)[0]]:[s,...E];return D.push(...S),U(...D)},c=new Map,m=d=>X(a(d),c,new Set,i,p),g=(d,S)=>{if(i.variables.has(S))return S;let E=d.map(V=>V.type).map(m),D=d.map(V=>V.name);i.imports.add("Tuple");let T={id:S,types:`[${D.map((V,A)=>`${V[0].toUpperCase()+V.slice(1)}: ${L(E[A])}`).join(", ")}]`,value:`Tuple(${E.join(", ")})`,directDependencies:new Set(E)};return i.variables.set(S,T),S},y="_emptyTuple",h=()=>(i.variables.has(y)||(i.imports.add("Tuple"),i.variables.set(y,{id:y,types:"[]",value:"Tuple()",directDependencies:new Set})),y),b=(d,S)=>{let E=e.pallets.find(B=>B.name===d).storage.items.find(B=>B.name===S);if(E.type.tag==="plain")return{key:h(),val:m(E.type.value)};let{key:D,value:T}=E.type.value,V=m(T);return{key:E.type.value.hashers.length===1?g([{name:"key",type:D}],p(D,"Tupled")):m(D),val:V}},o=(d,S)=>{let E=e.apis.find(D=>D.name===d)?.methods.find(D=>D.name===S);if(!E)throw null;return{args:g(E.inputs,`${d}${S}Args`),value:m(E.output)}},l=d=>(S,E)=>{let D=a(e.pallets.find(V=>V.name===S)[d]);if(D.type!=="enum")throw null;let T=U(m(D.id),E);return!i.variables.has(T)&&D.value[E].type==="primitive"&&i.variables.set(T,{id:T,value:"_void",types:"undefined",directDependencies:new Set}),T},n=(d,S)=>{let E=e.pallets.find(D=>D.name===d).constants.find(D=>D.name===S);return m(E.type)},t=()=>{let d=`import type {${[...i.typeImports].join(", ")}} from "@polkadot-api/client";
`,S=`import {${[...i.imports].join(", ")}} from "@polkadot-api/substrate-bindings";
`,m=[...t.variables.values()].map(S=>`type I${S.id} = ${S.types};
const ${S.id}: Codec<I${S.id}> = ${S.value};`).join(`
`,E=[...i.variables.values()].map(D=>`${i.enums.has(D.id)?`type E${D.id} = ${i.enums.get(D.id)};
export `:""}type ${D.id} = ${D.types};
const ${D.id}: Codec<${D.id}> = ${D.value};`).join(`
`);return`${a}${l}${m}`},i=a=>M[a]??t.variables.get(a)?.types??`I${a}`;return{buildDefinition:o,buildStorage:v,buildEvent:b("events"),buildError:b("errors"),buildCall:g,buildConstant:u,getTypeFromVarName:i,getCode:n}};var f=J(require("@polkadot-api/substrate-bindings"));var he=f.Hex(),Q=e=>e.type==="primitive"&&e.value==="u8",be=(e,t,p,r)=>{if(e.type==="primitive")return f[e.value];if(e.type==="compact")return f.compact;if(e.type==="bitSequence")return f.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return he;let s=g=>X(g,t,p,r),d=(g,u)=>{let n=s(g);return u?f.Vector(n,u):f.Vector(n)},o=g=>f.Tuple(...g.map(s)),h=g=>{let u=Object.fromEntries(Object.entries(g).map(([n,i])=>[n,s(i)]));return f.Struct(u)};if(e.type==="array")return Q(e.value)?e.len===32&&(e.id===0||e.id===1)?r:f.Hex(e.len):d(e.value,e.len);if(e.type==="sequence")return d(e.value);if(e.type==="tuple")return o(e.value);if(e.type==="struct")return h(e.value);let y=Object.entries(e.value).map(([g,u])=>{if(u.type==="primitive")return f._void;if(u.type==="tuple"&&u.value.length===1){let n=u.value[0];return g.startsWith("Raw")&&n.type==="array"&&Q(n.value)?f.fixedStr(n.len):s(n)}return u.type==="tuple"?o(u.value):h(u.value)}),c=Object.fromEntries(Object.keys(e.value).map((g,u)=>[g,y[u]])),v=Object.values(e.value).map(g=>g.idx);return v.every((g,u)=>g===u)?f.Enum(c):f.Enum(c,v)},X=L(be,f.Self,e=>e),ge=f.Tuple(),Se=e=>{let t=e.lookup,p=T(t),r=f.AccountId(),s=new Map,d=u=>X(p(u),s,new Set,r),o=e.pallets.find(u=>u.name==="System")?.constants.find(u=>u.name==="SS58Prefix"),h;if(o)try{let u=d(o.type).dec(o.value);typeof u=="number"&&(h=u,r=f.AccountId(u))}catch{}let y=new Map,c=(u,n)=>{let i=y.get(u);i||y.set(u,i=f.Storage(u));let a=e.pallets.find(C=>C.name===u).storage.items.find(C=>C.name===n),l=(C,...B)=>{let K=i(...B);return{...K,len:C,fallback:a.modifier===1?K.dec(a.fallback):void 0}};if(a.type.tag==="plain")return l(0,n,d(a.type.value).dec);let{key:m,value:S,hashers:E}=a.type.value,x=d(S),V=E.map(C=>f[C.tag]),j=V.length===1?[[d(m),V[0]]]:p(m).value.map((C,B)=>[d(C.id),V[B]]);return l(V.length,n,x.dec,...j)},v=(u,n)=>{let i=e.pallets.find(m=>m.name===u),a=p(i.calls);if(a.type!=="enum")throw null;let l=a.value[n];return{location:[i.index,l.idx],args:l.type==="primitive"?ge:f.Tuple(...Object.values(l.value).map(m=>d(m.id)))}},b=(u,n)=>{let i=e.pallets.find(a=>a.name===u).constants.find(a=>a.name===n);return d(i.type)},g=u=>(n,i)=>{let a=e.pallets.find(S=>S.name===n),l=p(a[u]);if(l.type!=="enum")throw null;let m=l.value[i];return{location:[a.index,m.idx],codec:m.type==="primitive"?f._void:d(l.id)}};return{buildDefinition:d,buildStorage:c,buildEvent:g("events"),buildError:g("errors"),buildCall:v,buildConstant:b,ss58Prefix:h}};var F=require("@polkadot-api/substrate-bindings");var Z=new TextEncoder,fe=Z.encode.bind(Z),k=(e,t)=>{let p=typeof t=="string",r=new Uint8Array((e.length+(p?1:0))*8),s=new DataView(r.buffer),d=0;p&&(s.setBigUint64(d,(0,F.h64)(fe(t))),d+=8);for(let o=0;o<e.length;o++,d+=8)s.setBigUint64(d,e[o]);return(0,F.h64)(r)},H={_void:0n,bool:1n,char:2n,str:3n,u8:4n,u16:5n,u32:6n,u64:7n,u128:8n,u256:9n,i8:5n,i16:5n,i32:6n,i64:7n,i128:8n,i256:9n,compacts:10n,compactb:11n,bitSequence:12n,historicMetaCompat:13n},De=14n,xe=(e,t,p)=>{if(t.has(e.id))return t.get(e.id);if(e.type==="primitive")return H[e.value];if(e.type==="compact")return H[e.isBig?"compactb":"compacts"];if(e.type==="bitSequence")return H.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return De;let r=c=>ee(c,t,p),s=(c,v)=>{let b=r(c);return v?k([b,BigInt(v)],"Vector(,)"):k([b],"Vector()")},d=c=>k(c.map(r)),o=c=>k(Object.values(c).map(r),JSON.stringify(Object.keys(c)));if(e.type==="array")return s(e.value,e.len);if(e.type==="sequence")return s(e.value);if(e.type==="tuple")return d(e.value);if(e.type==="struct")return o(e.value);let h=Object.values(e.value).map(c=>c.type==="primitive"?0n:c.type==="tuple"?d(c.value):o(c.value)),y=Object.keys(e.value);return y.push("Enum"),k(h,JSON.stringify({Enum:y}))},ee=L(xe,()=>0n,e=>e),Ee=e=>{let t=e.lookup,p=T(t),r=new Map,s=b=>ee(p(b),r,new Set),d=(b,g)=>{try{let u=e.pallets.find(m=>m.name===b).storage.items.find(m=>m.name===g);if(u.type.tag==="plain")return k([s(u.type.value)]);let{key:n,value:i}=u.type.value,a=s(i),l=u.type.value.hashers.length===1?k([s(n)]):s(n);return k([a,l])}catch{return null}},o=b=>{if(b.type==="primitive")return 0n;let g=Object.values(b.value).map(u=>s(u.id));return b.type==="tuple"?k(g):k(g,JSON.stringify(Object.keys(b.value)))},h=(b,g)=>{try{let u=e.pallets.find(i=>i.name===b),n=p(u.calls);if(n.type!=="enum")throw null;return o(n.value[g])}catch{return null}},y=b=>(g,u)=>{try{let n=e.pallets.find(a=>a.name===g),i=p(n[b]);if(i.type!=="enum")throw null;return o(i.value[u])}catch{return null}},c=(b,g)=>{try{let u=e.pallets.find(n=>n.name===b).constants.find(n=>n.name===g);return s(u.type)}catch{return null}},v=b=>(...g)=>b(...g)?.toString(32)??null;return{buildDefinition:v(s),buildStorage:v(d),buildCall:v(h),buildEvent:v(y("events")),buildError:v(y("errors")),buildConstant:v(c)}};var oe=require("@polkadot-api/utils"),A=require("@polkadot-api/substrate-bindings");var R=require("@polkadot-api/utils"),D=J(require("@polkadot-api/substrate-bindings")),ke=R.toHex,te=(e,t)=>D.createDecoder(p=>{let r=p,s=r.i,d=e(r),o=ke(new Uint8Array(r.buffer.slice(s,r.i)));return{...t,value:d,input:o}}),_=(e,t,p)=>{let r=te(t,{codec:e,...p});return Object.assign(r,{shape:{codec:e}})},q=(e,t,p)=>{let r=te(t,{codec:e.codec,...p});return Object.assign(r,{shape:e})},U=(e=42)=>{let t=D.enhanceDecoder(D.AccountId(e).dec,p=>({address:p,ss58Prefix:e}));return _("AccountId",t,{})},Ve=e=>_("BytesArray",D.Hex.dec(e),{len:e}),Ce=["_void","bool","char","str","u8","u16","u32","i8","i16","i32","u64","u128","u256","i64","i128","i256","compactNumber","compactBn","bitSequence"],Te=Object.fromEntries(Ce.map(e=>[e,_(e,D[e].dec)])),P={...Te,Bytes:_("Bytes",D.Hex.dec()),BytesArray:Ve,AccountId:U()},Pe=e=>q({codec:"Sequence",shape:e.shape},D.Vector.dec(e)),Le=(e,t)=>q({codec:"Array",shape:e.shape,len:t},D.Vector.dec(e,t)),we=(...e)=>q({codec:"Tuple",shape:e.map(t=>t.shape)},D.Tuple.dec(...e)),Ae=e=>q({codec:"Struct",shape:(0,R.mapStringRecord)(e,t=>t.shape)},D.Struct.dec(e)),Oe=(e,t)=>q({codec:"Enum",shape:(0,R.mapStringRecord)(e,p=>p.shape)},D.Enum.dec(e,t)),re=e=>{let t=r=>{let s=e(),d=s;return t=s,d(r)},p=r=>t(r);return p.shape={codec:"_void"},p},w={Sequence:Pe,Array:Le,Tuple:we,Struct:Ae,Enum:Oe};var N=(e,t,p)=>{let r=(0,A.enhanceDecoder)(e,s=>({...s,[t]:p}));return r.shape=e.shape,r},je=e=>(t,p,r,s,...d)=>{let{path:o}=s[t.id],h=e(t,p,r,s,...d);return o.length?N(h,"path",o):h},Re=(e,t,p,r,s)=>{if(e.type==="primitive")return P[e.value];if(e.type==="compact")return e.isBig?P.compactBn:P.compactNumber;if(e.type==="bitSequence")return P.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return P.Bytes;let d=i=>ie(i,t,p,r,s),o=(i,a)=>{let l=d(i);return a?w.Array(l,a):w.Sequence(l)},h=(i,a)=>N(w.Tuple(...i.map(d)),"innerDocs",a),y=(i,a)=>N(w.Struct((0,oe.mapStringRecord)(i,d)),"innerDocs",a);if(e.type==="array")return e.value.type==="primitive"&&e.value.value==="u8"?e.len===32&&(e.id===0||e.id===1)?s:P.BytesArray(e.len):o(e.value,e.len);if(e.type==="sequence")return o(e.value);if(e.type==="tuple")return h(e.value,e.innerDocs);if(e.type==="struct")return y(e.value,e.innerDocs);let c=Object.values(e.value).map(i=>i.type==="primitive"?P._void:i.type==="tuple"&&i.value.length===1?d(i.value[0]):i.type==="tuple"?h(i.value,i.innerDocs):y(i.value,i.innerDocs)),v=Object.fromEntries(Object.keys(e.value).map((i,a)=>[i,c[a]])),b=Object.values(e.value).map(i=>i.idx),u=b.every((i,a)=>i===a)?w.Enum(v):w.Enum(v,b),n=(0,A.enhanceDecoder)(u,i=>{let a=e.innerDocs[i.value.tag];return{...i,docs:a}});return n.shape=u.shape,n},qe=je(Re),ie=L(qe,re,(e,t)=>(t.shape=e.shape,e)),ne=e=>`0x${e.toString(16).padEnd(2,"0")}`,ae=e=>{let t=e.lookup,p=new Map,r=c=>ie(s(c),p,new Set,t,d),s=T(t),d=P.AccountId,o=e.pallets.find(c=>c.name==="System")?.constants.find(c=>c.name==="SS58Prefix");if(o)try{let c=r(o.type)(o.value).value;typeof c=="number"&&(d=U(c))}catch{}let h=c=>{let v=r(c);return{shape:v.shape,decoder:v}},y=(0,A.createDecoder)(c=>{let v=A.u8.dec(c),b=e.pallets.find(a=>a.index===v);if(!b)throw new Error("Invalid Pallet");let g={value:{name:b.name,idx:v},input:ne(c[0])},u=r(b.calls),n=u(c);if(n.codec!=="Enum")throw null;let i={value:{name:n.value.tag,idx:c[1]},input:ne(c[1]),docs:n.docs};return{pallet:g,call:i,args:{value:n.value.value,shape:u.shape}}});return{buildDefinition:h,callDecoder:y}};
`);return`${d}${S}${E}`},u=()=>[...i.enums.keys()],v=d=>F[d]??i.variables.get(d)?.types??d;return{buildDefinition:m,buildStorage:b,buildEvent:l("events"),buildError:l("errors"),buildCall:l("calls"),buildRuntimeCall:o,buildConstant:n,getTypeFromVarName:v,getCode:t,getEnums:u}};var f=Q(require("@polkadot-api/substrate-bindings"));var Z=require("@polkadot-api/utils"),Se=f.Bin(),fe=e=>e.type==="primitive"&&e.value==="u8",De=(e,s,i,r)=>{if(e.type==="primitive")return f[e.value];if(e.type==="compact")return f.compact;if(e.type==="bitSequence")return f.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return Se;let a=o=>ee(o,s,i,r),p=(o,l)=>{let n=a(o);return l?f.Vector(n,l):f.Vector(n)},c=o=>f.Tuple(...o.map(a)),m=o=>{let l=Object.fromEntries(Object.entries(o).map(([n,t])=>[n,a(t)]));return f.Struct(l)};if(e.type==="array")return fe(e.value)?e.len===32&&(e.id===0||e.id===1)?r:f.Bin(e.len):p(e.value,e.len);if(e.type==="sequence")return p(e.value);if(e.type==="tuple")return c(e.value);if(e.type==="struct")return m(e.value);if(e.type==="option")return f.Option(a(e.value));if(e.type==="result")return f.Result(a(e.value.ok),a(e.value.ko));let g=Object.values(e.value).map(o=>o.type==="primitive"?f._void:o.type==="tuple"&&o.value.length===1?a(o.value[0]):o.type==="tuple"?c(o.value):m(o.value)),y=Object.fromEntries(Object.keys(e.value).map((o,l)=>[o,g[l]])),h=Object.values(e.value).map(o=>o.idx);return h.every((o,l)=>o===l)?f.Variant(y):f.Variant(y,h)},ee=w(De,f.Self,e=>e),Ee=e=>{let s=e.lookup,i=P(s),r=f.AccountId(),a=new Map,p=t=>ee(i(t),a,new Set,r),c=e.pallets.find(t=>t.name==="System")?.constants.find(t=>t.name==="SS58Prefix"),m;if(c)try{let t=p(c.type).dec(c.value);typeof t=="number"&&(m=t,r=f.AccountId(t))}catch{}let g=new Map,y=(t,u)=>{let v=g.get(t);v||g.set(t,v=f.Storage(t));let d=e.pallets.find(O=>O.name===t).storage.items.find(O=>O.name===u),S=(O,...H)=>{let Y=v(...H);return{...Y,len:O,fallback:d.modifier===1?Y.dec(d.fallback):void 0}};if(d.type.tag==="plain")return S(0,u,p(d.type.value).dec);let{key:E,value:D,hashers:T}=d.type.value,V=p(D),A=T.map(O=>f[O.tag]),B=A.length===1?[[p(E),A[0]]]:i(E).value.map((O,H)=>[p(O.id),A[H]]);return S(A.length,u,V.dec,...B)},h=t=>t.type==="primitive"?f._void:t.type==="tuple"?f.Tuple(...Object.values(t.value).map(u=>p(u.id))):f.Struct((0,Z.mapObject)(t.value,u=>p(u.id))),b=(t,u)=>{let v=e.pallets.find(d=>d.name===t).constants.find(d=>d.name===u);return p(v.type)},o=t=>(u,v)=>{let d=e.pallets.find(D=>D.name===u),S=i(d[t]);if(S.type!=="enum")throw null;let E=S.value[v];return{location:[d.index,E.idx],codec:h(S.value[v])}},l=(t,u)=>{let v=e.pallets.find(E=>E.name===t),d=i(v.calls);if(d.type!=="enum")throw null;let S=d.value[u];return{location:[v.index,S.idx],args:h(d.value[u])}},n=(t,u)=>{let v=e.apis.find(d=>d.name===t)?.methods.find(d=>d.name===u);if(!v)throw null;return{args:f.Tuple(...v.inputs.map(d=>p(d.type))),value:p(v.output)}};return{buildDefinition:p,buildStorage:y,buildEvent:o("events"),buildError:o("errors"),buildRuntimeCall:n,buildCall:l,buildConstant:b,ss58Prefix:m}};var K=require("@polkadot-api/substrate-bindings");var te=new TextEncoder,xe=te.encode.bind(te),k=(e,s)=>{let i=typeof s=="string",r=new Uint8Array((e.length+(i?1:0))*8),a=new DataView(r.buffer),p=0;i&&(a.setBigUint64(p,(0,K.h64)(xe(s))),p+=8);for(let c=0;c<e.length;c++,p+=8)a.setBigUint64(p,e[c]);return(0,K.h64)(r)},N={_void:0n,bool:1n,char:2n,str:3n,u8:4n,u16:5n,u32:6n,u64:7n,u128:8n,u256:9n,i8:5n,i16:5n,i32:6n,i64:7n,i128:8n,i256:9n,compacts:10n,compactb:11n,bitSequence:12n,historicMetaCompat:13n},ke=14n,Ve=(e,s,i)=>{if(s.has(e.id))return s.get(e.id);if(e.type==="primitive")return N[e.value];if(e.type==="compact")return N[e.isBig?"compactb":"compacts"];if(e.type==="bitSequence")return N.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return ke;let r=y=>re(y,s,i),a=(y,h)=>{let b=r(y);return h?k([b,BigInt(h)],"Vector(,)"):k([b],"Vector()")},p=y=>k(y.map(r)),c=y=>k(Object.values(y).map(r),JSON.stringify(Object.keys(y)));if(e.type==="array")return a(e.value,e.len);if(e.type==="sequence")return a(e.value);if(e.type==="tuple")return p(e.value);if(e.type==="struct")return c(e.value);if(e.type==="option")return k([r(e.value)],"Option()");if(e.type==="result")return k([e.value.ok,e.value.ko].map(r),"Result()");let m=Object.values(e.value).map(y=>y.type==="primitive"?0n:y.type==="tuple"?p(y.value):c(y.value)),g=Object.keys(e.value);return g.push("Enum"),k(m,JSON.stringify({Enum:g}))},re=w(Ve,()=>0n,e=>e),Ce=e=>{let s=e.lookup,i=P(s),r=new Map,a=b=>re(i(b),r,new Set),p=(b,o)=>{try{let l=e.pallets.find(d=>d.name===b).storage.items.find(d=>d.name===o);if(l.type.tag==="plain")return k([a(l.type.value)]);let{key:n,value:t}=l.type.value,u=a(t),v=l.type.value.hashers.length===1?k([a(n)]):a(n);return k([u,v])}catch{return null}},c=(b,o)=>{try{let l=e.apis.find(t=>t.name===b)?.methods.find(t=>t.name===o);if(!l)throw null;let n=k(l.inputs.map(t=>a(t.type)),`(${l.inputs.map(t=>t.name).join(",")})`);return k([n,a(l.output)])}catch{return null}},m=b=>{if(b.type==="primitive")return 0n;let o=Object.values(b.value).map(l=>a(l.id));return b.type==="tuple"?k(o):k(o,JSON.stringify(Object.keys(b.value)))},g=b=>(o,l)=>{try{let n=e.pallets.find(u=>u.name===o),t=i(n[b]);if(t.type!=="enum")throw null;return m(t.value[l])}catch{return null}},y=(b,o)=>{try{let l=e.pallets.find(n=>n.name===b).constants.find(n=>n.name===o);return a(l.type)}catch{return null}},h=b=>(...o)=>b(...o)?.toString(32)??null;return{buildDefinition:h(a),buildRuntimeCall:h(c),buildStorage:h(p),buildCall:h(g("calls")),buildEvent:h(g("events")),buildError:h(g("errors")),buildConstant:h(y)}};var M=require("@polkadot-api/utils"),j=require("@polkadot-api/substrate-bindings");var q=require("@polkadot-api/utils"),x=Q(require("@polkadot-api/substrate-bindings")),Te=q.toHex,ne=(e,s)=>x.createDecoder(i=>{let r=i,a=r.i,p=e(r),c=Te(new Uint8Array(r.buffer.slice(a,r.i)));return{...s,value:p,input:c}}),I=(e,s,i)=>{let r=ne(s,{codec:e,...i});return Object.assign(r,{shape:{codec:e}})},$=(e,s,i)=>{let r=ne(s,{codec:e.codec,...i});return Object.assign(r,{shape:e})},G=(e=42)=>{let s=x.enhanceDecoder(x.AccountId(e).dec,i=>({address:i,ss58Prefix:e}));return I("AccountId",s,{})},Re=e=>I("BytesArray",x.Hex.dec(e),{len:e}),Oe=["_void","bool","char","str","u8","u16","u32","i8","i16","i32","u64","u128","u256","i64","i128","i256","compactNumber","compactBn","bitSequence"],Pe=Object.fromEntries(Oe.map(e=>[e,I(e,x[e].dec)])),R={...Pe,Bytes:I("Bytes",x.Hex.dec()),BytesArray:Re,AccountId:G()},Le=e=>$({codec:"Sequence",shape:e.shape},x.Vector.dec(e)),we=(e,s)=>$({codec:"Array",shape:e.shape,len:s},x.Vector.dec(e,s)),Ae=(...e)=>$({codec:"Tuple",shape:e.map(s=>s.shape)},x.Tuple.dec(...e)),$e=e=>$({codec:"Struct",shape:(0,q.mapStringRecord)(e,s=>s.shape)},x.Struct.dec(e)),je=(e,s)=>$({codec:"Enum",shape:(0,q.mapStringRecord)(e,i=>i.shape)},x.Variant.dec(e,s)),Be=e=>$({codec:"Option",shape:e.shape},x.Option.dec(e)),qe=(e,s)=>$({codec:"Result",shape:{ok:e.shape,ko:s.shape}},x.Result.dec(e,s)),oe=e=>{let s=r=>{let a=e(),p=a;return s=a,p(r)},i=r=>s(r);return i.shape={codec:"_void"},i},C={Sequence:Le,Array:we,Tuple:Ae,Struct:$e,Enum:je,Option:Be,Result:qe};var _e=C.Tuple(),ae=e=>(...s)=>{let i=e(...s);return{shape:i.shape,decoder:i}},J=(e,s,i)=>{let r=(0,j.enhanceDecoder)(e,a=>({...a,[s]:i}));return r.shape=e.shape,r},We=e=>(s,i,r,a,...p)=>{let{path:c}=a[s.id],m=e(s,i,r,a,...p);return c.length?J(m,"path",c):m},Ie=(e,s,i,r,a)=>{if(e.type==="primitive")return R[e.value];if(e.type==="compact")return e.isBig?R.compactBn:R.compactNumber;if(e.type==="bitSequence")return R.bitSequence;if(e.type==="sequence"&&e.value.type==="primitive"&&e.value.value==="u8")return R.Bytes;let p=t=>ie(t,s,i,r,a),c=(t,u)=>{let v=p(t);return u?C.Array(v,u):C.Sequence(v)},m=(t,u)=>J(C.Tuple(...t.map(p)),"innerDocs",u),g=(t,u)=>J(C.Struct((0,M.mapStringRecord)(t,p)),"innerDocs",u);if(e.type==="array")return e.value.type==="primitive"&&e.value.value==="u8"?e.len===32&&(e.id===0||e.id===1)?a:R.BytesArray(e.len):c(e.value,e.len);if(e.type==="sequence")return c(e.value);if(e.type==="tuple")return m(e.value,e.innerDocs);if(e.type==="struct")return g(e.value,e.innerDocs);if(e.type==="option")return C.Option(p(e.value));if(e.type==="result")return C.Result(p(e.value.ok),p(e.value.ko));let y=Object.values(e.value).map(t=>t.type==="primitive"?R._void:t.type==="tuple"&&t.value.length===1?p(t.value[0]):t.type==="tuple"?m(t.value,t.innerDocs):g(t.value,t.innerDocs)),h=Object.fromEntries(Object.keys(e.value).map((t,u)=>[t,y[u]])),b=Object.values(e.value).map(t=>t.idx),l=b.every((t,u)=>t===u)?C.Enum(h):C.Enum(h,b),n=(0,j.enhanceDecoder)(l,t=>{let u=e.innerDocs[t.value.type];return{...t,docs:u}});return n.shape=l.shape,n},Me=We(Ie),ie=w(Me,oe,(e,s)=>(s.shape=e.shape,e)),se=e=>`0x${e.toString(16).padEnd(2,"0")}`,ce=e=>{let s=e.lookup,i=new Map,r=o=>ie(a(o),i,new Set,s,p),a=P(s),p=R.AccountId,c=e.pallets.find(o=>o.name==="System")?.constants.find(o=>o.name==="SS58Prefix");if(c)try{let o=r(c.type)(c.value).value;typeof o=="number"&&(p=G(o))}catch{}let m=ae(r),g=(0,j.createDecoder)(o=>{let l=j.u8.dec(o),n=e.pallets.find(S=>S.index===l);if(!n)throw new Error("Invalid Pallet");let t={value:{name:n.name,idx:l},input:se(o[0])},u=r(n.calls),v=u(o);if(v.codec!=="Enum")throw null;let d={value:{name:v.value.type,idx:o[1]},input:se(o[1]),docs:v.docs};return{pallet:t,call:d,args:{value:v.value.value,shape:u.shape}}}),y=ae((o,l=!1)=>o.type==="primitive"?l?_e:R._void:o.type==="tuple"?C.Tuple(...Object.values(o.value).map(n=>r(n.id))):C.Struct((0,M.mapObject)(o.value,n=>r(n.id)))),h=o=>(l,n)=>{let t=e.pallets.find(d=>d.name===l),u=a(t[o]);if(u.type!=="enum")throw null;let v=u.value[n];return{location:[t.index,v.idx],view:y(v,o==="calls")}},b=(o,l)=>{let n=e.pallets.find(t=>t.name===o).constants.find(t=>t.name===l);return m(n.type)};return{buildDefinition:m,callDecoder:g,buildEvent:h("events"),buildError:h("errors"),buildCall:h("calls"),buildConstant:b}};
//# sourceMappingURL=index.js.map
{
"name": "@polkadot-api/metadata-builders",
"version": "0.0.1-56b4ad0a42cc57d8ef99401f196fcbe4691f8e41.1.0",
"version": "0.0.1-5c907acc11c645d42dfa0b29ff92fff89832d986.1.0",
"author": "Josep M Sobrepere (https://github.com/josepot)",

@@ -43,4 +43,4 @@ "repository": {

"dependencies": {
"@polkadot-api/substrate-bindings": "0.0.1-56b4ad0a42cc57d8ef99401f196fcbe4691f8e41.1.0",
"@polkadot-api/utils": "0.0.1-56b4ad0a42cc57d8ef99401f196fcbe4691f8e41.1.0"
"@polkadot-api/substrate-bindings": "0.0.1-5c907acc11c645d42dfa0b29ff92fff89832d986.1.0",
"@polkadot-api/utils": "0.0.1-5c907acc11c645d42dfa0b29ff92fff89832d986.1.0"
},

@@ -47,0 +47,0 @@ "scripts": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc