Im trying to publish contracts with libraries in them and im getting the below issue. Contracts without libraries publish just fine. Can someone who might have faced this issue before advise what the tweak required to the code is ?

import json
import web3

from web3 import Web3, HTTPProvider

from solc import compile_source

from web3.contract import ConciseContract

with open('BurnableCrowdsaleToken_Flat_v2.sol', 'r') as myfile:
  contract_source_code = myfile.read()

compiled_sol = compile_source(contract_source_code)
contract_interface = compiled_sol['<stdin>:BurnableCrowdsaleToken']

web3 = Web3(HTTPProvider('http://localhost:8545'))

from web3.middleware import geth_poa_middleware
web3.middleware_stack.inject(geth_poa_middleware, layer=0)

contract_ = web3.eth.contract(

acct = web3.eth.account.privateKeyToAccount('myprivatekey')

construct_txn = contract_.constructor("Burnable Token","BCT","50000","18","true").buildTransaction({
    'from': acct.address,
    'nonce': web3.eth.getTransactionCount(acct.address),
    'gas': 1728712,
    'gasPrice': web3.toWei('21', 'gwei')})

signed = acct.signTransaction(construct_txn)

tx_hash = web3.eth.sendRawTransaction(signed.rawTransaction)

tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)

print (tx_receipt.contractAddress)

contract solidity here


Error below

$ python3 contracts_priv.py 

Traceback (most recent call last):   File "/usr/local/lib/python3.5/dist-packages/web3/utils/formatters.py", line 68, in apply_formatters_to_dict
    yield key, formatters[key](item)   File "/usr/local/lib/python3.5/dist-packages/web3/utils/normalizers.py", line 166, in normalize_bytecode
    bytecode = HexBytes(bytecode)   File "/usr/local/lib/python3.5/dist-packages/hexbytes/main.py", line 17, in
    bytesval = hexstr_if_str(to_bytes, val)   File "/usr/local/lib/python3.5/dist-packages/eth_utils/conversions.py", line 146, in hexstr_if_str
    hexstr_or_primitive, ValueError: when sending a str, it must be a hex string. 

Got: '6060604052.....be907dd5250029'

Traceback (most recent call last):   File "contracts_priv.py", line 28, in <module>
    bytecode=contract_interface['bin'])   File "/usr/local/lib/python3.5/dist-packages/web3/eth.py", line 367, in contract
    ContractFactory = ContractFactoryClass.factory(self.web3, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/web3/contract.py", line 265, in factory
    normalizers=normalizers)   File "/usr/local/lib/python3.5/dist-packages/web3/utils/datatypes.py", line 33, in __new__
    namespace)   File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__   File "/usr/local/lib/python3.5/dist-packages/eth_utils/functional.py", line 22, in inner
    return callback(fn(*args, **kwargs))   File "/usr/local/lib/python3.5/dist-packages/web3/utils/formatters.py", line 70, in apply_formatters_to_dict
    raise type(exc)("Could not format value %r as field %r" % (item, key)) from exc ValueError: Could not format value '6060604052600....907dd5250029' as field 'bytecode'

3 Answers 3


py-solc-x includes a link_code method that will insert library addresses into your bytecode:

>>> from solcx import link_code
>>> unlinked_bytecode = "606060405260768060106000396000f3606060405260e060020a6000350463e7f09e058114601a575b005b60187f0c55699c00000000000000000000000000000000000000000000000000000000606090815273__TestA_________________________________90630c55699c906064906000906004818660325a03f41560025750505056"
>>> link_code(
...     unlinked_bytecode,
...     {'TestA': '0xd3cda913deb6f67967b99d67acdfa1712c293601'}
... )

This method is also available in the (now deprecated) py-solc library that you reference in your example.


Without using an explicit build tool like truffle, populus, etc, you'll have to do the linking yourself, manually.

If you inspect the bytecode, you'll find inserted (non-hex) text. It's intended that you splice in the address of the compiled & deployed library at that location.

  • do you have an example for this by any chance ? thanks for your help Aug 29, 2018 at 0:15

Change this:


To this:

  • It will work without the '0x' in web3 v4+
    – carver
    Aug 28, 2018 at 16:32
  • i tried this but the issue remains. Aug 29, 2018 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.