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-4720098dad2c69dafd379d4eb3a609154080c24d.1.0 to 0.0.1-492c132563ea6b40ae1fc5470dec4cd18768d182.1.0

104

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, ResultPayload } 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[];

@@ -156,5 +170,2 @@ }[];

};
} | {
tag: "historicMetaCompat";
value: string;
};

@@ -165,3 +176,3 @@ docs: string[];

type MetadataPrimitives = "bool" | "char" | "str" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "i8" | "i16" | "i32" | "i64" | "i128" | "i256";
declare const primitiveTypes: Record<MetadataPrimitives | "_void" | "compactNumber" | "compactBn" | "bitSequence" | "historicMetaCompat", string>;
declare const primitiveTypes: Record<MetadataPrimitives | "_void" | "compactNumber" | "compactBn" | "bitSequence", string>;
interface Variable {

@@ -176,5 +187,6 @@ id: string;

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

@@ -187,9 +199,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>;

@@ -217,3 +234,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: [

@@ -223,3 +245,2 @@ number,

];
args: Codec<any>;
};

@@ -230,12 +251,22 @@ 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;
buildError: (pallet: string, name: string) => string | null;
buildConstant: (pallet: string, constantName: string) => string | null;
buildComposite: (input: VoidVar | TupleVar | StructVar) => 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) => {

@@ -246,2 +277,6 @@ shape: Shape;

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

@@ -325,2 +360,10 @@ interface DecodedCall {

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

@@ -339,3 +382,3 @@ codec: "Tuple";

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

@@ -345,3 +388,3 @@ };

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

@@ -365,2 +408,13 @@ interface SequenceShape {

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

@@ -370,3 +424,3 @@ codec: "Enum";

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

@@ -378,2 +432,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,22 @@ const values = {};

if (def.tag === "variant") {
if (path.length === 1 && path[0] === "Option" && params.length === 1 && params[0].name === "T") {
const value = getLookupEntryDef(params[0].type);
return value.type === "primitive" && value.value === "_void" ? (
// Option(_void) would return a Codec<undefined> which makes no sense
// Therefore, we better treat it as a bool
{ type: "primitive", value: "bool" }
) : {
type: "option",
value
};
}
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)

@@ -143,13 +169,13 @@ return voidVar;

if (def.tag === "sequence") {
const value2 = getLookupEntryDef(def.value);
const value = getLookupEntryDef(def.value);
return {
type: "sequence",
value: value2
value
};
}
if (def.tag === "array") {
const value2 = getLookupEntryDef(def.value.type);
const value = getLookupEntryDef(def.value.type);
return {
type: "array",
value: value2,
value,
len: def.value.len

@@ -163,7 +189,7 @@ };

return getLookupEntryDef(def.value[0]);
const value2 = def.value.map((x) => getLookupEntryDef(x));
const value = def.value.map((x) => getLookupEntryDef(x));
const innerDocs = def.value.map((x) => lookupData[x].docs);
return {
type: "tuple",
value: value2,
value,
innerDocs

@@ -186,9 +212,4 @@ };

}
if (def.tag === "bitSequence") {
return { type: "bitSequence" };
}
const value = def.value;
return {
type: "primitive",
value
type: def.tag
};

@@ -238,8 +259,6 @@ });

compactBn: "bigint",
bitSequence: "{bitsLen: number, bytes: Uint8Array}",
historicMetaCompat: "any"
bitSequence: "{bitsLen: number, bytes: Uint8Array}"
};
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 toCamelCase = (...parts) => parts.map((part) => part[0].toUpperCase() + part.slice(1)).join("");
var getTypes = (varName) => primitiveTypes[varName] ?? varName;
var _buildSyntax = (input, cache, stack, declarations, getVarName) => {

@@ -250,2 +269,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") {

@@ -319,15 +350,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");

@@ -352,3 +371,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]) => {

@@ -384,8 +432,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)

@@ -402,3 +454,3 @@ });

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

@@ -416,3 +468,4 @@ directDependencies: /* @__PURE__ */ new Set([nonCircular])

typeImports: /* @__PURE__ */ new Set(["Codec"]),
variables: /* @__PURE__ */ new Map()
variables: /* @__PURE__ */ new Map(),
enums: /* @__PURE__ */ new Map()
};

