method.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 therefore 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 `false`.
  29. // Input specifies the required input parameters for this gives method.
  30. type Method struct {
  31. // Name is the method name used for internal representation. It's derived from
  32. // the raw name and a suffix will be added in the case of a function overload.
  33. //
  34. // e.g.
  35. // There are two functions have same name:
  36. // * foo(int,int)
  37. // * foo(uint,uint)
  38. // The method name of the first one will be resolved as foo while the second one
  39. // will be resolved as foo0.
  40. Name string
  41. // RawName is the raw method name parsed from ABI.
  42. RawName string
  43. Const bool
  44. Inputs Arguments
  45. Outputs Arguments
  46. }
  47. // Sig returns the methods string signature according to the ABI spec.
  48. //
  49. // Example
  50. //
  51. // function foo(uint32 a, int b) = "foo(uint32,int256)"
  52. //
  53. // Please note that "int" is substitute for its canonical representation "int256"
  54. func (method Method) Sig() string {
  55. types := make([]string, len(method.Inputs))
  56. for i, input := range method.Inputs {
  57. types[i] = input.Type.String()
  58. }
  59. return fmt.Sprintf("%v(%v)", method.RawName, strings.Join(types, ","))
  60. }
  61. func (method Method) String() string {
  62. inputs := make([]string, len(method.Inputs))
  63. for i, input := range method.Inputs {
  64. inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
  65. }
  66. outputs := make([]string, len(method.Outputs))
  67. for i, output := range method.Outputs {
  68. outputs[i] = output.Type.String()
  69. if len(output.Name) > 0 {
  70. outputs[i] += fmt.Sprintf(" %v", output.Name)
  71. }
  72. }
  73. constant := ""
  74. if method.Const {
  75. constant = "constant "
  76. }
  77. return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
  78. }
  79. // ID returns the canonical representation of the method's signature used by the
  80. // abi definition to identify method names and types.
  81. func (method Method) ID() []byte {
  82. return crypto.Keccak256([]byte(method.Sig()))[:4]
  83. }