Skip to content

Instantly share code, notes, and snippets.

@Hero-Development
Last active December 28, 2021 10:06
Show Gist options
  • Save Hero-Development/516ef4ded271b12bcc40a1700e675500 to your computer and use it in GitHub Desktop.
Save Hero-Development/516ef4ded271b12bcc40a1700e675500 to your computer and use it in GitHub Desktop.
Minting example using Web3.js, prechecks the transaction for errors
const handleMintClick = async (evt) => {
if( evt && evt.cancelable )
evt.preventDefault();
try{
//usually returns a BN (BigNumber)
const priceBN = await contract.methods.PRICE().call();
const quantityBN = new Web3.utils.BN( `${quantity}` );
const valueBN = priceBN.mul( quantityBN );
const sendArgs = {
from: accounts[0],
value: valueBN.toString()
}
//send the same arguments for both
await contract.methods.mint( mintAmount ).estimateGas( sendArgs );
const txn = await contract.methods.mint( mintAmount ).send( sendArgs );
}
catch( err ){
err = EthereumSession.getError( err )
if( err.code === 4001 ){
alert( "Please try again and confirm the transaction" );
}
else{
let error = String( err );
if( err.message ){
const code = err.code ? err.code +': ' : '';
error = code + err.message;
}
alert( "Oops! Mint failed...\n\n"+ error );
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment