init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// 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 '@plugnet/types';
|
||||
import json from '@plugnet/types/Metadata/v0/static';
|
||||
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
// Use the pre-generated metadata
|
||||
const metadata = new Metadata(json).asV0;
|
||||
const newExtrinsics = fromMetadata(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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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 { RuntimeModuleMetadata } from '@plugnet/types/Metadata/v0/Modules';
|
||||
import { Methods, ModulesWithMethods } from '@plugnet/types/primitive/Method';
|
||||
import MetadataV0 from '@plugnet/types/Metadata/v0';
|
||||
|
||||
import { stringCamelCase } from '@plugnet/util';
|
||||
|
||||
import createUnchecked from './utils/createUnchecked';
|
||||
import extrinsics from '.';
|
||||
|
||||
/**
|
||||
* 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: MetadataV0): ModulesWithMethods {
|
||||
let indexCount = -1;
|
||||
|
||||
const findIndex = (prefix: string): number => {
|
||||
indexCount++;
|
||||
|
||||
const mod = metadata.calls.find((item) => item.prefix.toString() === prefix);
|
||||
|
||||
if (!mod) {
|
||||
console.error(`Unable to find module index for '${prefix}'`);
|
||||
|
||||
// compatible with old versions
|
||||
return indexCount;
|
||||
}
|
||||
|
||||
return mod.index.toNumber();
|
||||
};
|
||||
|
||||
return metadata.modules.reduce((result, meta: RuntimeModuleMetadata) => {
|
||||
if (!meta.module.call || !meta.module.call.functions.length) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const prefix = stringCamelCase(meta.prefix.toString());
|
||||
const index = findIndex(meta.prefix.toString());
|
||||
|
||||
result[prefix] = meta.module.call.functions.reduce((newModule, funcMeta) => {
|
||||
// extrinsics.balances.set_balance -> extrinsics.balances.setBalance
|
||||
const funcName = stringCamelCase(funcMeta.name.toString());
|
||||
|
||||
newModule[funcName] = createUnchecked(prefix, funcName, index, funcMeta);
|
||||
|
||||
return newModule;
|
||||
}, {} as Methods);
|
||||
|
||||
return result;
|
||||
}, { ...extrinsics });
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 testingPairs from '@plugnet/keyring/testingPairs';
|
||||
import { Extrinsic } from '@plugnet/types';
|
||||
|
||||
import extrinsics from './static';
|
||||
|
||||
const keyring = testingPairs({ type: 'ed25519' }, false);
|
||||
|
||||
describe('extrinsics', () => {
|
||||
it('encodes extrinsic correctly (nobody)', () => {
|
||||
expect(
|
||||
new Extrinsic(
|
||||
extrinsics.timestamp.set(10101)
|
||||
).sign(
|
||||
keyring.nobody,
|
||||
{
|
||||
blockHash: new Uint8Array(),
|
||||
nonce: 1234
|
||||
}
|
||||
).toU8a(true)
|
||||
).toEqual(new Uint8Array([
|
||||
129, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 19, 0, 0, 0, 213, 157
|
||||
]));
|
||||
});
|
||||
|
||||
it('encodes an actual transfer (actual data)', () => {
|
||||
expect(
|
||||
new Extrinsic(
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey(), 6969)
|
||||
).sign(keyring.alice, {
|
||||
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
|
||||
nonce: 0
|
||||
}).toU8a()
|
||||
).toEqual(new Uint8Array([
|
||||
37, 2, 129, 255, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79, 245, 228, 136, 9, 232, 31, 206, 95, 236, 20, 206, 189, 109, 251, 83, 198, 37, 79, 144, 90, 72, 33, 101, 250, 52, 0, 66, 80, 222, 78, 34, 124, 6, 101, 244, 44, 168, 225, 239, 153, 185, 58, 113, 221, 221, 136, 43, 237, 9, 6, 201, 17, 56, 110, 105, 78, 173, 109, 176, 100, 24, 78, 164, 15, 0, 0, 3, 0, 255, 215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185, 229, 108
|
||||
]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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 '@plugnet/types/primitive/Method';
|
||||
|
||||
const extrinsics: ModulesWithMethods = {};
|
||||
|
||||
export default extrinsics;
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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 { Metadata } from '@plugnet/types';
|
||||
import metadataRpc from '@plugnet/types/Metadata/v0/static';
|
||||
import { ModulesWithMethods } from '@plugnet/types/primitive/Method';
|
||||
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
const staticMetadata: ModulesWithMethods = fromMetadata(
|
||||
new Metadata(metadataRpc).asV0
|
||||
);
|
||||
export default staticMetadata;
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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 { FunctionMetadata } from '@plugnet/types/Metadata/v0/Modules';
|
||||
import { MethodFunction } from '@plugnet/types/primitive/Method';
|
||||
import { Method } from '@plugnet/types';
|
||||
import { assert } from '@plugnet/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,
|
||||
index: number,
|
||||
meta: FunctionMetadata
|
||||
): MethodFunction {
|
||||
const callIndex = new Uint8Array([index, meta.id.toNumber()]);
|
||||
let extrinsicFn: any;
|
||||
|
||||
const expectedArgs = Method.filterOrigin(meta);
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user