2

I am trying to deploy a contract to a private Ethereum chain following instructions from here and using following code. I have used this same code several times with Ropsten and it worked perfectly but now it is giving following error.

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

Please help me, where I am making mistake.

 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') ``
2
  • Did you find a solution on this?
    – chatzich
    Aug 23, 2021 at 13:29
  • try to change your transaction hex. It must be in str datatype
    – Tabz
    Sep 1, 2021 at 12:40

1 Answer 1

1

In my case,the problem was the bytecode was empty, so it was being pass an empty string which was not being properly encoded. So to fix it I changed it to this_contract = self.w3.eth.contract(abi=abi, bytecode=bytes(bytecode.encode())) like this:

   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)

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.