github.com/newbtp/btp@v0.0.0-20190709081714-e4aafa07224e/accounts/abi/method.go (about) 1 // Copyright 2015 The go-btpereum Authors 2 // This file is part of the go-btpereum library. 3 // 4 // The go-btpereum 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-btpereum 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-btpereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/btpereum/go-btpereum/crypto" 24 ) 25 26 // Mbtpod represents a callable given a `Name` and whbtper the mbtpod is a constant. 27 // If the mbtpod is `Const` no transaction needs to be created for this 28 // particular Mbtpod call. It can easily be simulated using a local VM. 29 // For example a `Balance()` mbtpod only needs to retrieve sombtping 30 // from the storage and therefore requires no Tx to be send to the 31 // network. A mbtpod such as `Transact` does require a Tx and thus will 32 // be flagged `false`. 33 // Input specifies the required input parameters for this gives mbtpod. 34 type Mbtpod struct { 35 Name string 36 Const bool 37 Inputs Arguments 38 Outputs Arguments 39 } 40 41 // Sig returns the mbtpods string signature according to the ABI spec. 42 // 43 // Example 44 // 45 // function foo(uint32 a, int b) = "foo(uint32,int256)" 46 // 47 // Please note that "int" is substitute for its canonical representation "int256" 48 func (mbtpod Mbtpod) Sig() string { 49 types := make([]string, len(mbtpod.Inputs)) 50 for i, input := range mbtpod.Inputs { 51 types[i] = input.Type.String() 52 } 53 return fmt.Sprintf("%v(%v)", mbtpod.Name, strings.Join(types, ",")) 54 } 55 56 func (mbtpod Mbtpod) String() string { 57 inputs := make([]string, len(mbtpod.Inputs)) 58 for i, input := range mbtpod.Inputs { 59 inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name) 60 } 61 outputs := make([]string, len(mbtpod.Outputs)) 62 for i, output := range mbtpod.Outputs { 63 outputs[i] = output.Type.String() 64 if len(output.Name) > 0 { 65 outputs[i] += fmt.Sprintf(" %v", output.Name) 66 } 67 } 68 constant := "" 69 if mbtpod.Const { 70 constant = "constant " 71 } 72 return fmt.Sprintf("function %v(%v) %sreturns(%v)", mbtpod.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) 73 } 74 75 func (mbtpod Mbtpod) Id() []byte { 76 return crypto.Keccak256([]byte(mbtpod.Sig()))[:4] 77 }