Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RPC calls in Jupyter/Colab #124

Merged
merged 36 commits into from
Feb 7, 2024
Merged

Conversation

DanielSchiavini
Copy link
Collaborator

@DanielSchiavini DanielSchiavini commented Jan 24, 2024

Fixes #119
Depends on #132

What I did

Extended the interface with the browser to support RPC calls.

How I did it

It includes two new classes on the python side:

  • BrowserRpc calls the browser when a RPC call is made
  • boa.set_browser() combines BrowserRpc and BrowserSigner together to allow the environment to be used in boa
  • boa.set_network(url) works as an alternative to boa.set_env(NetworkEnv(url))

The JavaScript uses this method from ethers.js.

Tests are included, but unfortunately I had to mock all jupyter and tornado dependencies - they aren't installed in boa normally.

How to verify it

Install this branch and then run in Jupyter or Google Colab:

import boa
boa.set_browser()
  • boa.env.eoa should return your wallet address.
  • boa.env.get_gas_price() returns the current gas price.
  • Deploying and calling contracts should still work

image

Description for the changelog

  • Add new boa.set_browser() interface to interact with the browser wallet RPC in JupyterLab

Cute Animal Picture

image

@DanielSchiavini DanielSchiavini marked this pull request as ready for review January 26, 2024 11:51
boa/rpc.py Outdated
@@ -54,7 +55,24 @@ def from_json(cls, data):
return cls(message=data["message"], code=data["code"])


class EthereumRPC:
class RPC(ABC):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have some bad memories about ABC / abstractmethod performance. can we make it simpler?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove the abc or remove the base class completely for duck typing

DanielSchiavini added a commit to DanielSchiavini/titanoboa that referenced this pull request Feb 5, 2024
@DanielSchiavini DanielSchiavini changed the base branch from master to 119/rpc-refactor February 5, 2024 15:59
@DanielSchiavini DanielSchiavini changed the base branch from 119/rpc-refactor to master February 7, 2024 14:19
Create a BrowserSigner instance.
:param address: The account address. If not provided, it will be requested from the browser.
"""
if address:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if address:
if address is not None:

if method == "eth_getTransactionReceipt":
# we do the polling in the browser to avoid too many callbacks
# each callback generates currently 10px empty space in the frontend
timeout_ms = CALLBACK_TOKEN_TIMEOUT.total_seconds() * 1000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use a separate constant for this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g., WAIT_TRANSACTION_RECEIPT_TIMEOUT

console.log(`Callback ${token} => ${responseText}`);
const body = stringify(await parsePromise(func(...args)));
// console.log(`Boa: ${func.name}(${args.map(a => JSON.stringify(a)).join(',')}) = ${body};`);
return colab ? body : callbackAPI(token, body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we shouldn't return anything, since the return values of the two branches mean different things

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm except slight refactoring of handleCallback

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. thank you!

@charles-cooper charles-cooper merged commit b2b8c78 into master Feb 7, 2024
4 of 10 checks passed
@DanielSchiavini DanielSchiavini deleted the 119/browser-rpc branch February 7, 2024 15:26
@DanielSchiavini DanielSchiavini restored the 119/browser-rpc branch February 7, 2024 15:26
@DanielSchiavini DanielSchiavini deleted the 119/browser-rpc branch February 7, 2024 15:27
@DanielSchiavini DanielSchiavini restored the 119/browser-rpc branch February 7, 2024 15:27
@DanielSchiavini DanielSchiavini deleted the 119/browser-rpc branch February 7, 2024 15:30
@DanielSchiavini DanielSchiavini self-assigned this Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement RPC via browser wallet
2 participants