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