Work around optional callback for Promise (#656)
* Work around optional callback for Promise * Commit actual e2e test * Promise allows for submitExtrinsic
This commit is contained in:
@@ -7,7 +7,7 @@ import { Storage } from '@polkadot/storage/types';
|
||||
import { Codec, CodecArg, CodecCallback } from '@polkadot/types/types';
|
||||
import { RxResult } from './rx/types';
|
||||
import {
|
||||
ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions,
|
||||
ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiType,
|
||||
DecoratedRpc, DecoratedRpc$Method, DecoratedRpc$Section,
|
||||
Derive, DeriveSection,
|
||||
HashResult, U64Result,
|
||||
@@ -67,6 +67,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
private _runtimeMetadata?: Metadata;
|
||||
private _runtimeVersion?: RuntimeVersion;
|
||||
private _rx: Partial<ApiInterface$Rx> = {};
|
||||
private _type: ApiType;
|
||||
|
||||
/**
|
||||
* @description Create an instance of the class
|
||||
@@ -86,11 +87,12 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
constructor (provider: ApiOptions | ProviderInterface = {}) {
|
||||
constructor (provider: ApiOptions | ProviderInterface = {}, type: ApiType) {
|
||||
const options = isObject(provider) && isFunction((provider as ProviderInterface).send)
|
||||
? { provider } as ApiOptions
|
||||
: provider as ApiOptions;
|
||||
|
||||
this._type = type;
|
||||
this._eventemitter = new EventEmitter();
|
||||
this._rpcBase = new RpcBase(options.provider);
|
||||
|
||||
@@ -141,6 +143,13 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
return this._runtimeVersion as RuntimeVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The type of this API instance, either 'rxjs' or 'promise'
|
||||
*/
|
||||
get type (): ApiType {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Derived results that are injected into the API, allowing for combinations of various query results.
|
||||
*
|
||||
@@ -388,7 +397,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
|
||||
private decorateExtrinsicEntry<C, S> (method: MethodFunction, onCall: OnCallDefinition<C, S>): SubmittableExtrinsicFunction<C, S> {
|
||||
const decorated: any = (...params: Array<CodecArg>): SubmittableExtrinsic<C, S> =>
|
||||
new SubmittableExtrinsic(this._rx as ApiInterface$Rx, onCall, method(...params));
|
||||
new SubmittableExtrinsic(this.type, this._rx as ApiInterface$Rx, onCall, method(...params));
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as SubmittableExtrinsicFunction<C, S>;
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { KeyringPair } from '@polkadot/keyring/types';
|
||||
import { Extrinsic, ExtrinsicStatus, EventRecord, Index, Method, SignedBlock, Text } from '@polkadot/types/index';
|
||||
import { Extrinsic, ExtrinsicStatus, EventRecord, Hash, Index, Method, SignedBlock, Text } from '@polkadot/types/index';
|
||||
import { AnyNumber, AnyU8a, CodecCallback } from '@polkadot/types/types';
|
||||
import { ApiInterface$Rx, OnCallDefinition } from './types';
|
||||
import { ApiInterface$Rx, ApiType, OnCallDefinition } from './types';
|
||||
|
||||
import { Observable, of, combineLatest } from 'rxjs';
|
||||
import { first, map, switchMap } from 'rxjs/operators';
|
||||
@@ -13,11 +13,10 @@ import { Struct, Vector } from '@polkadot/types/codec';
|
||||
|
||||
import filterEvents from './util/filterEvents';
|
||||
|
||||
// Re-introduce once we use submitExtrinsic as well
|
||||
// type SumbitableResultResult<CodecResult, SubscriptionResult> =
|
||||
// CodecResult extends Observable<any>
|
||||
// ? Observable<SubmittableResult>
|
||||
// : Promise<SubmittableResult>;
|
||||
type SumbitableResultResult<CodecResult, SubscriptionResult> =
|
||||
CodecResult extends Observable<any>
|
||||
? Observable<SubmittableResult>
|
||||
: Promise<Hash>;
|
||||
|
||||
type SumbitableResultSubscription<CodecResult, SubscriptionResult> =
|
||||
SubscriptionResult extends Observable<any>
|
||||
@@ -58,10 +57,12 @@ export class SubmittableResult extends Struct {
|
||||
export default class SubmittableExtrinsic<CodecResult, SubscriptionResult> extends Extrinsic {
|
||||
private _api: ApiInterface$Rx;
|
||||
private _onCall: OnCallDefinition<CodecResult, SubscriptionResult>;
|
||||
private _noStatusCb?: boolean;
|
||||
|
||||
constructor (api: ApiInterface$Rx, onCall: OnCallDefinition<CodecResult, SubscriptionResult>, extrinsic: Extrinsic | Method) {
|
||||
constructor (type: ApiType, api: ApiInterface$Rx, onCall: OnCallDefinition<CodecResult, SubscriptionResult>, extrinsic: Extrinsic | Method) {
|
||||
super(extrinsic);
|
||||
|
||||
this._noStatusCb = type === 'rxjs';
|
||||
this._api = api;
|
||||
this._onCall = onCall;
|
||||
}
|
||||
@@ -92,7 +93,11 @@ export default class SubmittableExtrinsic<CodecResult, SubscriptionResult> exten
|
||||
);
|
||||
}
|
||||
|
||||
private sendObservable (): Observable<SubmittableResult> {
|
||||
private sendObservable (): Observable<Hash> {
|
||||
return this._api.rpc.author.submitExtrinsic(this) as Observable<Hash>;
|
||||
}
|
||||
|
||||
private subscribeObservable (): Observable<SubmittableResult> {
|
||||
return (this._api.rpc.author
|
||||
.submitAndWatchExtrinsic(this) as Observable<ExtrinsicStatus>)
|
||||
.pipe(
|
||||
@@ -102,14 +107,18 @@ export default class SubmittableExtrinsic<CodecResult, SubscriptionResult> exten
|
||||
);
|
||||
}
|
||||
|
||||
// FIXME We currently only cater for callback in Promise-world, submitExtrinsic -> Hash
|
||||
// should also be catered for returning SumbitableResultResult
|
||||
send (statusCb?: (result: SubmittableResult) => any): SumbitableResultSubscription<CodecResult, SubscriptionResult> {
|
||||
send (): SumbitableResultResult<CodecResult, SubscriptionResult>;
|
||||
send (statusCb: (result: SubmittableResult) => any): SumbitableResultSubscription<CodecResult, SubscriptionResult>;
|
||||
send (statusCb?: (result: SubmittableResult) => any): SumbitableResultResult<CodecResult, SubscriptionResult> | SumbitableResultSubscription<CodecResult, SubscriptionResult> {
|
||||
const isSubscription = this._noStatusCb || !!statusCb;
|
||||
|
||||
return this._onCall(
|
||||
() => this.sendObservable(),
|
||||
() => isSubscription
|
||||
? this.subscribeObservable()
|
||||
: this.sendObservable(),
|
||||
[],
|
||||
statusCb as CodecCallback,
|
||||
true
|
||||
isSubscription
|
||||
) as unknown as SumbitableResultSubscription<CodecResult, SubscriptionResult>;
|
||||
}
|
||||
|
||||
@@ -119,22 +128,27 @@ export default class SubmittableExtrinsic<CodecResult, SubscriptionResult> exten
|
||||
return this;
|
||||
}
|
||||
|
||||
// FIXME As per previous FIXME on send, same applies
|
||||
signAndSend (signerPair: KeyringPair, statusCb?: (result: SubmittableResult) => any): SumbitableResultSubscription<CodecResult, SubscriptionResult> {
|
||||
signAndSend (signerPair: KeyringPair): SumbitableResultResult<CodecResult, SubscriptionResult>;
|
||||
signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableResult) => any): SumbitableResultSubscription<CodecResult, SubscriptionResult>;
|
||||
signAndSend (signerPair: KeyringPair, statusCb?: (result: SubmittableResult) => any): SumbitableResultResult<CodecResult, SubscriptionResult> | SumbitableResultSubscription<CodecResult, SubscriptionResult> {
|
||||
const isSubscription = this._noStatusCb || !!statusCb;
|
||||
|
||||
return this._onCall(
|
||||
() => (this._api.query.system
|
||||
.accountNonce(signerPair.address()) as Observable<Index>)
|
||||
.pipe(
|
||||
first(),
|
||||
switchMap((nonce) =>
|
||||
this
|
||||
.sign(signerPair, nonce)
|
||||
.sendObservable()
|
||||
)
|
||||
switchMap((nonce) => {
|
||||
this.sign(signerPair, nonce);
|
||||
|
||||
return isSubscription
|
||||
? this.subscribeObservable()
|
||||
: this.sendObservable() as any; // ???
|
||||
})
|
||||
),
|
||||
[],
|
||||
statusCb as CodecCallback,
|
||||
true
|
||||
isSubscription
|
||||
) as unknown as SumbitableResultSubscription<CodecResult, SubscriptionResult>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ export default class ApiPromise extends ApiBase<CodecResult, SubscriptionResult>
|
||||
* ```
|
||||
*/
|
||||
constructor (options?: ApiOptions | ProviderInterface) {
|
||||
super(options);
|
||||
super(options, 'promise');
|
||||
|
||||
this._isReady = new Promise((resolveReady) =>
|
||||
super.once('ready', () =>
|
||||
|
||||
@@ -164,7 +164,7 @@ export default class ApiRx extends ApiBase<RxResult, RxResult> implements ApiRxI
|
||||
* ```
|
||||
*/
|
||||
constructor (provider?: ApiOptions | ProviderInterface) {
|
||||
super(provider);
|
||||
super(provider, 'rxjs');
|
||||
|
||||
this._isReady = from(
|
||||
// convinced you can observable from an event, however my mind groks this form better
|
||||
|
||||
@@ -121,7 +121,11 @@ export type ApiInterface$Rx = ApiInterface$Decorated<RxResult, RxResult>;
|
||||
|
||||
export type ApiInterface$Events = RpcRxInterface$Events | 'ready';
|
||||
|
||||
export type ApiType = 'promise' | 'rxjs';
|
||||
|
||||
export interface ApiBaseInterface<CodecResult, SubscriptionResult> extends Readonly<ApiInterface$Decorated<CodecResult, SubscriptionResult>> {
|
||||
readonly type: ApiType;
|
||||
|
||||
on: (type: ApiInterface$Events, handler: (...args: Array<any>) => any) => this;
|
||||
once: (type: ApiInterface$Events, handler: (...args: Array<any>) => any) => this;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,10 @@ describe.skip('e2e transactions', () => {
|
||||
let api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
api = await Api.create();
|
||||
if (!api) {
|
||||
api = await Api.create();
|
||||
}
|
||||
|
||||
jest.setTimeout(30000);
|
||||
done();
|
||||
});
|
||||
@@ -66,7 +69,15 @@ describe.skip('e2e transactions', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.skip('makes a proposal', async () => {
|
||||
it('makes a transfer (no callback)', async () => {
|
||||
const hash = await api.tx.balances
|
||||
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
|
||||
.signAndSend(keyring.dave);
|
||||
|
||||
expect(hash.toHex()).toHaveLength(66);
|
||||
});
|
||||
|
||||
it('makes a proposal', async () => {
|
||||
const nonce = await api.query.system.accountNonce(keyring.alice.address());
|
||||
|
||||
// don't wait for status, just get hash
|
||||
@@ -75,8 +86,6 @@ describe.skip('e2e transactions', () => {
|
||||
.sign(keyring.alice, nonce)
|
||||
.send();
|
||||
|
||||
expect(
|
||||
hash.toString()
|
||||
).not.toEqual('0x');
|
||||
expect(hash.toHex()).toHaveLength(66);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user