Allow new Extrinsic(method: Method) (#499)
* Allow new Extrinsic(method: Method) * Update types * Be explicit in returned types * Submittable with Method constructor
This commit is contained in:
@@ -4,26 +4,20 @@
|
||||
|
||||
import { KeyringPair } from '@polkadot/keyring/types';
|
||||
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
|
||||
import { ApiPromiseInterface } from './types';
|
||||
import { ApiPromiseInterface, SubmittableSendResult } from './types';
|
||||
|
||||
import { EventRecord, Extrinsic, ExtrinsicStatus, Hash, SignedBlock, u32 } from '@polkadot/types/index';
|
||||
|
||||
type SendResult = {
|
||||
events?: Array<EventRecord>,
|
||||
status: ExtrinsicStatus,
|
||||
type: string
|
||||
};
|
||||
import { EventRecord, Extrinsic, ExtrinsicStatus, Hash, Method, SignedBlock, u32 } from '@polkadot/types/index';
|
||||
|
||||
export default class SubmittableExtrinsic extends Extrinsic {
|
||||
private _api: ApiPromiseInterface;
|
||||
|
||||
constructor (api: ApiPromiseInterface, extrinsic: Extrinsic) {
|
||||
constructor (api: ApiPromiseInterface, extrinsic: Extrinsic | Method) {
|
||||
super(extrinsic);
|
||||
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
private checkStatus (statusCb: (result: SendResult) => any): (status: ExtrinsicStatus) => Promise<void> {
|
||||
private checkStatus (statusCb: (result: SubmittableSendResult) => any): (status: ExtrinsicStatus) => Promise<void> {
|
||||
return async (status: ExtrinsicStatus): Promise<void> => {
|
||||
let events: Array<any> | undefined = undefined;
|
||||
|
||||
@@ -51,7 +45,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
|
||||
};
|
||||
}
|
||||
|
||||
send (statusCb?: (result: SendResult) => any): Promise<Hash> {
|
||||
send (statusCb?: (result: SubmittableSendResult) => any): Promise<Hash> {
|
||||
if (!statusCb || !this._api.hasSubscriptions) {
|
||||
return this._api.rpc.author.submitExtrinsic(this);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { ApiPromiseInterface, QueryableStorageFunction, QueryableModuleStorage,
|
||||
|
||||
import Rpc from '@polkadot/rpc-core/index';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import { Extrinsic, Hash } from '@polkadot/types/index';
|
||||
import { Hash } from '@polkadot/types/index';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { MethodFunction, ModulesWithMethods } from '@polkadot/types/Method';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
@@ -217,7 +217,7 @@ export default class ApiPromise extends ApiBase<Rpc, QueryableStorage, Submittab
|
||||
|
||||
private decorateExtrinsicEntry (method: MethodFunction): SubmittableExtrinsicFunction {
|
||||
const decorated: any = (...args: Array<any>): SubmittableExtrinsic =>
|
||||
new SubmittableExtrinsic(this, new Extrinsic({ method: method(...args) }));
|
||||
new SubmittableExtrinsic(this, method(...args));
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as SubmittableExtrinsicFunction;
|
||||
}
|
||||
|
||||
@@ -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 { Hash } from '@polkadot/types/index';
|
||||
import { EventRecord, ExtrinsicStatus, Hash } from '@polkadot/types/index';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { MethodFunction } from '@polkadot/types/Method';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
@@ -40,3 +40,9 @@ export interface SubmittableExtrinsics {
|
||||
export interface ApiPromiseInterface extends ApiBaseInterface<Rpc, QueryableStorage, SubmittableExtrinsics> {
|
||||
readonly isReady: Promise<ApiPromiseInterface>;
|
||||
}
|
||||
|
||||
export type SubmittableSendResult = {
|
||||
events?: Array<EventRecord>,
|
||||
status: ExtrinsicStatus,
|
||||
type: string
|
||||
};
|
||||
|
||||
@@ -4,28 +4,22 @@
|
||||
|
||||
import { KeyringPair } from '@polkadot/keyring/types';
|
||||
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
|
||||
import { ApiRxInterface } from './types';
|
||||
import { ApiRxInterface, SubmittableSendResult } from './types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { EventRecord, Extrinsic, ExtrinsicStatus } from '@polkadot/types/index';
|
||||
|
||||
type SendResult = {
|
||||
events?: Array<EventRecord>,
|
||||
status: ExtrinsicStatus,
|
||||
type: string
|
||||
};
|
||||
import { Extrinsic, ExtrinsicStatus, Method } from '@polkadot/types/index';
|
||||
|
||||
export default class SubmittableExtrinsic extends Extrinsic {
|
||||
private _api: ApiRxInterface;
|
||||
|
||||
constructor (api: ApiRxInterface, extrinsic: Extrinsic) {
|
||||
constructor (api: ApiRxInterface, extrinsic: Extrinsic | Method) {
|
||||
super(extrinsic);
|
||||
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
send (): Observable<SendResult> {
|
||||
send (): Observable<SubmittableSendResult> {
|
||||
return this._api.rpc.author
|
||||
.submitAndWatchExtrinsic(this)
|
||||
.pipe(
|
||||
|
||||
@@ -11,7 +11,7 @@ import { catchError, map } from 'rxjs/operators';
|
||||
import Rpc from '@polkadot/rpc-core/index';
|
||||
import RpcRx from '@polkadot/rpc-rx/index';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import { Extrinsic, Hash } from '@polkadot/types/index';
|
||||
import { Hash } from '@polkadot/types/index';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { MethodFunction, ModulesWithMethods } from '@polkadot/types/Method';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
@@ -209,7 +209,7 @@ export default class ApiRx extends ApiBase<RpcRx, QueryableStorage, SubmittableE
|
||||
|
||||
private decorateExtrinsicEntry (method: MethodFunction): SubmittableExtrinsicFunction {
|
||||
const decorated: any = (...args: Array<any>): SubmittableExtrinsic =>
|
||||
new SubmittableExtrinsic(this, new Extrinsic({ method: method(...args) }));
|
||||
new SubmittableExtrinsic(this, method(...args));
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as SubmittableExtrinsicFunction;
|
||||
}
|
||||
|
||||
@@ -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 { Hash } from '@polkadot/types/index';
|
||||
import { EventRecord, ExtrinsicStatus, Hash } from '@polkadot/types/index';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { MethodFunction } from '@polkadot/types/Method';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
@@ -42,3 +42,9 @@ export interface ApiRxInterface extends ApiBaseInterface<RpcRx, QueryableStorage
|
||||
readonly isConnected: Observable<boolean>;
|
||||
readonly isReady: Observable<ApiRxInterface>;
|
||||
}
|
||||
|
||||
export type SubmittableSendResult = {
|
||||
events?: Array<EventRecord>,
|
||||
status: ExtrinsicStatus,
|
||||
type: string
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('fromMetadata', () => {
|
||||
|
||||
it('should return properly-encoded transactions', () => {
|
||||
expect(
|
||||
new Extrinsic({ method: newExtrinsics.timestamp.set([10101]) }).toU8a()
|
||||
new Extrinsic(newExtrinsics.timestamp.set([10101])).toU8a()
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
// length (encoded)
|
||||
|
||||
@@ -13,9 +13,9 @@ const keyring = testingPairs();
|
||||
describe('extrinsics', () => {
|
||||
it('encodes extrinsic correctly (nobody)', () => {
|
||||
expect(
|
||||
new Extrinsic({
|
||||
method: extrinsics.timestamp.set(10101)
|
||||
}).sign(
|
||||
new Extrinsic(
|
||||
extrinsics.timestamp.set(10101)
|
||||
).sign(
|
||||
keyring.nobody,
|
||||
1234,
|
||||
new Uint8Array()
|
||||
@@ -44,9 +44,9 @@ describe('extrinsics', () => {
|
||||
|
||||
it('encodes an actual transfer (actual data)', () => {
|
||||
expect(
|
||||
new Extrinsic({
|
||||
method: extrinsics.balances.transfer(keyring.bob.publicKey(), 6969)
|
||||
}).sign(keyring.alice, 0, '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f').toU8a()
|
||||
new Extrinsic(
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey(), 6969)
|
||||
).sign(keyring.alice, 0, '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f').toU8a()
|
||||
).toEqual(
|
||||
hexToU8a(
|
||||
'0x' +
|
||||
|
||||
@@ -34,17 +34,15 @@ type ExtrinsicValue = {
|
||||
* - left as is, to create an inherent
|
||||
*/
|
||||
export default class Extrinsic extends Struct {
|
||||
constructor (value?: ExtrinsicValue | AnyU8a) {
|
||||
constructor (value?: ExtrinsicValue | AnyU8a | Method) {
|
||||
super({
|
||||
signature: ExtrinsicSignature,
|
||||
method: Method
|
||||
}, Extrinsic.decodeExtrinsic(value));
|
||||
}, Extrinsic.decodeExtrinsic(value || {}));
|
||||
}
|
||||
|
||||
static decodeExtrinsic (value?: ExtrinsicValue | AnyU8a): object | Uint8Array {
|
||||
if (!value) {
|
||||
return {};
|
||||
} else if (isHex(value)) {
|
||||
static decodeExtrinsic (value: ExtrinsicValue | AnyU8a | Method): ExtrinsicValue | Array<number> | Uint8Array {
|
||||
if (isHex(value)) {
|
||||
// FIXME We manually add the length prefix for hex for now
|
||||
// https://github.com/paritytech/substrate/issues/889
|
||||
// Instead of the block below, it should simply be:
|
||||
@@ -58,9 +56,13 @@ export default class Extrinsic extends Struct {
|
||||
const [offset, length] = Compact.decodeU8a(value);
|
||||
|
||||
return value.subarray(offset, offset + length.toNumber());
|
||||
} else if (value instanceof Method) {
|
||||
return {
|
||||
method: value
|
||||
};
|
||||
}
|
||||
|
||||
return value as any;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user