method.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // FunctionType represents different types of functions a contract might have.
  23. type FunctionType int
  24. const (
  25. // Constructor represents the constructor of the contract.
  26. // The constructor function is called while deploying a contract.
  27. Constructor FunctionType = iota
  28. // Fallback represents the fallback function.
  29. // This function is executed if no other function matches the given function
  30. // signature and no receive function is specified.
  31. Fallback
  32. // Receive represents the receive function.
  33. // This function is executed on plain Ether transfers.
  34. Receive
  35. // Function represents a normal function.
  36. Function
  37. )
  38. // Method represents a callable given a `Name` and whether the method is a constant.
  39. // If the method is `Const` no transaction needs to be created for this
  40. // particular Method call. It can easily be simulated using a local VM.
  41. // For example a `Balance()` method only needs to retrieve something
  42. // from the storage and therefore requires no Tx to be sent to the
  43. // network. A method such as `Transact` does require a Tx and thus will
  44. // be flagged `false`.
  45. // Input specifies the required input parameters for this gives method.
  46. type Method struct {
  47. // Name is the method name used for internal representation. It's derived from
  48. // the raw name and a suffix will be added in the case of a function overload.
  49. //
  50. // e.g.
  51. // These are two functions that have the same name:
  52. // * foo(int,int)
  53. // * foo(uint,uint)
  54. // The method name of the first one will be resolved as foo while the second one
  55. // will be resolved as foo0.
  56. Name string
  57. RawName string // RawName is the raw method name parsed from ABI
  58. // Type indicates whether the method is a
  59. // special fallback introduced in solidity v0.6.0
  60. Type FunctionType
  61. // StateMutability indicates the mutability state of method,
  62. // the default value is nonpayable. It can be empty if the abi
  63. // is generated by legacy compiler.
  64. StateMutability string
  65. // Legacy indicators generated by compiler before v0.6.0
  66. Constant bool
  67. Payable bool
  68. Inputs Arguments
  69. Outputs Arguments
  70. str string
  71. // Sig returns the methods string signature according to the ABI spec.
  72. // e.g. function foo(uint32 a, int b) = "foo(uint32,int256)"
  73. // Please note that "int" is substitute for its canonical representation "int256"
  74. Sig string
  75. // ID returns the canonical representation of the method's signature used by the
  76. // abi definition to identify method names and types.
  77. ID []byte
  78. }
  79. // NewMethod creates a new Method.
  80. // A method should always be created using NewMethod.
  81. // It also precomputes the sig representation and the string representation
  82. // of the method.
  83. func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method {
  84. var (
  85. types = make([]string, len(inputs))
  86. inputNames = make([]string, len(inputs))
  87. outputNames = make([]string, len(outputs))
  88. )
  89. for i, input := range inputs {
  90. inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
  91. types[i] = input.Type.String()
  92. }
  93. for i, output := range outputs {
  94. outputNames[i] = output.Type.String()
  95. if len(output.Name) > 0 {
  96. outputNames[i] += fmt.Sprintf(" %v", output.Name)
  97. }
  98. }
  99. // calculate the signature and method id. Note only function
  100. // has meaningful signature and id.
  101. var (
  102. sig string
  103. id []byte
  104. )
  105. if funType == Function {
  106. sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
  107. id = crypto.Keccak256([]byte(sig))[:4]
  108. }
  109. // Extract meaningful state mutability of solidity method.
  110. // If it's default value, never print it.
  111. state := mutability
  112. if state == "nonpayable" {
  113. state = ""
  114. }
  115. if state != "" {
  116. state = state + " "
  117. }
  118. identity := fmt.Sprintf("function %v", rawName)
  119. if funType == Fallback {
  120. identity = "fallback"
  121. } else if funType == Receive {
  122. identity = "receive"
  123. } else if funType == Constructor {
  124. identity = "constructor"
  125. }
  126. str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))
  127. return Method{
  128. Name: name,
  129. RawName: rawName,
  130. Type: funType,
  131. StateMutability: mutability,
  132. Constant: isConst,
  133. Payable: isPayable,
  134. Inputs: inputs,
  135. Outputs: outputs,
  136. str: str,
  137. Sig: sig,
  138. ID: id,
  139. }
  140. }
  141. func (method Method) String() string {
  142. return method.str
  143. }
  144. // IsConstant returns the indicator whether the method is read-only.
  145. func (method Method) IsConstant() bool {
  146. return method.StateMutability == "view" || method.StateMutability == "pure" || method.Constant
  147. }
  148. // IsPayable returns the indicator whether the method can process
  149. // plain ether transfers.
  150. func (method Method) IsPayable() bool {
  151. return method.StateMutability == "payable" || method.Payable
  152. }