Skip to content

Instantly share code, notes, and snippets.

@jmakwana01
Created August 7, 2023 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmakwana01/29468ebd5fedb05099b361227db9ad5d to your computer and use it in GitHub Desktop.
Save jmakwana01/29468ebd5fedb05099b361227db9ad5d to your computer and use it in GitHub Desktop.
import { Biconomy } from "@biconomy/mexa";
const mint = async (e) => {
const wallet_address = magicConnected ? magic.account : account;
//biconomy intialization
let options = {
apiKey: 'xxxxxxxxxxxx',
debug: true,
contractAddresses: [address]
};
const biconomy = new Biconomy(window.ethereum, options);
const web3 = new Web3(biconomy.provider);
await biconomy.init();
//setting the contract
const nftContract: any = magicConnected ? await magic.getContractInstance(contract_address)
: type === 'erc721' ? new web3.eth.Contract(ERC721ABI, contract_address) : new web3.eth.Contract(ERC1155ABI, contract_address);
try {
const transactionObject = {
from: wallet_address,
value: web3.utils.toWei((mintPrice * amount).toString(), 'wei'),
// gasPrice: gasPrice,
signatureType: "PERSONAL_SIGN",
gasLimit: 1000000,
};
let gas = await nftContract.methods.mint(amount).estimateGas(transactionObject)
.then((gas) => {
console.log('Estimated gas:', gas);
})
.catch((error) => {
console.error('Error estimating gas:', error);
});
const result = await nftContract.methods.mint(amount).send("eth_sendTransaction", {
"from": wallet_address, // Replace with the user's address (account) you want to use for the transaction
"signatureType": "PERSONAL_SIGN",
"gasPrice": gasPrice,
"gasLimit": 1000000,
});
// Transaction Hash generated event
biconomy.on("txHashGenerated", (data: { transactionId: string; transactionHash: string }) => {
console.log("Transaction Hash generated:", data.transactionHash);
showSuccessMessage(`Transaction hash: ${data.transactionHash}`);
});
// Transaction Mined event
biconomy.on("txMined", (data: { msg: string; id: string; hash: string; receipt: string }) => {
console.log("Transaction Mined:", data);
});
// Error event
biconomy.on("onError", (data: { error: any; transactionId: string }) => {
console.log("Error:", data.error);
});
// Transaction Hash Changed event
biconomy.on("txHashChanged", (data: { transactionId: string; transactionHash: string }) => {
console.log("Transaction Hash Changed:", data.transactionHash);
});
} catch (error) {
// Handle error during transaction signing or relay
console.error('Error during transaction signing or relay:', error);
// Show error to the user or handle it gracefully
setIsWorking(false);
showError("Error while minting the NFT. Try again!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment