mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-10-21 11:16:21 +00:00
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/api authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { SiLookupTypeId } from '@pezkuwi/types/interfaces';
|
|
import type { StorageEntry } from '@pezkuwi/types/primitive/types';
|
|
import type { Registry } from '@pezkuwi/types/types';
|
|
|
|
import { isUndefined } from '@pezkuwi/util';
|
|
|
|
function sig ({ lookup }: Registry, { method, section }: StorageEntry, args: SiLookupTypeId[]): string {
|
|
return `${section}.${method}(${args.map((a) => lookup.getTypeDef(a).type).join(', ')})`;
|
|
}
|
|
|
|
// sets up the arguments in the form of [creator, args] ready to be used in a storage
|
|
// call. Additionally, it verifies that the correct number of arguments have been passed
|
|
export function extractStorageArgs (registry: Registry, creator: StorageEntry, _args: unknown[]): [StorageEntry, unknown[]] {
|
|
const args = _args.filter((a) => !isUndefined(a));
|
|
|
|
if (creator.meta.type.isPlain) {
|
|
if (args.length !== 0) {
|
|
throw new Error(`${sig(registry, creator, [])} does not take any arguments, ${args.length} found`);
|
|
}
|
|
} else {
|
|
const { hashers, key } = creator.meta.type.asMap;
|
|
const keys = hashers.length === 1
|
|
? [key]
|
|
: registry.lookup.getSiType(key).def.asTuple.map((t) => t);
|
|
|
|
if (args.length !== keys.length) {
|
|
throw new Error(`${sig(registry, creator, keys)} is a map, requiring ${keys.length} arguments, ${args.length} found`);
|
|
}
|
|
}
|
|
|
|
// pass as tuple
|
|
return [creator, args];
|
|
}
|