Solidity

Ganache 沒有顯示任何事件

  • November 14, 2021

我有一個發出事件的契約方法,但 ganache 似乎沒有捕捉到任何事件。我這樣說是因為當我在 ropsten 上部署我的契約時,事件被很好地捕捉到了,但只有在 ganache 中我才有這個問題。另外我正在使用 web3 js 來監視事件。

這是我發出事件的solidity合約:

pragma solidity ^0.5.0;

contract Marketplace {
   string public name;
   address owner;
   uint public fileCount = 0;
   mapping(uint => File) public files; 


   struct File
   {
       uint id;
       string name;
       address owner;
       address sharedWith;
   }

   event FileViewed(
       uint id,
       string name,
       address owner,
       address sharedWith
   );

   constructor() public {
       name = "File Sharing System";
       owner = msg.sender;
   }

   function viewFile(uint _id) public 
   {
       File memory _file = files[_id];
       require(msg.sender==_file.sharedWith || msg.sender==owner ,"Not shared with you!!");
       emit FileViewed(fileCount, _file.name, owner, _file.sharedWith);
   }
}

這是我正在監視事件的 App.js:

import React, { Component } from 'react';
import Web3 from 'web3';
import './App.css';
import Navbar from './Navbar';
import Main from './Main';
import Marketplace from '../abis/Marketplace.json';

class App extends Component {
 //const marketplace 

 async componentWillMount()
 {
   await this.loadWeb3()
   await this.loadBlockChainData()
 }

 async loadWeb3()
 {
   if(window.ethereum)
   {
     window.web3 = new Web3(window.ethereum);
     await window.ethereum.enable();
   }
   else if (window.web3)
   {
     window.web3 = new Web3(window.web3.currentProvider);
     await window.ethereum.enable();
   }
   else{
     window.alert("Non ethereum browser detected!!!!");
   }
 }

 async loadBlockChainData()
 {
   const web3 = window.web3;
   //load Account
   const accounts = await web3.eth.getAccounts();
   this.setState({account: accounts[0]});
   const networkId = await web3.eth.net.getId();
   const networkData = Marketplace.networks[networkId];
   if(networkData) 
   {
     const marketplace = web3.eth.Contract(Marketplace.abi, networkData.address);
     this.setState({marketplace});

     const fileCount = await marketplace.methods.fileCount().call();
     for(var i=1;i<=fileCount;i++)
     {
       const file = await marketplace.methods.files(i).call();
       this.setState({
         files: [...this.state.files, file]
       });
     }
   } 
   else 
   {
     window.alert('Marketplace contract not deployed to detected network.');
   }

 }


 constructor(props)
 {
   super(props);
   this.state =
   {
     account: '',
     fileCount: 0,
     files: [], 
     loading: false
   };

   this.viewFile = this.viewFile.bind(this);
 }



 viewFile(id)
 {
   this.setState({loading: true});
   this.state.marketplace.methods.viewFile(id).send({from: this.state.account})

   this.state.marketplace.events.FileViewed({
     fromBlock: 0
   }, function(error, event)
   {
     if(error)
     {
       window.alert("Error: ", error);
     }
     else
     {
       console.log(event);
     }
   })

 }

**注意:**我不想使用 ropsten 網路,因為記錄我的交易需要時間。

幫助將不勝感激,新的任何建議將不勝感激。

看起來您必須添加一個 Truffle 項目。

https://tonyyan.wordpress.com/2021/02/25/where-are-my-contracts-and-events-on-ganache/

引用自:https://ethereum.stackexchange.com/questions/76915