github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/accounts/abi/method.go (about)

     1  // Copyright 2015 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/intfoundation/intchain/crypto"
    24  )
    25  
    26  // Method represents a callable given a `Name` and whether the method is a constant.
    27  // If the method is `Const` no transaction needs to be created for this
    28  // particular Method call. It can easily be simulated using a local VM.
    29  // For example a `Balance()` method only needs to retrieve something
    30  // from the storage and therefore requires no Tx to be send to the
    31  // network. A method such as `Transact` does require a Tx and thus will
    32  // be flagged `false`.
    33  // Input specifies the required input parameters for this gives method.
    34  type Method struct {
    35  	// Name is the method name used for internal representation. It's derived from
    36  	// the raw name and a suffix will be added in the case of a function overload.
    37  	//
    38  	// e.g.
    39  	// There are two functions have same name:
    40  	// * foo(int,int)
    41  	// * foo(uint,uint)
    42  	// The method name of the first one will be resolved as foo while the second one
    43  	// will be resolved as foo0.
    44  	Name string
    45  	// RawName is the raw method name parsed from ABI.
    46  	RawName string
    47  	Const   bool
    48  	Inputs  Arguments
    49  	Outputs Arguments
    50  }
    51  
    52  // Sig returns the methods string signature according to the ABI spec.
    53  //
    54  // Example
    55  //
    56  //     function foo(uint32 a, int b) = "foo(uint32,int256)"
    57  //
    58  // Please note that "int" is substitute for its canonical representation "int256"
    59  func (method Method) Sig() string {
    60  	types := make([]string, len(method.Inputs))
    61  	for i, input := range method.Inputs {
    62  		types[i] = input.Type.String()
    63  	}
    64  	return fmt.Sprintf("%v(%v)", method.RawName, strings.Join(types, ","))
    65  }
    66  
    67  func (method Method) String() string {
    68  	inputs := make([]string, len(method.Inputs))
    69  	for i, input := range method.Inputs {
    70  		inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
    71  	}
    72  	outputs := make([]string, len(method.Outputs))
    73  	for i, output := range method.Outputs {
    74  		outputs[i] = output.Type.String()
    75  		if len(output.Name) > 0 {
    76  			outputs[i] += fmt.Sprintf(" %v", output.Name)
    77  		}
    78  	}
    79  	constant := ""
    80  	if method.Const {
    81  		constant = "constant "
    82  	}
    83  	return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
    84  }
    85  
    86  // ID returns the canonical representation of the method's signature used by the
    87  // abi definition to identify method names and types.
    88  func (method Method) ID() []byte {
    89  	return crypto.Keccak256([]byte(method.Sig()))[:4]
    90  }