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

feat: 764 add new rpc endpoints metamask #765

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

epsjunior
Copy link
Contributor

@epsjunior epsjunior commented Dec 19, 2024

Fixes #764

What

  • Replaced is_valid_address with is_address for Ethereum address validation in the accounts_manager.
  • Added methods to fetch block details, including get_block_by_number and get_block_by_hash, to the RPC endpoints.
  • Introduced new MetaMask integration functionality in the frontend:
    • Added support for fetching and selecting MetaMask accounts in AccountSelect.vue.
    • Updated useGenLayer and accounts.ts to handle MetaMask account logic and wallet selection.
  • Improved block transaction processing in transactions_processor.py:
    • Added methods to fetch block details and transactions based on block numbers.
    • Ensured compatibility with the Ethereum JSON-RPC standard.
  • Extended unit tests to cover MetaMask functionality and new backend methods.
  • Updated the global.d.ts file to define the ethereum object for TypeScript support.

Why

  • To enhance the user experience by integrating MetaMask for easier account management.
  • To align with Ethereum JSON-RPC standards for better compatibility with third-party tools.
  • To provide richer block and transaction details for the studio.

Testing Done

  • Verified MetaMask account fetching and wallet selection functionality in the frontend.
  • Ran unit tests for the new fetchMetaMaskAccount method.
  • Confirmed all existing tests pass successfully.
  • Manually tested the entire flow, including block and transaction data retrieval.

Decisions Made

  • Decided to use is_address over is_valid_address for Ethereum validation for improved consistency across the codebase.
  • Chose to fetch the block hash dynamically for JSON-RPC endpoints rather than relying on placeholders.
  • Opted for a backward-compatible approach when introducing MetaMask wallet support.

Checks

Reviewing Tips

  • Pay special attention to the changes in transactions_processor.py and the new MetaMask logic in the frontend.
  • Verify the integration of MetaMask functionality aligns with the overall architecture.
  • Review the JSON-RPC endpoint updates for compliance with Ethereum standards.

User-Facing Release Notes

  • Added MetaMask integration for easy wallet connection and account selection.
  • Enhanced block and transaction details retrieval via new JSON-RPC methods.

@epsjunior epsjunior linked an issue Dec 19, 2024 that may be closed by this pull request
@epsjunior epsjunior requested a review from cristiam86 December 19, 2024 06:29
Copy link

codecov bot commented Dec 21, 2024

Codecov Report

Attention: Patch coverage is 48.38710% with 32 lines in your changes missing coverage. Please review.

Project coverage is 18.38%. Comparing base (3ff73e5) to head (076dc54).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...rontend/src/components/Simulator/AccountSelect.vue 0.00% 19 Missing ⚠️
frontend/src/hooks/useGenlayer.ts 0.00% 8 Missing ⚠️
frontend/src/stores/accounts.ts 90.90% 3 Missing ⚠️
...nd/src/components/Simulator/ContractMethodItem.vue 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #765      +/-   ##
==========================================
+ Coverage   18.21%   18.38%   +0.17%     
==========================================
  Files         129      129              
  Lines       10076    10118      +42     
  Branches      302      306       +4     
==========================================
+ Hits         1835     1860      +25     
- Misses       8156     8173      +17     
  Partials       85       85              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@epsjunior epsjunior changed the title WIP: 764 add new rpc endpoints metamask Feat: 764 add new rpc endpoints metamask Dec 21, 2024
@epsjunior epsjunior changed the title Feat: 764 add new rpc endpoints metamask feat: 764 add new rpc endpoints metamask Dec 21, 2024
@epsjunior epsjunior self-assigned this Dec 23, 2024
backend/protocol_rpc/endpoints.py Show resolved Hide resolved
backend/protocol_rpc/endpoints.py Outdated Show resolved Hide resolved
Comment on lines +552 to +567
def get_block_by_number(
transactions_processor: TransactionsProcessor, block_number: str, full_tx: bool
) -> dict | None:
try:
block_number_int = int(block_number, 16)
except ValueError:
raise JSONRPCError(f"Invalid block number format: {block_number}")

block_details = transactions_processor.get_transactions_for_block(
block_number_int, include_full_tx=full_tx
)

if not block_details:
raise JSONRPCError(f"Block not found for number: {block_number}")

return block_details
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the signature dict | None? I don't see anywhere where it could return None

backend/protocol_rpc/endpoints.py Show resolved Hide resolved

receipt = {
"transactionHash": transaction_hash,
"transactionIndex": hex(0),
Copy link
Contributor

Choose a reason for hiding this comment

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

why 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because we just have one transaction per "block"

receipt = {
"transactionHash": transaction_hash,
"transactionIndex": hex(0),
"blockHash": transaction_hash,
Copy link
Contributor

Choose a reason for hiding this comment

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

is a block the same as a transaction for us? a small explanation in a comment would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have a block table or block hash rn, so yes, at this moment I'm considering transaction = block

transaction_hash: str,
) -> dict | None:

transaction = transactions_processor.get_transaction_by_hash(transaction_hash)
Copy link
Contributor

Choose a reason for hiding this comment

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

we could probably take advantage of the Transaction type if we returned some dataclass instead of a dict. This way we wouldn't need to do so many transaction.get below

else None
),
"logs": transaction.get("logs", []),
"logsBloom": "0x" + "00" * 256,
Copy link
Contributor

Choose a reason for hiding this comment

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

will we fill this with something at some point?

) -> dict:
transactions = (
self.session.query(Transactions)
.filter(Transactions.nonce == block_number)
Copy link
Contributor

Choose a reason for hiding this comment

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

we should explain somehow that block_number === nonce

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed from nonce to timestamp here: 20b83f0

timestamp is the only unique key on transactions table

Copy link

sonarqubecloud bot commented Jan 9, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
2.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

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.

Add new RPC endpoints (metamask)
3 participants