Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function types (): void {
// check correct types with `createType`
const balance = createType('Balance', 2);
const gas = createType('Gas', 2);
const compact = createType('Compact', 2);
// const random = createType('RandomType', 2); // This one should deliberately show a TS error
const gasUnsafe = createTypeUnsafe('Gas', [2]);
const overriddenUnsafe = createTypeUnsafe<header>('Gas', [2]);
console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe);
}
</header>
function extendLinkedMap (registry: Registry, { meta: { documentation, name, type } }: CreateItemFn, storageFn: StorageEntry, stringKey: string, hasher: HasherFunction): StorageEntry {
const headHash = new U8a(registry, hasher(`head of ${stringKey}`));
const headFn: any = (): U8a =>
headHash;
// metadata with a fallback value using the type of the key, the normal
// meta fallback only applies to actual entry values, create one for head
headFn.meta = new StorageEntryMetadata(registry, {
name,
modifier: createType(registry, 'StorageEntryModifierLatest', 1), // required
type: new StorageEntryType(registry, createType(registry, 'PlainTypeLatest', type.asMap.key), 0),
fallback: createType(registry, 'Bytes', createTypeUnsafe(registry, type.asMap.key.toString()).toHex()),
documentation
});
// here we pass the section/method through as well - these are not on
// the function itself, so specify these explicitly to the constructor
storageFn.headKey = createType(registry, 'StorageKey', headFn, {
method: storageFn.method,
section: `head of ${storageFn.section}`
});
return storageFn;
}
function toV4StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV3): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
// Convert the old type to the new type: there is one new field
// called `hasher`, which we initialize to xxHash (the default in
// v3).
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
? [createType(registry, 'MapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key: type.asMap.key,
value: type.asMap.value,
linked: type.asMap.linked
}), 1]
: [createType(registry, 'DoubleMapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: type.asDoubleMap.key2Hasher
}), 2];
return new StorageFunctionMetadata(registry, {
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(registry, newType, index)
});
}
const headFn: any = (): U8a =>
headHash;
// metadata with a fallback value using the type of the key, the normal
// meta fallback only applies to actual entry values, create one for head
headFn.meta = new StorageEntryMetadata(registry, {
name,
modifier: createType(registry, 'StorageEntryModifierLatest', 1), // required
type: new StorageEntryType(registry, createType(registry, 'PlainTypeLatest', type.asMap.key), 0),
fallback: createType(registry, 'Bytes', createTypeUnsafe(registry, type.asMap.key.toString()).toHex()),
documentation
});
// here we pass the section/method through as well - these are not on
// the function itself, so specify these explicitly to the constructor
storageFn.headKey = createType(registry, 'StorageKey', headFn, {
method: storageFn.method,
section: `head of ${storageFn.section}`
});
return storageFn;
}
return (registry: Registry): StorageEntry =>
createFunction(registry, {
meta: {
documentation: createType(registry, 'Vec
return (registry: Registry): StorageEntry =>
createFunction(registry, {
meta: {
documentation: createType(registry, 'Vec
function toV4StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV3): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
// Convert the old type to the new type: there is one new field
// called `hasher`, which we initialize to xxHash (the default in
// v3).
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
? [createType(registry, 'MapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key: type.asMap.key,
value: type.asMap.value,
linked: type.asMap.linked
}), 1]
: [createType(registry, 'DoubleMapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: type.asDoubleMap.key2Hasher
}), 2];
return new StorageFunctionMetadata(registry, {
documentation,
fallback,
function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV4): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
? [type.asMap, 1]
: [createType(registry, 'DoubleMapTypeV5', {
hasher: type.asDoubleMap.hasher,
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: toStorageHasher(registry, type.asDoubleMap.key2Hasher)
}), 2];
return new StorageFunctionMetadata(registry, {
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(registry, newType, index)
});
}