Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 27, 2024
1 parent 71e1202 commit 6215d72
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
45 changes: 25 additions & 20 deletions boa/integrations/jupyter/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,8 @@ def __init__(self, address=None, rpc=None):
if rpc is None:
rpc = BrowserRPC() # note: the browser window is global anyway
self._rpc = rpc
address = getattr(address, "address", address)
accounts = self._rpc.fetch("eth_requestAccounts", [], ADDRESS_TIMEOUT_MESSAGE)

if address is None and len(accounts) > 0:
address = accounts[0]

if address not in accounts:
raise ValueError(f"Address {address} is not available in the browser")

self.address = Address(address)
self._given_address = address
self.address = address

def send_transaction(self, tx_data: dict) -> dict:
"""
Expand All @@ -143,6 +135,19 @@ def sign_typed_data(self, full_message: dict[str, Any]) -> str:
TRANSACTION_TIMEOUT_MESSAGE,
)

def update(self):
address = getattr(self._given_address, "address", self._given_address)
accounts = self._rpc.fetch("eth_requestAccounts", [], ADDRESS_TIMEOUT_MESSAGE)

if address is None and len(accounts) > 0:
address = accounts[0]

if address not in accounts:
raise ValueError(f"Address {address} is not available in the browser")

self.address = Address(address)
return self.address


class BrowserEnv(NetworkEnv):
"""
Expand All @@ -154,19 +159,19 @@ class BrowserEnv(NetworkEnv):
def __init__(self, address=None, **kwargs):
super().__init__(self._rpc, **kwargs)
self.signer = BrowserSigner(address, self._rpc)
self._update_signer()

def _get_sender(self, sender=None) -> Address:
accounts = self._rpc.fetch("eth_requestAccounts", [], ADDRESS_TIMEOUT_MESSAGE)

if sender is None and len(accounts) > 0:
sender = accounts[0]
def _update_signer(self):
self.signer.update()
self.add_account(self.signer, force_eoa=True)

if sender not in accounts:
raise ValueError(f"Address {sender} is not available in the browser")
def execute_code(self, *args, **kwargs):
self._update_signer()
return super().execute_code(*args, **kwargs)

self.signer.address = Address(sender)
self._accounts[sender] = self.signer
return self.signer.address
def deploy(self, *args, **kwargs):
self._update_signer()
return super().deploy(*args, **kwargs)

def set_chain_id(self, chain_id: int | str):
self._rpc.fetch(
Expand Down
4 changes: 2 additions & 2 deletions tests/unitary/jupyter/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def test_browser_signer_colab(colab_eval_mock, mocked_token, display_mock):
def test_browser_load_signer(token, display_mock, mock_callback, account, mock_fork):
mock_callback("eth_requestAccounts", [account.address])
boa.set_browser_env()
assert boa.env._get_sender() == account.address
assert type(boa.env._accounts[account.address]).__name__ == BrowserSigner.__name__
assert boa.env.eoa == account.address
assert type(boa.env._accounts[boa.env.eoa]).__name__ == BrowserSigner.__name__


def test_browser_timeout():
Expand Down

0 comments on commit 6215d72

Please sign in to comment.