github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/event.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package abi 19 20 import ( 21 "fmt" 22 "strings" 23 24 "github.com/AigarNetwork/aigar/common" 25 "github.com/AigarNetwork/aigar/crypto" 26 ) 27 28 // Event is an event potentially triggered by the EVM's LOG mechanism. The Event 29 // holds type information (inputs) about the yielded output. Anonymous events 30 // don't get the signature canonical representation as the first LOG topic. 31 type Event struct { 32 // Name is the event name used for internal representation. It's derived from 33 // the raw name and a suffix will be added in the case of a event overload. 34 // 35 // e.g. 36 // There are two events have same name: 37 // * foo(int,int) 38 // * foo(uint,uint) 39 // The event name of the first one wll be resolved as foo while the second one 40 // will be resolved as foo0. 41 Name string 42 // RawName is the raw event name parsed from ABI. 43 RawName string 44 Anonymous bool 45 Inputs Arguments 46 } 47 48 func (e Event) String() string { 49 inputs := make([]string, len(e.Inputs)) 50 for i, input := range e.Inputs { 51 inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name) 52 if input.Indexed { 53 inputs[i] = fmt.Sprintf("%v indexed %v", input.Type, input.Name) 54 } 55 } 56 return fmt.Sprintf("event %v(%v)", e.RawName, strings.Join(inputs, ", ")) 57 } 58 59 // Sig returns the event string signature according to the ABI spec. 60 // 61 // Example 62 // 63 // event foo(uint32 a, int b) = "foo(uint32,int256)" 64 // 65 // Please note that "int" is substitute for its canonical representation "int256" 66 func (e Event) Sig() string { 67 types := make([]string, len(e.Inputs)) 68 for i, input := range e.Inputs { 69 types[i] = input.Type.String() 70 } 71 return fmt.Sprintf("%v(%v)", e.RawName, strings.Join(types, ",")) 72 } 73 74 // ID returns the canonical representation of the event's signature used by the 75 // abi definition to identify event names and types. 76 func (e Event) ID() common.Hash { 77 return common.BytesToHash(crypto.Keccak256([]byte(e.Sig()))) 78 }