Method -> Call, expose findMethod on api (#1222)

* Rename Method -> Call (interface, align with Rust)

* Export Call types from types, expose findCall
This commit is contained in:
Jaco Greeff
2019-07-29 09:04:37 +02:00
committed by GitHub
parent a06db4cfb9
commit fe35527a65
37 changed files with 164 additions and 161 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
import { createTypeUnsafe } from '../../codec/createType';
import Metadata from '../Metadata';
import Method from '../../primitive/Generic/Method';
import Call from '../../primitive/Generic/Call';
import { MetadataInterface } from '../types';
import { Codec } from '../../types';
@@ -54,7 +54,7 @@ export function defaultValues (rpcData: string): void {
describe('storage with default values', (): void => {
const metadata = new Metadata(rpcData);
Method.injectMethods(extrinsicsFromMeta(metadata));
Call.injectMethods(extrinsicsFromMeta(metadata));
metadata.asV7.modules
.filter(({ storage }): boolean => storage.isSome)
+3 -3
View File
@@ -11,7 +11,7 @@ import { CodecTo } from '../types';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { ClassOf } from '../codec/createType';
import Method from '../primitive/Generic/Method';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import Tuple from './Tuple';
@@ -56,7 +56,7 @@ describe('Tuple', (): void => {
});
it.skip('creates properly via actual hex string', (): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
const test = new (Tuple.with([
ClassOf('BlockNumber'), ClassOf('Proposal'), ClassOf('VoteThreshold')
@@ -64,7 +64,7 @@ describe('Tuple', (): void => {
))('0x62190000000000000003507b0a092230783432223a202230783433220a7d0a01');
expect((test[0] as BlockNumber).toNumber()).toEqual(6498);
expect((test[1] as Method).callIndex).toEqual(new Uint8Array([0, 3]));
expect((test[1] as Call).callIndex).toEqual(new Uint8Array([0, 3]));
expect((test[2] as VoteThreshold).toNumber()).toEqual(1);
});
+2 -2
View File
@@ -8,7 +8,7 @@ import { Codec, CodecTo } from '../types';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import AccountId from '../primitive/Generic/AccountId';
import Method from '../primitive/Generic/Method';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
import { createTypeUnsafe } from './createType';
import Vec from './Vec';
@@ -20,7 +20,7 @@ describe('Vec', (): void => {
beforeEach((): void => {
vector = new Vec(Text, ['1', '23', '345', '4567', new Text('56789')]);
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
it('wraps a sequence of values', (): void => {
+2 -2
View File
@@ -7,7 +7,7 @@ import './injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { createTypeUnsafe } from './codec/createType';
import GenericMethod from './primitive/Generic/Method';
import GenericCall from './primitive/Generic/Call';
import { Codec } from './types';
import * as exported from './index.types';
import * as definitions from './interfaces/definitions';
@@ -36,7 +36,7 @@ function testTypes (type: string, typeNames: string[]): void {
});
describe(`${type}:: default creation (empty bytes)`, (): void => {
GenericMethod.injectMethods(extrinsics);
GenericCall.injectMethods(extrinsics);
typeNames.forEach((name): void => {
it(`creates an empty ${name} (from empty bytes)`, (): void => {
+4 -4
View File
@@ -2,7 +2,7 @@
import { Compact, Option, Vec } from './codec';
import { Bytes, Data, Fixed64, H160, H256, H512, Null, StorageHasher, StorageKey, Text, Type, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive';
import { AccountId, AccountIdOf, AccountIndex, Address, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Method, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
import { AccountId, AccountIdOf, AccountIndex, Address, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
import { AuthorityId } from './interfaces/consensus';
import { Keys, SessionIndex } from './interfaces/session';
import { AuthorityWeight, NextAuthority, PendingPause, PendingResume, StoredPendingChange, StoredState } from './interfaces/grandpa';
@@ -182,9 +182,9 @@ export interface InterfaceRegistry {
Sr25519Signature: Sr25519Signature;
'Option<Sr25519Signature>': Option<Sr25519Signature>;
'Vec<Sr25519Signature>': Vec<Sr25519Signature>;
Method: Method;
'Option<Method>': Option<Method>;
'Vec<Method>': Vec<Method>;
Call: Call;
'Option<Call>': Option<Call>;
'Vec<Call>': Vec<Call>;
Origin: Origin;
'Option<Origin>': Option<Origin>;
'Vec<Origin>': Vec<Origin>;
@@ -21,7 +21,7 @@ export default {
]
},
PropIndex: 'u32',
Proposal: 'Method',
Proposal: 'Call',
ReferendumIndex: 'u32',
ReferendumInfo: {
end: 'BlockNumber',
@@ -2,7 +2,7 @@
import { Enum, Struct } from '../../codec';
import { u32 } from '../../primitive';
import { BlockNumber, Method } from '../runtime';
import { BlockNumber, Call } from '../runtime';
import { VoteThreshold } from '../elections';
/** Enum */
@@ -24,8 +24,8 @@ export interface Conviction extends Enum {
/** u32 */
export type PropIndex = u32;
/** Method */
export type Proposal = Method;
/** Call */
export type Proposal = Call;
/** u32 */
export type ReferendumIndex = u32;
@@ -7,7 +7,7 @@ import '../../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import createType from '../../codec/createType';
import Method from '../../primitive/Generic/Method';
import Call from '../../primitive/Generic/Call';
import events from '../../json/SignedBlock.002.json';
import immortalTxs from '../../json/SignedBlock.004.immortal.json';
import mortalTxs from '../../json/SignedBlock.004.mortal.json';
@@ -15,7 +15,7 @@ import knownMehods from '../../json/SignedBlock.005.json';
describe('SignedBlock', (): void => {
beforeEach((): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
it('decodes a full block', (): void => {
@@ -33,7 +33,7 @@ export default {
Sr25519Signature: 'Signature',
// extrinsic definition
Method: 'GenericMethod',
Call: 'GenericCall',
Origin: 'GenericOrigin',
ImmortalEra: 'GenericImmortalEra',
MortalEra: 'GenericMortalEra',
@@ -2,7 +2,7 @@
import { Codec } from '../../types';
import { Compact, Struct, Vec } from '../../codec';
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericImmortalEra, GenericMethod, GenericMortalEra, GenericOrigin, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive';
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericImmortalEra, GenericMortalEra, GenericOrigin, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive';
/** GenericAccountId */
export type AccountId = GenericAccountId;
@@ -28,6 +28,9 @@ export type Block = GenericBlock;
/** u64 */
export type BlockNumber = u64;
/** GenericCall */
export type Call = GenericCall;
/** [ConsensusEngineId, Bytes] & Codec */
export type Consensus = [ConsensusEngineId, Bytes] & Codec;
@@ -87,9 +90,6 @@ export type KeyValue = [StorageKey, StorageData] & Codec;
/** Vec<u8> */
export type LockIdentifier = Vec<u8>;
/** GenericMethod */
export type Method = GenericMethod;
/** u64 */
export type Moment = u64;
@@ -6,12 +6,12 @@ import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { hexToU8a } from '@polkadot/util';
import Method from '../Generic/Method';
import Call from '../Generic/Call';
import Extrinsic from './Extrinsic';
describe('Extrinsic', (): void => {
beforeAll((): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
describe('V1', (): void => {
@@ -11,7 +11,7 @@ import Base from '../../codec/Base';
import Compact from '../../codec/Compact';
import { FunctionMetadata } from '../../Metadata/v7/Calls';
import Address from '../Generic/Address';
import Method from '../Generic/Method';
import Call from '../Generic/Call';
import ExtrinsicV1, { ExtrinsicValueV1 } from './v1/Extrinsic';
import ExtrinsicV2, { ExtrinsicValueV2 } from './v2/Extrinsic';
import ExtrinsicEra from './ExtrinsicEra';
@@ -36,7 +36,7 @@ interface ExtrinsicOptions {
* - left as is, to create an inherent
*/
export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implements IExtrinsic {
public constructor (value: Extrinsic | ExtrinsicValue | AnyU8a | Method | undefined, { version }: ExtrinsicOptions = {}) {
public constructor (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: ExtrinsicOptions = {}) {
super(Extrinsic.decodeExtrinsic(value, version));
}
@@ -55,7 +55,7 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
}
}
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Method | undefined, version: number = DEFAULT_VERSION): ExtrinsicV1 | ExtrinsicV2 {
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicV1 | ExtrinsicV2 {
if (Array.isArray(value) || isHex(value)) {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
@@ -83,7 +83,7 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`);
return Extrinsic.decodeU8a(value.subarray(offset, total));
} else if (value instanceof Method) {
} else if (value instanceof Call) {
return Extrinsic.newFromValue({ method: value }, version);
}
@@ -95,28 +95,28 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
}
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Method]]
* @description The arguments passed to for the call, exposes args so it is compatible with [[Call]]
*/
public get args (): Codec[] {
return this.method.args;
}
/**
* @description Thge argument defintions, compatible with [[Method]]
* @description Thge argument defintions, compatible with [[Call]]
*/
public get argsDef (): ArgsDef {
return this.method.argsDef;
}
/**
* @description The actual `[sectionIndex, methodIndex]` as used in the Method
* @description The actual `[sectionIndex, methodIndex]` as used in the Call
*/
public get callIndex (): Uint8Array {
return this.method.callIndex;
}
/**
* @description The actual data for the Method
* @description The actual data for the Call
*/
public get data (): Uint8Array {
return this.method.data;
@@ -137,7 +137,7 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
}
/**
* @description `true` is method has `Origin` argument (compatibility with [[Method]])
* @description `true` is method has `Origin` argument (compatibility with [Call])
*/
public get hasOrigin (): boolean {
return this.method.hasOrigin;
@@ -165,9 +165,9 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
}
/**
* @description The [[Method]] this extrinsic wraps
* @description The [[Call]] this extrinsic wraps
*/
public get method (): Method {
public get method (): Call {
return this.raw.method;
}
@@ -7,12 +7,12 @@ import { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions }
import { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Generic/Method';
import Call from '../../Generic/Call';
import Address from '../../Generic/Address';
import ExtrinsicSignature from './ExtrinsicSignature';
export interface ExtrinsicValueV1 {
method?: Method;
method?: Call;
signature?: ExtrinsicSignature;
}
@@ -31,7 +31,7 @@ export default class ExtrinsicV1 extends Struct implements IExtrinsicImpl {
public constructor (value?: Uint8Array | ExtrinsicValueV1, { isSigned }: ExtrinsicV1Options = {}) {
super({
signature: ExtrinsicSignature,
method: Method
method: Call
}, ExtrinsicV1.decodeExtrinsic(value, isSigned));
}
@@ -43,7 +43,7 @@ export default class ExtrinsicV1 extends Struct implements IExtrinsicImpl {
} else if (isU8a(value)) {
// here we decode manually since we need to pull through the version information
const signature = new ExtrinsicSignature(value, { isSigned });
const method = new Method(value.subarray(signature.encodedLength));
const method = new Call(value.subarray(signature.encodedLength));
return {
method,
@@ -62,10 +62,10 @@ export default class ExtrinsicV1 extends Struct implements IExtrinsicImpl {
}
/**
* @description The [[Method]] this extrinsic wraps
* @description The [[Call]] this extrinsic wraps
*/
public get method (): Method {
return this.get('method') as Method;
public get method (): Call {
return this.get('method') as Call;
}
/**
@@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti
import createType, { ClassOf } from '../../../codec/createType';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import Method from '../../Generic/Method';
import Call from '../../Generic/Call';
import ExtrinsicEra from '../ExtrinsicEra';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
import ExtrinsicPayload from './ExtrinsicPayload';
@@ -123,7 +123,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
/**
* @description Generate a payload and pplies the signature from a keypair
*/
public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce }: SignatureOptions): IExtrinsicSignature {
public sign (method: Call, account: IKeyringPair, { blockHash, era, nonce }: SignatureOptions): IExtrinsicSignature {
const signer = createType('Address', account.publicKey);
const payload = new ExtrinsicPayload({
nonce,
@@ -5,14 +5,14 @@
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Method from '../../Generic/Method';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV2', (): void => {
beforeEach((): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
it('constructs a sane Uint8Array (default)', (): void => {
@@ -7,14 +7,14 @@ import { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions }
import { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Generic/Method';
import Call from '../../Generic/Call';
import Address from '../../Generic/Address';
import ExtrinsicSignature from './ExtrinsicSignature';
const TRANSACTION_VERSION = 2;
export interface ExtrinsicValueV2 {
method?: Method;
method?: Call;
signature?: ExtrinsicSignature;
}
@@ -28,24 +28,24 @@ interface ExtrinsicV2Options {
* The second generation of compact extrinsics
*/
export default class ExtrinsicV2 extends Struct implements IExtrinsicImpl {
public constructor (value?: Uint8Array | ExtrinsicValueV2 | Method, { isSigned }: ExtrinsicV2Options = {}) {
public constructor (value?: Uint8Array | ExtrinsicValueV2 | Call, { isSigned }: ExtrinsicV2Options = {}) {
super({
signature: ExtrinsicSignature,
method: Method
method: Call
}, ExtrinsicV2.decodeExtrinsic(value, isSigned));
}
public static decodeExtrinsic (value?: Method | Uint8Array | ExtrinsicValueV2, isSigned: boolean = false): ExtrinsicValueV2 {
public static decodeExtrinsic (value?: Call | Uint8Array | ExtrinsicValueV2, isSigned: boolean = false): ExtrinsicValueV2 {
if (!value) {
return {};
} else if (value instanceof ExtrinsicV2) {
return value;
} else if (value instanceof Method) {
} else if (value instanceof Call) {
return { method: value };
} else if (isU8a(value)) {
// here we decode manually since we need to pull through the version information
const signature = new ExtrinsicSignature(value, { isSigned });
const method = new Method(value.subarray(signature.encodedLength));
const method = new Call(value.subarray(signature.encodedLength));
return {
method,
@@ -64,10 +64,10 @@ export default class ExtrinsicV2 extends Struct implements IExtrinsicImpl {
}
/**
* @description The [[Method]] this extrinsic wraps
* @description The [[Call]] this extrinsic wraps
*/
public get method (): Method {
return this.get('method') as Method;
public get method (): Call {
return this.get('method') as Call;
}
/**
@@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti
import createType, { ClassOf } from '../../../codec/createType';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import Method from '../../Generic/Method';
import Call from '../../Generic/Call';
import ExtrinsicEra from '../ExtrinsicEra';
import ExtrinsicPayload from './ExtrinsicPayload';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
@@ -127,7 +127,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
/**
* @description Generate a payload and pplies the signature from a keypair
*/
public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce, tip }: SignatureOptions): IExtrinsicSignature {
public sign (method: Call, account: IKeyringPair, { blockHash, era, nonce, tip }: SignatureOptions): IExtrinsicSignature {
const signer = createType('Address', account.publicKey);
const payload = new ExtrinsicPayload({
blockHash,
@@ -7,12 +7,12 @@ import '../../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import block00300 from '../../json/SignedBlock.003.00.json';
import Method from './Method';
import Call from './Call';
import Block from './Block';
describe('Block', (): void => {
beforeEach((): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
it('has a valid toRawType', (): void => {
@@ -4,16 +4,16 @@
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Method from './Method';
import Call from './Call';
describe('Method', (): void => {
describe('Call', (): void => {
beforeEach((): void => {
Method.injectMethods(extrinsics);
Call.injectMethods(extrinsics);
});
it('handles decoding correctly (bare)', (): void => {
expect(
new Method({
new Call({
args: [],
callIndex: [5, 1] // balances.setBalance
}).toU8a()
@@ -22,7 +22,7 @@ describe('Method', (): void => {
it('handles creation from a hex value properly', (): void => {
expect(
new Method('0x0501').toU8a()
new Call('0x0501').toU8a()
).toEqual(new Uint8Array([5, 1, 0, 0, 0])); // balances.setBalance
});
@@ -34,19 +34,19 @@ describe('Method', (): void => {
it('is false with no arguments', (): void => {
expect(
new Method(test, { args: [] } as any).hasOrigin
new Call(test, { args: [] } as any).hasOrigin
).toEqual(false);
});
it('is false with first argument as non-Origin', (): void => {
expect(
new Method(test, { args: [{ name: 'a', type: 'u32' }] } as any).hasOrigin
new Call(test, { args: [{ name: 'a', type: 'u32' }] } as any).hasOrigin
).toEqual(false);
});
it('is false with first argument as non-Origin', (): void => {
expect(
new Method(test, { args: [{ name: 'a', type: 'Origin' }] } as any).hasOrigin
new Call(test, { args: [{ name: 'a', type: 'Origin' }] } as any).hasOrigin
).toEqual(true);
});
});
@@ -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 { AnyU8a, ArgsDef, Codec, IMethod } from '../../types';
import { AnyU8a, ArgsDef, CallFunction, Codec, IMethod, ModulesWithCalls } from '../../types';
import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util';
@@ -13,7 +13,7 @@ import { FunctionMetadata as FunctionMetadataV7, FunctionArgumentMetadata } from
interface DecodeMethodInput {
args: any;
callIndex: MethodIndex | Uint8Array;
callIndex: CallIndex | Uint8Array;
}
interface DecodedMethod extends DecodeMethodInput {
@@ -21,55 +21,38 @@ interface DecodedMethod extends DecodeMethodInput {
meta: FunctionMetadataV7;
}
export interface MethodFunction {
(...args: any[]): Method;
callIndex: Uint8Array;
meta: FunctionMetadataV7;
method: string;
section: string;
toJSON: () => any;
}
export interface Methods {
[key: string]: MethodFunction;
}
export interface ModulesWithMethods {
[key: string]: Methods; // Will hold modules returned by state_getMetadata
}
const FN_UNKNOWN: Partial<MethodFunction> = {
const FN_UNKNOWN: Partial<CallFunction> = {
method: 'unknown',
section: 'unknown'
};
const injected: Record<string, MethodFunction> = {};
const injected: Record<string, CallFunction> = {};
/**
* @name MethodIndex
* @name CallIndex
* @description
* A wrapper around the `[sectionIndex, methodIndex]` value that uniquely identifies a method
*/
export class MethodIndex extends U8aFixed {
export class CallIndex extends U8aFixed {
public constructor (value?: AnyU8a) {
super(value, 16);
}
}
/**
* @name Method
* @name Call
* @description
* Extrinsic function descriptor, as defined in
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*/
export default class Method extends Struct implements IMethod {
export default class Call extends Struct implements IMethod {
protected _meta: FunctionMetadataV7;
public constructor (value: any, meta?: FunctionMetadataV7) {
const decoded = Method.decodeMethod(value, meta);
const decoded = Call.decodeCall(value, meta);
super({
callIndex: MethodIndex,
callIndex: CallIndex,
args: Struct.with(decoded.argsDef)
}, decoded);
@@ -86,19 +69,19 @@ export default class Method extends Struct implements IMethod {
* @param _meta - Metadata to use, so that `injectMethods` lookup is not
* necessary.
*/
private static decodeMethod (value: DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataV7): DecodedMethod {
private static decodeCall (value: DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataV7): DecodedMethod {
if (isHex(value)) {
return Method.decodeMethod(hexToU8a(value), _meta);
return Call.decodeCall(hexToU8a(value), _meta);
} else if (isU8a(value)) {
// The first 2 bytes are the callIndex
const callIndex = value.subarray(0, 2);
// Find metadata with callIndex
const meta = _meta || Method.findFunction(callIndex).meta;
const meta = _meta || Call.findFunction(callIndex).meta;
return {
args: value.subarray(2),
argsDef: Method.getArgsDef(meta),
argsDef: Call.getArgsDef(meta),
callIndex,
meta
};
@@ -107,22 +90,22 @@ export default class Method extends Struct implements IMethod {
const { args, callIndex } = value;
// Get the correct lookupIndex
const lookupIndex = callIndex instanceof MethodIndex
const lookupIndex = callIndex instanceof CallIndex
? callIndex.toU8a()
: callIndex;
// Find metadata with callIndex
const meta = _meta || Method.findFunction(lookupIndex).meta;
const meta = _meta || Call.findFunction(lookupIndex).meta;
return {
args,
argsDef: Method.getArgsDef(meta),
argsDef: Call.getArgsDef(meta),
meta,
callIndex
};
}
throw new Error(`Method: Cannot decode value '${value}' of type ${typeof value}`);
throw new Error(`Call: Cannot decode value '${value}' of type ${typeof value}`);
}
// If the extrinsic function has an argument of type `Origin`, we ignore it
@@ -142,8 +125,8 @@ export default class Method extends Struct implements IMethod {
//
// As a convenience helper though, we return the full constructor function,
// which includes the meta, name, section & actual interface for calling
public static findFunction (callIndex: Uint8Array): MethodFunction {
assert(Object.keys(injected).length > 0, 'Calling Method.findFunction before extrinsics have been injected.');
public static findFunction (callIndex: Uint8Array): CallFunction {
assert(Object.keys(injected).length > 0, 'Calling Call.findFunction before extrinsics have been injected.');
return injected[callIndex.toString()] || FN_UNKNOWN;
}
@@ -155,7 +138,7 @@ export default class Method extends Struct implements IMethod {
* @param meta - The function metadata used to get the definition.
*/
private static getArgsDef (meta: FunctionMetadataV7): ArgsDef {
return Method.filterOrigin(meta).reduce((result, { name, type }): ArgsDef => {
return Call.filterOrigin(meta).reduce((result, { name, type }): ArgsDef => {
const Type = getTypeClass(
getTypeDef(type.toString())
);
@@ -167,7 +150,7 @@ export default class Method extends Struct implements IMethod {
// This is called/injected by the API on init, allowing a snapshot of
// the available system extrinsics to be used in lookups
public static injectMethods (moduleMethods: ModulesWithMethods): void {
public static injectMethods (moduleMethods: ModulesWithCalls): void {
Object.values(moduleMethods).forEach((methods): void =>
Object.values(methods).forEach((method): void => {
injected[method.callIndex.toString()] = method;
@@ -187,14 +170,14 @@ export default class Method extends Struct implements IMethod {
* @description Thge argument defintions
*/
public get argsDef (): ArgsDef {
return Method.getArgsDef(this.meta);
return Call.getArgsDef(this.meta);
}
/**
* @description The encoded `[sectionIndex, methodIndex]` identifier
*/
public get callIndex (): Uint8Array {
return (this.get('callIndex') as MethodIndex).toU8a();
return (this.get('callIndex') as CallIndex).toU8a();
}
/**
@@ -224,14 +207,14 @@ export default class Method extends Struct implements IMethod {
* @description Returns the name of the method
*/
public get methodName (): string {
return Method.findFunction(this.callIndex).method;
return Call.findFunction(this.callIndex).method;
}
/**
* @description Returns the module containing the method
*/
public get sectionName (): string {
return Method.findFunction(this.callIndex).section;
return Call.findFunction(this.callIndex).section;
}
/**
@@ -6,9 +6,9 @@ export { default as GenericAccountId } from './AccountId';
export { default as GenericAccountIndex } from './AccountIndex';
export { default as GenericAddress } from './Address';
export { default as GenericBlock } from './Block';
export { default as GenericCall } from './Call';
export { default as GenericConsensusEngineId } from './ConsensusEngineId';
export { default as GenericEvent } from './Event';
export { default as GenericDigest, DigestItem as GenericDigestItem } from './Digest';
export { default as GenericMethod } from './Method';
export { default as GenericOrigin } from './Origin';
export { default as GenericVote } from './Vote';
+2 -2
View File
@@ -8,7 +8,7 @@ import fs from 'fs';
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
import interfaces from '../../../type-jsonrpc/src';
import Method from '../primitive/Generic/Method';
import Call from '../primitive/Generic/Call';
import Metadata from '../Metadata';
import rpcdata from '../Metadata/static';
import MetadataV7, { ModuleMetadataV7 } from '../Metadata/v7';
@@ -150,7 +150,7 @@ function addExtrinsics (metadata: MetadataV7): string {
return orderedMethods.reduce((md, func): string => {
const methodName = stringCamelCase(func.name.toString());
const args = Method.filterOrigin(func).map(({ name, type }): string => `${name}: ` + '`' + type + '`').join(', ');
const args = Call.filterOrigin(func).map(({ name, type }): string => `${name}: ` + '`' + type + '`').join(', ');
const doc = func.documentation.reduce((md, doc): string => `${md} ${doc}`, '');
const renderSignature = `${md}\n▸ **${methodName}**(${args})`;
const renderSummary = `${doc ? `\n- **summary**: ${doc}\n` : '\n'}`;
+17 -4
View File
@@ -9,11 +9,24 @@ import BN from 'bn.js';
import Compact from './codec/Compact';
import U8a from './codec/U8a';
import { FunctionMetadata } from './Metadata/v7/Calls';
import Method from './primitive/Generic/Method';
import Call from './primitive/Generic/Call';
import Address from './primitive/Generic/Address';
export * from './codec/types';
export interface CallFunction {
(...args: any[]): Call;
callIndex: Uint8Array;
meta: FunctionMetadata;
method: string;
section: string;
toJSON: () => any;
}
export type Calls = Record<string, CallFunction>;
export type ModulesWithCalls = Record<string, Calls>;
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IKeyringPair {
address: string;
@@ -169,7 +182,7 @@ export interface ExtrinsicPayloadValue {
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec {
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: Uint8Array | string): IExtrinsicSignature;
sign (method: Method, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature;
sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
@@ -180,7 +193,7 @@ export interface IExtrinsicEra extends Codec {
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsicImpl extends Codec {
readonly method: Method;
readonly method: Call;
readonly signature: IExtrinsicSignature;
readonly version: number;
@@ -191,7 +204,7 @@ export interface IExtrinsicImpl extends Codec {
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsic extends ExtrinsicSignatureBase, IMethod {
readonly length: number;
readonly method: Method;
readonly method: Call;
readonly type: number;
readonly version: number;