Hardhat localnetwork can't fire the event of the contract

47 Views Asked by At

i am writing a script that will mint the Nft in hardhat, requestNft is emitting the event NftRequested, but when i use hardhat to mint it in the local enviroment, i don't get the requestId that is indexed.

   event NftRequested(uint256 indexed requestId, address indexed requester);

   function requestNft() public payable returns (uint256 requestId) {
       // Pay some mintfee to mint NFT
       if (msg.value < i_mintFee) {
           revert RandomIpfsNft__NeedMoreETHSent();
       }
       requestId = i_vrfCoordinator.requestRandomWords(
           i_gasLane,
           i_subscriptionID,
           REQUEST_CONFIRMATIONS,
           i_callbackGasLimit,
           NUM_WORDS
       );

       // create a mapping between people who call the requestNft and requestId
       // so when we fulfillRandomWords we properly assign the dogs to them
       s_requestIdToSender[requestId] = msg.sender;
       emit NftRequested(requestId, msg.sender);
   }

The requestNft is executed, and trnasaction satus is success, but i cant get any indexed parameter from logs, logs is even undifened.

// Random IPFS NFT
 const randomIpfsNft = await ethers.getContract("RandomIpfsNFT", deployer);
 const mintFee = await randomIpfsNft.getMintFee();
 const randomIpfsNftMintTx = await randomIpfsNft.requestNft({
   value: mintFee,
 });

 console.log(randomIpfsNft.target);
 const randomIpfsNftMintTxReceipt = await randomIpfsNftMintTx.wait(1);
 // Check if the transaction status is successful
 if (randomIpfsNftMintTxReceipt.status === 1) {
   console.log("Transaction successful!");
 } else {
   console.error(
     "Transaction reverted or failed. Check the transaction receipt for details."
   );
 }

 // Need to listen for response
 await new Promise(async (resolve, reject) => {
   setTimeout(() => reject("Timeout: 'NFTMinted' event did not fire"), 300000); // 5 minute timeout time
   // setup listener for our event
   randomIpfsNft.once("NftMinted", async () => {
     console.log(
       `Random IPFS NFT index 0 tokenURI: ${await randomIpfsNft.tokenURI(0)}`
     );
     resolve();
   });
   if (chainId == 31337) {
     // from receipt we can get our event log
     const requestId =
       randomIpfsNftMintTxReceipt.logs[0].args.requestId.toString();
     const vrfCoordinatorV2Mock = await ethers.getContract(
       "VRFCoordinatorV2Mock",
       deployer
     );
     await vrfCoordinatorV2Mock.fulfillRandomWords(
       requestId,
       randomIpfsNft.target
     );
   }
 });

error:TypeError: Cannot read properties of undefined (reading 'requestId'), i dont know why if the function is succefully executed, why the event is not fired? the deployment of the contract in the local enviroment is ok

my github repo for my code: https://github.com/BOBSTRONK/hardhat_NFT/tree/main

After try to deploy and mint in the sepolia testNetwork, everything works, so the event is emitting, but still not working in the local hardhat network

2

There are 2 best solutions below

0
On

The problem here was requestRandomWords method is also emitting a event, so this should be logs[1] instead of logs[0].

0
On

try changing this line

randomIpfsNftMintTxReceipt.logs[0].args.requestId.toString();

to

randomIpfsNftMintTxReceipt.logs[0].topics[0];