@@ -20,75 +20,17 @@ import (
2020 "encoding/json"
2121 "fmt"
2222 "io"
23- "strings "
23+ "math "
2424
25- "github.com/ethereum/go-ethereum/crypto"
25+ "github.com/ethereum/go-ethereum/common"
26+ "github.com/ethereum/go-ethereum/logger"
27+ "github.com/ethereum/go-ethereum/logger/glog"
2628)
2729
28- // Callable method given a `Name` and whether the method is a constant.
29- // If the method is `Const` no transaction needs to be created for this
30- // particular Method call. It can easily be simulated using a local VM.
31- // For example a `Balance()` method only needs to retrieve something
32- // from the storage and therefor requires no Tx to be send to the
33- // network. A method such as `Transact` does require a Tx and thus will
34- // be flagged `true`.
35- // Input specifies the required input parameters for this gives method.
36- type Method struct {
37- Name string
38- Const bool
39- Inputs []Argument
40- Return Type // not yet implemented
41- }
42-
43- // Returns the methods string signature according to the ABI spec.
44- //
45- // Example
46- //
47- // function foo(uint32 a, int b) = "foo(uint32,int256)"
48- //
49- // Please note that "int" is substitute for its canonical representation "int256"
50- func (m Method ) String () (out string ) {
51- out += m .Name
52- types := make ([]string , len (m .Inputs ))
53- i := 0
54- for _ , input := range m .Inputs {
55- types [i ] = input .Type .String ()
56- i ++
57- }
58- out += "(" + strings .Join (types , "," ) + ")"
59-
60- return
61- }
62-
63- func (m Method ) Id () []byte {
64- return crypto .Sha3 ([]byte (m .String ()))[:4 ]
65- }
66-
67- // Argument holds the name of the argument and the corresponding type.
68- // Types are used when packing and testing arguments.
69- type Argument struct {
70- Name string
71- Type Type
72- }
73-
74- func (a * Argument ) UnmarshalJSON (data []byte ) error {
75- var extarg struct {
76- Name string
77- Type string
78- }
79- err := json .Unmarshal (data , & extarg )
80- if err != nil {
81- return fmt .Errorf ("argument json err: %v" , err )
82- }
83-
84- a .Type , err = NewType (extarg .Type )
85- if err != nil {
86- return err
87- }
88- a .Name = extarg .Name
89-
90- return nil
91- }
30+ // Executer is an executer method for performing state executions. It takes one
31+ // argument which is the input data and expects output data to be returned as
32+ // multiple 32 byte word length concatenated slice
33+ type Executer func (datain []byte ) []byte
9234
9335// The ABI holds information about a contract's context and available
9436// invokable methods. It will allow you to type check function calls and
@@ -97,6 +39,18 @@ type ABI struct {
9739 Methods map [string ]Method
9840}
9941
42+ // JSON returns a parsed ABI interface and error if it failed.
43+ func JSON (reader io.Reader ) (ABI , error ) {
44+ dec := json .NewDecoder (reader )
45+
46+ var abi ABI
47+ if err := dec .Decode (& abi ); err != nil {
48+ return ABI {}, err
49+ }
50+
51+ return abi , nil
52+ }
53+
10054// tests, tests whether the given input would result in a successful
10155// call. Checks argument list count and matches input to `input`.
10256func (abi ABI ) pack (name string , args ... interface {}) ([]byte , error ) {
@@ -145,6 +99,55 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
14599 return packed , nil
146100}
147101
102+ // toGoType parses the input and casts it to the proper type defined by the ABI
103+ // argument in t.
104+ func toGoType (t Argument , input []byte ) interface {} {
105+ switch t .Type .T {
106+ case IntTy :
107+ return common .BytesToBig (input )
108+ case UintTy :
109+ return common .BytesToBig (input )
110+ case BoolTy :
111+ return common .BytesToBig (input ).Uint64 () > 0
112+ case AddressTy :
113+ return common .BytesToAddress (input )
114+ case HashTy :
115+ return common .BytesToHash (input )
116+ }
117+ return nil
118+ }
119+
120+ // Call executes a call and attemps to parse the return values and returns it as
121+ // an interface. It uses the executer method to perform the actual call since
122+ // the abi knows nothing of the lower level calling mechanism.
123+ //
124+ // Call supports all abi types and includes multiple return values. When only
125+ // one item is returned a single interface{} will be returned, if a contract
126+ // method returns multiple values an []interface{} slice is returned.
127+ func (abi ABI ) Call (executer Executer , name string , args ... interface {}) interface {} {
128+ callData , err := abi .Pack (name , args ... )
129+ if err != nil {
130+ glog .V (logger .Debug ).Infoln ("pack error:" , err )
131+ return nil
132+ }
133+
134+ output := executer (callData )
135+
136+ method := abi .Methods [name ]
137+ ret := make ([]interface {}, int (math .Max (float64 (len (method .Outputs )), float64 (len (output )/ 32 ))))
138+ for i := 0 ; i < len (ret ); i += 32 {
139+ index := i / 32
140+ ret [index ] = toGoType (method .Outputs [index ], output [i :i + 32 ])
141+ }
142+
143+ // return single interface
144+ if len (ret ) == 1 {
145+ return ret [0 ]
146+ }
147+
148+ return ret
149+ }
150+
148151func (abi * ABI ) UnmarshalJSON (data []byte ) error {
149152 var methods []Method
150153 if err := json .Unmarshal (data , & methods ); err != nil {
@@ -158,14 +161,3 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
158161
159162 return nil
160163}
161-
162- func JSON (reader io.Reader ) (ABI , error ) {
163- dec := json .NewDecoder (reader )
164-
165- var abi ABI
166- if err := dec .Decode (& abi ); err != nil {
167- return ABI {}, err
168- }
169-
170- return abi , nil
171- }
0 commit comments