2

This question already has an answer here:

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)

marked as duplicate by carver, Achala Dissanayake, Malone, Ismael, ivicaa Mar 24 '18 at 7:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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
'0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'
>>> acct.privateKey
b"\xb2\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf1\xe5\xb3d"

# 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
HexBytes('0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428')
>>> signed.hash
HexBytes('0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60')
>>> signed.r
4487286261793418179817841024889747115779324305375823110249149479905075174044
>>> signed.s
30785525769477805655994251009256770582792548537338581640010273753578382951464
>>> signed.v
37

# When you run sendRawTransaction, you get back the hash of the transaction:
>>> w3.eth.sendRawTransaction(signed.rawTransaction)  
'0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60'

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