github.com/jimmyx0x/go-ethereum@v1.10.28/accounts/abi/event.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/crypto"
    25  )
    26  
    27  // Event is an event potentially triggered by the EVM's LOG mechanism. The Event
    28  // holds type information (inputs) about the yielded output. Anonymous events
    29  // don't get the signature canonical representation as the first LOG topic.
    30  type Event struct {
    31  	// Name is the event name used for internal representation. It's derived from
    32  	// the raw name and a suffix will be added in the case of event overloading.
    33  	//
    34  	// e.g.
    35  	// These are two events that have the same name:
    36  	// * foo(int,int)
    37  	// * foo(uint,uint)
    38  	// The event name of the first one will be resolved as foo while the second one
    39  	// will be resolved as foo0.
    40  	Name string
    41  
    42  	// RawName is the raw event name parsed from ABI.
    43  	RawName   string
    44  	Anonymous bool
    45  	Inputs    Arguments
    46  	str       string
    47  
    48  	// Sig contains the string signature according to the ABI spec.
    49  	// e.g.	 event foo(uint32 a, int b) = "foo(uint32,int256)"
    50  	// Please note that "int" is substitute for its canonical representation "int256"
    51  	Sig string
    52  
    53  	// ID returns the canonical representation of the event's signature used by the
    54  	// abi definition to identify event names and types.
    55  	ID common.Hash
    56  }
    57  
    58  // NewEvent creates a new Event.
    59  // It sanitizes the input arguments to remove unnamed arguments.
    60  // It also precomputes the id, signature and string representation
    61  // of the event.
    62  func NewEvent(name, rawName string, anonymous bool, inputs Arguments) Event {
    63  	// sanitize inputs to remove inputs without names
    64  	// and precompute string and sig representation.
    65  	names := make([]string, len(inputs))
    66  	types := make([]string, len(inputs))
    67  	for i, input := range inputs {
    68  		if input.Name == "" {
    69  			inputs[i] = Argument{
    70  				Name:    fmt.Sprintf("arg%d", i),
    71  				Indexed: input.Indexed,
    72  				Type:    input.Type,
    73  			}
    74  		} else {
    75  			inputs[i] = input
    76  		}
    77  		// string representation
    78  		names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name)
    79  		if input.Indexed {
    80  			names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name)
    81  		}
    82  		// sig representation
    83  		types[i] = input.Type.String()
    84  	}
    85  
    86  	str := fmt.Sprintf("event %v(%v)", rawName, strings.Join(names, ", "))
    87  	sig := fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
    88  	id := common.BytesToHash(crypto.Keccak256([]byte(sig)))
    89  
    90  	return Event{
    91  		Name:      name,
    92  		RawName:   rawName,
    93  		Anonymous: anonymous,
    94  		Inputs:    inputs,
    95  		str:       str,
    96  		Sig:       sig,
    97  		ID:        id,
    98  	}
    99  }
   100  
   101  func (e Event) String() string {
   102  	return e.str
   103  }