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 { AnyJson, Registry } from '@polkadot/types/types';
|
||||
import { ApiObject } from '../types';
|
||||
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import Abi from '../Abi';
|
||||
@@ -12,13 +12,13 @@ import Abi from '../Abi';
|
||||
export default abstract class Base<ApiType extends ApiTypes> {
|
||||
public readonly abi: Abi;
|
||||
|
||||
public readonly api: ApiObject<ApiType>;
|
||||
public readonly api: ApiBase<ApiType>;
|
||||
|
||||
public readonly registry: Registry;
|
||||
|
||||
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
|
||||
? abi
|
||||
: new Abi(abi);
|
||||
|
||||
@@ -1,30 +1,22 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// 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 { AccountId, EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, CodecArg, IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
|
||||
import { AbiConstructor, ApiObject } from '../types';
|
||||
import { AnyJson, CodecArg, ISubmittableResult } from '@polkadot/types/types';
|
||||
import { AbiConstructor } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import { applyOnEvent, encodeMessage } from '../util';
|
||||
import Base from './Base';
|
||||
import Contract from './Contract';
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
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 {
|
||||
export class BlueprintSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
|
||||
public readonly 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> {
|
||||
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);
|
||||
|
||||
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> {
|
||||
return this.api.tx.contracts.instantiate(endowment, gasLimit, this.codeHash, encodeMessage(this.registry, this.abi.findConstructor(constructorOrIndex), params)) as SubmittableExtrinsic<ApiType>;
|
||||
}
|
||||
|
||||
public createContract (constructorOrIndex: AbiConstructor | number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): BlueprintCreate<ApiType> {
|
||||
return {
|
||||
// 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)
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
};
|
||||
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))
|
||||
.withResultTransform((result: ISubmittableResult) =>
|
||||
new BlueprintSubmittableResult(result, applyOnEvent(result, 'Instantiated', (record: EventRecord) =>
|
||||
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiTypes, DecorateMethod, SignerOptions } from '@polkadot/api/types';
|
||||
import { AccountId, Address, EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
|
||||
import { ApiObject } from '../types';
|
||||
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, ISubmittableResult } from '@polkadot/types/types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { compactAddLength, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import Abi from '../Abi';
|
||||
@@ -16,14 +15,7 @@ import { applyOnEvent } from '../util';
|
||||
import Base from './Base';
|
||||
import Blueprint from './Blueprint';
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
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 {
|
||||
export class CodeSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
|
||||
public readonly 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> {
|
||||
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);
|
||||
|
||||
this.code = u8aToU8a(wasm);
|
||||
}
|
||||
|
||||
public createBlueprint = (): CodePutCode<ApiType> => {
|
||||
return {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
signAndSend: this._decorateMethod(
|
||||
(account: IKeyringPair | string | AccountId, options?: SignerOptions): CodePutCodeResultSubscription<ApiType> =>
|
||||
this.api.rx.tx.contracts
|
||||
.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)
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
};
|
||||
public createBlueprint (): SubmittableExtrinsic<ApiType, CodeSubmittableResult<ApiType>> {
|
||||
return this.api.tx.contracts
|
||||
.putCode(compactAddLength(this.code))
|
||||
.withResultTransform((result: ISubmittableResult) =>
|
||||
new CodeSubmittableResult(result, applyOnEvent(result, 'CodeStored', (record: EventRecord) =>
|
||||
new Blueprint<ApiType>(this.api, this.abi, record.event.data[0] as Hash, this._decorateMethod)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ import { ApiTypes, DecorateMethod, ObsInnerType } from '@polkadot/api/types';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { AccountId, ContractExecResult } from '@polkadot/types/interfaces';
|
||||
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 { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { assert, isFunction } from '@polkadot/util';
|
||||
|
||||
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> {
|
||||
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);
|
||||
|
||||
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> {
|
||||
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> {
|
||||
|
||||
@@ -12,6 +12,6 @@ import Blueprint from '../base/Blueprint';
|
||||
|
||||
export default class PromiseBlueprint extends Blueprint<'promise'> {
|
||||
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'> {
|
||||
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'> {
|
||||
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'> {
|
||||
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'> {
|
||||
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'> {
|
||||
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 { Codec, TypeDef } from '@polkadot/types/types';
|
||||
|
||||
import { ApiPromise, ApiRx } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import Abi from './Abi';
|
||||
|
||||
export type ApiObject<ApiType extends ApiTypes> = ApiType extends 'rxjs'
|
||||
? ApiRx
|
||||
: ApiPromise;
|
||||
|
||||
export interface ContractBase<ApiType extends ApiTypes> {
|
||||
readonly abi: Abi;
|
||||
readonly api: ApiObject<ApiType>;
|
||||
readonly api: ApiBase<ApiType>;
|
||||
|
||||
getMessage: (name: string) => AbiMessage;
|
||||
messages: AbiMessage[];
|
||||
|
||||
Reference in New Issue
Block a user