Retrieve events as part of send (#493)

* WIP

* Pass through events for transaction

* Remove unused import

* Re-add block hash, extends ExtrinsicStatus to match

* Revert timeout reset change
This commit is contained in:
Jaco Greeff
2018-12-19 20:02:33 +01:00
committed by GitHub
parent 775703bc21
commit 7a4dc5f1c3
3 changed files with 34 additions and 9 deletions
@@ -6,9 +6,10 @@ import { KeyringPair } from '@polkadot/keyring/types';
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
import { ApiPromiseInterface } from './types';
import { Extrinsic, ExtrinsicStatus, Hash } from '@polkadot/types/index';
import { EventRecord, Extrinsic, ExtrinsicStatus, Hash, SignedBlock, u32 } from '@polkadot/types/index';
type SendResult = {
events?: Array<EventRecord>,
status: ExtrinsicStatus,
type: string
};
@@ -22,17 +23,40 @@ export default class SubmittableExtrinsic extends Extrinsic {
this._api = api;
}
private checkStatus (statusCb: (result: SendResult) => any): (status: ExtrinsicStatus) => Promise<void> {
return async (status: ExtrinsicStatus): Promise<void> => {
let events: Array<any> | undefined = undefined;
if (status.type === 'Finalised') {
const blockHash = status.value as Hash;
const { block: { extrinsics } }: SignedBlock = await this._api.rpc.chain.getBlock(blockHash);
const allEvents: Array<EventRecord> = await this._api.query.system.events.at(blockHash) as any;
const index = extrinsics
.map((ext) => ext.hash.toHex())
.indexOf(this.hash.toHex());
if (index !== -1) {
events = allEvents.filter(({ phase }) =>
phase.type === 'ApplyExtrinsic' && (phase.value as u32).eqn(index)
);
}
}
statusCb({
events,
status,
type: status.type
});
};
}
send (statusCb?: (result: SendResult) => any): Promise<Hash> {
if (!statusCb || !this._api.hasSubscriptions) {
return this._api.rpc.author.submitExtrinsic(this);
}
return this._api.rpc.author.submitAndWatchExtrinsic(this, (status: ExtrinsicStatus) =>
statusCb({
status,
type: status.type
})
);
return this._api.rpc.author.submitAndWatchExtrinsic(this, this.checkStatus(statusCb));
}
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash?: AnyU8a): SubmittableExtrinsic {
+2 -1
View File
@@ -8,9 +8,10 @@ import { ApiRxInterface } from './types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Extrinsic, ExtrinsicStatus } from '@polkadot/types/index';
import { EventRecord, Extrinsic, ExtrinsicStatus } from '@polkadot/types/index';
type SendResult = {
events?: Array<EventRecord>,
status: ExtrinsicStatus,
type: string
};
+1 -1
View File
@@ -28,7 +28,7 @@ describe.skip('e2e transactions', () => {
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, nonce)
.send((status) => {
if (status.type === 'Finalised') {
if (status.type === 'Finalised' && status.events && status.events.length) {
done();
}
});