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

@ -53,6 +53,14 @@ export interface Event extends Log {
getTransactionReceipt: () => Promise<TransactionReceipt>; 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 { 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' }) 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); throw new Error('invalid type - ' + method.type);

@ -26,7 +26,7 @@ import { version } from './_version';
//////////////////////// ////////////////////////
// Types // Types
import { ContractFunction, Event, EventFilter } from './contract'; import { ContractFunction, ContractTransaction, Event, EventFilter } from './contract';
//////////////////////// ////////////////////////
@ -72,6 +72,7 @@ export {
// Types // Types
ContractFunction, ContractFunction,
ContractTransaction,
Event, Event,
EventFilter EventFilter
}; };

@ -103,7 +103,7 @@ export interface TransactionResponse extends Transaction {
raw?: string, raw?: string,
// This function waits until the transaction has been mined // 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; export type EventType = string | Array<string> | Filter;

@ -702,13 +702,13 @@ export class BaseProvider extends Provider {
_setFastBlockNumber(blockNumber: number): void { _setFastBlockNumber(blockNumber: number): void {
// Older block, maybe a stale request // 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 // Update the time we updated the blocknumber
this._fastQueryDate = getTime(); this._fastQueryDate = getTime();
// Newer block number, use it // Newer block number, use it
if (blockNumber > this._fastBlockNumber) { if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber; this._fastBlockNumber = blockNumber;
this._fastBlockNumberPromise = Promise.resolve(blockNumber); this._fastBlockNumberPromise = Promise.resolve(blockNumber);
} }