Add withResultTransform to Submittables (use in api-contracts) (#2707)
* Allow SubmittableResult transforms (use in contracts) * withResultTransform
This commit is contained in:
@@ -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)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user