我正试图按照这里的说明并使用下面的代码,将合同部署到私有Ethereum链。我在Ropsten中使用了几次相同的代码,它工作得很完美,但是现在它出现了以下错误。
Unsupported type: The primitive argument must be one of: bytes,bytearray, int or bool and not str
请帮帮我,在我犯错误的地方。
import web3,time from datetime import datetime from solc import compile_source from web3 import Web3,HTTPProvider from web3.contract import ConciseContract from eth_account import Account import csv,random,sys from web3.auto import w3 import binascii number_of_files=0;
def deploy_contract(compiled_sol):
print('Deploying contract ..')
contract_id, contract_interface = compiled_sol.popitem()
w3 = Web3(Web3.HTTPProvider('http://192.168.122.31:8543'))
if(w3.isConnected()):
print('we are connected!')
else:
print('Sorry not connnected!')
# w3.eth.enable_unaudited_features()
account = Account()
acct = account.privateKeyToAccount(private_key)
contract_ = w3.eth.contract(abi = contract_interface['abi'], bytecode = contract_interface['bin'])
print('We have done this ',contract_)
print(acct.address)
unlocked = w3.geth.personal.unlock_account(acct.address,"xx")
contract_data = contract_.constructor().buildTransaction({
'from': acct.address,
'gas': 1728712,
'gasPrice':w3.toWei('21','gwei'),
'chainId': 5333
})
print('Transaction built ....')
contract_data['to']=""
trans_hash = w3.eth.sendTransaction(contract_data)
time_transaction_sent = datetime.now()
receipt = w3.eth.waitForTransactionReceipt(trans_hash.hex())
time_receipt_recieved = datetime.now()
block_n = receipt['blockNumber']
gas_used = receipt['gasUsed']
contract_address = receipt['contractAddress'] contract_code =
pragma solidity ^0.4.25;
contract SimpleStorage {
string source_address;
string dest_address;
uint protocol;
constructor() public {
field_one = '';
field_two = '';
field_three = 0;
}
function set_params (string field_one, string field_two, uint field_three) public {
field_one=field_one;
field_two=field_two;
field_three=field_three;
}
function get_field_one() public returns (string) {
return field_one;
}
function get_field_two() public returns (string) {
return field_two;
}
function get_field_three() public returns (uint) {
return field_three;
}
function get_time_stamp() public returns (uint256){
return now;
} }
try:
compiled_sol = compile_source(contract_code)
time_compilation_ended = datetime.now()
print('compiled source ..')
deploy_contract(compiled_sol) except Exception as e:
print(e, 'Error in contract code') ``发布于 2023-01-19 04:29:35
在我的例子中,问题是bytecode是空的,所以它正在传递一个空字符串,而该字符串没有被正确编码。因此,为了修复它,我将其改为this_contract = self.w3.eth.contract(abi=abi, bytecode=bytes(bytecode.encode())),如下所示:
this_contract = self.w3.eth.contract(abi=abi, bytecode=bytes(bytecode.encode()))
tx={
"ch this_contract = self.w3.eth.contract(abi=abi, bytecode=bytes(bytecode.encode()))
tx={
"chainId": self.w3.eth.chain_id,
"gasPrice": int(self.w3.eth.gas_price),
"from": self.account.address,
"nonce": self.w3.eth.get_transaction_count(self.account.address),
}
print(tx)
transaction = this_contract.constructor().buildTransaction(
tx
)
signed_txn = self.w3.eth.account.sign_transaction(transaction, private_key=self.account.key)
print("Deploying Contract!")
# Send it!
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)ainId": self.w3.eth.chain_id,
"gasPrice": int(self.w3.eth.gas_price),
"from": self.account.address,
"nonce": self.w3.eth.get_transaction_count(self.account.address),
}
print(tx)
transaction = this_contract.constructor().buildTransaction(
tx
)
signed_txn = self.w3.eth.account.sign_transaction(transaction, private_key=self.account.key)
print("Deploying Contract!")
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)https://ethereum.stackexchange.com/questions/93753
复制相似问题