content: import pytest
  import asyncio
  import json
  import math
  from random import (
      randint,
  )
  import re
  from typing import (
      TYPE_CHECKING,
      Any,
      Callable,
      Union,
      cast,
  )
  
  import eth_abi as abi
  from eth_typing import (
      BlockNumber,
      ChecksumAddress,
      HexAddress,
      HexStr,
  )
  from eth_utils import (
      is_boolean,
      is_bytes,
      is_checksum_address,
      is_dict,
      is_integer,
      is_list_like,
      is_same_address,
      is_string,
      remove_0x_prefix,
      to_bytes,
  )
  from eth_utils.toolz import (
      assoc,
      merge,
  )
  from hexbytes import (
      HexBytes,
  )
  
  from web3._utils.ens import (
      ens_addresses,
  )
  from web3._utils.error_formatters_utils import (
      PANIC_ERROR_CODES,
  )
  from web3._utils.fee_utils import (
      PRIORITY_FEE_MIN,
  )
  from web3._utils.method_formatters import (
      to_hex_if_integer,
  )
  from web3._utils.module_testing.module_testing_utils import (
      assert_contains_log,
      async_mock_offchain_lookup_request_response,
      mock_offchain_lookup_request_response,
  )
  from web3._utils.module_testing.utils import (
      RequestMocker,
  )
  from web3._utils.type_conversion import (
      to_hex_if_bytes,
  )
  from web3.exceptions import (
      BlockNotFound,
      ContractCustomError,
      ContractLogicError,
      ContractPanicError,
      InvalidAddress,
      InvalidTransaction,
      MultipleFailedRequests,
      NameNotFound,
      OffchainLookup,
      TimeExhausted,
      TooManyRequests,
      TransactionNotFound,
      TransactionTypeMismatch,
      Web3RPCError,
      Web3ValidationError,
      Web3ValueError,
  )
  from web3.middleware import (
      ExtraDataToPOAMiddleware,
      SignAndSendRawMiddlewareBuilder,
  )
  from web3.types import (
      ENS,
      BlockData,
      FilterParams,
      Nonce,
      RPCEndpoint,
      StateOverrideParams,
      SyncStatus,
      TxData,
      TxParams,
      Wei,
  )
  
  UNKNOWN_ADDRESS = ChecksumAddress(
      HexAddress(HexStr("0xdEADBEeF00000000000000000000000000000000"))
  )
  
  UNKNOWN_HASH = HexStr(
      "0xdeadbeef00000000000000000000000000000000000000000000000000000000"
  )
  # "test offchain lookup" as an abi-encoded string
  OFFCHAIN_LOOKUP_TEST_DATA = "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001474657374206f6666636861696e206c6f6f6b7570000000000000000000000000"  # noqa: E501
  OFFCHAIN_LOOKUP_4BYTE_DATA = "0x556f1830"
  OFFCHAIN_LOOKUP_RETURN_DATA = "00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001a0da96d05a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002c68747470733a2f2f776562332e70792f676174657761792f7b73656e6465727d2f7b646174617d2e6a736f6e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001768747470733a2f2f776562332e70792f6761746577617900000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001474657374206f6666636861696e206c6f6f6b757000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001474657374206f6666636861696e206c6f6f6b7570000000000000000000000000"  # noqa: E501
  # "web3py" as an abi-encoded string
  WEB3PY_AS_HEXBYTES = "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067765623370790000000000000000000000000000000000000000000000000000"  # noqa: E501
  
  
  if TYPE_CHECKING:
      from _pytest.monkeypatch import MonkeyPatch  # noqa: F401
  
      from web3.contract import (  # noqa: F401
          AsyncContract,
          Contract,
      )
      from web3.main import (  # noqa: F401
          AsyncWeb3,
          Web3,
      )
  
  
  def abi_encoded_offchain_lookup_contract_address(
      w3: Union["Web3", "AsyncWeb3[Any]"],
      offchain_lookup_contract: Union["Contract", "AsyncContract"],
  ) -> HexAddress:
      return HexAddress(
          remove_0x_prefix(
              w3.to_hex(
                  abi.encode(
                      ["address"],
                      [to_bytes(hexstr=offchain_lookup_contract.address)],
                  )
              )
          )
      )
  
  
  class AsyncEthModuleTest:
      @pytest.mark.asyncio
      async def test_eth_gas_price(self, async_w3: "AsyncWeb3[Any]") -> None:
          gas_price = await async_w3.eth.gas_price
  
          assert gas_price > 0
  
      @pytest.mark.asyncio
      async def test_is_connected(self, async_w3: "AsyncWeb3[Any]") -> None:
          is_connected = await async_w3.is_connected()
          assert is_connected is True
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_legacy(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "gasPrice": await async_w3.eth.gas_price,
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 21000
          assert txn["gasPrice"] == txn_params["gasPrice"]
  
      @pytest.mark.asyncio
      async def test_eth_modify_transaction_legacy(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "gasPrice": async_w3.to_wei(
                  1, "gwei"
              ),  # must be greater than base_fee post London
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
  
          modified_txn_hash = await async_w3.eth.modify_transaction(
              txn_hash, gasPrice=(cast(Wei, txn_params["gasPrice"] * 2)), value=Wei(2)
          )
          modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)
  
          assert is_same_address(
              modified_txn["from"], cast(ChecksumAddress, txn_params["from"])
          )
          assert is_same_address(
              modified_txn["to"], cast(ChecksumAddress, txn_params["to"])
          )
          assert modified_txn["value"] == 2
          assert modified_txn["gas"] == 21000
          assert modified_txn["gasPrice"] == cast(int, txn_params["gasPrice"]) * 2
  
      @pytest.mark.asyncio
      async def test_eth_modify_transaction(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
              "maxFeePerGas": async_w3.to_wei(2, "gwei"),
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
  
          modified_txn_hash = await async_w3.eth.modify_transaction(
              txn_hash,
              value=Wei(2),
              maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"] * 2)),
              maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"] * 2)),
          )
          modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)
  
          assert is_same_address(
              modified_txn["from"], cast(ChecksumAddress, txn_params["from"])
          )
          assert is_same_address(
              modified_txn["to"], cast(ChecksumAddress, txn_params["to"])
          )
          assert modified_txn["value"] == 2
          assert modified_txn["gas"] == 21000
          assert (
              modified_txn["maxPriorityFeePerGas"]
              == cast(Wei, txn_params["maxPriorityFeePerGas"]) * 2
          )
          assert modified_txn["maxFeePerGas"] == cast(Wei, txn_params["maxFeePerGas"]) * 2
  
      @pytest.mark.asyncio
      async def test_async_eth_sign_transaction(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": async_w3.to_wei(2, "gwei"),
              "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
              "nonce": Nonce(0),
          }
          result = await async_w3.eth.sign_transaction(txn_params)
          signatory_account = async_w3.eth.account.recover_transaction(result["raw"])
          assert async_keyfile_account_address_dual_type == signatory_account
          assert result["tx"]["to"] == txn_params["to"]
          assert result["tx"]["value"] == txn_params["value"]
          assert result["tx"]["gas"] == txn_params["gas"]
          assert result["tx"]["maxFeePerGas"] == txn_params["maxFeePerGas"]
          assert (
              result["tx"]["maxPriorityFeePerGas"] == txn_params["maxPriorityFeePerGas"]
          )
          assert result["tx"]["nonce"] == txn_params["nonce"]
  
      @pytest.mark.asyncio
      async def test_eth_sign_typed_data(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
          async_skip_if_testrpc: Callable[["AsyncWeb3[Any]"], None],
      ) -> None:
          validJSONMessage = """
              {
                  "types": {
                      "EIP712Domain": [
                          {"name": "name", "type": "string"},
                          {"name": "version", "type": "string"},
                          {"name": "chainId", "type": "uint256"},
                          {"name": "verifyingContract", "type": "address"}
                      ],
                      "Person": [
                          {"name": "name", "type": "string"},
                          {"name": "wallet", "type": "address"}
                      ],
                      "Mail": [
                          {"name": "from", "type": "Person"},
                          {"name": "to", "type": "Person"},
                          {"name": "contents", "type": "string"}
                      ]
                  },
                  "primaryType": "Mail",
                  "domain": {
                      "name": "Ether Mail",
                      "version": "1",
                      "chainId": "0x01",
                      "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
                  },
                  "message": {
                      "from": {
                          "name": "Cow",
                          "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
                      },
                      "to": {
                          "name": "Bob",
                          "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
                      },
                      "contents": "Hello, Bob!"
                  }
              }
          """
          async_skip_if_testrpc(async_w3)
          signature = HexBytes(
              await async_w3.eth.sign_typed_data(
                  async_keyfile_account_address_dual_type, json.loads(validJSONMessage)
              )
          )
          assert len(signature) == 32 + 32 + 1
  
      @pytest.mark.asyncio
      async def test_invalid_eth_sign_typed_data(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
          async_skip_if_testrpc: Callable[["AsyncWeb3[Any]"], None],
      ) -> None:
          async_skip_if_testrpc(async_w3)
          invalid_typed_message = """
              {
                  "types": {
                      "EIP712Domain": [
                          {"name": "name", "type": "string"},
                          {"name": "version", "type": "string"},
                          {"name": "chainId", "type": "uint256"},
                          {"name": "verifyingContract", "type": "address"}
                      ],
                      "Person": [
                          {"name": "name", "type": "string"},
                          {"name": "wallet", "type": "address"}
                      ],
                      "Mail": [
                          {"name": "from", "type": "Person"},
                          {"name": "to", "type": "Person[2]"},
                          {"name": "contents", "type": "string"}
                      ]
                  },
                  "primaryType": "Mail",
                  "domain": {
                      "name": "Ether Mail",
                      "version": "1",
                      "chainId": "0x01",
                      "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
                  },
                  "message": {
                      "from": {
                          "name": "Cow",
                          "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
                      },
                      "to": [{
                          "name": "Bob",
                          "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
                      }],
                      "contents": "Hello, Bob!"
                  }
              }
          """
          with pytest.raises(
              Web3ValueError,
              match=r".*Expected 2 items for array type Person\[2\], got 1 items.*",
          ):
              await async_w3.eth.sign_typed_data(
                  async_keyfile_account_address_dual_type,
                  json.loads(invalid_typed_message),
              )
  
      @pytest.mark.asyncio
      async def test_async_eth_sign_transaction_legacy(
          self, async_w3: "AsyncWeb3[Any]", async_keyfile_account_address: ChecksumAddress
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address,
              "to": async_keyfile_account_address,
              "value": Wei(1),
              "gas": 21000,
              "gasPrice": await async_w3.eth.gas_price,
              "nonce": Nonce(0),
          }
          result = await async_w3.eth.sign_transaction(txn_params)
          signatory_account = async_w3.eth.account.recover_transaction(result["raw"])
          assert async_keyfile_account_address == signatory_account
          assert result["tx"]["to"] == txn_params["to"]
          assert result["tx"]["value"] == txn_params["value"]
          assert result["tx"]["gas"] == txn_params["gas"]
          assert result["tx"]["gasPrice"] == txn_params["gasPrice"]
          assert result["tx"]["nonce"] == txn_params["nonce"]
  
      @pytest.mark.asyncio
      async def test_async_eth_sign_transaction_hex_fees(
          self, async_w3: "AsyncWeb3[Any]", async_keyfile_account_address: ChecksumAddress
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address,
              "to": async_keyfile_account_address,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": hex(async_w3.to_wei(2, "gwei")),
              "maxPriorityFeePerGas": hex(async_w3.to_wei(1, "gwei")),
              "nonce": Nonce(0),
          }
          result = await async_w3.eth.sign_transaction(txn_params)
          signatory_account = async_w3.eth.account.recover_transaction(result["raw"])
          assert async_keyfile_account_address == signatory_account
          assert result["tx"]["to"] == txn_params["to"]
          assert result["tx"]["value"] == txn_params["value"]
          assert result["tx"]["gas"] == txn_params["gas"]
          assert result["tx"]["maxFeePerGas"] == int(str(txn_params["maxFeePerGas"]), 16)
          assert result["tx"]["maxPriorityFeePerGas"] == int(
              str(txn_params["maxPriorityFeePerGas"]), 16
          )
          assert result["tx"]["nonce"] == txn_params["nonce"]
  
      @pytest.mark.asyncio
      async def test_async_eth_sign_transaction_ens_names(
          self, async_w3: "AsyncWeb3[Any]", async_keyfile_account_address: ChecksumAddress
      ) -> None:
          with ens_addresses(
              async_w3, {"unlocked-account.eth": async_keyfile_account_address}
          ):
              txn_params: TxParams = {
                  "from": "unlocked-account.eth",
                  "to": "unlocked-account.eth",
                  "value": Wei(1),
                  "gas": 21000,
                  "maxFeePerGas": async_w3.to_wei(2, "gwei"),
                  "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
                  "nonce": Nonce(0),
              }
              result = await async_w3.eth.sign_transaction(txn_params)
              signatory_account = async_w3.eth.account.recover_transaction(result["raw"])
              assert async_keyfile_account_address == signatory_account
              assert result["tx"]["to"] == async_keyfile_account_address
              assert result["tx"]["value"] == txn_params["value"]
              assert result["tx"]["gas"] == txn_params["gas"]
              assert result["tx"]["maxFeePerGas"] == txn_params["maxFeePerGas"]
              assert (
                  result["tx"]["maxPriorityFeePerGas"]
                  == txn_params["maxPriorityFeePerGas"]
              )
              assert result["tx"]["nonce"] == txn_params["nonce"]
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": async_w3.to_wei(3, "gwei"),
              "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
          }
  
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 21000
          assert txn["maxFeePerGas"] == txn_params["maxFeePerGas"]
          assert txn["maxPriorityFeePerGas"] == txn_params["maxPriorityFeePerGas"]
          assert txn["gasPrice"] <= txn["maxFeePerGas"]  # effective gas price
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_default_fees(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
          }
  
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 21000
          assert is_integer(txn["maxPriorityFeePerGas"])
          assert is_integer(txn["maxFeePerGas"])
          assert txn["gasPrice"] <= txn["maxFeePerGas"]  # effective gas price
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_hex_fees(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": hex(250 * 10**9),
              "maxPriorityFeePerGas": hex(2 * 10**9),
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 21000
          assert txn["maxFeePerGas"] == 250 * 10**9
          assert txn["maxPriorityFeePerGas"] == 2 * 10**9
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_no_gas(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "maxFeePerGas": Wei(250 * 10**9),
              "maxPriorityFeePerGas": Wei(2 * 10**9),
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 121000  # 21000 + buffer
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_with_gas_price(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "gasPrice": Wei(1),
              "maxFeePerGas": Wei(250 * 10**9),
              "maxPriorityFeePerGas": Wei(2 * 10**9),
          }
          with pytest.raises(TransactionTypeMismatch):
              await async_w3.eth.send_transaction(txn_params)
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_no_priority_fee(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": Wei(250 * 10**9),
          }
          with pytest.raises(
              InvalidTransaction, match="maxPriorityFeePerGas must be defined"
          ):
              await async_w3.eth.send_transaction(txn_params)
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_no_max_fee(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          maxPriorityFeePerGas = async_w3.to_wei(2, "gwei")
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxPriorityFeePerGas": maxPriorityFeePerGas,
          }
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert is_same_address(txn["from"], cast(ChecksumAddress, txn_params["from"]))
          assert is_same_address(txn["to"], cast(ChecksumAddress, txn_params["to"]))
          assert txn["value"] == 1
          assert txn["gas"] == 21000
  
      @pytest.mark.asyncio
      async def test_eth_send_transaction_max_fee_less_than_tip(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": Wei(1 * 10**9),
              "maxPriorityFeePerGas": Wei(2 * 10**9),
          }
          with pytest.raises(
              InvalidTransaction, match="maxFeePerGas must be >= maxPriorityFeePerGas"
          ):
              await async_w3.eth.send_transaction(txn_params)
  
      @pytest.mark.asyncio
      async def test_validation_middleware_chain_id_mismatch(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          wrong_chain_id = 1234567890
          actual_chain_id = await async_w3.eth.chain_id
  
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
              "maxFeePerGas": async_w3.to_wei(2, "gwei"),
              "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
              "chainId": wrong_chain_id,
          }
          with pytest.raises(
              Web3ValidationError,
              match=f"The transaction declared chain ID {wrong_chain_id}, "
              f"but the connected node is on {actual_chain_id}",
          ):
              await async_w3.eth.send_transaction(txn_params)
  
      @pytest.mark.asyncio
      async def test_ExtraDataToPOAMiddleware(
          self, async_w3: "AsyncWeb3[Any]", request_mocker: type[RequestMocker]
      ) -> None:
          async_w3.middleware_onion.inject(ExtraDataToPOAMiddleware, "poa", layer=0)
          extra_data = f"0x{'ff' * 33}"
  
          async with request_mocker(
              async_w3,
              mock_results={"eth_getBlockByNumber": {"extraData": extra_data}},
          ):
              block = await async_w3.eth.get_block("latest")
  
          assert "extraData" not in block
          assert block["proofOfAuthorityData"] == to_bytes(hexstr=extra_data)
  
          # clean up
          async_w3.middleware_onion.remove("poa")
  
      @pytest.mark.asyncio
      async def test_async_eth_send_raw_transaction(
          self, async_w3: "AsyncWeb3[Any]", keyfile_account_pkey: HexStr
      ) -> None:
          keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
          txn = {
              "chainId": 131277322940537,  # the chainId set for the fixture
              "from": keyfile_account.address,
              "to": keyfile_account.address,
              "value": Wei(0),
              "gas": 21000,
              "nonce": await async_w3.eth.get_transaction_count(
                  keyfile_account.address, "pending"
              ),
              "gasPrice": 10**9,
          }
          signed = keyfile_account.sign_transaction(txn)
          txn_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
          assert txn_hash == HexBytes(signed.hash)
  
      @pytest.mark.asyncio
      async def test_async_sign_and_send_raw_middleware(
          self, async_w3: "AsyncWeb3[Any]", keyfile_account_pkey: HexStr
      ) -> None:
          keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
          txn: TxParams = {
              "from": keyfile_account.address,
              "to": keyfile_account.address,
              "value": Wei(0),
              "gas": 21000,
          }
          async_w3.middleware_onion.inject(
              SignAndSendRawMiddlewareBuilder.build(keyfile_account), "signing", layer=0
          )
          txn_hash = await async_w3.eth.send_transaction(txn)
          assert isinstance(txn_hash, HexBytes)
  
          # clean up
          async_w3.middleware_onion.remove("signing")
  
      @pytest.mark.asyncio
      async def test_async_sign_authorization_send_raw_and_send_set_code_transactions(
          self,
          async_w3: "AsyncWeb3[Any]",
          keyfile_account_pkey: HexStr,
          async_math_contract: "AsyncContract",
      ) -> None:
          keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
  
          chain_id = await async_w3.eth.chain_id
          nonce = await async_w3.eth.get_transaction_count(keyfile_account.address)
  
          auth = {
              "chainId": chain_id,
              "address": async_math_contract.address,
              "nonce": nonce + 1,
          }
          signed_auth = keyfile_account.sign_authorization(auth)
  
          # get current math counter and increase it only in the delegation by n
          math_counter = await async_math_contract.functions.counter().call()
          data = async_math_contract.encode_abi("incrementCounter", [math_counter + 1337])
          txn: TxParams = {
              "chainId": chain_id,
              "to": keyfile_account.address,
              "value": Wei(0),
              "gas": 200_000,
              "nonce": nonce,
              "maxPriorityFeePerGas": Wei(10**9),
              "maxFeePerGas": Wei(10**9),
              "data": data,
              "authorizationList": [signed_auth],
          }
  
          # test eth_sendRawTransaction
          signed = keyfile_account.sign_transaction(txn)
          tx_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
          get_tx = await async_w3.eth.get_transaction(tx_hash)
          await async_w3.eth.wait_for_transaction_receipt(tx_hash)
  
          code = await async_w3.eth.get_code(keyfile_account.address)
          assert code.to_0x_hex() == f"0xef0100{async_math_contract.address[2:].lower()}"
          delegated = async_w3.eth.contract(
              address=keyfile_account.address, abi=async_math_contract.abi
          )
  
          # assert the math counter is increased by 1337 only in delegated acct
          assert await async_math_contract.functions.counter().call() == math_counter
          delegated_call = await delegated.functions.counter().call(
              block_identifier="latest"
          )
          assert delegated_call == math_counter + 1337
  
          assert len(get_tx["authorizationList"]) == 1
          get_auth = get_tx["authorizationList"][0]
          assert get_auth["chainId"] == chain_id
          assert get_auth["address"] == async_math_contract.address
          assert get_auth["nonce"] == nonce + 1
          assert isinstance(get_auth["yParity"], int)
          assert isinstance(get_auth["r"], HexBytes)
          assert isinstance(get_auth["s"], HexBytes)
  
          # reset code
          reset_auth = {
              "chainId": chain_id,
              "address": "0x" + ("00" * 20),
              "nonce": nonce + 3,
          }
          signed_reset_auth = keyfile_account.sign_authorization(reset_auth)
          reset_code_txn = merge(
              txn,
              {
                  "from": keyfile_account.address,
                  "authorizationList": [signed_reset_auth],
                  "nonce": nonce + 2,
              },
          )
  
          # test eth_sendTransaction
          reset_tx_hash = await async_w3.eth.send_transaction(reset_code_txn)
          await async_w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10)
  
          reset_code = await async_w3.eth.get_code(keyfile_account.address)
          assert reset_code == HexBytes("0x")
  
      @pytest.mark.asyncio
      async def test_GasPriceStrategyMiddleware(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
          }
          two_gwei_in_wei = async_w3.to_wei(2, "gwei")
  
          def gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei:
              return two_gwei_in_wei
  
          async_w3.eth.set_gas_price_strategy(gas_price_strategy)
  
          txn_hash = await async_w3.eth.send_transaction(txn_params)
          txn = await async_w3.eth.get_transaction(txn_hash)
  
          assert txn["gasPrice"] == two_gwei_in_wei
          async_w3.eth.set_gas_price_strategy(None)  # reset strategy
  
      @pytest.mark.asyncio
      async def test_gas_price_strategy_middleware_hex_value(
          self,
          async_w3: "AsyncWeb3[Any]",
          async_keyfile_account_address_dual_type: ChecksumAddress,
      ) -> None:
          txn_params: TxParams = {
              "from": async_keyfile_account_address_dual_type,
              "to": async_keyfile_account_address_dual_type,
              "value": Wei(1),
              "gas": 21000,
          }
          two_gwei_in_wei = async_w3.to_wei(2, "gwei")
  
  