Skip to content

Commit

Permalink
Merge pull request #1516 from BoostryJP/upgrade-web3beta
Browse files Browse the repository at this point in the history
Upgrade web3py to v7
  • Loading branch information
YoshihitoAso authored Jun 2, 2024
2 parents 8e37bf9 + 3d09eb1 commit 399980a
Show file tree
Hide file tree
Showing 83 changed files with 533 additions and 796 deletions.
40 changes: 24 additions & 16 deletions app/api/routers/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def send_raw_transaction(
for raw_tx_hex in raw_tx_hex_list:
try:
raw_tx = decode(HexBytes(raw_tx_hex))
to_contract_address = to_checksum_address(raw_tx[3].hex())
to_contract_address = to_checksum_address(raw_tx[3].to_0x_hex())
except Exception as err:
LOG.warning(f"RLP decoding failed: {err}")
continue
Expand Down Expand Up @@ -205,7 +205,7 @@ async def send_raw_transaction(
# Get the contract address of the execution target.
try:
raw_tx = decode(HexBytes(raw_tx_hex))
to_contract_address = to_checksum_address(raw_tx[3].hex())
to_contract_address = to_checksum_address(raw_tx[3].to_0x_hex())
LOG.debug(raw_tx)
except Exception as err:
result.append({"id": i + 1, "status": 0, "transaction_hash": None})
Expand Down Expand Up @@ -266,13 +266,13 @@ async def send_raw_transaction(
)
if tx["status"] == 0:
# inspect reason of transaction fail
err_msg = await inspect_tx_failure(tx_hash.hex())
err_msg = await inspect_tx_failure(tx_hash)
code, message = error_code_msg(err_msg)
result.append(
{
"id": i + 1,
"status": 0,
"transaction_hash": tx_hash.hex(),
"transaction_hash": tx_hash.to_0x_hex(),
"error_code": code,
"error_msg": message,
}
Expand All @@ -290,28 +290,36 @@ async def send_raw_transaction(
from_address = Account.recover_transaction(raw_tx_hex)
except Exception as err:
result.append(
{"id": i + 1, "status": 0, "transaction_hash": tx_hash.hex()}
{"id": i + 1, "status": 0, "transaction_hash": tx_hash.to_0x_hex()}
)
LOG.error(f"get sender address from signed transaction failed: {err}")
continue
nonce = int("0x0" if raw_tx[0].hex() == "0x" else raw_tx[0].hex(), 16)
nonce = int(
"0x0" if raw_tx[0].to_0x_hex() == "0x" else raw_tx[0].to_0x_hex(), 16
)
txpool_inspect = await async_web3.geth.txpool.inspect()
if from_address in txpool_inspect.queued:
if str(nonce) in txpool_inspect.queued[from_address]:
if from_address in txpool_inspect["queued"]:
if str(nonce) in txpool_inspect["queued"][from_address]:
status = 0 # execution failure

result.append(
{"id": i + 1, "status": status, "transaction_hash": tx_hash.hex()}
{"id": i + 1, "status": status, "transaction_hash": tx_hash.to_0x_hex()}
)
LOG.warning(f"Transaction receipt timeout: {time_exhausted_err}")
continue
except Exception as err:
result.append({"id": i + 1, "status": 0, "transaction_hash": tx_hash.hex()})
result.append(
{"id": i + 1, "status": 0, "transaction_hash": tx_hash.to_0x_hex()}
)
LOG.error(f"Transaction failed: {err}")
continue

result.append(
{"id": i + 1, "status": tx["status"], "transaction_hash": tx_hash.hex()}
{
"id": i + 1,
"status": tx["status"],
"transaction_hash": tx_hash.to_0x_hex(),
}
)

return json_response({**SuccessResponse.default(), "data": result})
Expand Down Expand Up @@ -346,7 +354,7 @@ async def send_raw_transaction_no_wait(
for raw_tx_hex in raw_tx_hex_list:
try:
raw_tx = decode(HexBytes(raw_tx_hex))
to_contract_address = to_checksum_address(raw_tx[3].hex())
to_contract_address = to_checksum_address(raw_tx[3].to_0x_hex())
except Exception as err:
LOG.warning(f"RLP decoding failed: {err}")
continue
Expand Down Expand Up @@ -389,7 +397,7 @@ async def send_raw_transaction_no_wait(
# Get the contract address of the execution target.
try:
raw_tx = decode(HexBytes(raw_tx_hex))
to_contract_address = to_checksum_address(raw_tx[3].hex())
to_contract_address = to_checksum_address(raw_tx[3].to_0x_hex())
LOG.debug(raw_tx)
except Exception as err:
result.append({"id": i + 1, "status": 0})
Expand Down Expand Up @@ -442,7 +450,7 @@ async def send_raw_transaction_no_wait(
continue

result.append(
{"id": i + 1, "status": 1, "transaction_hash": transaction_hash.hex()}
{"id": i + 1, "status": 1, "transaction_hash": transaction_hash.to_0x_hex()}
)

return json_response({**SuccessResponse.default(), "data": result})
Expand Down Expand Up @@ -489,7 +497,7 @@ async def wait_for_transaction_receipt(
return json_response({**SuccessResponse.default(), "data": result})


async def inspect_tx_failure(tx_hash: str) -> str:
async def inspect_tx_failure(tx_hash: HexBytes) -> str:
tx = await async_web3.eth.get_transaction(tx_hash)

# build a new transaction to replay:
Expand All @@ -502,7 +510,7 @@ async def inspect_tx_failure(tx_hash: str) -> str:

# replay the transaction locally:
try:
await async_web3.eth.call(replay_tx, tx.blockNumber - 1)
await async_web3.eth.call(replay_tx, tx["blockNumber"] - 1)
except ContractLogicError as e:
if len(e.args) == 0:
raise e
Expand Down
30 changes: 15 additions & 15 deletions app/api/routers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ async def list_all_e2e_messaging_event_logs(
contract_event = getattr(contract.events, attr)
try:
events = await contract_event.get_logs(
fromBlock=request_query.from_block,
toBlock=request_query.to_block,
from_block=request_query.from_block,
to_block=request_query.to_block,
argument_filters=argument_filters_dict,
)
except Web3ValidationError:
Expand All @@ -120,7 +120,7 @@ async def list_all_e2e_messaging_event_logs(
{
"event": event["event"],
"args": dict(event["args"]),
"transaction_hash": event["transactionHash"].hex(),
"transaction_hash": event["transactionHash"].to_0x_hex(),
"block_number": block_number,
"block_timestamp": block_timestamp,
"log_index": event["logIndex"],
Expand Down Expand Up @@ -190,8 +190,8 @@ async def list_all_ibet_escrow_event_logs(
contract_event = getattr(contract.events, attr)
try:
events = await contract_event.get_logs(
fromBlock=request_query.from_block,
toBlock=request_query.to_block,
from_block=request_query.from_block,
to_block=request_query.to_block,
argument_filters=argument_filters_dict,
)
except Web3ValidationError:
Expand All @@ -205,7 +205,7 @@ async def list_all_ibet_escrow_event_logs(
{
"event": event["event"],
"args": dict(event["args"]),
"transaction_hash": event["transactionHash"].hex(),
"transaction_hash": event["transactionHash"].to_0x_hex(),
"block_number": block_number,
"block_timestamp": block_timestamp,
"log_index": event["logIndex"],
Expand Down Expand Up @@ -285,8 +285,8 @@ async def list_all_ibet_security_token_escrow_event_logs(
contract_event = getattr(contract.events, attr)
try:
events = await contract_event.get_logs(
fromBlock=request_query.from_block,
toBlock=request_query.to_block,
from_block=request_query.from_block,
to_block=request_query.to_block,
argument_filters=argument_filters_dict,
)
except Web3ValidationError:
Expand All @@ -300,7 +300,7 @@ async def list_all_ibet_security_token_escrow_event_logs(
{
"event": event["event"],
"args": dict(event["args"]),
"transaction_hash": event["transactionHash"].hex(),
"transaction_hash": event["transactionHash"].to_0x_hex(),
"block_number": block_number,
"block_timestamp": block_timestamp,
"log_index": event["logIndex"],
Expand Down Expand Up @@ -381,8 +381,8 @@ async def list_all_ibet_security_token_escrow_event_logs(
contract_event = getattr(contract.events, attr)
try:
events = await contract_event.get_logs(
fromBlock=request_query.from_block,
toBlock=request_query.to_block,
from_block=request_query.from_block,
to_block=request_query.to_block,
argument_filters=argument_filters_dict,
)
except Web3ValidationError:
Expand All @@ -396,7 +396,7 @@ async def list_all_ibet_security_token_escrow_event_logs(
{
"event": event["event"],
"args": dict(event["args"]),
"transaction_hash": event["transactionHash"].hex(),
"transaction_hash": event["transactionHash"].to_0x_hex(),
"block_number": block_number,
"block_timestamp": block_timestamp,
"log_index": event["logIndex"],
Expand Down Expand Up @@ -455,8 +455,8 @@ async def list_all_ibet_security_token_interface_event_logs(
contract_event = getattr(contract.events, attr)
try:
events = await contract_event.get_logs(
fromBlock=request_query.from_block,
toBlock=request_query.to_block,
from_block=request_query.from_block,
to_block=request_query.to_block,
argument_filters=argument_filters_dict,
)
except Web3ValidationError:
Expand All @@ -470,7 +470,7 @@ async def list_all_ibet_security_token_interface_event_logs(
{
"event": event["event"],
"args": dict(event["args"]),
"transaction_hash": event["transactionHash"].hex(),
"transaction_hash": event["transactionHash"].to_0x_hex(),
"block_number": block_number,
"block_timestamp": block_timestamp,
"log_index": event["logIndex"],
Expand Down
6 changes: 3 additions & 3 deletions app/utils/web3_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from web3 import AsyncHTTPProvider, AsyncWeb3, HTTPProvider, Web3
from web3.eth import AsyncEth
from web3.geth import AsyncGeth
from web3.middleware import async_geth_poa_middleware, geth_poa_middleware
from web3.middleware import ExtraDataToPOAMiddleware
from web3.net import AsyncNet
from web3.types import RPCEndpoint, RPCResponse

Expand Down Expand Up @@ -78,7 +78,7 @@ def _get_web3(request_timeout: int) -> Web3:
web3 = Web3(
FailOverHTTPProvider(request_kwargs={"timeout": request_timeout})
)
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
web3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)
thread_local.web3 = web3

return web3
Expand Down Expand Up @@ -116,7 +116,7 @@ def _get_web3(request_timeout: int) -> AsyncWeb3:
async_web3 = AsyncWeb3(
AsyncFailOverHTTPProvider(request_kwargs={"timeout": request_timeout})
)
async_web3.middleware_onion.inject(async_geth_poa_middleware, layer=0)
async_web3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)
thread_local.async_web3 = async_web3

return async_web3
Expand Down
30 changes: 16 additions & 14 deletions batch/indexer_Block_Tx_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,34 @@ async def process(self):
# Synchronize block data
block_model = IDXBlockData()
block_model.number = block_data.get("number")
block_model.parent_hash = block_data.get("parentHash").hex()
block_model.sha3_uncles = block_data.get("sha3Uncles").hex()
block_model.parent_hash = block_data.get("parentHash").to_0x_hex()
block_model.sha3_uncles = block_data.get("sha3Uncles").to_0x_hex()
block_model.miner = block_data.get("miner")
block_model.state_root = block_data.get("stateRoot").hex()
block_model.transactions_root = block_data.get("transactionsRoot").hex()
block_model.receipts_root = block_data.get("receiptsRoot").hex()
block_model.logs_bloom = block_data.get("logsBloom").hex()
block_model.state_root = block_data.get("stateRoot").to_0x_hex()
block_model.transactions_root = block_data.get(
"transactionsRoot"
).to_0x_hex()
block_model.receipts_root = block_data.get("receiptsRoot").to_0x_hex()
block_model.logs_bloom = block_data.get("logsBloom").to_0x_hex()
block_model.difficulty = block_data.get("difficulty")
block_model.gas_limit = block_data.get("gasLimit")
block_model.gas_used = block_data.get("gasUsed")
block_model.timestamp = block_data.get("timestamp")
block_model.proof_of_authority_data = block_data.get(
"proofOfAuthorityData"
).hex()
block_model.mix_hash = block_data.get("mixHash").hex()
block_model.nonce = block_data.get("nonce").hex()
block_model.hash = block_data.get("hash").hex()
).to_0x_hex()
block_model.mix_hash = block_data.get("mixHash").to_0x_hex()
block_model.nonce = block_data.get("nonce").to_0x_hex()
block_model.hash = block_data.get("hash").to_0x_hex()
block_model.size = block_data.get("size")

transactions: Sequence[TxData] = block_data.get("transactions")
transaction_hash_list = []
for transaction in transactions:
# Synchronize tx data
tx_model = IDXTxData()
tx_model.hash = transaction.get("hash").hex()
tx_model.block_hash = transaction.get("blockHash").hex()
tx_model.hash = transaction.get("hash").to_0x_hex()
tx_model.block_hash = transaction.get("blockHash").to_0x_hex()
tx_model.block_number = transaction.get("blockNumber")
tx_model.transaction_index = transaction.get("transactionIndex")
tx_model.from_address = to_checksum_address(transaction.get("from"))
Expand All @@ -100,14 +102,14 @@ async def process(self):
if transaction.get("to")
else None
)
tx_model.input = transaction.get("input").hex()
tx_model.input = transaction.get("input").to_0x_hex()
tx_model.gas = transaction.get("gas")
tx_model.gas_price = transaction.get("gasPrice")
tx_model.value = transaction.get("value")
tx_model.nonce = transaction.get("nonce")
local_session.add(tx_model)

transaction_hash_list.append(transaction.get("hash").hex())
transaction_hash_list.append(transaction.get("hash").to_0x_hex())

block_model.transactions = transaction_hash_list
local_session.add(block_model)
Expand Down
4 changes: 2 additions & 2 deletions batch/indexer_Consume_Coupon.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ async def __sync_consume(
for token in self.token_list:
try:
events = await token.events.Consume.get_logs(
fromBlock=block_from, toBlock=block_to
from_block=block_from, to_block=block_to
)
except ABIEventFunctionNotFound:
events = []
try:
for event in events:
args = event["args"]
transaction_hash = event["transactionHash"].hex()
transaction_hash = event["transactionHash"].to_0x_hex()
block_timestamp = datetime.fromtimestamp(
(await async_web3.eth.get_block(event["blockNumber"]))[
"timestamp"
Expand Down
Loading

0 comments on commit 399980a

Please sign in to comment.