Add withResultTransform to Submittables (use in api-contracts) (#2707)
* Allow SubmittableResult transforms (use in contracts) * withResultTransform
This commit is contained in:
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||||
import { AnyJson, Registry } from '@polkadot/types/types';
|
import { AnyJson, Registry } from '@polkadot/types/types';
|
||||||
import { ApiObject } from '../types';
|
|
||||||
|
|
||||||
|
import ApiBase from '@polkadot/api/base';
|
||||||
import { assert } from '@polkadot/util';
|
import { assert } from '@polkadot/util';
|
||||||
|
|
||||||
import Abi from '../Abi';
|
import Abi from '../Abi';
|
||||||
@@ -12,13 +12,13 @@ import Abi from '../Abi';
|
|||||||
export default abstract class Base<ApiType extends ApiTypes> {
|
export default abstract class Base<ApiType extends ApiTypes> {
|
||||||
public readonly abi: Abi;
|
public readonly abi: Abi;
|
||||||
|
|
||||||
public readonly api: ApiObject<ApiType>;
|
public readonly api: ApiBase<ApiType>;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
protected readonly _decorateMethod: DecorateMethod<ApiType>;
|
protected readonly _decorateMethod: DecorateMethod<ApiType>;
|
||||||
|
|
||||||
constructor (api: ApiObject<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>) {
|
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>) {
|
||||||
this.abi = abi instanceof Abi
|
this.abi = abi instanceof Abi
|
||||||
? abi
|
? abi
|
||||||
: new Abi(abi);
|
: new Abi(abi);
|
||||||
|
|||||||
@@ -1,30 +1,22 @@
|
|||||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import { ApiTypes, DecorateMethod, SignerOptions } from '@polkadot/api/types';
|
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||||
import { AccountId, EventRecord, Hash } from '@polkadot/types/interfaces';
|
import { AccountId, EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||||
import { AnyJson, CodecArg, IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
|
import { AnyJson, CodecArg, ISubmittableResult } from '@polkadot/types/types';
|
||||||
import { AbiConstructor, ApiObject } from '../types';
|
import { AbiConstructor } from '../types';
|
||||||
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { map } from 'rxjs/operators';
|
|
||||||
import { SubmittableResult } from '@polkadot/api';
|
import { SubmittableResult } from '@polkadot/api';
|
||||||
|
import ApiBase from '@polkadot/api/base';
|
||||||
|
|
||||||
import Abi from '../Abi';
|
import Abi from '../Abi';
|
||||||
import { applyOnEvent, encodeMessage } from '../util';
|
import { applyOnEvent, encodeMessage } from '../util';
|
||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
import Contract from './Contract';
|
import Contract from './Contract';
|
||||||
|
|
||||||
// eslint-disable-next-line no-use-before-define
|
export class BlueprintSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
|
||||||
type BlueprintCreateResultSubscription<ApiType extends ApiTypes> = Observable<BlueprintCreateResult<ApiType>>;
|
|
||||||
|
|
||||||
export interface BlueprintCreate<ApiType extends ApiTypes> {
|
|
||||||
signAndSend (account: IKeyringPair | string | AccountId, options?: SignerOptions): BlueprintCreateResultSubscription<ApiType>;
|
|
||||||
}
|
|
||||||
|
|
||||||
class BlueprintCreateResult<ApiType extends ApiTypes> extends SubmittableResult {
|
|
||||||
public readonly contract?: Contract<ApiType>;
|
public readonly contract?: Contract<ApiType>;
|
||||||
|
|
||||||
constructor (result: ISubmittableResult, contract?: Contract<ApiType>) {
|
constructor (result: ISubmittableResult, contract?: Contract<ApiType>) {
|
||||||
@@ -34,36 +26,22 @@ class BlueprintCreateResult<ApiType extends ApiTypes> extends SubmittableResult
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE Experimental, POC, bound to change
|
|
||||||
export default class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
|
export default class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||||
public readonly codeHash: Hash;
|
public readonly codeHash: Hash;
|
||||||
|
|
||||||
constructor (api: ApiObject<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>, codeHash: string | Hash | Uint8Array) {
|
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, codeHash: string | Hash | Uint8Array, decorateMethod: DecorateMethod<ApiType>) {
|
||||||
super(api, abi, decorateMethod);
|
super(api, abi, decorateMethod);
|
||||||
|
|
||||||
this.codeHash = this.registry.createType('Hash', codeHash);
|
this.codeHash = this.registry.createType('Hash', codeHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public instantiate (constructorOrIndex: AbiConstructor | number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): SubmittableExtrinsic<ApiType> {
|
public createContract (constructorOrIndex: AbiConstructor | number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): SubmittableExtrinsic<ApiType, BlueprintSubmittableResult<ApiType>> {
|
||||||
return this.api.tx.contracts.instantiate(endowment, gasLimit, this.codeHash, encodeMessage(this.registry, this.abi.findConstructor(constructorOrIndex), params)) as SubmittableExtrinsic<ApiType>;
|
return this.api.tx.contracts
|
||||||
}
|
.instantiate(endowment, gasLimit, this.codeHash, encodeMessage(this.registry, this.abi.findConstructor(constructorOrIndex), params))
|
||||||
|
.withResultTransform((result: ISubmittableResult) =>
|
||||||
public createContract (constructorOrIndex: AbiConstructor | number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): BlueprintCreate<ApiType> {
|
new BlueprintSubmittableResult(result, applyOnEvent(result, 'Instantiated', (record: EventRecord) =>
|
||||||
return {
|
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
))
|
||||||
signAndSend: this._decorateMethod(
|
);
|
||||||
(account: IKeyringPair | string | AccountId, options?: SignerOptions): BlueprintCreateResultSubscription<ApiType> =>
|
|
||||||
this.api.rx.tx.contracts
|
|
||||||
.instantiate(endowment, gasLimit, this.codeHash, encodeMessage(this.registry, this.abi.findConstructor(constructorOrIndex), params))
|
|
||||||
.signAndSend(account, options)
|
|
||||||
.pipe(
|
|
||||||
map((result) =>
|
|
||||||
new BlueprintCreateResult(result, applyOnEvent(result, 'Instantiated', (record: EventRecord) =>
|
|
||||||
new Contract<ApiType>(this.api, this.abi, this._decorateMethod, record.event.data[1] as AccountId)
|
|
||||||
))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import { ApiTypes, DecorateMethod, SignerOptions } from '@polkadot/api/types';
|
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||||
import { AccountId, Address, EventRecord, Hash } from '@polkadot/types/interfaces';
|
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||||
import { AnyJson, IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
|
import { EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||||
import { ApiObject } from '../types';
|
import { AnyJson, ISubmittableResult } from '@polkadot/types/types';
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { map } from 'rxjs/operators';
|
|
||||||
import { SubmittableResult } from '@polkadot/api';
|
import { SubmittableResult } from '@polkadot/api';
|
||||||
|
import ApiBase from '@polkadot/api/base';
|
||||||
import { compactAddLength, u8aToU8a } from '@polkadot/util';
|
import { compactAddLength, u8aToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
import Abi from '../Abi';
|
import Abi from '../Abi';
|
||||||
@@ -16,14 +15,7 @@ import { applyOnEvent } from '../util';
|
|||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
import Blueprint from './Blueprint';
|
import Blueprint from './Blueprint';
|
||||||
|
|
||||||
// eslint-disable-next-line no-use-before-define
|
export class CodeSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
|
||||||
type CodePutCodeResultSubscription<ApiType extends ApiTypes> = Observable<CodePutCodeResult<ApiType>>;
|
|
||||||
|
|
||||||
export interface CodePutCode<ApiType extends ApiTypes> {
|
|
||||||
signAndSend (account: IKeyringPair | string | AccountId | Address, options?: SignerOptions): CodePutCodeResultSubscription<ApiType>;
|
|
||||||
}
|
|
||||||
|
|
||||||
class CodePutCodeResult<ApiType extends ApiTypes> extends SubmittableResult {
|
|
||||||
public readonly blueprint?: Blueprint<ApiType>;
|
public readonly blueprint?: Blueprint<ApiType>;
|
||||||
|
|
||||||
constructor (result: ISubmittableResult, blueprint?: Blueprint<ApiType>) {
|
constructor (result: ISubmittableResult, blueprint?: Blueprint<ApiType>) {
|
||||||
@@ -33,32 +25,22 @@ class CodePutCodeResult<ApiType extends ApiTypes> extends SubmittableResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE Experimental, POC, bound to change
|
|
||||||
export default class Code<ApiType extends ApiTypes> extends Base<ApiType> {
|
export default class Code<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||||
public readonly code: Uint8Array;
|
public readonly code: Uint8Array;
|
||||||
|
|
||||||
constructor (api: ApiObject<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>, wasm: Uint8Array | string) {
|
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, wasm: Uint8Array | string, decorateMethod: DecorateMethod<ApiType>) {
|
||||||
super(api, abi, decorateMethod);
|
super(api, abi, decorateMethod);
|
||||||
|
|
||||||
this.code = u8aToU8a(wasm);
|
this.code = u8aToU8a(wasm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public createBlueprint = (): CodePutCode<ApiType> => {
|
public createBlueprint (): SubmittableExtrinsic<ApiType, CodeSubmittableResult<ApiType>> {
|
||||||
return {
|
return this.api.tx.contracts
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
.putCode(compactAddLength(this.code))
|
||||||
signAndSend: this._decorateMethod(
|
.withResultTransform((result: ISubmittableResult) =>
|
||||||
(account: IKeyringPair | string | AccountId, options?: SignerOptions): CodePutCodeResultSubscription<ApiType> =>
|
new CodeSubmittableResult(result, applyOnEvent(result, 'CodeStored', (record: EventRecord) =>
|
||||||
this.api.rx.tx.contracts
|
new Blueprint<ApiType>(this.api, this.abi, record.event.data[0] as Hash, this._decorateMethod)
|
||||||
.putCode(compactAddLength(this.code))
|
))
|
||||||
.signAndSend(account, options)
|
);
|
||||||
.pipe(
|
|
||||||
map((result) =>
|
|
||||||
new CodePutCodeResult(result, applyOnEvent(result, 'CodeStored', (record: EventRecord) =>
|
|
||||||
new Blueprint<ApiType>(this.api, this.abi, this._decorateMethod, record.event.data[0] as Hash)
|
|
||||||
))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ import { ApiTypes, DecorateMethod, ObsInnerType } from '@polkadot/api/types';
|
|||||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||||
import { AccountId, ContractExecResult } from '@polkadot/types/interfaces';
|
import { AccountId, ContractExecResult } from '@polkadot/types/interfaces';
|
||||||
import { AnyJson, CodecArg, IKeyringPair } from '@polkadot/types/types';
|
import { AnyJson, CodecArg, IKeyringPair } from '@polkadot/types/types';
|
||||||
import { ApiObject, AbiMessage, ContractCallOutcome } from '../types';
|
import { AbiMessage, ContractCallOutcome } from '../types';
|
||||||
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { SubmittableResult } from '@polkadot/api';
|
import { SubmittableResult } from '@polkadot/api';
|
||||||
|
import ApiBase from '@polkadot/api/base';
|
||||||
import { assert, isFunction } from '@polkadot/util';
|
import { assert, isFunction } from '@polkadot/util';
|
||||||
|
|
||||||
import Abi from '../Abi';
|
import Abi from '../Abi';
|
||||||
@@ -36,7 +37,7 @@ export type ContractCallResult<CallType extends ContractCallTypes> = CallType ex
|
|||||||
export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
|
export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||||
public readonly address: AccountId;
|
public readonly address: AccountId;
|
||||||
|
|
||||||
constructor (api: ApiObject<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>, address: string | AccountId) {
|
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, address: string | AccountId, decorateMethod: DecorateMethod<ApiType>) {
|
||||||
super(api, abi, decorateMethod);
|
super(api, abi, decorateMethod);
|
||||||
|
|
||||||
this.address = this.registry.createType('AccountId', address);
|
this.address = this.registry.createType('AccountId', address);
|
||||||
@@ -47,7 +48,7 @@ export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public exec (messageOrIndex: AbiMessage | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): SubmittableExtrinsic<ApiType> {
|
public exec (messageOrIndex: AbiMessage | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): SubmittableExtrinsic<ApiType> {
|
||||||
return this.api.tx.contracts.call(this.address, value, gasLimit, encodeMessage(this.registry, this.abi.findMessage(messageOrIndex), params)) as SubmittableExtrinsic<ApiType>;
|
return this.api.tx.contracts.call(this.address, value, gasLimit, encodeMessage(this.registry, this.abi.findMessage(messageOrIndex), params));
|
||||||
}
|
}
|
||||||
|
|
||||||
public read (messageOrIndex: AbiMessage | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): ContractRead<ApiType> {
|
public read (messageOrIndex: AbiMessage | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): ContractRead<ApiType> {
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ import Blueprint from '../base/Blueprint';
|
|||||||
|
|
||||||
export default class PromiseBlueprint extends Blueprint<'promise'> {
|
export default class PromiseBlueprint extends Blueprint<'promise'> {
|
||||||
constructor (api: ApiPromise, abi: AnyJson | Abi, codeHash: string | Hash) {
|
constructor (api: ApiPromise, abi: AnyJson | Abi, codeHash: string | Hash) {
|
||||||
super(api, abi, decorateMethod, codeHash);
|
super(api, abi, codeHash, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ import Code from '../base/Code';
|
|||||||
|
|
||||||
export default class PromiseCode extends Code<'promise'> {
|
export default class PromiseCode extends Code<'promise'> {
|
||||||
constructor (api: ApiPromise, abi: AnyJson | Abi, wasm: string | Uint8Array) {
|
constructor (api: ApiPromise, abi: AnyJson | Abi, wasm: string | Uint8Array) {
|
||||||
super(api, abi, decorateMethod, wasm);
|
super(api, abi, wasm, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ import Contract from '../base/Contract';
|
|||||||
|
|
||||||
export default class PromiseContract extends Contract<'promise'> {
|
export default class PromiseContract extends Contract<'promise'> {
|
||||||
constructor (api: ApiPromise, abi: AnyJson | Abi, address: string | AccountId) {
|
constructor (api: ApiPromise, abi: AnyJson | Abi, address: string | AccountId) {
|
||||||
super(api, abi, decorateMethod, address);
|
super(api, abi, address, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ import Blueprint from '../base/Blueprint';
|
|||||||
|
|
||||||
export default class RxBlueprint extends Blueprint<'rxjs'> {
|
export default class RxBlueprint extends Blueprint<'rxjs'> {
|
||||||
constructor (api: ApiRx, abi: AnyJson | Abi, codeHash: string | Hash) {
|
constructor (api: ApiRx, abi: AnyJson | Abi, codeHash: string | Hash) {
|
||||||
super(api, abi, decorateMethod, codeHash);
|
super(api, abi, codeHash, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ import Code from '../base/Code';
|
|||||||
|
|
||||||
export default class RxCode extends Code<'rxjs'> {
|
export default class RxCode extends Code<'rxjs'> {
|
||||||
constructor (api: ApiRx, abi: AnyJson | Abi, wasm: string | Uint8Array) {
|
constructor (api: ApiRx, abi: AnyJson | Abi, wasm: string | Uint8Array) {
|
||||||
super(api, abi, decorateMethod, wasm);
|
super(api, abi, wasm, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ import Contract from '../base/Contract';
|
|||||||
|
|
||||||
export default class RxContract extends Contract<'rxjs'> {
|
export default class RxContract extends Contract<'rxjs'> {
|
||||||
constructor (api: ApiRx, abi: AnyJson | Abi, address: string | AccountId) {
|
constructor (api: ApiRx, abi: AnyJson | Abi, address: string | AccountId) {
|
||||||
super(api, abi, decorateMethod, address);
|
super(api, abi, address, decorateMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,12 @@ import { ApiTypes } from '@polkadot/api/types';
|
|||||||
import { ContractExecResult, InkSelector } from '@polkadot/types/interfaces';
|
import { ContractExecResult, InkSelector } from '@polkadot/types/interfaces';
|
||||||
import { Codec, TypeDef } from '@polkadot/types/types';
|
import { Codec, TypeDef } from '@polkadot/types/types';
|
||||||
|
|
||||||
import { ApiPromise, ApiRx } from '@polkadot/api';
|
import ApiBase from '@polkadot/api/base';
|
||||||
import Abi from './Abi';
|
import Abi from './Abi';
|
||||||
|
|
||||||
export type ApiObject<ApiType extends ApiTypes> = ApiType extends 'rxjs'
|
|
||||||
? ApiRx
|
|
||||||
: ApiPromise;
|
|
||||||
|
|
||||||
export interface ContractBase<ApiType extends ApiTypes> {
|
export interface ContractBase<ApiType extends ApiTypes> {
|
||||||
readonly abi: Abi;
|
readonly abi: Abi;
|
||||||
readonly api: ApiObject<ApiType>;
|
readonly api: ApiBase<ApiType>;
|
||||||
|
|
||||||
getMessage: (name: string) => AbiMessage;
|
getMessage: (name: string) => AbiMessage;
|
||||||
messages: AbiMessage[];
|
messages: AbiMessage[];
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ interface SubmittableOptions<ApiType extends ApiTypes> {
|
|||||||
decorateMethod: ApiBase<ApiType>['_decorateMethod'];
|
decorateMethod: ApiBase<ApiType>['_decorateMethod'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const identity = <T> (input: T): T => input;
|
||||||
|
|
||||||
export default function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
|
export default function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
|
||||||
// an instance of the base extrinsic for us to extend
|
// an instance of the base extrinsic for us to extend
|
||||||
const ExtrinsicBase = api.registry.createClass('Extrinsic');
|
const ExtrinsicBase = api.registry.createClass('Extrinsic');
|
||||||
@@ -29,12 +31,21 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
|
|||||||
return class Submittable extends ExtrinsicBase implements SubmittableExtrinsic<ApiType> {
|
return class Submittable extends ExtrinsicBase implements SubmittableExtrinsic<ApiType> {
|
||||||
readonly #ignoreStatusCb: boolean;
|
readonly #ignoreStatusCb: boolean;
|
||||||
|
|
||||||
|
#transformResult: (input: ISubmittableResult) => ISubmittableResult = identity;
|
||||||
|
|
||||||
constructor (registry: Registry, extrinsic: Call | Extrinsic | Uint8Array | string) {
|
constructor (registry: Registry, extrinsic: Call | Extrinsic | Uint8Array | string) {
|
||||||
super(registry, extrinsic, { version: api.extrinsicType });
|
super(registry, extrinsic, { version: api.extrinsicType });
|
||||||
|
|
||||||
this.#ignoreStatusCb = apiType === 'rxjs';
|
this.#ignoreStatusCb = apiType === 'rxjs';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// adds a transform to the result, applied before result is returned
|
||||||
|
withResultTransform (transform: (input: ISubmittableResult) => ISubmittableResult): this {
|
||||||
|
this.#transformResult = transform;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
// calculate the payment info for this transaction (if signed and submitted)
|
// calculate the payment info for this transaction (if signed and submitted)
|
||||||
public paymentInfo (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittablePaymentResult<ApiType> {
|
public paymentInfo (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittablePaymentResult<ApiType> {
|
||||||
if (isString(optionsOrHash) || isU8a(optionsOrHash)) {
|
if (isString(optionsOrHash) || isU8a(optionsOrHash)) {
|
||||||
@@ -200,7 +211,7 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
|
|||||||
|
|
||||||
#observeStatus = (hash: Hash, status: ExtrinsicStatus): Observable<ISubmittableResult> => {
|
#observeStatus = (hash: Hash, status: ExtrinsicStatus): Observable<ISubmittableResult> => {
|
||||||
if (!status.isFinalized && !status.isInBlock) {
|
if (!status.isFinalized && !status.isInBlock) {
|
||||||
return of(new SubmittableResult({ status }));
|
return of(this.#transformResult(new SubmittableResult({ status })));
|
||||||
}
|
}
|
||||||
|
|
||||||
const blockHash = status.isInBlock
|
const blockHash = status.isInBlock
|
||||||
@@ -209,10 +220,10 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
|
|||||||
|
|
||||||
return api.derive.tx.events(blockHash).pipe(
|
return api.derive.tx.events(blockHash).pipe(
|
||||||
map(({ block, events }): ISubmittableResult =>
|
map(({ block, events }): ISubmittableResult =>
|
||||||
new SubmittableResult({
|
this.#transformResult(new SubmittableResult({
|
||||||
events: filterEvents(hash, block, events, status),
|
events: filterEvents(hash, block, events, status),
|
||||||
status
|
status
|
||||||
})
|
}))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ export type SubmittablePaymentResult<ApiType extends ApiTypes> =
|
|||||||
? Observable<RuntimeDispatchInfo>
|
? Observable<RuntimeDispatchInfo>
|
||||||
: Promise<RuntimeDispatchInfo>;
|
: Promise<RuntimeDispatchInfo>;
|
||||||
|
|
||||||
export type SubmittableResultResult<ApiType extends ApiTypes> =
|
export type SubmittableResultResult<ApiType extends ApiTypes, R extends ISubmittableResult = ISubmittableResult> =
|
||||||
ApiType extends 'rxjs'
|
ApiType extends 'rxjs'
|
||||||
? Observable<ISubmittableResult>
|
? Observable<R>
|
||||||
: Promise<Hash>;
|
: Promise<Hash>;
|
||||||
|
|
||||||
export type SubmittableResultSubscription<ApiType extends ApiTypes> =
|
export type SubmittableResultSubscription<ApiType extends ApiTypes, R extends ISubmittableResult = ISubmittableResult> =
|
||||||
ApiType extends 'rxjs'
|
ApiType extends 'rxjs'
|
||||||
? Observable<ISubmittableResult>
|
? Observable<R>
|
||||||
: Promise<() => void>;
|
: Promise<() => void>;
|
||||||
|
|
||||||
export type SubmittableThis<ApiType extends ApiTypes, THIS> =
|
export type SubmittableThis<ApiType extends ApiTypes, THIS> =
|
||||||
@@ -42,12 +42,14 @@ export interface SignerOptions {
|
|||||||
|
|
||||||
export type AddressOrPair = IKeyringPair | string | AccountId | Address;
|
export type AddressOrPair = IKeyringPair | string | AccountId | Address;
|
||||||
|
|
||||||
export interface SubmittableExtrinsic<ApiType extends ApiTypes> extends Extrinsic {
|
export interface SubmittableExtrinsic<ApiType extends ApiTypes, R extends ISubmittableResult = ISubmittableResult> extends Extrinsic {
|
||||||
|
withResultTransform (transform: (input: ISubmittableResult) => ISubmittableResult): this;
|
||||||
|
|
||||||
paymentInfo (account: AddressOrPair, options?: Partial<SignerOptions>): SubmittablePaymentResult<ApiType>;
|
paymentInfo (account: AddressOrPair, options?: Partial<SignerOptions>): SubmittablePaymentResult<ApiType>;
|
||||||
|
|
||||||
send(): SubmittableResultResult<ApiType>;
|
send(): SubmittableResultResult<ApiType>;
|
||||||
|
|
||||||
send(statusCb: Callback<ISubmittableResult>): SubmittableResultSubscription<ApiType>;
|
send(statusCb: Callback<R>): SubmittableResultSubscription<ApiType, R>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated
|
* @deprecated
|
||||||
@@ -56,9 +58,9 @@ export interface SubmittableExtrinsic<ApiType extends ApiTypes> extends Extrinsi
|
|||||||
|
|
||||||
signAsync(account: AddressOrPair, _options?: Partial<SignerOptions>): SubmittableThis<ApiType, this>;
|
signAsync(account: AddressOrPair, _options?: Partial<SignerOptions>): SubmittableThis<ApiType, this>;
|
||||||
|
|
||||||
signAndSend(account: AddressOrPair, options?: Partial<SignerOptions>): SubmittableResultResult<ApiType>;
|
signAndSend(account: AddressOrPair, options?: Partial<SignerOptions>): SubmittableResultResult<ApiType, R>;
|
||||||
|
|
||||||
signAndSend(account: AddressOrPair, statusCb: Callback<ISubmittableResult>): SubmittableResultSubscription<ApiType>;
|
signAndSend(account: AddressOrPair, statusCb: Callback<R>): SubmittableResultSubscription<ApiType>;
|
||||||
|
|
||||||
signAndSend(account: AddressOrPair, options: Partial<SignerOptions>, statusCb?: Callback<ISubmittableResult>): SubmittableResultSubscription<ApiType>;
|
signAndSend(account: AddressOrPair, options: Partial<SignerOptions>, statusCb?: Callback<R>): SubmittableResultSubscription<ApiType, R>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user