Skip to content

Instantly share code, notes, and snippets.

@shanerbaner82
Created August 31, 2021 17:36
Show Gist options
  • Save shanerbaner82/c7059fe3439fb4f799199d6a77ff24e5 to your computer and use it in GitHub Desktop.
Save shanerbaner82/c7059fe3439fb4f799199d6a77ff24e5 to your computer and use it in GitHub Desktop.
Mint w Gas
async mint(context, amount) {
const mintValue = web3.utils.toWei((amount * context.state.contractData.nftPrice).toString(), 'ether');
const contract = new web3.eth.Contract(ABI, process.env.CONTRACT_ADDRESS);
try {
const value = web3.utils.toHex(mintValue);
const data = contract.methods.mintFunction(amount).encodeABI();
const transactionParameters = {
to: process.env.CONTRACT_ADDRESS,
from: context.state.walletAddress,
value,
data
};
const gasEstimate = await window.ethereum.request({method: 'eth_estimateGas',params: [transactionParameters]});
const maxPriorityGas = await web3.eth.getMaxPriorityFeePerGas();
const baseFee = Number(await web3.eth.getBlock("pending").baseFeePerGas)
const max = Number(maxPriorityGas) + baseFee - 1;
transactionParameters.gas = gasEstimate;
transactionParameters.maxPriorityFeePerGas = maxPriorityGas;
transactionParameters.maxFeePerGas = max;
const txHash = await window.ethereum
.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
});
window.open("https://etherscan.io/tx/" + txHash);
} catch (error) {
alert(error.message)
}
}
@lixuejiang
Copy link

nice job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment