0

I have events like this in my .sol file

event userActivated(address owner, address contractAddr);
event userDeactivated(address owner, address contractAddr);

And I am using ethclient Go module to parse events. here is the code

func listEvents() string{
    var tempEvents Event
    client, err := ethclient.Dial(RpcPath)
    if err!=nil{
        glog.Fatal("Client error",err)
    }
    query := ethereum.FilterQuery{
        FromBlock: nil,
        ToBlock:   nil,
        Addresses: []common.Address{},
   }
   logs, err := client.FilterLogs(context.Background(), query)
   if err != nil {
      glog.Fatal(err)
   }
   //get ABI for parsing data
   contractAbi, err := 
   abi.JSON(strings.NewReader(string(contracts.NewUserABI)))
   if err != nil {
      glog.Fatal("ABI error",err)
   }
   for _, vLog := range logs {
       //fmt.Println(string(vLog))
       err := contractAbi.Unpack(&tempEvents, "userActivated", vLog.Data)
      if err != nil {
            glog.Fatal(err)
      } 
      fmt.Println(tempEvents.ContractAddr.Hex(),tempEvents.Owner.Hex())
   }
  if err!=nil{
      glog.Fatal(err.Error())
  }
return ""

}

My event structure is here

type Event struct {
    Owner common.Address
    ContractAddr common.Address
}

But it lists both events, userActivated and userDeactivated even if I gave unPack parameter as "userActivated"

Is there any way to check event name in log data?

1 Answer 1

1

The log structure includes a topics array. The first topic is the signature of the event.

var topics [4]string
for i := range vLog.Topics {
  topics[i] = vLog.Topics[i].Hex()
}

fmt.Println(topics[0]) // 0xe79e73da417710ae99aa2088575580a60415d359acfad9cdd3382d59c80281d4

To calculate the topic for an event

eventSignature := []byte("ItemSet(bytes32,bytes32)")
hash := crypto.Keccak256Hash(eventSignature)
fmt.Println(hash.Hex()) // 0xe79e73da417710ae99aa2088575580a60415d359acfad9cdd3382d59c80281d4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.