Skip to content

Commit

Permalink
Add account RPC endpoints (#3976)
Browse files Browse the repository at this point in the history
* add new account module

* update arguments

* update changelog

* replace U256 to Number

* add eth_accounts method
  • Loading branch information
Mr-Leshiy authored May 10, 2022
1 parent 41b64d7 commit 1b0c0b3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- Add new Ethreum RPC endpoints for getting block info: eth_getBlockByHash, eth_getBlockByNumber, eth_getBlockTransactionCountByHash, eth_getBlockTransactionCountByNumber, eth_getUncleCountByBlockHash, eth_getUncleCountByBlockNumber, eth_blockNumber
- Add new Ethreum RPC endpoints for transaction handling: eth_sendTransaction, eth_sendRawTransaction, eth_getTransactionByHash, eth_getTransactionByBlockHashAndIndex, eth_getTransactionByBlockNumberAndIndex, eth_getTransactionReceipt, eth_signTransaction, eth_estimateGas, eth_sign
- Add new Ethreum RPC endpoints for getting chain info: eth_chainId, eth_syncing, eth_gasPrice, eth_protocolVersion
- Add new Ethreum RPC endpoints for account handling: eth_accounts, eth_getTransactionCount, eth_getBalance, eth_getCode, eth_getStorageAt

## Release 0.13.0

**New features:**
Expand Down
48 changes: 48 additions & 0 deletions jormungandr/src/jrpc/eth_account/logic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::Error;
use crate::{
context::Context,
jrpc::eth_types::{block_number::BlockNumber, bytes::Bytes, number::Number},
};
use chain_evm::ethereum_types::{H160, H256};

pub fn accounts(_context: &Context) -> Result<Vec<H160>, Error> {
// TODO implement
Ok(vec![H160::zero()])
}

pub fn get_transaction_count(
_address: H160,
_block_number: BlockNumber,
_context: &Context,
) -> Result<Number, Error> {
// TODO implement
Ok(0.into())
}

pub fn get_balance(
_address: H160,
_block_number: BlockNumber,
_context: &Context,
) -> Result<Number, Error> {
// TODO implement
Ok(0.into())
}

pub fn get_code(
_address: H160,
_block_number: BlockNumber,
_context: &Context,
) -> Result<Bytes, Error> {
// TODO implement
Ok(Default::default())
}

pub fn get_storage_at(
_address: H160,
_key: H256,
_block_number: BlockNumber,
_context: &Context,
) -> Result<H256, Error> {
// TODO implement
Ok(H256::zero())
}
57 changes: 57 additions & 0 deletions jormungandr/src/jrpc/eth_account/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use jsonrpsee_http_server::RpcModule;

use crate::context::ContextLock;

mod logic;

#[derive(Debug, thiserror::Error)]
pub enum Error {}

pub fn eth_account_module(context: ContextLock) -> RpcModule<ContextLock> {
let mut module = RpcModule::new(context);

module
.register_async_method("eth_accounts", |_, context| async move {
let context = context.read().await;
logic::accounts(&context).map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();

module
.register_async_method("eth_getTransactionCount", |params, context| async move {
let context = context.read().await;
let (address, block_number) = params.parse()?;
logic::get_transaction_count(address, block_number, &context)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();

module
.register_async_method("eth_getBalance", |params, context| async move {
let context = context.read().await;
let (address, block_number) = params.parse()?;
logic::get_balance(address, block_number, &context)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();

module
.register_async_method("eth_getCode", |params, context| async move {
let context = context.read().await;
let (address, block_number) = params.parse()?;
logic::get_code(address, block_number, &context)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();

module
.register_async_method("eth_getStorageAt", |params, context| async move {
let context = context.read().await;
let (address, key, block_number) = params.parse()?;
logic::get_storage_at(address, key, block_number, &context)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
})
.unwrap();

module
}
8 changes: 7 additions & 1 deletion jormungandr/src/jrpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "evm")]
mod eth_account;
#[cfg(feature = "evm")]
mod eth_block_info;
#[cfg(feature = "evm")]
mod eth_chain_info;
Expand Down Expand Up @@ -35,7 +37,11 @@ pub async fn start_jrpc_server(config: Config, _context: ContextLock) {
.unwrap();

modules
.merge(eth_transaction::eth_transaction_module(_context))
.merge(eth_transaction::eth_transaction_module(_context.clone()))
.unwrap();

modules
.merge(eth_account::eth_account_module(_context))
.unwrap();
}

Expand Down

0 comments on commit 1b0c0b3

Please sign in to comment.