Added automatic event parsing for contract transaction receipts from tx.wait.

This commit is contained in:
Richard Moore 2018-10-04 19:54:15 -04:00
parent f5c7ccbb80
commit 248158130e
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
4 changed files with 49 additions and 5 deletions

View File

@ -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 {
@ -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);

View File

@ -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
};

View File

@ -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;

View File

@ -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);
}