Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust known lookup with support for [u8;32] #5764

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions packages/types/src/metadata/decorate/storage/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { PortableType } from '../../../interfaces/index.js';
import type { StorageEntry } from '../../../primitive/types.js';
import type { Registry } from '../../../types/index.js';

import { getTypeDef } from '@polkadot/types-create';

import { createFunction } from './createFunction.js';

export interface ManualMetadata {
Expand All @@ -18,8 +20,8 @@ interface ManualDefinition {
section: string;
}

function findSiPrimitive (registry: Registry, _prim: string): PortableType | undefined {
const prim = _prim.toLowerCase();
function findSiPrimitive (registry: Registry, type: string): PortableType | undefined {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: type is a keyword in Typescript; I'd suggest using a different identifier

const prim = type.toLowerCase();

return registry.lookup.types.find((t) =>
(
Expand All @@ -32,27 +34,49 @@ function findSiPrimitive (registry: Registry, _prim: string): PortableType | und
);
}

function findSiType (registry: Registry, orig: string): PortableType | undefined {
let portable = findSiPrimitive(registry, orig);
function findSiType (registry: Registry, type: string): PortableType | undefined {
let portable = findSiPrimitive(registry, type);

if (!portable && orig === 'Bytes') {
// some types are either Sequence or Arrays, cater for these
// specifically (these all come from the base substrate known keys)
if (!portable && (type === 'Bytes' || type.startsWith('[u8;'))) {
const u8 = findSiPrimitive(registry, 'u8');

if (u8) {
portable = registry.lookup.types.find((t) =>
(
t.type.def.isSequence &&
t.type.def.asSequence.type.eq(u8.id)
) || (
t.type.def.isHistoricMetaCompat &&
t.type.def.asHistoricMetaCompat.eq(orig)
)
);
if (type === 'Bytes') {
portable = registry.lookup.types.find((t) =>
(
t.type.def.isSequence &&
t.type.def.asSequence.type.eq(u8.id)
) || (
t.type.def.isHistoricMetaCompat &&
t.type.def.asHistoricMetaCompat.eq(type)
)
);
} else {
const td = getTypeDef(type);

portable = registry.lookup.types.find((t) =>
(
t.type.def.isArray &&
t.type.def.asArray.eq({
len: td.length,
type: u8.id
})
) || (
t.type.def.isHistoricMetaCompat &&
t.type.def.asHistoricMetaCompat.eq(type)
)
);
}
}
}

if (!portable) {
console.warn(`Unable to map ${orig} to a lookup index`);
// Not fatal, however if this happens the storage key using this
// type will not return valid values, rather it will most probably
// be decoded incorrectly
console.warn(`Unable to map ${type} to a lookup index`);
}

return portable;
Expand Down