MEV protection and multiple JSON-RPCs configuration

Web-Ethereum-Defi package supports creating eth_defi.provider.multi_provider.MultiProviderWeb3 subclass instances of web3.Web3 core connector.

These instances support multiple JSON-RPC providers, mainly


The multi RPC endpoints can be created with eth_defi.provider.multi_provider.create_multi_provider_web3().

Instead of giving a single RPC endpoint URL, you give a list URLs.

  • List can be newline separated or space separated

  • For MEV protection endpoint, you prefix your URL with mev+ - this endpoint is always used for eth_sendTransaction and eth_sendRawTransction JSON-RPC endpoints


   from eth_defi.provider.multi_provider import MultiProviderWeb3
   from eth_defi.provider.multi_provider import create_multi_provider_web3

   # Uses to broadcast transactions
   # and two separate nodes for reading blockchain data
   config = "mev+"
   web3: MultiProviderWeb3 = create_multi_provider_web3(config)

   # If one provider fails, MultiProviderWeb3 will switch to the next one
   print(f"Currently active call endpoint for JSON-RPC is {web3.get_active_call_provider()}")

You can also have it new-line separated for readability:
config = """

If you want to keep using a single RPC endpoint, you do not need to do any changes:

# Pasing a single RPC endpoint URL is ok
web3 = create_multi_provider_web3("")

Because JSON-RPC provider URLs contains API keys the preferred way to pass them around is using environment variables.

In your UNIX shell:

# Passing single provider: This URL may contain API key

# Passing multiple providers: These URLs may contain API key

And then:

import os
from eth_defi.provider.multi_provider import create_multi_provider_web3

web3 = create_multi_provider_web3(os.environ["JSON_RPC_POLYGON"])