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

Configuring

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

Example:

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


   # Uses MEVblocker.io to broadcast transactions
   # and two separate nodes for reading blockchain data
   config = "mev+https://rpc.mevblocker.io https://myethereumnode1.example.com https://fallback.example.com"
   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 = """
    mev+https://rpc.mevblocker.io
    https://myethereumnode1.example.com
    https://fallback.example.com
    """

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("https://polygon-rpc.com")

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
export JSON_RPC_POLYGON=https://polygon-rpc.com/

# Passing multiple providers: These URLs may contain API key
export JSON_RPC_BINANCE=https://bsc-rpc.gateway.pokt.network/ https://bsc-dataseed.bnbchain.org https://bsc.nodereal.io

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"])