Skip to content

Commit

Permalink
Added automatic event parsing for contract transaction receipts from …
Browse files Browse the repository at this point in the history
…tx.wait.
  • Loading branch information
ricmoo committed Oct 4, 2018
1 parent f5c7ccb commit 2481581
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
45 changes: 44 additions & 1 deletion src.ts/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export interface Event extends Log {
getTransactionReceipt: () => Promise<TransactionReceipt>;
}

export interface ContractReceipt extends TransactionReceipt {
events?: Array<Event>;
}

export interface ContractTransaction extends TransactionResponse {
wait(confirmations?: number): Promise<ContractReceipt>;
}

///////////////////////////////

export class VoidSigner extends Signer {
Expand Down Expand Up @@ -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 = (<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);
Expand Down
3 changes: 2 additions & 1 deletion src.ts/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { version } from './_version';
////////////////////////
// Types

import { ContractFunction, Event, EventFilter } from './contract';
import { ContractFunction, ContractTransaction, Event, EventFilter } from './contract';


////////////////////////
Expand Down Expand Up @@ -72,6 +72,7 @@ export {
// Types

ContractFunction,
ContractTransaction,
Event,
EventFilter
};
Expand Down
2 changes: 1 addition & 1 deletion src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface TransactionResponse extends Transaction {
raw?: string,

// This function waits until the transaction has been mined
wait: (timeout?: number) => Promise<TransactionReceipt>
wait: (confirmations?: number) => Promise<TransactionReceipt>
};

export type EventType = string | Array<string> | Filter;
Expand Down
4 changes: 2 additions & 2 deletions src.ts/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2481581

Please sign in to comment.