Contract-Deployment

使用 web3.py 與庫發布契約

  • April 5, 2021

我試圖在其中發布帶有庫的契約,但我遇到了以下問題。沒有庫的契約發布得很好。在建議對程式碼進行什麼調整之前可能遇到過這個問題的人可以嗎?

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)

契約穩固性在這裡

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

下面的錯誤

$ 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'

如果不使用 truffle、populus 等顯式建構工具,您必須自己手動進行連結。

如果您檢查字節碼,您會發現插入的(非十六進制)文本。打算在該位置拼接已編譯和部署的庫的地址。

改變這個:

bytecode=contract_interface['bin']

對此:

bytecode='0x'+contract_interface['bin']

引用自:https://ethereum.stackexchange.com/questions/57573