Ethereum JSON RPC client.
| 1016 | |
| 1017 | |
| 1018 | class JSONRPCClient: |
| 1019 | """Ethereum JSON RPC client.""" |
| 1020 | |
| 1021 | def __init__( |
| 1022 | self, |
| 1023 | web3: Web3, |
| 1024 | privkey: PrivateKey, |
| 1025 | gas_price_strategy: Callable = rpc_gas_price_strategy, |
| 1026 | block_num_confirmations: int = 0, |
| 1027 | ) -> None: |
| 1028 | if len(privkey) != 32: |
| 1029 | raise ValueError("Invalid private key") |
| 1030 | |
| 1031 | if block_num_confirmations < 0: |
| 1032 | raise ValueError("Number of confirmations has to be positive") |
| 1033 | |
| 1034 | monkey_patch_web3(web3, gas_price_strategy) |
| 1035 | |
| 1036 | version = web3.clientVersion |
| 1037 | supported, eth_node, _ = is_supported_client(version) |
| 1038 | |
| 1039 | if eth_node is None or supported is VersionSupport.UNSUPPORTED: |
| 1040 | raise EthNodeInterfaceError(f'Unsupported Ethereum client "{version}"') |
| 1041 | if supported is VersionSupport.WARN: |
| 1042 | log.warning(f'Unsupported Ethereum client version "{version}"') |
| 1043 | |
| 1044 | address = privatekey_to_address(privkey) |
| 1045 | available_nonce = discover_next_available_nonce(web3, eth_node, address) |
| 1046 | |
| 1047 | self.eth_node = eth_node |
| 1048 | self.privkey = privkey |
| 1049 | self.address = address |
| 1050 | self.web3 = web3 |
| 1051 | self.default_block_num_confirmations = block_num_confirmations |
| 1052 | |
| 1053 | # Ask for the chain id only once and store it here |
| 1054 | self.chain_id = ChainID(self.web3.eth.chain_id) |
| 1055 | |
| 1056 | self._available_nonce = available_nonce |
| 1057 | self._nonce_lock = Semaphore() |
| 1058 | |
| 1059 | log.debug( |
| 1060 | "JSONRPCClient created", |
| 1061 | node=to_checksum_address(self.address), |
| 1062 | available_nonce=available_nonce, |
| 1063 | client=version, |
| 1064 | ) |
| 1065 | |
| 1066 | def __repr__(self) -> str: |
| 1067 | return ( |
| 1068 | f"<JSONRPCClient " |
| 1069 | f"node:{to_checksum_address(self.address)} nonce:{self._available_nonce}" |
| 1070 | f">" |
| 1071 | ) |
| 1072 | |
| 1073 | def block_number(self) -> BlockNumber: |
| 1074 | """Return the most recent block.""" |
| 1075 | return self.web3.eth.block_number |
no outgoing calls