I am trying to deploy a contract to a test network like Ropsten.

I am running through Infura using the HTTPProvider. I need to be able to sign the deployment of the contract locally, but can not figure out how to do this with the current API.

Any pointers?

  • The testnet name you likely mean is Ropsten. – Noel Maersk May 7 '18 at 11:02

I'll assume you already have a private key available, using something like extracting a private key from a geth keyfile, and that you are using Web3.py v4.2.0.

The general approach is to:

  • construct your contract with any initialization arguments
  • build the transaction for the contract constructor
  • sign the transaction with your private key
  • broadcast the signed transaction
  • wait for the transaction to be mined

One convenient way to build the transaction locally is to use the constructor() method on a contract object. For example:

deploy_txn = my_contract.constructor(*init_args).buildTransaction()

Then, sign the transaction locally:

signed = w3.eth.account.signTransaction(deploy_txn, private_key)

Finally, broadcast the transaction, and wait for it to be mined:

txn_hash = w3.eth.sendRawTransaction(signed.rawTransaction)  
txn_receipt = w3.eth.waitForTransactionReceipt(txn_hash)

To use the contract, you can rebuild the contract object with its new address. The address of your new contract is available at txn_receipt['contractAddress'].

| improve this answer | |

You can take a look at (my) furiate script, which submits transactions to Infura using a local account.

It's slightly dated, and (currently) has a web3.py beta in requirements.txt, but should work with the latest v4 stable (v4.2.0, I think).

It uses the eth_account package separately, but should be possible in about the same way with web3.py, which incorporates eth_account's functionality.

In particular:

P.S. There's a backup repo on GitHub if you don't have a GitLab account, but would still like to "star" it for later. ;)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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