@@ -423,3 +476,14 @@ const lookupData = metadata.lookup;

const { path } = lookupData[idx];
const parts = path.length === 0 ? ["cdc" + idx] : ["c", ...path];
let parts;
if (path.length === 0)
parts = ["cdc" + idx];
else if (path[0] === "sp_runtime")
parts = [path.slice(-1)[0]];
else if (
// Junctions Junctions, etc
path.length > 2 && path.slice(-2)[0].toUpperCase() === path.slice(-2)[1].toUpperCase()
)
parts = path.slice(0, -1);
else
parts = [...path];
parts.push(...post);

@@ -481,10 +545,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, {

@@ -499,22 +572,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) => {

@@ -527,3 +580,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";
`;

@@ -536,8 +589,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 {

@@ -548,6 +606,8 @@ buildDefinition,

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

@@ -558,7 +618,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")

@@ -584,5 +646,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);

@@ -596,2 +657,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) => {

@@ -611,6 +679,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) => {

@@ -665,16 +732,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))
);
};

@@ -690,8 +751,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 {

@@ -702,2 +783,3 @@ buildDefinition,

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

@@ -713,38 +795,60 @@ buildConstant,

var encodeText = textEncoder.encode.bind(textEncoder);
var getChecksum = (values, shape) => {
const hasShape = typeof shape === "string";
const res = new Uint8Array((values.length + (hasShape ? 1 : 0)) * 8);
var getChecksum = (values) => {
const res = new Uint8Array(values.length * 8);
const dv = new DataView(res.buffer);
let offset = 0;
if (hasShape) {
dv.setBigUint64(offset, (0, import_substrate_bindings.h64)(encodeText(shape)));
offset += 8;
}
for (let i = 0; i < values.length; i++, offset += 8)
dv.setBigUint64(offset, values[i]);
for (let i = 0; i < values.length; i++)
dv.setBigUint64(i * 8, values[i]);
return (0, import_substrate_bindings.h64)(res);
};
var primitiveChecksums = {
_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
var getStringChecksum = (values) => getChecksum(values.map((v) => (0, import_substrate_bindings.h64)(encodeText(v))));
var shapeIds = {
primitive: 0n,
vector: 1n,
tuple: 2n,
struct: 3n,
option: 4n,
result: 5n,
enum: 6n
};
var bytesChecksum = 14n;
var runtimePrimitiveIds = {
undefined: 0n,
number: 1n,
string: 2n,
bigint: 3n,
boolean: 4n,
bitSequence: 5n,
// {bitsLen: number, bytes: Uint8Array}
byteSequence: 6n,
// Binary
accountId: 7n
// SS58String
};
var metadataPrimitiveIds = {
_void: runtimePrimitiveIds.undefined,
bool: runtimePrimitiveIds.boolean,
char: runtimePrimitiveIds.string,
str: runtimePrimitiveIds.string,
u8: runtimePrimitiveIds.number,
u16: runtimePrimitiveIds.number,
u32: runtimePrimitiveIds.number,
u64: runtimePrimitiveIds.bigint,
u128: runtimePrimitiveIds.bigint,
u256: runtimePrimitiveIds.bigint,
i8: runtimePrimitiveIds.number,
i16: runtimePrimitiveIds.number,
i32: runtimePrimitiveIds.number,
i64: runtimePrimitiveIds.bigint,
i128: runtimePrimitiveIds.bigint,
i256: runtimePrimitiveIds.bigint
};
var structLikeBuilder = (shapeId, input, innerChecksum) => {
const sortedEntries = Object.entries(input).sort(
([a], [b]) => a.localeCompare(b)
);
const keysChecksum = getStringChecksum(sortedEntries.map(([key]) => key));
const valuesChecksum = getChecksum(
sortedEntries.map(([, entry]) => innerChecksum(entry))
);
return getChecksum([shapeId, keysChecksum, valuesChecksum]);
};
var _buildChecksum = (input, cache, stack) => {

@@ -754,26 +858,27 @@ if (cache.has(input.id))

if (input.type === "primitive")
return primitiveChecksums[input.value];
return getChecksum([shapeIds.primitive, metadataPrimitiveIds[input.value]]);
if (input.type === "compact")
return primitiveChecksums[input.isBig ? "compactb" : "compacts"];
return getChecksum([
shapeIds.primitive,
runtimePrimitiveIds[input.isBig ? "bigint" : "number"]
]);
if (input.type === "bitSequence")
return primitiveChecksums.bitSequence;
return getChecksum([shapeIds.primitive, runtimePrimitiveIds.bitSequence]);
if (input.type === "sequence" && input.value.type === "primitive" && input.value.value === "u8") {
return bytesChecksum;
return getChecksum([shapeIds.primitive, runtimePrimitiveIds.byteSequence]);
}
if (input.type === "AccountId32") {
return getChecksum([shapeIds.primitive, runtimePrimitiveIds.accountId]);
}
const buildNextChecksum = (nextInput) => buildChecksum(nextInput, cache, stack);
const buildVector = (inner, len) => {
const innerChecksum = buildNextChecksum(inner);
return len ? getChecksum([innerChecksum, BigInt(len)], "Vector(,)") : getChecksum([innerChecksum], "Vector()");
};
const buildTuple = (value) => getChecksum(value.map(buildNextChecksum));
const buildStruct = (value) => {
return getChecksum(
Object.values(value).map(buildNextChecksum),
JSON.stringify(Object.keys(value))
);
};
if (input.type === "array")
return buildVector(input.value, input.len);
if (input.type === "sequence")
return buildVector(input.value);
if (input.type === "array") {
const innerChecksum = buildNextChecksum(input.value);
return getChecksum([shapeIds.vector, innerChecksum, BigInt(input.len)]);
}
if (input.type === "sequence") {
const innerChecksum = buildNextChecksum(input.value);
return getChecksum([shapeIds.vector, innerChecksum]);
}
const buildTuple = (entries) => getChecksum([shapeIds.tuple, ...entries.map(buildNextChecksum)]);
const buildStruct = (entries) => structLikeBuilder(shapeIds.struct, entries, buildNextChecksum);
if (input.type === "tuple")

@@ -783,10 +888,20 @@ return buildTuple(input.value);

return buildStruct(input.value);
const dependencies = Object.values(input.value).map((v) => {
if (v.type === "primitive")
return 0n;
return v.type === "tuple" ? buildTuple(v.value) : buildStruct(v.value);
if (input.type === "option")
return getChecksum([shapeIds.option, buildNextChecksum(input.value)]);
if (input.type === "result")
return getChecksum([
shapeIds.result,
buildNextChecksum(input.value.ok),
buildNextChecksum(input.value.ko)
]);
return structLikeBuilder(shapeIds.enum, input.value, (entry) => {
switch (entry.type) {
case "primitive":
return metadataPrimitiveIds._void;
case "tuple":
return buildTuple(entry.value);
case "struct":
return buildStruct(entry.value);
}
});
const keys = Object.keys(input.value);
keys.push("Enum");
return getChecksum(dependencies, JSON.stringify({ Enum: keys }));
};

@@ -807,6 +922,6 @@ var buildChecksum = withCache(

if (storageEntry.type.tag === "plain")
return getChecksum([buildDefinition(storageEntry.type.value)]);
return buildDefinition(storageEntry.type.value);
const { key, value } = storageEntry.type.value;
const val = buildDefinition(value);
const returnKey = storageEntry.type.value.hashers.length === 1 ? getChecksum([buildDefinition(key)]) : buildDefinition(key);
const returnKey = buildDefinition(key);
return getChecksum([val, returnKey]);

@@ -817,15 +932,15 @@ } catch (_) {

};
const buildEnumEntry = (entry) => {
if (entry.type === "primitive")
return 0n;
const values = Object.values(entry.value).map((l) => buildDefinition(l.id));
return entry.type === "tuple" ? getChecksum(values) : getChecksum(values, JSON.stringify(Object.keys(entry.value)));
};
const buildCall = (pallet, callName) => {
const buildRuntimeCall = (api, method) => {
try {
const palletEntry = metadata.pallets.find((x) => x.name === pallet);
const callsLookup = getLookupEntryDef(palletEntry.calls);
if (callsLookup.type !== "enum")
const entry = metadata.apis.find((x) => x.name === api)?.methods.find((x) => x.name === method);
if (!entry)
throw null;
return buildEnumEntry(callsLookup.value[callName]);
const argNamesChecksum = getStringChecksum(
entry.inputs.map((x) => x.name)
);
const argValuesChecksum = getChecksum(
entry.inputs.map((x) => buildDefinition(x.type))
);
const outputChecksum = buildDefinition(entry.output);
return getChecksum([argNamesChecksum, argValuesChecksum, outputChecksum]);
} catch (_) {

@@ -835,2 +950,17 @@ return null;

};
const buildComposite = (input) => {
if (input.type === "primitive")
return getChecksum([0n]);
if (input.type === "tuple") {
const values = Object.values(input.value).map(
(entry) => buildDefinition(entry.id)
);
return getChecksum([shapeIds.tuple, ...values]);
}
return structLikeBuilder(
shapeIds.struct,
input.value,
(entry) => buildDefinition(entry.id)
);
};
const buildVariant = (variantType) => (pallet, name) => {

@@ -844,3 +974,3 @@ try {

throw null;
return buildEnumEntry(callsLookup.value[name]);
return buildComposite(callsLookup.value[name]);
} catch (_) {

@@ -861,7 +991,9 @@ return null;

buildDefinition: toStringEnhancer(buildDefinition),
buildRuntimeCall: toStringEnhancer(buildRuntimeCall),
buildStorage: toStringEnhancer(buildStorage),
buildCall: toStringEnhancer(buildCall),
buildCall: toStringEnhancer(buildVariant("calls")),
buildEvent: toStringEnhancer(buildVariant("events")),
buildError: toStringEnhancer(buildVariant("errors")),
buildConstant: toStringEnhancer(buildConstant)
buildConstant: toStringEnhancer(buildConstant),
buildComposite: toStringEnhancer(buildComposite)
};

@@ -871,9 +1003,9 @@ };

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

@@ -952,9 +1084,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) => {

@@ -976,6 +1116,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) => {

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

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

@@ -1012,3 +1164,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",

@@ -1019,3 +1171,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);
}

@@ -1030,2 +1182,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) => {

@@ -1046,3 +1202,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 {

@@ -1083,9 +1239,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) => {

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

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

@@ -1122,4 +1272,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, ResultPayload } 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[];

@@ -156,5 +170,2 @@ }[];

};
} | {
tag: "historicMetaCompat";
value: string;
};

@@ -165,3 +176,3 @@ docs: string[];

type MetadataPrimitives = "bool" | "char" | "str" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "i8" | "i16" | "i32" | "i64" | "i128" | "i256";
declare const primitiveTypes: Record<MetadataPrimitives | "_void" | "compactNumber" | "compactBn" | "bitSequence" | "historicMetaCompat", string>;
declare const primitiveTypes: Record<MetadataPrimitives | "_void" | "compactNumber" | "compactBn" | "bitSequence", string>;
interface Variable {

@@ -176,5 +187,6 @@ id: string;

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

@@ -187,9 +199,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>;

@@ -217,3 +234,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: [

@@ -223,3 +245,2 @@ number,

];
args: Codec<any>;
};

@@ -230,12 +251,22 @@ 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;
buildError: (pallet: string, name: string) => string | null;
buildConstant: (pallet: string, constantName: string) => string | null;
buildComposite: (input: VoidVar | TupleVar | StructVar) => 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) => {

@@ -246,2 +277,6 @@ shape: Shape;

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

@@ -325,2 +360,10 @@ interface DecodedCall {

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

@@ -339,3 +382,3 @@ codec: "Tuple";

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

@@ -345,3 +388,3 @@ };

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

@@ -365,2 +408,13 @@ interface SequenceShape {

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

@@ -370,3 +424,3 @@ codec: "Enum";

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

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

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

"dependencies": {
"@polkadot-api/substrate-bindings": "0.0.1-4720098dad2c69dafd379d4eb3a609154080c24d.1.0",
"@polkadot-api/utils": "0.0.1-4720098dad2c69dafd379d4eb3a609154080c24d.1.0"
"@polkadot-api/utils": "0.0.1-492c132563ea6b40ae1fc5470dec4cd18768d182.1.0",
"@polkadot-api/substrate-bindings": "0.0.1-492c132563ea6b40ae1fc5470dec4cd18768d182.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