From 47d0a8c40981b9af78cef44d7ac1f057254d0db6 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Fri, 10 Apr 2020 09:49:54 +0800 Subject: [PATCH] accounts/abi: address marius's comment --- accounts/abi/abi.go | 18 ++++++++++++++---- accounts/abi/bind/bind.go | 8 ++++---- accounts/abi/method.go | 10 +++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index b6e23342c2..4b88a52cef 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -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{ @@ -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, @@ -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" { @@ -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, @@ -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 +} diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index f1064d5ec3..c98f8b4d4c 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -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. @@ -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, ", ")) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 23c1004466..217c3d2e68 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -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 @@ -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)) @@ -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, ", "))