Fix some Codeclimate smells (#450)
* Fix some Codeclimate smells * Cleanup Metadata complexity * Cleanup uniqueTypes * Bump deps
This commit is contained in:
@@ -30,9 +30,9 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-storage#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.1.5",
|
||||
"@polkadot/keyring": "^0.33.9",
|
||||
"@polkadot/keyring": "^0.33.11",
|
||||
"@polkadot/types": "^0.33.5",
|
||||
"@polkadot/util": "^0.33.9",
|
||||
"@polkadot/util-crypto": "^0.33.9"
|
||||
"@polkadot/util": "^0.33.11",
|
||||
"@polkadot/util-crypto": "^0.33.11"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
import json from '@polkadot/types/Metadata.rpc';
|
||||
import json from '@polkadot/types/Metadata/static';
|
||||
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import metadataRpc from '@polkadot/types/Metadata.rpc';
|
||||
import metadataRpc from '@polkadot/types/Metadata/static';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { StorageFunctionMetadata, StorageFunctionModifier, StorageFunctionType } from '@polkadot/types/Metadata';
|
||||
import { StorageFunctionMetadata, StorageFunctionModifier, StorageFunctionType } from '@polkadot/types/Metadata/Modules';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
import Text from '@polkadot/types/Text';
|
||||
import Vector from '@polkadot/types/codec/Vector';
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ describe('createFunction', () => {
|
||||
createFunction(
|
||||
'Timestamp',
|
||||
'Now',
|
||||
{ type: {} }
|
||||
{ type: {} } as any
|
||||
)()
|
||||
).toEqual(
|
||||
Uint8Array.from([64, 14, 73, 68, 207, 217, 141, 111, 76, 195, 116, 209, 111, 90, 78, 63, 156]) // Length-prefixed
|
||||
@@ -22,7 +22,7 @@ describe('createFunction', () => {
|
||||
createFunction(
|
||||
'Substrate',
|
||||
':auth:len',
|
||||
{ type: {} },
|
||||
{ type: {} } as any,
|
||||
{
|
||||
isUnhashed: true,
|
||||
method: 'authorityCount'
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import Compact from '@polkadot/types/codec/Compact';
|
||||
import { createType } from '@polkadot/types/codec';
|
||||
import { StorageFunctionMetadata } from '@polkadot/types/Metadata';
|
||||
import { StorageFunctionMetadata } from '@polkadot/types/Metadata/Modules';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
import { Text } from '@polkadot/types/index';
|
||||
import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
@@ -25,12 +25,7 @@ export interface CreateItemOptions {
|
||||
* are not known at runtime (from state_getMetadata), they need to be supplied
|
||||
* by us manually at compile time.
|
||||
*/
|
||||
export default function createFunction (
|
||||
section: Text,
|
||||
method: Text,
|
||||
meta: StorageFunctionMetadata,
|
||||
options: CreateItemOptions = {}
|
||||
): StorageFunction {
|
||||
export default function createFunction (section: Text | string, method: Text | string, meta: StorageFunctionMetadata, options: CreateItemOptions = {}): StorageFunction {
|
||||
let storageFn: any;
|
||||
|
||||
// NOTE Here we assume everything in the 'Substrate' prefix is unhashed. (Despite not passing empty, i.e. '',
|
||||
@@ -45,28 +40,22 @@ export default function createFunction (
|
||||
// - storage.balances.freeBalance(address)
|
||||
// - storage.timestamp.blockPeriod()
|
||||
storageFn = (arg?: any): Uint8Array => {
|
||||
const key = stringToU8a(`${section.toString()} ${method.toString()}`);
|
||||
|
||||
if (!meta.type.isMap) {
|
||||
return Compact.addLengthPrefix(
|
||||
xxhashAsU8a(
|
||||
stringToU8a(`${section.toString()} ${method.toString()}`),
|
||||
128
|
||||
)
|
||||
xxhashAsU8a(key, 128)
|
||||
);
|
||||
}
|
||||
|
||||
assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);
|
||||
|
||||
const type = meta.type.asMap.key.toString(); // Argument type, as string
|
||||
const param = createType(type, arg).toU8a(true);
|
||||
|
||||
// StorageKey is a Bytes, so is length-prefixed
|
||||
return Compact.addLengthPrefix(
|
||||
xxhashAsU8a(
|
||||
u8aConcat(
|
||||
stringToU8a(`${section.toString()} ${method.toString()}`),
|
||||
createType(type, arg).toU8a(true)
|
||||
),
|
||||
128
|
||||
)
|
||||
xxhashAsU8a(u8aConcat(key, param), 128)
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -74,8 +63,7 @@ export default function createFunction (
|
||||
storageFn.meta = meta;
|
||||
storageFn.method = stringLowerFirst((options.method || method).toString());
|
||||
storageFn.section = stringLowerFirst(section.toString());
|
||||
storageFn.toJSON = (): any =>
|
||||
meta.toJSON();
|
||||
storageFn.toJSON = (): any => meta.toJSON();
|
||||
|
||||
return storageFn as StorageFunction;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user