github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/abi/method.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package abi
    13  
    14  import (
    15  	"fmt"
    16  	"strings"
    17  
    18  	"github.com/Sberex/go-sberex/crypto"
    19  )
    20  
    21  // Method represents a callable given a `Name` and whether the method is a constant.
    22  // If the method is `Const` no transaction needs to be created for this
    23  // particular Method call. It can easily be simulated using a local VM.
    24  // For example a `Balance()` method only needs to retrieve something
    25  // from the storage and therefor requires no Tx to be send to the
    26  // network. A method such as `Transact` does require a Tx and thus will
    27  // be flagged `true`.
    28  // Input specifies the required input parameters for this gives method.
    29  type Method struct {
    30  	Name    string
    31  	Const   bool
    32  	Inputs  Arguments
    33  	Outputs Arguments
    34  }
    35  
    36  // Sig returns the methods string signature according to the ABI spec.
    37  //
    38  // Example
    39  //
    40  //     function foo(uint32 a, int b)    =    "foo(uint32,int256)"
    41  //
    42  // Please note that "int" is substitute for its canonical representation "int256"
    43  func (method Method) Sig() string {
    44  	types := make([]string, len(method.Inputs))
    45  	i := 0
    46  	for _, input := range method.Inputs {
    47  		types[i] = input.Type.String()
    48  		i++
    49  	}
    50  	return fmt.Sprintf("%v(%v)", method.Name, strings.Join(types, ","))
    51  }
    52  
    53  func (method Method) String() string {
    54  	inputs := make([]string, len(method.Inputs))
    55  	for i, input := range method.Inputs {
    56  		inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type)
    57  	}
    58  	outputs := make([]string, len(method.Outputs))
    59  	for i, output := range method.Outputs {
    60  		if len(output.Name) > 0 {
    61  			outputs[i] = fmt.Sprintf("%v ", output.Name)
    62  		}
    63  		outputs[i] += output.Type.String()
    64  	}
    65  	constant := ""
    66  	if method.Const {
    67  		constant = "constant "
    68  	}
    69  	return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
    70  }
    71  
    72  func (method Method) Id() []byte {
    73  	return crypto.Keccak256([]byte(method.Sig()))[:4]
    74  }