mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
accounts/abi: improved readability in NewMethod, nitpicks
This commit is contained in:
parent
07226788bf
commit
536f669215
2 changed files with 20 additions and 16 deletions
|
|
@ -140,7 +140,6 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
|
||||||
switch field.Type {
|
switch field.Type {
|
||||||
case "constructor":
|
case "constructor":
|
||||||
abi.Constructor = NewMethod("", "", Constructor, field.StateMutability, field.Constant, field.Payable, field.Inputs, nil)
|
abi.Constructor = NewMethod("", "", Constructor, field.StateMutability, field.Constant, field.Payable, field.Inputs, nil)
|
||||||
// empty defaults to function according to the abi spec
|
|
||||||
case "function":
|
case "function":
|
||||||
name := abi.methodName(field.Name)
|
name := abi.methodName(field.Name)
|
||||||
abi.Methods[name] = NewMethod(name, field.Name, Function, field.StateMutability, field.Constant, field.Payable, field.Inputs, field.Outputs)
|
abi.Methods[name] = NewMethod(name, field.Name, Function, field.StateMutability, field.Constant, field.Payable, field.Inputs, field.Outputs)
|
||||||
|
|
@ -164,6 +163,8 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
|
||||||
case "event":
|
case "event":
|
||||||
name := abi.eventName(field.Name)
|
name := abi.eventName(field.Name)
|
||||||
abi.Events[name] = NewEvent(name, field.Name, field.Anonymous, field.Inputs)
|
abi.Events[name] = NewEvent(name, field.Name, field.Anonymous, field.Inputs)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("abi: could not recognize type %v of field %v", field.Type, field.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -92,23 +92,31 @@ type Method struct {
|
||||||
// It also precomputes the sig representation and the string representation
|
// It also precomputes the sig representation and the string representation
|
||||||
// of the method.
|
// of the method.
|
||||||
func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method {
|
func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method {
|
||||||
// inputs
|
var (
|
||||||
inputNames := make([]string, len(inputs))
|
types = make([]string, len(inputs))
|
||||||
types := make([]string, len(inputs))
|
inputNames = make([]string, len(inputs))
|
||||||
|
outputNames = make([]string, len(outputs))
|
||||||
|
)
|
||||||
for i, input := range inputs {
|
for i, input := range inputs {
|
||||||
inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
|
inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
|
||||||
types[i] = input.Type.String()
|
types[i] = input.Type.String()
|
||||||
}
|
}
|
||||||
// outputs
|
|
||||||
outputNames := make([]string, len(outputs))
|
|
||||||
for i, output := range outputs {
|
for i, output := range outputs {
|
||||||
outputNames[i] = output.Type.String()
|
outputNames[i] = output.Type.String()
|
||||||
if len(output.Name) > 0 {
|
if len(output.Name) > 0 {
|
||||||
outputNames[i] += fmt.Sprintf(" %v", output.Name)
|
outputNames[i] += fmt.Sprintf(" %v", output.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// calculate the signature.
|
// calculate the signature and method id. Note only function
|
||||||
sig := fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
|
// has meaningful signature and id.
|
||||||
|
var (
|
||||||
|
sig string
|
||||||
|
id []byte
|
||||||
|
)
|
||||||
|
if funType == Function {
|
||||||
|
sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ","))
|
||||||
|
id = crypto.Keccak256([]byte(sig))[:4]
|
||||||
|
}
|
||||||
// Extract meaningful state mutability of solidity method.
|
// Extract meaningful state mutability of solidity method.
|
||||||
// If it's default value, never print it.
|
// If it's default value, never print it.
|
||||||
state := mutability
|
state := mutability
|
||||||
|
|
@ -121,18 +129,14 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str
|
||||||
identity := fmt.Sprintf("function %v", rawName)
|
identity := fmt.Sprintf("function %v", rawName)
|
||||||
if funType == Fallback {
|
if funType == Fallback {
|
||||||
identity = "fallback"
|
identity = "fallback"
|
||||||
// The fallback function does not have any meaningful signature.
|
|
||||||
sig = ""
|
|
||||||
} else if funType == Receive {
|
} else if funType == Receive {
|
||||||
identity = "receive"
|
identity = "receive"
|
||||||
// The receive function does not have any meaningful signature.
|
} else if funType == Constructor {
|
||||||
sig = ""
|
identity = "constructor"
|
||||||
}
|
}
|
||||||
|
|
||||||
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))
|
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))
|
||||||
id := crypto.Keccak256([]byte(sig))[:4]
|
|
||||||
|
|
||||||
method := Method{
|
return Method{
|
||||||
Name: name,
|
Name: name,
|
||||||
RawName: rawName,
|
RawName: rawName,
|
||||||
Type: funType,
|
Type: funType,
|
||||||
|
|
@ -145,7 +149,6 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str
|
||||||
Sig: sig,
|
Sig: sig,
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
return method
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (method Method) String() string {
|
func (method Method) String() string {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue