首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Web3.py错误:不支持的类型:基本参数必须是:字节、字节数组之一

Web3.py错误:不支持的类型:基本参数必须是:字节、字节数组之一
EN

Ethereum用户
提问于 2021-02-21 07:46:52
回答 1查看 649关注 0票数 1

我正试图按照这里的说明并使用下面的代码,将合同部署到私有Ethereum链。我在Ropsten中使用了几次相同的代码,它工作得很完美,但是现在它出现了以下错误。

Unsupported type: The primitive argument must be one of: bytes,bytearray, int or bool and not str

请帮帮我,在我犯错误的地方。

代码语言:javascript
运行
复制
 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') ``
EN

回答 1

Ethereum用户

发布于 2023-01-19 04:29:35

在我的例子中,问题是bytecode是空的,所以它正在传递一个空字符串,而该字符串没有被正确编码。因此,为了修复它,我将其改为this_contract = self.w3.eth.contract(abi=abi, bytecode=bytes(bytecode.encode())),如下所示:

代码语言:javascript
运行
复制
   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)
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/93753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档