Skip to content

Instantly share code, notes, and snippets.

@ylv-io
Last active January 14, 2019 12:16
Show Gist options
  • Save ylv-io/469aae000f3ac4708b713b8f1e0914b7 to your computer and use it in GitHub Desktop.
Save ylv-io/469aae000f3ac4708b713b8f1e0914b7 to your computer and use it in GitHub Desktop.
const getTransactionReceipt = async (hash) => {
let receipt = null;
while (receipt === null) {
// we are going to check every second if transation is mined or not, once it is mined we'll leave the loop
receipt = await getTransactionReceiptPromise(hash);
await wait(1000);
}
return receipt;
};
function getTransactionReceiptPromise(hash) {
// here we just promisify getTransactionReceipt function for convenience
return new Promise(((resolve, reject) => {
web3js.eth.getTransactionReceipt(hash, function(err, data) {
if (err !== null) reject(err);
else resolve(data);
});
}));
}
handleSubmit = async () => {
try {
const account = getAccount();
const hash = await yourContractInstance.yourMethod.sendTransaction(arg1, arg2, {
from: account,
// if payable, specify value
// value: web3js.toWei(value, 'ether')
});
// update progress UI
setSubmitting(false);
// reset transaction related form, if any
resetForm();
// waiting for transaction receipt
const receipt = await getTransactionReceipt(hash);
// once it is avaiable, we can update UI, state
updateEverything();
} catch (e) {
// if user cancel transaction at Metamask UI we'll get error and handle it here
console.log(e);
// update progress UI anyway
setSubmitting(false);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment