@@ -31,6 +31,7 @@ import (
31
31
"unicode"
32
32
33
33
"github.com/ethereum/go-ethereum/accounts/abi"
34
+ "github.com/ethereum/go-ethereum/log"
34
35
)
35
36
36
37
// Lang is a target programming language selector to generate bindings for.
@@ -46,10 +47,13 @@ const (
46
47
// to be used as is in client code, but rather as an intermediate struct which
47
48
// enforces compile time type safety and naming convention opposed to having to
48
49
// manually maintain hard coded strings that break on runtime.
49
- func Bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang ) (string , error ) {
50
+ func Bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang , libs map [ string ] string ) (string , error ) {
50
51
// Process each individual contract requested binding
51
52
contracts := make (map [string ]* tmplContract )
52
53
54
+ // Map used to flag each encountered library as such
55
+ isLib := make (map [string ]struct {})
56
+
53
57
for i := 0 ; i < len (types ); i ++ {
54
58
// Parse the actual ABI to generate the binding for
55
59
evmABI , err := abi .JSON (strings .NewReader (abis [i ]))
@@ -137,21 +141,44 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
137
141
contracts [types [i ]] = & tmplContract {
138
142
Type : capitalise (types [i ]),
139
143
InputABI : strings .Replace (strippedABI , "\" " , "\\ \" " , - 1 ),
140
- InputBin : strings .TrimSpace (bytecodes [i ]),
144
+ InputBin : strings .TrimPrefix ( strings . TrimSpace (bytecodes [i ]), "0x" ),
141
145
Constructor : evmABI .Constructor ,
142
146
Calls : calls ,
143
147
Transacts : transacts ,
144
148
Events : events ,
149
+ Libraries : make (map [string ]string ),
145
150
Structs : structs ,
146
151
}
152
+ // Function 4-byte signatures are stored in the same sequence
153
+ // as types, if available.
147
154
if len (fsigs ) > i {
148
155
contracts [types [i ]].FuncSigs = fsigs [i ]
149
156
}
157
+ // Parse library references.
158
+ for pattern , name := range libs {
159
+ matched , err := regexp .Match ("__\\ $" + pattern + "\\ $__" , []byte (contracts [types [i ]].InputBin ))
160
+ if err != nil {
161
+ log .Error ("Could not search for pattern" , "pattern" , pattern , "contract" , contracts [types [i ]], "err" , err )
162
+ }
163
+ if matched {
164
+ contracts [types [i ]].Libraries [pattern ] = name
165
+ // keep track that this type is a library
166
+ if _ , ok := isLib [name ]; ! ok {
167
+ isLib [name ] = struct {}{}
168
+ }
169
+ }
170
+ }
171
+ }
172
+ // Check if that type has already been identified as a library
173
+ for i := 0 ; i < len (types ); i ++ {
174
+ _ , ok := isLib [types [i ]]
175
+ contracts [types [i ]].Library = ok
150
176
}
151
177
// Generate the contract template data content and render it
152
178
data := & tmplData {
153
179
Package : pkg ,
154
180
Contracts : contracts ,
181
+ Libraries : libs ,
155
182
}
156
183
buffer := new (bytes.Buffer )
157
184
0 commit comments