github.com/Gessiux/neatchain@v1.3.1/chain/accounts/abi/abi.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "fmt" 23 "io" 24 25 "github.com/Gessiux/neatchain/utilities/common" 26 ) 27 28 // The ABI holds information about a contract's context and available 29 // invokable methods. It will allow you to type check function calls and 30 // packs data accordingly. 31 type ABI struct { 32 Constructor Method 33 Methods map[string]Method 34 Events map[string]Event 35 } 36 37 // JSON returns a parsed ABI interface and error if it failed. 38 func JSON(reader io.Reader) (ABI, error) { 39 dec := json.NewDecoder(reader) 40 41 var abi ABI 42 if err := dec.Decode(&abi); err != nil { 43 return ABI{}, err 44 } 45 46 return abi, nil 47 } 48 49 // Pack the given method name to conform the ABI. Method call's data 50 // will consist of method_id, args0, arg1, ... argN. Method id consists 51 // of 4 bytes and arguments are all 32 bytes. 52 // Method ids are created from the first 4 bytes of the hash of the 53 // methods string signature. (signature = baz(uint32,string32)) 54 func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { 55 // Fetch the ABI of the requested method 56 if name == "" { 57 // constructor 58 arguments, err := abi.Constructor.Inputs.Pack(args...) 59 if err != nil { 60 return nil, err 61 } 62 return arguments, nil 63 64 } 65 method, exist := abi.Methods[name] 66 if !exist { 67 return nil, fmt.Errorf("method '%s' not found", name) 68 } 69 70 arguments, err := method.Inputs.Pack(args...) 71 if err != nil { 72 return nil, err 73 } 74 // Pack up the method ID too if not a constructor and return 75 return append(method.ID(), arguments...), nil 76 } 77 78 // Unpack output in v according to the abi specification 79 func (abi ABI) Unpack(v interface{}, name string, data []byte) (err error) { 80 // since there can't be naming collisions with contracts and events, 81 // we need to decide whether we're calling a method or an event 82 if method, ok := abi.Methods[name]; ok { 83 if len(data)%32 != 0 { 84 return fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data) 85 } 86 return method.Outputs.Unpack(v, data) 87 } 88 if event, ok := abi.Events[name]; ok { 89 return event.Inputs.Unpack(v, data) 90 } 91 return fmt.Errorf("abi: could not locate named method or event") 92 } 93 94 // UnpackIntoMap unpacks a log into the provided map[string]interface{} 95 func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) { 96 // since there can't be naming collisions with contracts and events, 97 // we need to decide whether we're calling a method or an event 98 if method, ok := abi.Methods[name]; ok { 99 if len(data)%32 != 0 { 100 return fmt.Errorf("abi: improperly formatted output") 101 } 102 return method.Outputs.UnpackIntoMap(v, data) 103 } 104 if event, ok := abi.Events[name]; ok { 105 return event.Inputs.UnpackIntoMap(v, data) 106 } 107 return fmt.Errorf("abi: could not locate named method or event") 108 } 109 110 // UnpackMethodInputs output in v according to the abi specification 111 func (abi ABI) UnpackMethodInputs(v interface{}, name string, input []byte) (err error) { 112 if len(input) == 0 { 113 return fmt.Errorf("abi: unmarshalling empty output") 114 } 115 if method, ok := abi.Methods[name]; ok { 116 return method.Inputs.Unpack(v, input) 117 } 118 return fmt.Errorf("abi: could not locate named method or event") 119 } 120 121 // UnmarshalJSON implements json.Unmarshaler interface 122 func (abi *ABI) UnmarshalJSON(data []byte) error { 123 var fields []struct { 124 Type string 125 Name string 126 Constant bool 127 StateMutability string 128 Anonymous bool 129 Inputs []Argument 130 Outputs []Argument 131 } 132 133 if err := json.Unmarshal(data, &fields); err != nil { 134 return err 135 } 136 137 abi.Methods = make(map[string]Method) 138 abi.Events = make(map[string]Event) 139 for _, field := range fields { 140 switch field.Type { 141 case "constructor": 142 abi.Constructor = Method{ 143 Inputs: field.Inputs, 144 } 145 // empty defaults to function according to the abi spec 146 case "function", "": 147 name := field.Name 148 _, ok := abi.Methods[name] 149 for idx := 0; ok; idx++ { 150 name = fmt.Sprintf("%s%d", field.Name, idx) 151 _, ok = abi.Methods[name] 152 } 153 isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view" 154 abi.Methods[name] = Method{ 155 Name: name, 156 RawName: field.Name, 157 Const: isConst, 158 Inputs: field.Inputs, 159 Outputs: field.Outputs, 160 } 161 case "event": 162 name := field.Name 163 _, ok := abi.Events[name] 164 for idx := 0; ok; idx++ { 165 name = fmt.Sprintf("%s%d", field.Name, idx) 166 _, ok = abi.Events[name] 167 } 168 abi.Events[name] = Event{ 169 Name: name, 170 RawName: field.Name, 171 Anonymous: field.Anonymous, 172 Inputs: field.Inputs, 173 } 174 } 175 } 176 177 return nil 178 } 179 180 // MethodById looks up a method by the 4-byte id 181 // returns nil if none found 182 func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { 183 if len(sigdata) < 4 { 184 return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata)) 185 } 186 for _, method := range abi.Methods { 187 if bytes.Equal(method.ID(), sigdata[:4]) { 188 return &method, nil 189 } 190 } 191 return nil, fmt.Errorf("no method with id: %#x", sigdata[:4]) 192 } 193 194 // EventByID looks an event up by its topic hash in the 195 // ABI and returns nil if none found. 196 func (abi *ABI) EventByID(topic common.Hash) (*Event, error) { 197 for _, event := range abi.Events { 198 if bytes.Equal(event.ID().Bytes(), topic.Bytes()) { 199 return &event, nil 200 } 201 } 202 return nil, fmt.Errorf("no event with id: %#x", topic.Hex()) 203 }