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:
@@ -4,7 +4,7 @@
|
||||
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
||||
import { Hash, RuntimeVersion, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { AnyFunction, Codec, CodecArg, RegistryTypes } from '@polkadot/types/types';
|
||||
import { AnyFunction, CallFunction, Codec, CodecArg, ModulesWithCalls, RegistryTypes } from '@polkadot/types/types';
|
||||
import {
|
||||
ApiInterfaceRx, ApiInterfaceEvents, ApiOptions, ApiTypes, DecorateMethodOptions,
|
||||
DecoratedRpc, DecoratedRpcSection,
|
||||
@@ -23,13 +23,12 @@ import { Storage } from '@polkadot/api-metadata/storage/types';
|
||||
import storageFromMeta from '@polkadot/api-metadata/storage/fromMetadata';
|
||||
import RpcCore from '@polkadot/rpc-core';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { getTypeRegistry, GenericEvent, GenericMethod, Metadata, Null, u64 } from '@polkadot/types';
|
||||
import { getTypeRegistry, GenericCall, GenericEvent, Metadata, Null, u64 } from '@polkadot/types';
|
||||
import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage';
|
||||
import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants';
|
||||
import { MethodFunction, ModulesWithMethods } from '@polkadot/types/primitive/Generic/Method';
|
||||
import * as interfacesTypes from '@polkadot/types/interfaces/definitions';
|
||||
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
|
||||
import { assert, compactStripLength, isFunction, isObject, isUndefined, logger, u8aToHex } from '@polkadot/util';
|
||||
import { assert, compactStripLength, isFunction, isObject, isUndefined, logger, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { cryptoWaitReady } from '@polkadot/util-crypto';
|
||||
|
||||
import createSubmittable, { SubmittableExtrinsic } from './SubmittableExtrinsic';
|
||||
@@ -209,6 +208,13 @@ export default abstract class ApiBase<ApiType> {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Finds the definition for a specific [[Call]] based on the index supplied
|
||||
*/
|
||||
public findCall (callIndex: Uint8Array | string): CallFunction {
|
||||
return GenericCall.findFunction(u8aToU8a(callIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair
|
||||
*/
|
||||
@@ -534,7 +540,7 @@ export default abstract class ApiBase<ApiType> {
|
||||
// only inject if we are not a clone (global init)
|
||||
if (!this._options.source) {
|
||||
GenericEvent.injectMetadata(this.runtimeMetadata);
|
||||
GenericMethod.injectMethods(extrinsics);
|
||||
GenericCall.injectMethods(extrinsics);
|
||||
|
||||
// detect the extrinsic version in-use based on the last block
|
||||
const lastBlock: SignedBlock = await this._rpcCore.chain.getBlock().toPromise();
|
||||
@@ -600,7 +606,7 @@ export default abstract class ApiBase<ApiType> {
|
||||
});
|
||||
}
|
||||
|
||||
private decorateExtrinsics<ApiType> (extrinsics: ModulesWithMethods, decorateMethod: ApiBase<ApiType>['decorateMethod']): SubmittableExtrinsics<ApiType> {
|
||||
private decorateExtrinsics<ApiType> (extrinsics: ModulesWithCalls, decorateMethod: ApiBase<ApiType>['decorateMethod']): SubmittableExtrinsics<ApiType> {
|
||||
const creator = (value: Uint8Array | string): SubmittableExtrinsic<ApiType> =>
|
||||
createSubmittable(this.type, this._rx as ApiInterfaceRx, decorateMethod, value);
|
||||
|
||||
@@ -617,7 +623,7 @@ export default abstract class ApiBase<ApiType> {
|
||||
}, creator as unknown as SubmittableExtrinsics<ApiType>);
|
||||
}
|
||||
|
||||
private decorateExtrinsicEntry<ApiType> (method: MethodFunction, decorateMethod: ApiBase<ApiType>['decorateMethod']): SubmittableExtrinsicFunction<ApiType> {
|
||||
private decorateExtrinsicEntry<ApiType> (method: CallFunction, decorateMethod: ApiBase<ApiType>['decorateMethod']): SubmittableExtrinsicFunction<ApiType> {
|
||||
const decorated =
|
||||
(...params: CodecArg[]): SubmittableExtrinsic<ApiType> =>
|
||||
createSubmittable(this.type, this._rx as ApiInterfaceRx, decorateMethod, method(...params));
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
|
||||
import { createType, GenericMethod } from '@polkadot/types';
|
||||
import { createType, GenericCall } from '@polkadot/types';
|
||||
|
||||
import SignerPayload from './SignerPayload';
|
||||
|
||||
@@ -21,7 +21,7 @@ describe('SignerPayload', (): void => {
|
||||
};
|
||||
|
||||
beforeEach((): void => {
|
||||
GenericMethod.injectMethods(extrinsics);
|
||||
GenericCall.injectMethods(extrinsics);
|
||||
});
|
||||
|
||||
it('creates a valid JSON output', (): void => {
|
||||
@@ -32,7 +32,7 @@ describe('SignerPayload', (): void => {
|
||||
blockNumber: '0x231d30',
|
||||
era: createType('ExtrinsicEra', { current: 2301232, period: 200 }),
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
method: createType('Method', '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c'),
|
||||
method: createType('Call', '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c'),
|
||||
nonce: 0x1234,
|
||||
tip: 0x5678,
|
||||
version: 2
|
||||
|
||||
@@ -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 { Address, Balance, BlockNumber, ExtrinsicEra, Hash, Index, Method } from '@polkadot/types/interfaces';
|
||||
import { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index } from '@polkadot/types/interfaces';
|
||||
import { SignerPayload as ISignerPayload } from './types';
|
||||
|
||||
import { ClassOf, Compact, Struct, u8 } from '@polkadot/types';
|
||||
@@ -13,7 +13,7 @@ export interface SignerPayloadType {
|
||||
blockNumber: BlockNumber;
|
||||
era: ExtrinsicEra;
|
||||
genesisHash: Hash;
|
||||
method: Method;
|
||||
method: Call;
|
||||
nonce: Compact<Index>;
|
||||
tip: Compact<Balance>;
|
||||
version: u8;
|
||||
@@ -25,7 +25,7 @@ export default class SignerPayload extends Struct.with({
|
||||
blockNumber: ClassOf('BlockNumber'),
|
||||
era: ClassOf('ExtrinsicEra'),
|
||||
genesisHash: ClassOf('Hash'),
|
||||
method: ClassOf('Method'),
|
||||
method: ClassOf('Call'),
|
||||
nonce: ClassOf('Compact<Index>'),
|
||||
tip: ClassOf('Compact<Balance>'),
|
||||
version: u8
|
||||
|
||||
@@ -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 { AccountId, Address, ExtrinsicEra, ExtrinsicStatus, EventRecord, Hash, Header, Index, Method, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { AccountId, Address, Call, ExtrinsicEra, ExtrinsicStatus, EventRecord, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { AnyNumber, AnyU8a, Callback, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '@polkadot/types/types';
|
||||
import { ApiInterfaceRx, ApiTypes } from './types';
|
||||
|
||||
@@ -110,7 +110,7 @@ export default function createSubmittableExtrinsic<ApiType> (
|
||||
type: ApiTypes,
|
||||
api: ApiInterfaceRx,
|
||||
decorateMethod: ApiBase<ApiType>['decorateMethod'],
|
||||
extrinsic: Method | Uint8Array | string,
|
||||
extrinsic: Call | Uint8Array | string,
|
||||
trackingCb?: Callback<ISubmittableResult>
|
||||
): SubmittableExtrinsic<ApiType> {
|
||||
const _extrinsic = createType('Extrinsic', extrinsic, { version: api.extrinsicType }) as unknown as SubmittableExtrinsic<ApiType>;
|
||||
|
||||
@@ -60,7 +60,7 @@ export default async function test (): Promise<void> {
|
||||
|
||||
const transfer = api.tx.balances.transfer(keyring.bob.address, 12345);
|
||||
|
||||
console.log('transfer as Method', transfer as IMethod);
|
||||
console.log('transfer as Call', transfer as IMethod);
|
||||
console.log('transfer as Extrinsic', transfer as IExtrinsic);
|
||||
|
||||
// simple "return the hash" variant
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
import { AnyFunction, Callback, Codec, CodecArg, IExtrinsic, RegistryTypes, SignatureOptions } from '@polkadot/types/types';
|
||||
import { AnyFunction, Callback, CallFunction, Codec, CodecArg, IExtrinsic, RegistryTypes, SignatureOptions } from '@polkadot/types/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
@@ -11,7 +11,6 @@ import { DeriveCustom } from '@polkadot/api-derive';
|
||||
import { Constants } from '@polkadot/api-metadata/consts/types';
|
||||
import { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
|
||||
import { u64 } from '@polkadot/types';
|
||||
import { MethodFunction } from '@polkadot/types/primitive/Generic/Method';
|
||||
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
|
||||
|
||||
import ApiBase from './Base';
|
||||
@@ -164,7 +163,7 @@ export interface QueryableStorage<ApiType> {
|
||||
[index: string]: QueryableModuleStorage<ApiType>;
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsicFunction<ApiType> extends MethodFunction {
|
||||
export interface SubmittableExtrinsicFunction<ApiType> extends CallFunction {
|
||||
(...params: CodecArg[]): SubmittableExtrinsic<ApiType>;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user