Contracts attachment (#818)
* WIP... just getting ti to work * Export ContractAbi from types * e2e testing * Add selector encoding (calls) * Linting * Exapnd e2e tests * TODO comments * Add ABI types & isConstant flag * Updated with actual steps (progress) * Linting * Cleanup event code * Bump deps * Bump deps (keyring mnemonic fix)
This commit is contained in:
@@ -36,6 +36,6 @@
|
||||
"@polkadot/rpc-rx": "^0.76.0-beta.2",
|
||||
"@polkadot/storage": "^0.76.0-beta.2",
|
||||
"@polkadot/types": "^0.76.0-beta.2",
|
||||
"@polkadot/util-crypto": "^0.76.0-beta.4"
|
||||
"@polkadot/util-crypto": "^0.76.0-beta.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@ import { RxResult } from './rx/types';
|
||||
import {
|
||||
ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiType,
|
||||
DecoratedRpc, DecoratedRpc$Method, DecoratedRpc$Section,
|
||||
Derive, DeriveSection,
|
||||
HashResult, U64Result,
|
||||
Derive, DeriveSection, HashResult, U64Result,
|
||||
OnCallDefinition, OnCallFunction,
|
||||
QueryableModuleStorage, QueryableStorage, QueryableStorageFunction,
|
||||
SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics, Signer
|
||||
@@ -31,7 +30,7 @@ import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
|
||||
import { assert, compactStripLength, isFunction, isObject, isUndefined, logger, u8aToHex } from '@polkadot/util';
|
||||
import { cryptoWaitReady } from '@polkadot/util-crypto';
|
||||
|
||||
import { createSubmittableExtrinsic, SubmittableExtrinsic } from './SubmittableExtrinsic';
|
||||
import createSubmittable, { SubmittableExtrinsic } from './SubmittableExtrinsic';
|
||||
|
||||
type MetaDecoration = {
|
||||
callIndex?: Uint8Array,
|
||||
@@ -68,8 +67,8 @@ function rxOnCall (
|
||||
}
|
||||
|
||||
export default abstract class ApiBase<CodecResult, SubscriptionResult> implements ApiBaseInterface<CodecResult, SubscriptionResult> {
|
||||
private _eventemitter: EventEmitter;
|
||||
private _derive?: Derive<CodecResult, SubscriptionResult>;
|
||||
private _eventemitter: EventEmitter;
|
||||
private _extrinsics?: SubmittableExtrinsics<CodecResult, SubscriptionResult>;
|
||||
private _genesisHash?: Hash;
|
||||
private _isReady: boolean = false;
|
||||
@@ -474,7 +473,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> =>
|
||||
createSubmittableExtrinsic(this.type, this._rx as ApiInterface$Rx, onCall, method(...params));
|
||||
createSubmittable(this.type, this._rx as ApiInterface$Rx, onCall, method(...params));
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as SubmittableExtrinsicFunction<C, S>;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,15 @@ export class SubmittableResult extends Struct {
|
||||
get status (): ExtrinsicStatus {
|
||||
return this.get('status') as ExtrinsicStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Finds an EventRecord for the specified method & section
|
||||
*/
|
||||
findRecord (section: string, method: string): EventRecord | undefined {
|
||||
return this.events.find(({ event }) =>
|
||||
event.section === section && event.method === method
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsic<CodecResult, SubscriptionResult> extends IExtrinsic {
|
||||
@@ -60,7 +69,7 @@ export interface SubmittableExtrinsic<CodecResult, SubscriptionResult> extends I
|
||||
signAndSend (account: KeyringPair | string | AccountId | Address, statusCb: StatusCb): SumbitableResultSubscription<CodecResult, SubscriptionResult>;
|
||||
}
|
||||
|
||||
export function createSubmittableExtrinsic<CodecResult, SubscriptionResult> (type: ApiType, api: ApiInterface$Rx, onCall: OnCallDefinition<CodecResult, SubscriptionResult>, extrinsic: Method): SubmittableExtrinsic<CodecResult, SubscriptionResult> {
|
||||
export default function createSubmittableExtrinsic<CodecResult, SubscriptionResult> (type: ApiType, api: ApiInterface$Rx, onCall: OnCallDefinition<CodecResult, SubscriptionResult>, extrinsic: Method, trackingCb?: (result: SubmittableResult) => any): SubmittableExtrinsic<CodecResult, SubscriptionResult> {
|
||||
const _extrinsic = new (getTypeRegistry().getOrThrow('Extrinsic'))(extrinsic) as SubmittableExtrinsic<CodecResult, SubscriptionResult>;
|
||||
const _noStatusCb = type === 'rxjs';
|
||||
|
||||
@@ -72,12 +81,11 @@ export function createSubmittableExtrinsic<CodecResult, SubscriptionResult> (typ
|
||||
|
||||
function statusObservable (status: ExtrinsicStatus): Observable<SubmittableResult> {
|
||||
if (!status.isFinalized) {
|
||||
return of(
|
||||
new SubmittableResult({
|
||||
status,
|
||||
type: status.type
|
||||
})
|
||||
);
|
||||
const result = new SubmittableResult({ status });
|
||||
|
||||
trackingCb && trackingCb(result);
|
||||
|
||||
return of(result);
|
||||
}
|
||||
|
||||
const blockHash = status.asFinalized;
|
||||
@@ -86,13 +94,16 @@ export function createSubmittableExtrinsic<CodecResult, SubscriptionResult> (typ
|
||||
api.rpc.chain.getBlock(blockHash) as Observable<SignedBlock>,
|
||||
api.query.system.events.at(blockHash) as Observable<Vector<EventRecord>>
|
||||
).pipe(
|
||||
map(([signedBlock, allEvents]) =>
|
||||
new SubmittableResult({
|
||||
map(([signedBlock, allEvents]) => {
|
||||
const result = new SubmittableResult({
|
||||
events: filterEvents(_extrinsic.hash, signedBlock, allEvents),
|
||||
status,
|
||||
type: status.type
|
||||
})
|
||||
)
|
||||
status
|
||||
});
|
||||
|
||||
trackingCb && trackingCb(result);
|
||||
|
||||
return result;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,7 @@ import { DeriveCustom } from '@polkadot/api-derive';
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
||||
import { RpcRxInterface$Events } from '@polkadot/rpc-rx/types';
|
||||
import { Hash, Metadata, RuntimeVersion, u64 as U64 } from '@polkadot/types';
|
||||
import {
|
||||
CodecArg,
|
||||
CodecCallback,
|
||||
IExtrinsic,
|
||||
RegistryTypes,
|
||||
SignatureOptions
|
||||
} from '@polkadot/types/types';
|
||||
import { CodecArg, CodecCallback, IExtrinsic, RegistryTypes, SignatureOptions } from '@polkadot/types/types';
|
||||
import { MethodFunction } from '@polkadot/types/primitive/Method';
|
||||
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
|
||||
|
||||
|
||||
@@ -22,6 +22,6 @@ export default function filterEvents (extHash: U8a, { block: { extrinsics, heade
|
||||
|
||||
return allEvents.filter(({ phase }) =>
|
||||
// only ApplyExtrinsic has the extrinsic index
|
||||
phase.type === 'ApplyExtrinsic' && phase.asApplyExtrinsic.eqn(index)
|
||||
phase.isApplyExtrinsic && phase.asApplyExtrinsic.eqn(index)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
{
|
||||
"name": "Erc20",
|
||||
"deploy": {
|
||||
"args": [
|
||||
{
|
||||
"name": "init_value",
|
||||
"type": "Balance"
|
||||
}
|
||||
]
|
||||
},
|
||||
"messages": [
|
||||
{
|
||||
"name": "total_supply",
|
||||
"selector": 949181842,
|
||||
"mutates": false,
|
||||
"args": [],
|
||||
"return_type": "Balance"
|
||||
},
|
||||
{
|
||||
"name": "balance_of",
|
||||
"selector": 3827153702,
|
||||
"mutates": false,
|
||||
"args": [
|
||||
{
|
||||
"name": "owner",
|
||||
"type": "Address"
|
||||
}
|
||||
],
|
||||
"return_type": "Balance"
|
||||
},
|
||||
{
|
||||
"name": "allowance",
|
||||
"selector": 3057219184,
|
||||
"mutates": false,
|
||||
"args": [
|
||||
{
|
||||
"name": "owner",
|
||||
"type": "Address"
|
||||
},
|
||||
{
|
||||
"name": "spender",
|
||||
"type": "Address"
|
||||
}
|
||||
],
|
||||
"return_type": "Balance"
|
||||
},
|
||||
{
|
||||
"name": "transfer",
|
||||
"selector": 3374842663,
|
||||
"mutates": true,
|
||||
"args": [
|
||||
{
|
||||
"name": "to",
|
||||
"type": "Address"
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": "Balance"
|
||||
}
|
||||
],
|
||||
"return_type": "bool"
|
||||
},
|
||||
{
|
||||
"name": "approve",
|
||||
"selector": 431186122,
|
||||
"mutates": true,
|
||||
"args": [
|
||||
{
|
||||
"name": "spender",
|
||||
"type": "Address"
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": "Balance"
|
||||
}
|
||||
],
|
||||
"return_type": "bool"
|
||||
},
|
||||
{
|
||||
"name": "transfer_from",
|
||||
"selector": 197482595,
|
||||
"mutates": true,
|
||||
"args": [
|
||||
{
|
||||
"name": "from",
|
||||
"type": "Address"
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"type": "Address"
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": "Balance"
|
||||
}
|
||||
],
|
||||
"return_type": "bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,92 @@
|
||||
// Copyright 2017-2019 @polkadot/api 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 fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { ContractAbi } from '@polkadot/types';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import json from '../data/erc20.json';
|
||||
import Api from '../../src/promise';
|
||||
|
||||
describe.skip('e2e contracts', () => {
|
||||
let address;
|
||||
let codeHash;
|
||||
let keyring;
|
||||
let abi;
|
||||
let api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
if (!api) {
|
||||
api = await Api.create();
|
||||
|
||||
abi = new ContractAbi(json);
|
||||
keyring = testingPairs({ type: 'sr25519' });
|
||||
}
|
||||
|
||||
jest.setTimeout(10000);
|
||||
done();
|
||||
});
|
||||
|
||||
it('has the attached methods', () => {
|
||||
expect(Object.keys(abi.messages)).toEqual(
|
||||
['totalSupply', 'balanceOf', 'allowance', 'transfer', 'approve', 'transferFrom']
|
||||
);
|
||||
});
|
||||
|
||||
it('allows putCode', (done) => {
|
||||
const code = fs.readFileSync(path.join(__dirname, '../data/erc20.wasm')).toString('hex');
|
||||
|
||||
api.tx.contract
|
||||
.putCode(200000, `0x${code}`)
|
||||
.signAndSend(keyring.eve, (result) => {
|
||||
console.error('putCode', JSON.stringify(result));
|
||||
|
||||
if (result.status.isFinalized) {
|
||||
const record = result.findRecord('contract', 'CodeStored');
|
||||
|
||||
if (record) {
|
||||
codeHash = record.event.data[0];
|
||||
|
||||
done();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('allows contract create', (done) => {
|
||||
expect(codeHash).toBeDefined();
|
||||
|
||||
api.tx.contract
|
||||
.create(1234, 12345, codeHash, abi.deploy(12345))
|
||||
.signAndSend(keyring.bob, (result) => {
|
||||
console.error('create', JSON.stringify(result));
|
||||
|
||||
if (result.status.isFinalized) {
|
||||
const record = result.findRecord('contract', 'Instantiated');
|
||||
|
||||
if (record) {
|
||||
address = record.event.data[1];
|
||||
|
||||
done();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('allows contract call', (done) => {
|
||||
expect(address).toBeDefined();
|
||||
|
||||
api.tx.contract
|
||||
.call(address, 123, 12345, abi.messages.balanceOf(keyring.alice.address()))
|
||||
.signAndSend(keyring.bob, (result) => {
|
||||
console.error('call', JSON.stringify(result));
|
||||
|
||||
if (result.status.isFinalized && result.findRecord('system', 'ExtrinsicSuccess')) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user