Hardhat localnetwork 无法触发合约事件

问题描述 投票:0回答:1

我正在编写一个脚本,将在hardhat中铸造Nft,requestNft正在发出事件NftRequested,但是当我使用hardhat在本地环境中铸造它时,我没有得到索引的requestId。

   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);
   }

requestNft被执行,并且trnasaction状态成功,但是我无法从日志中获取任何索引参数,日志甚至是未定义的。

// 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
     );
   }
 });

错误:TypeError:无法读取未定义的属性(读取'requestId'),我不知道为什么如果函数成功执行,为什么事件没有被触发?合约在本地环境部署就ok了

我的代码的 github 存储库:https://github.com/BOBSTRONK/hardhat_NFT/tree/main

尝试在 sepolia testNetwork 中部署和铸造后,一切正常,因此事件正在发出,但在本地安全帽网络中仍然无法工作

javascript solidity smartcontracts hardhat
1个回答
0
投票

这里的问题是 requestRandomWords 方法也发出了一个事件,所以这应该是logs[1]而不是logs[0]。

© www.soinside.com 2019 - 2024. All rights reserved.