UNPKG

1.14 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/api authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { assert, isUndefined } from '@polkadot/util';
4
5function sig({
6 lookup
7}, {
8 method,
9 section
10}, args) {
11 return `${section}.${method}(${args.map(a => lookup.getTypeDef(a).type).join(', ')})`;
12} // sets up the arguments in the form of [creator, args] ready to be used in a storage
13// call. Additionally, it verifies that the correct number of arguments have been passed
14
15
16export function extractStorageArgs(registry, creator, _args) {
17 const args = _args.filter(a => !isUndefined(a));
18
19 if (creator.meta.type.isPlain) {
20 assert(args.length === 0, () => `${sig(registry, creator, [])} does not take any arguments, ${args.length} found`);
21 } else {
22 const {
23 hashers,
24 key
25 } = creator.meta.type.asMap;
26 const keys = hashers.length === 1 ? [key] : registry.lookup.getSiType(key).def.asTuple.map(t => t);
27 assert(args.length === keys.length, () => `${sig(registry, creator, keys)} is a map, requiring ${keys.length} arguments, ${args.length} found`);
28 } // pass as tuple
29
30
31 return [creator, args];
32}
\No newline at end of file