I have a little portfolio dApp that mints NFTs.
After a successful mint, I want the UI to display the tokenId of the minted NFT.
I'm minting successfully and the event is being triggered - {tokenId} logs, but setNFTzMinted is never called.
const askContractToMintNft = async () => {
try {
const { ethereum } = window;
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
// 3 params we need to connect to our contract
const connectedContract = new ethers.Contract(CONTRACT_ADDRESS, myEpicNft.abi, signer);
console.log("Going to pop wallet now to pay gas...")
let nftTxn = await connectedContract.makeAnEpicNFT();
setIsLoading(true);
console.log("Mining...please wait.")
await nftTxn.wait();
setIsLoading(false);
console.log(`Mined, see transaction: https://rinkeby.etherscan.io/tx/${nftTxn.hash}`);
await connectedContract.on("NewEpicNFTMinted", (from, tokenId) => {
console.log({tokenId});
setNFTzMinted(tokenId.toNumber());
});
console.log({NFTzMinted})
} else {
console.log("Ethereum object doesn't exist!");
}
} catch (error) {
console.log(error)
}
}
Does anyone know what is going on? I could always run my view function to get the tokenId, but it seems like an extra step.
I feel like the callback function on connectedContract should work.
Thanks! JVj
console.log({NFTzMinted})
is getting called before the state has updated.