github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/event.go (about)

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