Lazy decorate (part 6, API internals) (#4150)
* Lazy decorate (part 6, API internals) * Rework tx decoration * Adjust metadata decoration triggers * Reformat registry lazy decoration * storage
This commit is contained in:
@@ -59,21 +59,22 @@ function getVariantStringIdx ({ index }: SiVariant): string {
|
||||
function injectErrors (_: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError>>): void {
|
||||
const lazySection = ({ errors, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, errors.unwrap()),
|
||||
({ docs, fields, index, name }: SiVariant): RegistryError => ({
|
||||
args: getFieldArgs(lookup, fields),
|
||||
docs: docs.map(valueToString),
|
||||
fields,
|
||||
index: index.toNumber(),
|
||||
method: name.toString(),
|
||||
name: name.toString(),
|
||||
section
|
||||
}),
|
||||
getVariantStringIdx
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, errors.unwrap()),
|
||||
({ docs, fields, index, name }: SiVariant): RegistryError => ({
|
||||
args: getFieldArgs(lookup, fields),
|
||||
docs: docs.map(valueToString),
|
||||
fields,
|
||||
index: index.toNumber(),
|
||||
method: name.toString(),
|
||||
name: name.toString(),
|
||||
section
|
||||
}),
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
@@ -91,24 +92,25 @@ function injectErrors (_: Registry, { lookup, pallets }: MetadataLatest, version
|
||||
function injectEvents (registry: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, Constructor<GenericEventData>>>): void {
|
||||
const lazySection = ({ events, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, events.unwrap()),
|
||||
(variant: SiVariant): Constructor<GenericEventData> => {
|
||||
const meta = registry.createType('EventMetadataLatest', {
|
||||
...variant,
|
||||
args: getFieldArgs(lookup, variant.fields)
|
||||
});
|
||||
|
||||
return class extends GenericEventData {
|
||||
constructor (registry: Registry, value: Uint8Array) {
|
||||
super(registry, value, meta, section, variant.name.toString());
|
||||
}
|
||||
};
|
||||
},
|
||||
getVariantStringIdx
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, events.unwrap()),
|
||||
(variant: SiVariant): Constructor<GenericEventData> => {
|
||||
const meta = registry.createType('EventMetadataLatest', {
|
||||
...variant,
|
||||
args: getFieldArgs(lookup, variant.fields)
|
||||
});
|
||||
|
||||
return class extends GenericEventData {
|
||||
constructor (registry: Registry, value: Uint8Array) {
|
||||
super(registry, value, meta, section, variant.name.toString());
|
||||
}
|
||||
};
|
||||
},
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
@@ -126,14 +128,15 @@ function injectEvents (registry: Registry, { lookup, pallets }: MetadataLatest,
|
||||
function injectExtrinsics (registry: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction>>): void {
|
||||
const lazySection = ({ calls, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, calls.unwrap()),
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, index, section),
|
||||
getVariantStringIdx
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, calls.unwrap()),
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, index, section),
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
|
||||
@@ -24,17 +24,13 @@ export function decorateConstants (registry: Registry, { pallets }: MetadataLate
|
||||
const result: Constants = {};
|
||||
|
||||
const lazySection = ({ constants, name }: PalletMetadataLatest): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
constants,
|
||||
(constants: PalletConstantMetadataLatest[]) =>
|
||||
lazyMethods(
|
||||
constants,
|
||||
(constant: PalletConstantMetadataLatest) =>
|
||||
createConstantCodec(registry, constant),
|
||||
objectNameToCamel
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
constants,
|
||||
(constant: PalletConstantMetadataLatest) =>
|
||||
createConstantCodec(registry, constant),
|
||||
objectNameToCamel
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -42,17 +42,13 @@ export function decorateErrors (registry: Registry, { lookup, pallets }: Metadat
|
||||
const result: Errors = {};
|
||||
|
||||
const lazySection = ({ errors, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(errors.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createIsError(registry, lookup, variant, sectionIndex),
|
||||
objectNameToString
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(errors.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createIsError(registry, lookup, v, sectionIndex),
|
||||
objectNameToString
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -29,17 +29,13 @@ export function decorateEvents (registry: Registry, { lookup, pallets }: Metadat
|
||||
const result: Events = {};
|
||||
|
||||
const lazySection = ({ events, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(events.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createIsEvent(registry, lookup, variant, sectionIndex),
|
||||
objectNameToString
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(events.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createIsEvent(registry, lookup, v, sectionIndex),
|
||||
objectNameToString
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -48,17 +48,13 @@ export function decorateExtrinsics (registry: Registry, { lookup, pallets }: Met
|
||||
const lazySection = ({ calls, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
const sectionName = stringCamelCase(name);
|
||||
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(calls.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, sectionIndex, sectionName),
|
||||
objectNameToCamel
|
||||
),
|
||||
() => sectionName
|
||||
lazyMethod(result, sectionName, () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(calls.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createCallFunction(registry, lookup, v, sectionIndex, sectionName),
|
||||
objectNameToCamel
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -23,24 +23,20 @@ export function decorateStorage (registry: Registry, { pallets }: MetadataLatest
|
||||
const { items, prefix: _prefix } = storage.unwrap();
|
||||
const prefix = _prefix.toString();
|
||||
|
||||
lazyMethod(
|
||||
result,
|
||||
items,
|
||||
(items: StorageEntryMetadataLatest[]) =>
|
||||
lazyMethods(
|
||||
items,
|
||||
(meta: StorageEntryMetadataLatest) =>
|
||||
createFunction(registry, { meta, method: meta.name.toString(), prefix, section }, {}),
|
||||
objectNameFirstLower,
|
||||
{
|
||||
palletVersion: createRuntimeFunction(
|
||||
{ method: 'palletVersion', prefix, section },
|
||||
createKeyRaw(registry, { method: ':__STORAGE_VERSION__:', prefix: name.toString() }, [], [], []),
|
||||
{ docs: 'Returns the current pallet version from storage', type: 'u16' }
|
||||
)(registry)
|
||||
}
|
||||
),
|
||||
() => section
|
||||
lazyMethod(result, section, () =>
|
||||
lazyMethods(
|
||||
items,
|
||||
(meta: StorageEntryMetadataLatest) =>
|
||||
createFunction(registry, { meta, method: meta.name.toString(), prefix, section }, {}),
|
||||
objectNameFirstLower,
|
||||
{
|
||||
palletVersion: createRuntimeFunction(
|
||||
{ method: 'palletVersion', prefix, section },
|
||||
createKeyRaw(registry, { method: ':__STORAGE_VERSION__:', prefix: name.toString() }, [], [], []),
|
||||
{ docs: 'Returns the current pallet version from storage', type: 'u16' }
|
||||
)(registry)
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user