The async/await patern works with JavaScript Promises.
In order to make web3.js functions asynchronous, you need to pass a callback parameter:
As this API is designed to work with a local RPC node, all its
functions use synchronous HTTP requests by default.
If you want to make an asynchronous request, you can pass an optional
callback as the last parameter to most functions. All callbacks are
using an error first callback style:
web3.eth.getBlock(48, function(error, result){
if(!error)
console.log(JSON.stringify(result));
else
console.error(error);
})
If you want to then make this work with the async/await pattern, you will need to then wrap this asynchronous function into a promise, which is of the format:
new Promise( /* executor */ function(resolve, reject) { ... } );
Rather than do this over an over for each web3.js function, you can automate this process by creating a wrapper for javascript functions which includes the callback parameter and wraps it in a promise as so:
const promisify = (inner) =>
new Promise((resolve, reject) =>
inner((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
})
);
This can then be used really simply with nearly all existing web3.js functions:
async function getBalance() {
var address, wei, balance
address = document.getElementById("address").value;
wei = promisify(cb => web3.eth.getBalance(address, cb)) // simply wrap web3.js functions with the promisify function, also passing in a callback parameter
try {
balance = web3.fromWei(await wei, 'ether')
document.getElementById("output").innerHTML = balance + " ETH";
} catch (error) {
document.getElementById("output").innerHTML = error;
}
}
All of this information can be found on my blog where you can also find other small projects I have done which use this pattern.
The problem here is the amount of Ether you are sending as a part of your request.
In your code you have the following line:
require(msg.value > .01 ether);
This means you must send a minimum of .01 ETH
to the contract, or it will revert
like you are seeing.
Taking you transaction hash, and looking up the transaction on Etherscan:
https://rinkeby.etherscan.io/tx/0xaed58f7d6c230f41b30c2d6f45e057e4d63188fa1542540767ee90aa9f567ba8
We can see that you are only sending 100 wei
to the contract when calling the function. You simply need to increase this to .01 ETH
, and it should get past the revert
error.
Best Answer
You can get this in status field of receipt. If status is 1, it means the transaction was successful, if 0, it means transaction failed.
This ability was introduced by EIP-658