Skip to content

Commit

Permalink
Use sign middleware from web3py
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <[email protected]>
  • Loading branch information
cyc60 committed Sep 23, 2024
1 parent 8a63840 commit 695f367
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 488 deletions.
986 changes: 505 additions & 481 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sentry-sdk = "==1.32.0"
py-ecc = "==6.0.0"
gql = {extras = ["aiohttp"], version = "==3.5.0"}
multiproof = { git = "https://github.com/stakewise/multiproof.git", rev = "v0.1.8" }
sw-utils = {git = "https://github.com/stakewise/sw-utils.git", rev = "v0.6.22"}
sw-utils = {git = "https://github.com/stakewise/sw-utils.git", rev = "v0.6.24"}
staking-deposit = { git = "https://github.com/ethereum/staking-deposit-cli.git", rev = "v2.4.0" }
pycryptodomex = "3.19.1"
click = "==8.1.7"
Expand Down
4 changes: 3 additions & 1 deletion src/commands/recover.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from eth_utils import add_0x_prefix
from sw_utils.consensus import EXITED_STATUSES, ValidatorStatus

from src.common.clients import consensus_client, execution_client
from src.common.clients import consensus_client, execution_client, setup_clients
from src.common.contracts import v2_pool_contract, vault_contract
from src.common.credentials import CredentialManager
from src.common.execution import SECONDS_PER_MONTH
Expand Down Expand Up @@ -149,6 +149,8 @@ async def main(
config: VaultConfig,
) -> None:
setup_logging()
await setup_clients()

validators = await _fetch_registered_validators()
if not validators:
raise click.ClickException('No registered validators')
Expand Down
3 changes: 3 additions & 0 deletions src/commands/remote_signer_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from aiohttp import ClientTimeout
from eth_typing import HexAddress

from src.common.clients import setup_clients
from src.common.contracts import vault_contract
from src.common.logging import LOG_LEVELS, setup_logging
from src.common.startup_check import wait_for_execution_node
Expand Down Expand Up @@ -138,6 +139,8 @@ def remote_signer_setup(

async def main() -> None:
setup_logging()
await setup_clients()

keystore_files = LocalKeystore.list_keystore_files()
if len(keystore_files) == 0:
raise click.ClickException('Keystores not found.')
Expand Down
3 changes: 3 additions & 0 deletions src/commands/start_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import src
from src.common.checks import wait_execution_catch_up_consensus
from src.common.clients import setup_clients
from src.common.consensus import get_chain_finalized_head
from src.common.execution import WalletTask, update_oracles_cache
from src.common.logging import setup_logging
Expand All @@ -29,6 +30,8 @@
async def start_base() -> None:
setup_logging()
setup_sentry()
await setup_clients()

log_start()

if not settings.skip_startup_checks:
Expand Down
16 changes: 11 additions & 5 deletions src/common/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from sw_utils import (
ExtendedAsyncBeacon,
IpfsFetchClient,
construct_async_sign_and_send_raw_middleware,
get_consensus_client,
get_execution_client,
)
from web3 import AsyncWeb3
from web3.middleware.signing import async_construct_sign_and_send_raw_middleware

from src.common.wallet import hot_wallet
from src.config.settings import settings
Expand All @@ -28,8 +28,9 @@ def create_db_dir(self) -> None:


class ExecutionClient:
@cached_property
def client(self) -> AsyncWeb3:
client: AsyncWeb3

async def setup(self) -> None:
w3 = get_execution_client(
settings.execution_endpoints,
timeout=settings.execution_timeout,
Expand All @@ -40,11 +41,12 @@ def client(self) -> AsyncWeb3:
# For read-only queries account may be omitted.
if hot_wallet.can_load():
w3.middleware_onion.add(
construct_async_sign_and_send_raw_middleware(hot_wallet.account)
await async_construct_sign_and_send_raw_middleware(hot_wallet.account)
)
w3.eth.default_account = hot_wallet.address

return w3
self.client = w3
return None

def __getattr__(self, item): # type: ignore
return getattr(self.client, item)
Expand Down Expand Up @@ -83,3 +85,7 @@ async def fetch_json(self, ipfs_hash: str) -> dict | list:
execution_client = cast(AsyncWeb3, ExecutionClient())
consensus_client = cast(ExtendedAsyncBeacon, ConsensusClient())
ipfs_fetch_client = IpfsLazyFetchClient()


async def setup_clients() -> None:
await execution_client.setup() # type: ignore

0 comments on commit 695f367

Please sign in to comment.