-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
0878ebf
eba59c4
52a01a2
609624f
61f5ea3
b737596
1749490
31e30a9
5f44f64
b8a5e88
a9e8215
71cea26
076dc54
acc22b1
581c086
20b83f0
1849613
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,7 +368,7 @@ def get_balance( | |
|
||
|
||
def get_transaction_count( | ||
transactions_processor: TransactionsProcessor, address: str | ||
transactions_processor: TransactionsProcessor, address: str, block: str | ||
) -> int: | ||
return transactions_processor.get_transaction_count(address) | ||
|
||
|
@@ -390,6 +390,11 @@ async def call( | |
from_address = params["from"] if "from" in params else None | ||
data = params["data"] | ||
|
||
if from_address is None: | ||
return base64.b64encode(b"\x00' * 31 + b'\x01").decode( | ||
"ascii" | ||
) # Return '1' as a uint256 | ||
AgustinRamiroDiaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if from_address and not accounts_manager.is_valid_address(from_address): | ||
raise InvalidAddressError(from_address) | ||
|
||
|
@@ -531,6 +536,113 @@ def set_finality_window_time(consensus: ConsensusAlgorithm, time: int) -> None: | |
consensus.set_finality_window_time(time) | ||
|
||
|
||
def get_chain_id() -> str: | ||
return hex(61_999) | ||
|
||
|
||
def get_net_version() -> str: | ||
return "61999" | ||
AgustinRamiroDiaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_block_number(transactions_processor: TransactionsProcessor) -> str: | ||
transaction_count = transactions_processor.get_highest_timestamp() | ||
return hex(transaction_count) | ||
|
||
|
||
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 | ||
Comment on lines
+552
to
+567
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the signature |
||
|
||
|
||
def get_gas_price() -> str: | ||
gas_price_in_wei = 20 * 10**9 | ||
return hex(gas_price_in_wei) | ||
AgustinRamiroDiaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_transaction_receipt( | ||
transactions_processor: TransactionsProcessor, | ||
transaction_hash: str, | ||
) -> dict | None: | ||
|
||
transaction = transactions_processor.get_transaction_by_hash(transaction_hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could probably take advantage of the |
||
|
||
if not transaction: | ||
return None | ||
|
||
receipt = { | ||
"transactionHash": transaction_hash, | ||
"transactionIndex": hex(0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we just have one transaction per "block" |
||
"blockHash": transaction_hash, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"blockNumber": hex(transaction.get("block_number", 0)), | ||
"from": transaction.get("from_address"), | ||
"to": transaction.get("to_address") if transaction.get("to_address") else None, | ||
"cumulativeGasUsed": hex(transaction.get("gas_used", 21000)), | ||
"gasUsed": hex(transaction.get("gas_used", 21000)), | ||
"contractAddress": ( | ||
transaction.get("contract_address") | ||
if transaction.get("contract_address") | ||
else None | ||
), | ||
"logs": transaction.get("logs", []), | ||
"logsBloom": "0x" + "00" * 256, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will we fill this with something at some point? |
||
"status": hex(1 if transaction.get("status", True) else 0), | ||
} | ||
|
||
return receipt | ||
|
||
|
||
def get_block_by_hash( | ||
transactions_processor: TransactionsProcessor, | ||
transaction_hash: str, | ||
full_tx: bool = False, | ||
) -> dict | None: | ||
|
||
transaction = transactions_processor.get_transaction_by_hash(transaction_hash) | ||
|
||
if not transaction: | ||
return None | ||
|
||
block_details = { | ||
"hash": transaction_hash, | ||
"parentHash": "0x" + "00" * 32, | ||
"number": hex(transaction.get("block_number", 0)), | ||
"timestamp": hex(transaction.get("timestamp", 0)), | ||
"nonce": "0x" + "00" * 8, | ||
"transactionsRoot": "0x" + "00" * 32, | ||
"stateRoot": "0x" + "00" * 32, | ||
"receiptsRoot": "0x" + "00" * 32, | ||
"miner": "0x" + "00" * 20, | ||
"difficulty": "0x1", | ||
"totalDifficulty": "0x1", | ||
"size": "0x0", | ||
"extraData": "0x", | ||
"gasLimit": hex(transaction.get("gas_limit", 8000000)), | ||
"gasUsed": hex(transaction.get("gas_used", 21000)), | ||
"logsBloom": "0x" + "00" * 256, | ||
"transactions": [], | ||
} | ||
|
||
if full_tx: | ||
block_details["transactions"].append(transaction) | ||
else: | ||
block_details["transactions"].append(transaction_hash) | ||
|
||
return block_details | ||
|
||
|
||
def get_contract(consensus_service: ConsensusService, contract_name: str) -> dict: | ||
""" | ||
Get contract instance by name | ||
|
@@ -689,3 +801,22 @@ def register_all_rpc_endpoints( | |
partial(get_contract, consensus_service), | ||
method_name="sim_getConsensusContract", | ||
) | ||
register_rpc_endpoint(get_chain_id, method_name="eth_chainId") | ||
register_rpc_endpoint(get_net_version, method_name="net_version") | ||
register_rpc_endpoint( | ||
partial(get_block_number, transactions_processor), | ||
method_name="eth_blockNumber", | ||
) | ||
register_rpc_endpoint( | ||
partial(get_block_by_number, transactions_processor), | ||
method_name="eth_getBlockByNumber", | ||
) | ||
register_rpc_endpoint(get_gas_price, method_name="eth_gasPrice") | ||
register_rpc_endpoint( | ||
partial(get_transaction_receipt, transactions_processor), | ||
method_name="eth_getTransactionReceipt", | ||
) | ||
register_rpc_endpoint( | ||
partial(get_block_by_hash, transactions_processor), | ||
method_name="eth_getBlockByHash", | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// global.d.ts | ||
interface Window { | ||
ethereum?: { | ||
isMetaMask?: boolean; | ||
request: (args: { method: string; params?: unknown[] }) => Promise<Array>; | ||
on: (method: string, callback: Function) => {}; | ||
}; | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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