Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

- [#6387](https://github.com/ChainSafe/forest/pull/6387) Implemented `Filecoin.EthGetTransactionCount` for API v2.

- [#6403](https://github.com/ChainSafe/forest/pull/6403) Implemented `Filecoin.EthGetBalance` for API v2.

### Changed

- [#6368](https://github.com/ChainSafe/forest/pull/6368): Migrated build and development tooling from Makefile to `mise`. Contributors should install `mise` and use `mise run` commands instead of `make` commands.
Expand Down
43 changes: 39 additions & 4 deletions src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ impl RpcMethod<2> for EthGetBalance {
const PARAM_NAMES: [&'static str; 2] = ["address", "blockParam"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the balance of an Ethereum address at the specified block state");

type Params = (EthAddress, BlockNumberOrHash);
type Ok = EthBigInt;
Expand All @@ -872,18 +874,51 @@ impl RpcMethod<2> for EthGetBalance {
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(address, block_param): Self::Params,
) -> Result<Self::Ok, ServerError> {
let fil_addr = address.to_filecoin_address()?;
let ts = tipset_by_block_number_or_hash(
ctx.chain_store(),
block_param,
ResolveNullTipset::TakeOlder,
)?;
let state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?;
let actor = state.get_required_actor(&fil_addr)?;
Ok(EthBigInt(actor.balance.atto().clone()))
let balance = eth_get_balance(&ctx, &address, &ts)?;
Ok(balance)
}
}

pub enum EthGetBalanceV2 {}
impl RpcMethod<2> for EthGetBalanceV2 {
const NAME: &'static str = "Filecoin.EthGetBalance";
const NAME_ALIAS: Option<&'static str> = Some("eth_getBalance");
const PARAM_NAMES: [&'static str; 2] = ["address", "blockParam"];
const API_PATHS: BitFlags<ApiPaths> = make_bitflags!(ApiPaths::V2);
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the balance of an Ethereum address at the specified block state");

Comment thread
sudo-shashank marked this conversation as resolved.
type Params = (EthAddress, ExtBlockNumberOrHash);
type Ok = EthBigInt;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(address, block_param): Self::Params,
) -> Result<Self::Ok, ServerError> {
let ts = tipset_by_block_number_or_hash_v2(&ctx, block_param, ResolveNullTipset::TakeOlder)
.await?;
let balance = eth_get_balance(&ctx, &address, &ts)?;
Ok(balance)
}
}

fn eth_get_balance<DB: Blockstore>(
ctx: &Ctx<DB>,
address: &EthAddress,
ts: &Tipset,
) -> Result<EthBigInt> {
let fil_addr = address.to_filecoin_address()?;
let state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?;
let actor = state.get_required_actor(&fil_addr)?;
Ok(EthBigInt(actor.balance.atto().clone()))
}
Comment thread
sudo-shashank marked this conversation as resolved.

fn get_tipset_from_hash<DB: Blockstore>(
chain_store: &ChainStore<DB>,
block_hash: &EthHash,
Expand Down
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ macro_rules! for_each_rpc_method {
$callback!($crate::rpc::eth::EthFeeHistoryV2);
$callback!($crate::rpc::eth::EthGasPrice);
$callback!($crate::rpc::eth::EthGetBalance);
$callback!($crate::rpc::eth::EthGetBalanceV2);
$callback!($crate::rpc::eth::EthGetBlockByHash);
$callback!($crate::rpc::eth::EthGetBlockByNumber);
$callback!($crate::rpc::eth::EthGetBlockReceipts);
Expand Down
71 changes: 71 additions & 0 deletions src/tool/subcommands/api_cmd/api_compare_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,77 @@ fn eth_tests_with_tipset<DB: Blockstore>(store: &Arc<DB>, shared_tipset: &Tipset
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff38c072f286e3b20b3954ca9f99c05fbecc64aa").unwrap(),
ExtBlockNumberOrHash::from_block_number(shared_tipset.epoch()),
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_block_number(shared_tipset.epoch()),
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_block_number_object(shared_tipset.epoch()),
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_block_hash_object(block_hash.clone(), false),
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_block_hash_object(block_hash.clone(), true),
))
.unwrap(),
),
RpcTest::identity(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_predefined(ExtPredefined::Earliest),
))
.unwrap(),
)
.policy_on_rejected(PolicyOnRejected::PassWithQuasiIdenticalError),
RpcTest::basic(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_predefined(ExtPredefined::Pending),
))
.unwrap(),
),
RpcTest::basic(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_predefined(ExtPredefined::Latest),
))
.unwrap(),
),
RpcTest::basic(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_predefined(ExtPredefined::Safe),
))
.unwrap(),
),
RpcTest::basic(
EthGetBalanceV2::request((
EthAddress::from_str("0xff000000000000000000000000000000000003ec").unwrap(),
ExtBlockNumberOrHash::from_predefined(ExtPredefined::Finalized),
))
.unwrap(),
),
Comment thread
sudo-shashank marked this conversation as resolved.
RpcTest::identity(
EthGetBlockByNumber::request((
ExtBlockNumberOrHash::from_block_number(shared_tipset.epoch()),
Expand Down
1 change: 1 addition & 0 deletions src/tool/subcommands/api_cmd/test_snapshots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ filecoin_ethfeehistory_1737446676883828.rpcsnap.json.zst
filecoin_ethfeehistory_v2_1767605175056660.rpcsnap.json.zst
filecoin_ethgasprice_1758725940980141.rpcsnap.json.zst
filecoin_ethgetbalance_1740048634848277.rpcsnap.json.zst
filecoin_ethgetbalance_v2_1768188109932986.rpcsnap.json.zst
filecoin_ethgetblockbyhash_1740132537807408.rpcsnap.json.zst
filecoin_ethgetblockbynumber_1737446676696328.rpcsnap.json.zst
filecoin_ethgetblockreceipts_1740132537907751.rpcsnap.json.zst
Expand Down
Loading