aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web3-wrapper
diff options
context:
space:
mode:
authorAlex Browne <stephenalexbrowne@gmail.com>2018-05-23 08:52:27 +0800
committerAlex Browne <stephenalexbrowne@gmail.com>2018-05-24 06:09:17 +0800
commitb05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96 (patch)
tree8ab5d6d7cbe3834f91947518898aba0ecd764d8e /packages/web3-wrapper
parentf6b81f588d98e09e7a5806902d94e2892d029481 (diff)
downloaddexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar.gz
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar.bz2
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar.lz
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar.xz
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.tar.zst
dexon-sol-tools-b05a3b7aed9d2dcbc6ce4177eeb7db85675b7a96.zip
Check transaction receipt status codes where applicable
Diffstat (limited to 'packages/web3-wrapper')
-rw-r--r--packages/web3-wrapper/src/web3_wrapper.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts
index 91a1af870..6ab63ab1b 100644
--- a/packages/web3-wrapper/src/web3_wrapper.ts
+++ b/packages/web3-wrapper/src/web3_wrapper.ts
@@ -328,6 +328,10 @@ export class Web3Wrapper {
}
/**
* Waits for a transaction to be mined and returns the transaction receipt.
+ * Note that just because a transaction was mined does not mean it was
+ * successful. You need to check the status code of the transaction receipt
+ * to find out if it was successful, or use the helper method
+ * awaitTransactionSuccessAsync.
* @param txHash Transaction hash
* @param pollingIntervalMs How often (in ms) should we check if the transaction is mined.
* @param timeoutMs How long (in ms) to poll for transaction mined until aborting.
@@ -377,6 +381,28 @@ export class Web3Wrapper {
const txReceipt = await txReceiptPromise;
return txReceipt;
}
+ /**
+ * Waits for a transaction to be mined and returns the transaction receipt.
+ * Unlike awaitTransactionMinedAsync, it will throw if the receipt has a
+ * status that is not equal to 1. A status of 0 or null indicates that the
+ * transaction was mined, but failed. See:
+ * https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgettransactionreceipt
+ * @param txHash Transaction hash
+ * @param pollingIntervalMs How often (in ms) should we check if the transaction is mined.
+ * @param timeoutMs How long (in ms) to poll for transaction mined until aborting.
+ * @return Transaction receipt with decoded log args.
+ */
+ public async awaitTransactionSuccessAsync(
+ txHash: string,
+ pollingIntervalMs: number = 1000,
+ timeoutMs?: number,
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const receipt = await this.awaitTransactionMinedAsync(txHash, pollingIntervalMs, timeoutMs);
+ if (receipt.status !== 1) {
+ throw new Error('Transaction failed (transaction receipt did not have a status of 1)');
+ }
+ return receipt;
+ }
private async _sendRawPayloadAsync<A>(payload: Partial<JSONRPCRequestPayload>): Promise<A> {
const sendAsync = this._web3.currentProvider.sendAsync.bind(this._web3.currentProvider);
const response = await promisify<JSONRPCResponsePayload>(sendAsync)(payload);