Created
January 15, 2018 10:52
-
-
Save naps62/e987323357fb7630871eb293e0308c36 to your computer and use it in GitHub Desktop.
waiting for a transaction to be mined in truffle tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toTxHash = (value) => { | |
if (typeof value === "string") { | |
// this is probably a tx hash already | |
return value; | |
} else if (typeof value.receipt === "object") { | |
// this is probably a tx object | |
return value.receipt.transactionHash; | |
} else { | |
throw "Unsupported tx type: " + value; | |
} | |
} | |
const mineTx = (promiseOrTx, interval) => { | |
return Promise.resolve(promiseOrTx) | |
.then(tx => { | |
const txHash = toTxHash(tx); | |
return new Promise((resolve, reject) => { | |
const getReceipt = () => { | |
web3.eth.getTransactionReceipt(txHash, (error, receipt) => { | |
if (error) { | |
reject(error); | |
} else if (receipt) { | |
resolve(receipt); | |
} else { | |
setTimeout(getReceipt, interval || 500); | |
} | |
}) | |
} | |
getReceipt(); | |
}) | |
}); | |
} | |
module.exports = mineTx; |
Hi, do you have any objection to me including this code in an open-source project please?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage:
const mineTx = require("./helpers/mineTx.js");