@@ -46,7 +46,7 @@ def solc_arguments(libraries=None, combined='bin,abi', optimize=True):
46
46
if optimize :
47
47
args .append ('--optmize' )
48
48
49
- if libraries is not None :
49
+ if libraries is not None and len ( libraries ) :
50
50
addresses = [
51
51
'{name}:{address}' .format (name = name , address = address )
52
52
for name , address in libraries .items ()
@@ -155,12 +155,17 @@ def solidity_library_symbol(library_name):
155
155
# the symbol is always 40 characters in length with the minimum of two
156
156
# leading and trailing underscores
157
157
length = min (len (library_name ), 36 )
158
- symbol = bytearray ('_' * 40 )
159
- symbol [2 :length ] = library_name [:length ]
160
- return str (symbol )
161
158
159
+ library_piece = library_name [:length ]
160
+ hold_piece = '_' * (36 - length )
162
161
163
- def solidity_resolve_address (hex_code , library_name , library_address ):
162
+ return '__{library}{hold}__' .format (
163
+ library = library_piece ,
164
+ hold = hold_piece ,
165
+ )
166
+
167
+
168
+ def solidity_resolve_address (hex_code , library_symbol , library_address ):
164
169
""" Change the bytecode to use the given library address.
165
170
166
171
Args:
@@ -172,12 +177,32 @@ def solidity_resolve_address(hex_code, library_name, library_address):
172
177
bin: The bytecode encoded in hexadecimal with the library references
173
178
resolved.
174
179
"""
175
- symbol = solidity_library_symbol (library_name )
176
- return hex_code .replace (symbol , library_address )
180
+ return hex_code .replace (library_symbol , library_address )
181
+
182
+
183
+ def solidity_resolve_symbols (hex_code , libraries ):
184
+ symbol_address = {
185
+ solidity_library_symbol (library_name ): address
186
+ for library_name , address in libraries .items ()
187
+ }
188
+
189
+ for unresolved in solidity_unresolved_symbols (hex_code ):
190
+ address = symbol_address [unresolved ]
191
+ hex_code = solidity_resolve_address (hex_code , unresolved , address )
192
+
193
+ return hex_code
177
194
178
195
179
196
def solidity_unresolved_symbols (hex_code ):
180
- """ Return the unresolved symbols contained in the `hex_code`. """
197
+ """ Return the unresolved symbols contained in the `hex_code`.
198
+
199
+ Note:
200
+ The binary representation should not be provided since this function
201
+ relies on the fact that the '_' is invalid in hex encoding.
202
+
203
+ Args:
204
+ hex_code (str): The bytecode encoded as hexadecimal.
205
+ """
181
206
iterator = iter (hex_code )
182
207
symbol_names = []
183
208
0 commit comments