More ergonomic contracts interfaces (RxJs only initial) (#1015)

* More ergnomic contracts interfaces

* extend SubmittableResult

* Allow creation of contract from blueprint

* "decorate" Contract (just playing with interfaces)

* Make actual messages calls

* Rename to Rx{Blueprint, Cond, Contract}

* Add exports

* Support contract & contracts endpoints

* Remove unneeded ApiType generics (may return)

* Comment 2.x/1.x detection
This commit is contained in:
Jaco Greeff
2019-06-21 16:48:00 +02:00
committed by GitHub
parent 5e5bb9984b
commit f30c516099
10 changed files with 288 additions and 45 deletions
+82
View File
@@ -0,0 +1,82 @@
// Copyright 2017-2019 @polkadot/api-contract authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ISubmittableResult, SubmittableResult } from '@polkadot/api/SubmittableExtrinsic';
import { IKeyringPair } from '@polkadot/types/types';
import { ContractABI } from './types';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiRx } from '@polkadot/api';
import { AccountId, Address, Hash } from '@polkadot/types';
import { compactAddLength, u8aToU8a } from '@polkadot/util';
import Abi from './Abi';
import RxBase from './RxBase';
import RxBlueprint from './RxBlueprint';
// Ok, tried, failed, eventually ... well, we are only trying with RxJs as a
// start, so take a big fat shortcut with this version, real intended version
// follows this and is commented out
type ICodePutCodeResultSubscription = Observable<CodePutCodeResult>;
// type ICodePutCodeResultSubscription<ApiType> =
// ApiType extends 'rxjs'
// ? Observable<CodePutCodeResult>
// : Promise<() => void>;
// unlike the ISubmittableExtrinsic, it doesn't extend IExtrinsic and only
// implements one var iant of signAndSend - this is purely done to see what
// is hanging where, with the bare-mainimum from a simplicity perspective
// Very obvious is that it supports only 1 version of signAndSend
// (we need to revisit this, but probably ok for initial?)
export interface ICodePutCode {
signAndSend (account: IKeyringPair | string | AccountId | Address): ICodePutCodeResultSubscription;
}
class CodePutCodeResult extends SubmittableResult {
readonly blueprint?: RxBlueprint;
constructor (result: ISubmittableResult, blueprint?: RxBlueprint) {
super(result);
this.blueprint = blueprint;
}
}
// NOTE Experimental, POC, bound to change
export default class RxCode extends RxBase {
readonly code: Uint8Array;
constructor (api: ApiRx, abi: ContractABI | Abi, wasm: string | Uint8Array) {
super(api, abi);
this.code = u8aToU8a(wasm);
}
public createBlueprint (maxGas: number | BN): ICodePutCode {
const signAndSend = (account: IKeyringPair | string | AccountId | Address): ICodePutCodeResultSubscription => {
return this.apiContracts
.putCode(maxGas, compactAddLength(this.code))
.signAndSend(account)
.pipe(
map((result: ISubmittableResult) => {
let blueprint: RxBlueprint | undefined;
if (result.isFinalized) {
const record = result.findRecord('contract', 'CodeStored');
if (record) {
blueprint = new RxBlueprint(this.api, this.abi, record.event.data[0] as Hash);
}
}
return new CodePutCodeResult(result, blueprint);
})
);
};
return { signAndSend };
}
}