Revert "Update extrinsics FromMetadata. Drop storages with doublemap type when converting v3 to v2 (#888)" (#903)

This reverts commit 97032e7e86.
This commit is contained in:
Jaco Greeff
2019-05-14 16:21:32 +02:00
committed by GitHub
parent 5ac7841fd9
commit 57715c50c0
21 changed files with 56 additions and 313 deletions
@@ -5,11 +5,11 @@
import { Extrinsic, Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v0/static';
import fromV0 from '.';
import fromMetadata from './fromMetadata';
// Use the pre-generated metadata
const metadata = new Metadata(json).asV0;
const newExtrinsics = fromV0(metadata);
const newExtrinsics = fromMetadata(metadata);
describe('fromMetadata', () => {
it('should throw if an incorrect number of args is supplied', () => {
@@ -2,13 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV0 from '@polkadot/types/Metadata/v0';
import { RuntimeModuleMetadata } from '@polkadot/types/Metadata/v0/Modules';
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import MetadataV0 from '@polkadot/types/Metadata/v0';
import { stringCamelCase } from '@polkadot/util';
import extrinsics from '../../index';
import createUnchecked from './createUnchecked';
import createUnchecked from './utils/createUnchecked';
import extrinsics from '.';
/**
* Extend a storage object with the storage modules & module functions present
@@ -17,7 +18,7 @@ import createUnchecked from './createUnchecked';
* @param extrinsics - An extrinsics object to be extended.
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromV0 (metadata: MetadataV0): ModulesWithMethods {
export default function fromMetadata (metadata: MetadataV0): ModulesWithMethods {
let indexCount = -1;
const findIndex = (prefix: string): number => {
@@ -1,26 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ModulesWithMethods } from '@polkadot/types/primitive/Method';
import Metadata from '@polkadot/types/Metadata';
import fromV0 from './v0';
import fromV4 from './v4';
/**
* Extend a storage object with the storage modules & module functions present
* in the metadata.
*
* @param extrinsics - An extrinsics object to be extended.
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: Metadata): ModulesWithMethods {
if (metadata.version === 4) {
// For v4, we use fromV4 function
return fromV4(metadata.asV4);
}
// For v0-v3 we convert to v0, and use the fromV0 function to parse
return fromV0(metadata.asV0);
}
@@ -1,7 +0,0 @@
// Copyright 2017-2019 @polkadot/storage authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import fromMetadata from './fromMetadata';
export default fromMetadata;
@@ -1,44 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataCall } from '@polkadot/types/Metadata/v1/Calls';
import { MethodFunction } from '@polkadot/types/primitive/Method';
import { Method } from '@polkadot/types';
import { assert } from '@polkadot/util';
/**
* From the metadata of a function in the module's storage, generate the function
* that will return the an UncheckExtrinsic.
*
* @param index - Index of the module section in the modules array.
*/
export default function createDescriptor (
section: string,
method: string,
indexCount: number,
meta: MetadataCall,
index: number
): MethodFunction {
const callIndex = new Uint8Array([indexCount, index]);
const expectedArgs = Method.filterOrigin(meta);
const extrinsicFn = (...args: any[]): Method => {
assert(expectedArgs.length.valueOf() === args.length, `Extrinsic ${section}.${method} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`);
return new Method({
args,
callIndex
}, meta);
};
extrinsicFn.callIndex = callIndex;
extrinsicFn.meta = meta;
extrinsicFn.method = method;
extrinsicFn.section = section;
extrinsicFn.toJSON = (): any =>
meta.toJSON();
return extrinsicFn as MethodFunction;
}
@@ -1,39 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Extrinsic, Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v4/static';
import fromV4 from '.';
// Use the pre-generated metadata
const metadata = new Metadata(json).asV4;
const newExtrinsics = fromV4(metadata);
describe('fromMetadata', () => {
it('should throw if an incorrect number of args is supplied', () => {
expect(() => newExtrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/);
});
it('should return a value if the storage function does not expect an argument', () => {
expect(() => newExtrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', 2, 3)).not.toThrow();
});
it('should return properly-encoded transactions', () => {
expect(
new Extrinsic(newExtrinsics.timestamp.set([10101])).toU8a()
).toEqual(
new Uint8Array([
// length (encoded)
4 << 2,
// version, no signature
1,
// index
0, 0,
// values, Compact<Moment>
116
])
);
});
});
@@ -1,40 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataModule } from '@polkadot/types/Metadata/v4/Metadata';
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import MetadataV4 from '@polkadot/types/Metadata/v4';
import { stringCamelCase } from '@polkadot/util';
import extrinsics from '../../index';
import createUnchecked from './createUnchecked';
/**
* Extend a storage object with the storage modules & module functions present
* in the metadata.
*
* @param extrinsics - An extrinsics object to be extended.
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromV4 (metadata: MetadataV4): ModulesWithMethods {
let indexCount = -1;
return metadata.modules.reduce((result, module: MetadataModule) => {
if (module.calls.isNone || module.calls.isEmpty) {
return result;
}
indexCount++;
const prefix = stringCamelCase(module.prefix.toString());
result[prefix] = module.calls.unwrap().reduce((newModule, call, index) => {
const funcName = stringCamelCase(call.name.toString());
newModule[funcName] = createUnchecked(prefix, funcName, indexCount, call, index);
return newModule;
}, {} as Methods);
return result;
}, { ...extrinsics });
}
+1 -1
View File
@@ -9,6 +9,6 @@ import { ModulesWithMethods } from '@polkadot/types/primitive/Method';
import fromMetadata from './fromMetadata';
const staticMetadata: ModulesWithMethods = fromMetadata(
new Metadata(metadataRpc)
new Metadata(metadataRpc).asV0
);
export default staticMetadata;