github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/method.go (about)

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