I've been cloning many Defi apps on Github and reading their implementations of Web3 / Javascript with the contract.
Seems like most are just calling the contracts methods using the promise style, and less common is the event emitter style.
async function buyCredits() {
try {
const receipt = await contract.methods.buyCredits().send({
from: account,
gas: 233000,
value: choice.value,
});
// Is it safe to assume this block was mined here
// and the user has the new
const credits = await contract.methods.usersCredits().call({
from: account,
});
alert('It was mined!');
console.log(`This user has ${credits} credits`);
} catch (e) {
alert("It NOT mined!");
}
}
Where buyCredits
is just an external payable contract function that gives users credits for ether.
Is this the most reliable way? From the Web3 docs, it seems like the promise to contract methods just return the tx receipt and that does not guarantee the block is mined.
Are there any tutorials, pointers, etc.. on doing this in the most reliable and recommended way? Thanks!