github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/abi/method.go (about)

     1  package abi
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/neatlab/neatio/utilities/crypto"
     8  )
     9  
    10  type Method struct {
    11  	Name string
    12  
    13  	RawName string
    14  	Const   bool
    15  	Inputs  Arguments
    16  	Outputs Arguments
    17  }
    18  
    19  func (method Method) Sig() string {
    20  	types := make([]string, len(method.Inputs))
    21  	for i, input := range method.Inputs {
    22  		types[i] = input.Type.String()
    23  	}
    24  	return fmt.Sprintf("%v(%v)", method.RawName, strings.Join(types, ","))
    25  }
    26  
    27  func (method Method) String() string {
    28  	inputs := make([]string, len(method.Inputs))
    29  	for i, input := range method.Inputs {
    30  		inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
    31  	}
    32  	outputs := make([]string, len(method.Outputs))
    33  	for i, output := range method.Outputs {
    34  		outputs[i] = output.Type.String()
    35  		if len(output.Name) > 0 {
    36  			outputs[i] += fmt.Sprintf(" %v", output.Name)
    37  		}
    38  	}
    39  	constant := ""
    40  	if method.Const {
    41  		constant = "constant "
    42  	}
    43  	return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
    44  }
    45  
    46  func (method Method) ID() []byte {
    47  	return crypto.Keccak256([]byte(method.Sig()))[:4]
    48  }