Skip to content

Commit

Permalink
accounts/abi: address marius's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Apr 10, 2020
1 parent b821eda commit 47d0a8c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
18 changes: 14 additions & 4 deletions accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
case "fallback":
// New introduced function type in v0.6.0, check more detail
// here https://solidity.readthedocs.io/en/v0.6.0/contracts.html#fallback-function
if abi.Fallback.Fallback {
if abi.HasFallback() {
return errors.New("only single fallback is allowed")
}
abi.Fallback = Method{
Expand All @@ -182,7 +182,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
// The `StateMutability` can only be payable or nonpayable,
// so the constant is always false.
StateMutability: field.StateMutability,
Fallback: true,
IsFallback: true,

// Fallback doesn't have any input or output
Inputs: nil,
Expand All @@ -195,7 +195,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
case "receive":
// New introduced function type in v0.6.0, check more detail
// here https://solidity.readthedocs.io/en/v0.6.0/contracts.html#fallback-function
if abi.Receive.Receive {
if abi.HasReceive() {
return errors.New("only single receive is allowed")
}
if field.StateMutability != "payable" {
Expand All @@ -208,7 +208,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
// The `StateMutability` can only be payable, so constant
// is always true while payable is always false.
StateMutability: field.StateMutability,
Receive: true,
IsReceive: true,

// Receive doesn't have any input or output
Inputs: nil,
Expand Down Expand Up @@ -260,3 +260,13 @@ func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
}
return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
}

// HasFallback returns an indicator whether a fallback function is included.
func (abi *ABI) HasFallback() bool {
return abi.Fallback.IsFallback
}

// HasReceive returns an indicator whether a receive function is included.
func (abi *ABI) HasReceive() bool {
return abi.Receive.IsReceive
}
8 changes: 4 additions & 4 deletions accounts/abi/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
events[original.Name] = &tmplEvent{Original: original, Normalized: normalized}
}
// Add two special fallback functions if they exist
if evmABI.Fallback.Fallback {
if evmABI.HasFallback() {
fallback = &tmplMethod{Original: evmABI.Fallback}
}
if evmABI.Receive.Receive {
if evmABI.HasReceive() {
receive = &tmplMethod{Original: evmABI.Receive}
}
// There is no easy way to pass arbitrary java objects to the Go side.
Expand Down Expand Up @@ -639,9 +639,9 @@ func formatMethod(method abi.Method, structs map[string]*tmplStruct) string {
state = state + " "
}
identity := fmt.Sprintf("function %v", method.RawName)
if method.Fallback {
if method.IsFallback {
identity = "fallback"
} else if method.Receive {
} else if method.IsReceive {
identity = "receive"
}
return fmt.Sprintf("%s(%v) %sreturns(%v)", identity, strings.Join(inputs, ", "), state, strings.Join(outputs, ", "))
Expand Down
10 changes: 5 additions & 5 deletions accounts/abi/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Method struct {

// The following two flags indicates whether the method is a
// special fallback introduced in solidity v0.6.0
Fallback bool
Receive bool
IsFallback bool
IsReceive bool

Inputs Arguments
Outputs Arguments
Expand All @@ -72,7 +72,7 @@ type Method struct {
func (method Method) Sig() string {
// Short circuit if the method is special. Fallback
// and Receive don't have signature at all.
if method.Fallback || method.Receive {
if method.IsFallback || method.IsReceive {
return ""
}
types := make([]string, len(method.Inputs))
Expand Down Expand Up @@ -104,9 +104,9 @@ func (method Method) String() string {
state = state + " "
}
identity := fmt.Sprintf("function %v", method.RawName)
if method.Fallback {
if method.IsFallback {
identity = "fallback"
} else if method.Receive {
} else if method.IsReceive {
identity = "receive"
}
return fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputs, ", "), state, strings.Join(outputs, ", "))
Expand Down

0 comments on commit 47d0a8c

Please sign in to comment.