accounts/abi: implement new fallback functions (#20764)

* accounts/abi: implement new fackball functions

In Solidity v0.6.0, the original fallback is separated
into two different sub types: fallback and receive.

This PR addes the support for parsing new format abi
and the relevant abigen functionalities.

* accounts/abi: fix unit tests

* accounts/abi: minor fixes

* accounts/abi, mobile: support jave binding

* accounts/abi: address marius's comment

* accounts/abi: Work around the uin64 conversion issue

Co-authored-by: Guillaume Ballet <gballet@gmail.com>
This commit is contained in:
gary rong
2020-04-15 15:23:58 +08:00
committed by GitHub
parent 2a836bb259
commit 00064ddcfb
12 changed files with 445 additions and 94 deletions

View File

@ -41,10 +41,23 @@ type Method struct {
// * foo(uint,uint)
// The method name of the first one will be resolved as foo while the second one
// will be resolved as foo0.
Name string
// RawName is the raw method name parsed from ABI.
RawName string
Const bool
Name string
RawName string // RawName is the raw method name parsed from ABI
// StateMutability indicates the mutability state of method,
// the default value is nonpayable. It can be empty if the abi
// is generated by legacy compiler.
StateMutability string
// Legacy indicators generated by compiler before v0.6.0
Constant bool
Payable bool
// The following two flags indicates whether the method is a
// special fallback introduced in solidity v0.6.0
IsFallback bool
IsReceive bool
Inputs Arguments
Outputs Arguments
}
@ -57,6 +70,11 @@ type Method struct {
//
// Please note that "int" is substitute for its canonical representation "int256"
func (method Method) Sig() string {
// Short circuit if the method is special. Fallback
// and Receive don't have signature at all.
if method.IsFallback || method.IsReceive {
return ""
}
types := make([]string, len(method.Inputs))
for i, input := range method.Inputs {
types[i] = input.Type.String()
@ -76,11 +94,22 @@ func (method Method) String() string {
outputs[i] += fmt.Sprintf(" %v", output.Name)
}
}
constant := ""
if method.Const {
constant = "constant "
// Extract meaningful state mutability of solidity method.
// If it's default value, never print it.
state := method.StateMutability
if state == "nonpayable" {
state = ""
}
return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
if state != "" {
state = state + " "
}
identity := fmt.Sprintf("function %v", method.RawName)
if method.IsFallback {
identity = "fallback"
} else if method.IsReceive {
identity = "receive"
}
return fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputs, ", "), state, strings.Join(outputs, ", "))
}
// ID returns the canonical representation of the method's signature used by the
@ -88,3 +117,14 @@ func (method Method) String() string {
func (method Method) ID() []byte {
return crypto.Keccak256([]byte(method.Sig()))[:4]
}
// IsConstant returns the indicator whether the method is read-only.
func (method Method) IsConstant() bool {
return method.StateMutability == "view" || method.StateMutability == "pure" || method.Constant
}
// IsPayable returns the indicator whether the method can process
// plain ether transfers.
func (method Method) IsPayable() bool {
return method.StateMutability == "payable" || method.Payable
}