This commit is contained in:
RJ Catalano 2017-06-08 08:17:13 +00:00 committed by GitHub
commit 90ae0aed3c
4 changed files with 17 additions and 56 deletions

View file

@ -32,12 +32,14 @@ import (
// invokable methods. It will allow you to type check function calls and
// packs data accordingly.
type ABI struct {
Constructor Method
Methods map[string]Method
Events map[string]Event
Constructor Method `json:"constructor"`
Fallback Method `json:"fallback"`
Methods map[string]Method `json:"function"`
Events map[string]Event `json:"event"`
}
// JSON returns a parsed ABI interface and error if it failed.
// Note [RJ] - Can we deprecate this?
func JSON(reader io.Reader) (ABI, error) {
dec := json.NewDecoder(reader)
@ -372,46 +374,3 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
return nil
}
func (abi *ABI) UnmarshalJSON(data []byte) error {
var fields []struct {
Type string
Name string
Constant bool
Indexed bool
Anonymous bool
Inputs []Argument
Outputs []Argument
}
if err := json.Unmarshal(data, &fields); err != nil {
return err
}
abi.Methods = make(map[string]Method)
abi.Events = make(map[string]Event)
for _, field := range fields {
switch field.Type {
case "constructor":
abi.Constructor = Method{
Inputs: field.Inputs,
}
// empty defaults to function according to the abi spec
case "function", "":
abi.Methods[field.Name] = Method{
Name: field.Name,
Const: field.Constant,
Inputs: field.Inputs,
Outputs: field.Outputs,
}
case "event":
abi.Events[field.Name] = Event{
Name: field.Name,
Anonymous: field.Anonymous,
Inputs: field.Inputs,
}
}
}
return nil
}

View file

@ -24,9 +24,9 @@ import (
// Argument holds the name of the argument and the corresponding type.
// Types are used when packing and testing arguments.
type Argument struct {
Name string
Type Type
Indexed bool // indexed is only used by events
Name string `json:"name"`
Type Type `json:"type"`
Indexed bool `json:"indexed"` // indexed is only used by events
}
func (a *Argument) UnmarshalJSON(data []byte) error {

View file

@ -28,9 +28,9 @@ import (
// holds type information (inputs) about the yielded output. Anonymous events
// don't get the signature canonical representation as the first LOG topic.
type Event struct {
Name string
Anonymous bool
Inputs []Argument
Name string `json:"name"`
Anonymous bool `json:"anonymous"`
Inputs []Argument `json:"inputs"`
}
// Id returns the canonical representation of the event's signature used by the

View file

@ -33,10 +33,12 @@ import (
// be flagged `true`.
// Input specifies the required input parameters for this gives method.
type Method struct {
Name string
Const bool
Inputs []Argument
Outputs []Argument
Name string `json:"name"`
Const bool `json:"constant"`
Inputs []Argument `json:"inputs"`
Outputs []Argument `json:"outputs"`
//to add
// Payable bool `json:"payable"`
}
func (m Method) pack(method Method, args ...interface{}) ([]byte, error) {