method.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package abi
  17. import (
  18. "fmt"
  19. "strings"
  20. "github.com/ethereum/go-ethereum/crypto"
  21. )
  22. // Method represents a callable given a `Name` and whether the method is a constant.
  23. // If the method is `Const` no transaction needs to be created for this
  24. // particular Method call. It can easily be simulated using a local VM.
  25. // For example a `Balance()` method only needs to retrieve something
  26. // from the storage and therefor requires no Tx to be send to the
  27. // network. A method such as `Transact` does require a Tx and thus will
  28. // be flagged `true`.
  29. // Input specifies the required input parameters for this gives method.
  30. type Method struct {
  31. Name string
  32. Const bool
  33. Inputs Arguments
  34. Outputs Arguments
  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. func (method Method) String() string {
  53. inputs := make([]string, len(method.Inputs))
  54. for i, input := range method.Inputs {
  55. inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type)
  56. }
  57. outputs := make([]string, len(method.Outputs))
  58. for i, output := range method.Outputs {
  59. if len(output.Name) > 0 {
  60. outputs[i] = fmt.Sprintf("%v ", output.Name)
  61. }
  62. outputs[i] += output.Type.String()
  63. }
  64. constant := ""
  65. if method.Const {
  66. constant = "constant "
  67. }
  68. return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
  69. }
  70. func (method Method) Id() []byte {
  71. return crypto.Keccak256([]byte(method.Sig()))[:4]
  72. }