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-f000825b6d9815197ad6ec30ca3d96ef36aa4e8c.1.0 to 0.0.1-f20759816d245c27d4255f0e3f7a086f0b114d90.1.0

101

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';

@@ -21,3 +21,6 @@ type VoidVar = {

};
type TerminalVar = PrimitiveVar | CompactVar | BitSequenceVar;
type AccountId32 = {
type: "AccountId32";
};
type TerminalVar = PrimitiveVar | CompactVar | BitSequenceVar | AccountId32;
type TupleVar = {

@@ -40,2 +43,13 @@ type: "tuple";

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

@@ -50,3 +64,3 @@ type: "sequence";

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

@@ -61,3 +75,3 @@ type LookupEntry = {

name: string;
type: number | void | undefined;
type: number | undefined;
}[];

@@ -67,5 +81,5 @@ def: {

value: {
name: string | void | undefined;
name: string | undefined;
type: number;
typeName: string | void | undefined;
typeName: string | undefined;
docs: string[];

@@ -78,5 +92,5 @@ }[];

fields: {
name: string | void | undefined;
name: string | undefined;
type: number;
typeName: string | void | undefined;
typeName: string | undefined;
docs: string[];

@@ -174,5 +188,6 @@ }[];

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;

@@ -185,9 +200,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>;

@@ -215,3 +235,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: [

@@ -221,3 +246,2 @@ number,

];
args: Codec<any>;
};

@@ -228,6 +252,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;

@@ -238,3 +263,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) => {

@@ -245,2 +278,6 @@ shape: Shape;

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

@@ -324,2 +361,13 @@ interface DecodedCall {

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

@@ -338,3 +386,3 @@ codec: "Tuple";

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

@@ -344,3 +392,3 @@ };

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

@@ -364,2 +412,13 @@ interface SequenceShape {

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

@@ -369,3 +428,3 @@ codec: "Enum";

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

@@ -377,2 +436,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 AccountId32, 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 };

@@ -75,9 +75,15 @@ "use strict";

};
let isAccountId32SearchOn = true;
const getLookupEntryDef = withCache2((id) => {
const { def } = lookupData[id];
const { def, path, params } = lookupData[id];
if (def.tag === "composite") {
if (def.value.length === 0)
return voidVar;
if (def.value.length === 1)
if (def.value.length === 1) {
if (isAccountId32SearchOn && path.join(",") === "sp_core,crypto,AccountId32") {
isAccountId32SearchOn = false;
return { type: "AccountId32" };
}
return getLookupEntryDef(def.value[0].type);
}
let allKey = true;

@@ -103,2 +109,17 @@ const values = {};

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)

@@ -239,4 +260,3 @@ return voidVar;

var toCamelCase = (...parts) => parts[0] + parts.slice(1).map((part) => part[0].toUpperCase() + part.slice(1)).join("");
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) => {

@@ -247,2 +267,14 @@ if (input.type === "primitive") {

}
if (input.type === "AccountId32") {
declarations.imports.add("AccountId");
const id = "_accountId";
declarations.variables.set(id, {
id,
value: `AccountId()`,
types: "SS58String",
directDependencies: /* @__PURE__ */ new Set()
});
declarations.typeImports.add("SS58String");
return id;
}
if (input.type === "compact") {

@@ -316,15 +348,3 @@ const importVal = input.isBig ? "compactBn" : "compactNumber";

if (input.type === "array") {
if (isBytes(input.value)) {
if (input.len === 32 && (input.id === 0 || input.id === 1)) {
declarations.imports.add("AccountId");
const id = "_accountId";
declarations.variables.set(id, {
id,
value: `AccountId()`,
types: "SS58String",
directDependencies: /* @__PURE__ */ new Set()
});
declarations.typeImports.add("SS58String");
return id;
}
if (input.value.type === "primitive" && input.value.value === "u8") {
declarations.imports.add("Bin");

@@ -349,3 +369,32 @@ declarations.variables.set(varId, {

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]) => {

@@ -381,8 +430,12 @@ if (value.type === "primitive") {

)}}${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)

@@ -399,3 +452,3 @@ });

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

@@ -409,7 +462,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()
};

@@ -420,3 +474,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);

@@ -478,10 +532,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, {

@@ -496,22 +559,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) => {

@@ -524,3 +567,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";
`;

@@ -533,8 +576,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 {

@@ -545,6 +593,8 @@ buildDefinition,

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

@@ -555,7 +605,9 @@ };

var scale = __toESM(require("@polkadot-api/substrate-bindings"));
var import_utils = require("@polkadot-api/utils");
var _bytes = scale.Bin();
var isBytes2 = (input) => input.type === "primitive" && input.value === "u8";
var _buildCodec = (input, cache, stack, _accountId) => {
if (input.type === "primitive")
return scale[input.value];
if (input.type === "AccountId32")
return _accountId;
if (input.type === "compact")

@@ -581,5 +633,4 @@ return scale.compact;

if (input.type === "array") {
if (isBytes2(input.value)) {
return input.len === 32 && (input.id === 0 || input.id === 1) ? _accountId : scale.Bin(input.len);
}
if (input.value.type === "primitive" && input.value.value === "u8")
return scale.Bin(input.len);
return buildVector(input.value, input.len);

@@ -593,2 +644,9 @@ }

return buildStruct(input.value);
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) => {

@@ -608,6 +666,5 @@ if (v.type === "primitive")

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) => {

@@ -662,16 +719,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))
);
};

@@ -687,8 +738,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 {

@@ -699,2 +770,3 @@ buildDefinition,

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

@@ -778,2 +850,12 @@ 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()"
);
if (input.type === "AccountId32") {
return getChecksum([primitiveChecksums.u8, 32n], "AccountId32");
}
const dependencies = Object.values(input.value).map((v) => {

@@ -811,2 +893,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) => {

@@ -818,13 +914,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) => {

@@ -854,4 +939,5 @@ try {

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

@@ -864,9 +950,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) => {

@@ -945,9 +1031,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) => {

@@ -969,6 +1063,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) => {

@@ -990,2 +1094,4 @@ const decoder = (0, import_substrate_bindings2.enhanceDecoder)(input, (x) => ({

return primitives[input.value];
if (input.type === "AccountId32")
return _accountId;
if (input.type === "compact")

@@ -1005,3 +1111,3 @@ return input.isBig ? primitives.compactBn : primitives.compactNumber;

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

@@ -1012,3 +1118,3 @@ innerDocs

if (input.value.type === "primitive" && input.value.value === "u8") {
return input.len === 32 && (input.id === 0 || input.id === 1) ? _accountId : primitives.BytesArray(input.len);
return primitives.BytesArray(input.len);
}

@@ -1023,2 +1129,6 @@ return buildVector(input.value, input.len);

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) => {

@@ -1039,3 +1149,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 {

@@ -1076,9 +1186,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) => {

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

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

@@ -1115,4 +1219,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';

@@ -21,3 +21,6 @@ type VoidVar = {

};
type TerminalVar = PrimitiveVar | CompactVar | BitSequenceVar;
type AccountId32 = {
type: "AccountId32";
};
type TerminalVar = PrimitiveVar | CompactVar | BitSequenceVar | AccountId32;
type TupleVar = {

@@ -40,2 +43,13 @@ type: "tuple";

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

@@ -50,3 +64,3 @@ type: "sequence";

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

@@ -61,3 +75,3 @@ type LookupEntry = {

name: string;
type: number | void | undefined;
type: number | undefined;
}[];

@@ -67,5 +81,5 @@ def: {

value: {
name: string | void | undefined;
name: string | undefined;
type: number;
typeName: string | void | undefined;
typeName: string | undefined;
docs: string[];

@@ -78,5 +92,5 @@ }[];

fields: {
name: string | void | undefined;
name: string | undefined;
type: number;
typeName: string | void | undefined;
typeName: string | undefined;
docs: string[];

@@ -174,5 +188,6 @@ }[];

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;

@@ -185,9 +200,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>;

@@ -215,3 +235,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: [

@@ -221,3 +246,2 @@ number,

];
args: Codec<any>;
};

@@ -228,6 +252,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;

@@ -238,3 +263,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) => {

@@ -245,2 +278,6 @@ shape: Shape;

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

@@ -324,2 +361,13 @@ interface DecodedCall {

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

@@ -338,3 +386,3 @@ codec: "Tuple";

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

@@ -344,3 +392,3 @@ };

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

@@ -364,2 +412,13 @@ interface SequenceShape {

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

@@ -369,3 +428,3 @@ codec: "Enum";

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

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

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

"dependencies": {
"@polkadot-api/substrate-bindings": "0.0.1-f000825b6d9815197ad6ec30ca3d96ef36aa4e8c.1.0",
"@polkadot-api/utils": "0.0.1-f000825b6d9815197ad6ec30ca3d96ef36aa4e8c.1.0"
"@polkadot-api/substrate-bindings": "0.0.1-f20759816d245c27d4255f0e3f7a086f0b114d90.1.0",
"@polkadot-api/utils": "0.0.1-f20759816d245c27d4255f0e3f7a086f0b114d90.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