aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/abi.go
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2017-12-21 17:26:30 +0800
committerMartin Holst Swende <martin@swende.se>2017-12-23 02:26:52 +0800
commit73d4a57d47d3381faa0516b319fa5598e71681f9 (patch)
tree20f6860fb4a25ba584ac6899ba7ed8bc23e38219 /accounts/abi/abi.go
parent81d4cafb324d8c4352a40d2cf33c2085d029cd37 (diff)
downloaddexon-73d4a57d47d3381faa0516b319fa5598e71681f9.tar.gz
dexon-73d4a57d47d3381faa0516b319fa5598e71681f9.tar.zst
dexon-73d4a57d47d3381faa0516b319fa5598e71681f9.zip
acounts/abi: refactor abi, generalize abi pack/unpack to Arguments
Diffstat (limited to 'accounts/abi/abi.go')
-rw-r--r--accounts/abi/abi.go39
1 files changed, 15 insertions, 24 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go
index 6170c7062..7229a67bf 100644
--- a/accounts/abi/abi.go
+++ b/accounts/abi/abi.go
@@ -50,25 +50,25 @@ func JSON(reader io.Reader) (ABI, error) {
// methods string signature. (signature = baz(uint32,string32))
func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
// Fetch the ABI of the requested method
- var method Method
-
if name == "" {
- method = abi.Constructor
- } else {
- m, exist := abi.Methods[name]
- if !exist {
- return nil, fmt.Errorf("method '%s' not found", name)
+ // constructor
+ arguments, err := abi.Constructor.Inputs.Pack(args...)
+ if err != nil {
+ return nil, err
}
- method = m
+ return arguments, nil
+
}
- arguments, err := method.pack(args...)
+ method, exist := abi.Methods[name]
+ if !exist {
+ return nil, fmt.Errorf("method '%s' not found", name)
+ }
+
+ arguments, err := method.Inputs.Pack(args...)
if err != nil {
return nil, err
}
// Pack up the method ID too if not a constructor and return
- if name == "" {
- return arguments, nil
- }
return append(method.Id(), arguments...), nil
}
@@ -77,26 +77,17 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
if len(output) == 0 {
return fmt.Errorf("abi: unmarshalling empty output")
}
-
// since there can't be naming collisions with contracts and events,
// we need to decide whether we're calling a method or an event
- var unpack unpacker
if method, ok := abi.Methods[name]; ok {
if len(output)%32 != 0 {
return fmt.Errorf("abi: improperly formatted output")
}
- unpack = method
+ return method.Outputs.Unpack(v, output)
} else if event, ok := abi.Events[name]; ok {
- unpack = event
- } else {
- return fmt.Errorf("abi: could not locate named method or event")
- }
-
- // requires a struct to unpack into for a tuple return...
- if unpack.isTupleReturn() {
- return unpack.tupleUnpack(v, output)
+ return event.Inputs.Unpack(v, output)
}
- return unpack.singleUnpack(v, output)
+ return fmt.Errorf("abi: could not locate named method or event")
}
// UnmarshalJSON implements json.Unmarshaler interface