I am trying to use web3.py with Infura as my HTTP Provider, and several things aren't working. I modified the example code from the web3.py Documentation to use my own contract, which is published on the Ropsten network.

My question is how do I generate an Ethereum account so that w3.eth.accounts[0] returns something besides an empty list? I have checked the documentation and cannot find how to do this. I have also tried replacing w3.eth.accounts[0] with other addresses (like my Metamask address), but this results in the following error:

Traceback (most recent call last):
  File "C:\Users\Eugene\Documents\UCLA\Year 1\ECE 209AS\Blockchain Project\test.py", line 242, in <module>
    tx_hash = contract.deploy(transaction={'from': '0xc0615cb1a1a57422d3887fa4874958578013e842'})
  File "C:\Users\Eugene\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web3\contract.py", line 308, in deploy
    deploy_transaction['data'] = cls._encode_constructor_data(args, kwargs)
  File "C:\Users\Eugene\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eth_utils\string.py", line 108, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "C:\Users\Eugene\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web3\contract.py", line 749, in _encode_constructor_data
    arguments = merge_args_and_kwargs(constructor_abi, args, kwargs)
  File "C:\Users\Eugene\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eth_utils\string.py", line 92, in inner
    return fn(*text_args, **text_kwargs)
  File "C:\Users\Eugene\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web3\utils\abi.py", line 185, in merge_args_and_kwargs
    len(args) + len(kwargs),
TypeError: Incorrect argument count.  Expected '1'.  Got '0'

Here is my code with the abi and bytecode omitted for brevity.

import json
import web3

from web3 import Web3, HTTPProvider, TestRPCProvider
from web3.contract import ConciseContract
# web3.py instance
w3 = Web3(HTTPProvider('https://ropsten.infura.io/SdO4U3ydQdgK3D3eNE2Y'))

# Instantiate and deploy contract
contract = w3.eth.contract(abi=[
], bytecode={
    "linkReferences": {},
    "object": "...",
    "opcodes": "...",
    "sourceMap": "..."

# Get transaction hash from deployed contract
tx_hash = contract.deploy(transaction={'from': w3.eth.accounts[0], 'gas': 410000})

# Get tx receipt to get contract address
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
contract_address = tx_receipt['contractAddress']

# Contract instance in concise mode
# contract_instance = w3.eth.contract(contract_interface['abi'], contract_address, ContractFactoryClass=ConciseContract)

Infura cannot (and should not) host a private key for you. It would be their ether and not yours. :)

If you want to work with local keys and Infura, check out the eth-account library, which has eth_account.Account.create():

>>> from eth_account import Account
>>> acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
>>> acct.address
>>> acct.privateKey

# These methods are also available: signHash(), signTransaction(), encrypt()
# They correspond to the same-named methods in Account.*
# but without the private key argument

Once you've created an account locally, you can sign the transaction locally and broadcast it:

>>> transaction = {
...     'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
...     'value': 1000000000,
...     'gas': 2000000,
...     'gasPrice': 234567897654321,
...     'nonce': 0,
...     'chainId': 1
... }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
>>> signed = w3.eth.account.signTransaction(transaction, key)
>>> signed.rawTransaction
>>> signed.hash
>>> signed.r
>>> signed.s
>>> signed.v

# When you run sendRawTransaction, you get back the hash of the transaction:
>>> w3.eth.sendRawTransaction(signed.rawTransaction)  
| improve this answer | |

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