github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/abi/method.go (about) 1 package abi 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/quickchainproject/quickchain/crypto" 8 ) 9 10 // Method represents a callable given a `Name` and whether the method is a constant. 11 // If the method is `Const` no transaction needs to be created for this 12 // particular Method call. It can easily be simulated using a local VM. 13 // For example a `Balance()` method only needs to retrieve something 14 // from the storage and therefor requires no Tx to be send to the 15 // network. A method such as `Transact` does require a Tx and thus will 16 // be flagged `true`. 17 // Input specifies the required input parameters for this gives method. 18 type Method struct { 19 Name string 20 Const bool 21 Inputs Arguments 22 Outputs Arguments 23 } 24 25 // Sig returns the methods string signature according to the ABI spec. 26 // 27 // Example 28 // 29 // function foo(uint32 a, int b) = "foo(uint32,int256)" 30 // 31 // Please note that "int" is substitute for its canonical representation "int256" 32 func (method Method) Sig() string { 33 types := make([]string, len(method.Inputs)) 34 i := 0 35 for _, input := range method.Inputs { 36 types[i] = input.Type.String() 37 i++ 38 } 39 return fmt.Sprintf("%v(%v)", method.Name, strings.Join(types, ",")) 40 } 41 42 func (method Method) String() string { 43 inputs := make([]string, len(method.Inputs)) 44 for i, input := range method.Inputs { 45 inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type) 46 } 47 outputs := make([]string, len(method.Outputs)) 48 for i, output := range method.Outputs { 49 if len(output.Name) > 0 { 50 outputs[i] = fmt.Sprintf("%v ", output.Name) 51 } 52 outputs[i] += output.Type.String() 53 } 54 constant := "" 55 if method.Const { 56 constant = "constant " 57 } 58 return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) 59 } 60 61 func (method Method) Id() []byte { 62 return crypto.Keccak256([]byte(method.Sig()))[:4] 63 }