github.com/klaytn/klaytn@v1.12.1/accounts/abi/event.go (about)

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