1

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(
    abi=contract_interface['abi'],
    bytecode=contract_interface['bin'])

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

https://gist.github.com/troowala/2860203ffb2cad3d38cd3c0301423a28

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
__new__
    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'
0

Change this:

bytecode=contract_interface['bin']

To this:

bytecode='0x'+contract_interface['bin']
  • It will work without the '0x' in web3 v4+ – carver Aug 28 '18 at 16:32
  • i tried this but the issue remains. – Tanveer Roowala Aug 29 '18 at 0:14
0

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 – Tanveer Roowala Aug 29 '18 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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