Migrating your code from v4 to v5

Web3.py follows Semantic Versioning, which means that version 5 introduced backwards-incompatible changes. If your project depends on Web3.py v4, then you’ll probably need to make some changes.

Here are the most common required updates:

Python 3.5 no longer supported

You will need to upgrade to either Python 3.6 or 3.7

eth-abi v1 no longer supported

You will need to upgrade the eth-abi dependency to v2

Changes to base API

JSON-RPC Updates

In v4, JSON-RPC calls that looked up transactions or blocks and didn’t find them, returned None. Now if a transaction or block is not found, a BlockNotFound or a TransactionNotFound error will be thrown as appropriate. This applies to the following web3 methods:

Removed Methods

  • contract.buildTransaction was removed for contract.functions.buildTransaction.<method name>
  • contract.deploy was removed for contract.constructor.transact
  • contract.estimateGas was removed for contract.functions.<method name>.estimateGas
  • contract.call was removed for contract.<functions/events>.<method name>.call
  • contract.transact was removed for contract.<functions/events>.<method name>.transact
  • contract.eventFilter was removed for contract.events.<event name>.createFilter
  • middleware_stack was renamed to middleware_onion()
  • web3.miner.hashrate was a duplicate of hashrate() and was removed.
  • web3.version.network was a duplicate of version() and was removed.
  • web3.providers.tester.EthereumTesterProvider and web3.providers.tester.TestRPCProvider have been removed for EthereumTesterProvider()
  • web3.eth.enableUnauditedFeatures was removed
  • web3.txpool was moved to txpool()
  • web3.version.node was removed for web3.clientVersion
  • web3.version.ethereum was removed for protocolVersion()
  • Relocated personal RPC endpoints to reflect Parity and Geth implementations:
  • Relocated web3.admin module to web3.geth namespace
  • Relocated web3.miner module to web3.geth namespace

Deprecated Methods

Expect the following methods to be removed in v6:

  • web3.sha3 was deprecated for keccak()
  • web3.soliditySha3 was deprecated for solidityKeccak()
  • chainId() was deprecated for chainId(). Follow issue #1293 for details
  • web3.eth.getCompilers() was deprecated and will not be replaced
  • getTransactionFromBlock() was deprecated for getTransactionByBlock()

Deprecated ConciseContract and ImplicitContract

The ConciseContract and ImplicitContract have been deprecated and will be removed in v6.

ImplicitContract instances will need to use the verbose syntax. For example:

contract.functions.<function name>.transact({})

ConciseContract has been replaced with the ContractCaller API. Instead of using the ConciseContract factory, you can now use:

contract.caller.<function_name>

or the classic contract syntax:

contract.functions.<function name>.call().

Some more concrete examples can be found in the ContractCaller docs

Manager Provider

In v5, only a single provider will be allowed. While allowing multiple providers is a feature we’d like to support in the future, the way that multiple providers was handled in v4 wasn’t ideal. The only thing they could do was fall back. There was no mechanism for any round robin, nor was there any control around which provider was chosen. Eventually, the idea is to expand the Manager API to support injecting custom logic into the provider selection process.

For now, manager.providers has changed to manager.provider. Similarly, instances of web3.providers have been changed to web3.provider.

Testnet Changes

Web3.py will no longer automatically look up a testnet connection in IPCProvider. Something like from web3.auto.infura.ropsten import w3 should be used instead.

ENS

Web3.py has stopped inferring the .eth TLD on domain names. If a domain name is used instead of an address, you’ll need to specify the TLD. An InvalidTLD error will be thrown if the TLD is missing.

Required Infura API Key

In order to interact with Infura after March 27, 2019, you’ll need to set an environment variable called WEB3_INFURA_PROJECT_ID. You can get a project id by visiting https://infura.io/register.