From 248158130e437b14f9d42666e0cdefa33a6be74d Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 4 Oct 2018 19:54:15 -0400 Subject: [PATCH] Added automatic event parsing for contract transaction receipts from tx.wait. --- src.ts/contract.ts | 45 ++++++++++++++++++++++++++- src.ts/ethers.ts | 3 +- src.ts/providers/abstract-provider.ts | 2 +- src.ts/providers/base-provider.ts | 4 +-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src.ts/contract.ts b/src.ts/contract.ts index db23bcba6b..8aea4bdf11 100644 --- a/src.ts/contract.ts +++ b/src.ts/contract.ts @@ -53,6 +53,14 @@ export interface Event extends Log { getTransactionReceipt: () => Promise; } +export interface ContractReceipt extends TransactionReceipt { + events?: Array; +} + +export interface ContractTransaction extends TransactionResponse { + wait(confirmations?: number): Promise; +} + /////////////////////////////// export class VoidSigner extends Signer { @@ -256,7 +264,42 @@ function runMethod(contract: Contract, functionName: string, estimateOnly: boole errors.throwError('cannot override from in a transaction', errors.UNSUPPORTED_OPERATION, { operation: 'sendTransaction' }) } - return contract.signer.sendTransaction(tx); + return contract.signer.sendTransaction(tx).then((tx) => { + let wait = tx.wait.bind(tx); + + tx.wait = (confirmations?: number) => { + return wait(confirmations).then((receipt: ContractReceipt) => { + receipt.events = receipt.logs.map((log) => { + let event: Event = (deepCopy(log)); + + let parsed = this.interface.parseLog(log); + if (parsed) { + event.args = parsed.values; + event.decode = parsed.decode; + event.event = parsed.name; + event.eventSignature = parsed.signature; + } + + event.removeListener = () => { return this.provider; } + event.getBlock = () => { + return this.provider.getBlock(receipt.blockHash); + } + event.getTransaction = () => { + return this.provider.getTransaction(receipt.transactionHash); + } + event.getTransactionReceipt = () => { + return Promise.resolve(receipt); + } + + return event; + }); + + return receipt; + }); + }; + + return tx; + }); } throw new Error('invalid type - ' + method.type); diff --git a/src.ts/ethers.ts b/src.ts/ethers.ts index 2e2be8e872..8f909bca88 100644 --- a/src.ts/ethers.ts +++ b/src.ts/ethers.ts @@ -26,7 +26,7 @@ import { version } from './_version'; //////////////////////// // Types -import { ContractFunction, Event, EventFilter } from './contract'; +import { ContractFunction, ContractTransaction, Event, EventFilter } from './contract'; //////////////////////// @@ -72,6 +72,7 @@ export { // Types ContractFunction, + ContractTransaction, Event, EventFilter }; diff --git a/src.ts/providers/abstract-provider.ts b/src.ts/providers/abstract-provider.ts index e85ba7fb13..8adb1ca4aa 100644 --- a/src.ts/providers/abstract-provider.ts +++ b/src.ts/providers/abstract-provider.ts @@ -103,7 +103,7 @@ export interface TransactionResponse extends Transaction { raw?: string, // This function waits until the transaction has been mined - wait: (timeout?: number) => Promise + wait: (confirmations?: number) => Promise }; export type EventType = string | Array | Filter; diff --git a/src.ts/providers/base-provider.ts b/src.ts/providers/base-provider.ts index 7544a1c019..e685f4adfc 100644 --- a/src.ts/providers/base-provider.ts +++ b/src.ts/providers/base-provider.ts @@ -702,13 +702,13 @@ export class BaseProvider extends Provider { _setFastBlockNumber(blockNumber: number): void { // Older block, maybe a stale request - if (blockNumber < this._fastBlockNumber) { return; } + if (this._fastBlockNumber != null && blockNumber < this._fastBlockNumber) { return; } // Update the time we updated the blocknumber this._fastQueryDate = getTime(); // Newer block number, use it - if (blockNumber > this._fastBlockNumber) { + if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) { this._fastBlockNumber = blockNumber; this._fastBlockNumberPromise = Promise.resolve(blockNumber); }