mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor(rpc): extract common logic for getting system txs
This commit is contained in:
@ -106,46 +106,15 @@ impl<Eth: EthWrapper> HlSystemTransactionExt<Eth> {
|
|||||||
pub fn new(eth_api: Eth) -> Self {
|
pub fn new(eth_api: Eth) -> Self {
|
||||||
Self { eth_api, _marker: PhantomData }
|
Self { eth_api, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
async fn get_system_txs_by_block_id(
|
||||||
impl<Eth: EthWrapper>
|
|
||||||
EthSystemTransactionApiServer<RpcTransaction<Eth::NetworkTypes>, RpcReceipt<Eth::NetworkTypes>>
|
|
||||||
for HlSystemTransactionExt<Eth>
|
|
||||||
where
|
|
||||||
jsonrpsee_types::ErrorObject<'static>: From<<Eth as EthApiTypes>::Error>,
|
|
||||||
{
|
|
||||||
/// Returns the system transactions for a given block hash.
|
|
||||||
/// Semi-compliance with the `eth_getSystemTxsByBlockHash` RPC method introduced by hl-node.
|
|
||||||
/// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/json-rpc
|
|
||||||
///
|
|
||||||
/// NOTE: Method name differs from hl-node because we retrieve transaction data from EVM
|
|
||||||
/// (signature recovery for 'from' address, EVM hash calculation) rather than HyperCore.
|
|
||||||
async fn get_evm_system_txs_by_block_hash(
|
|
||||||
&self,
|
&self,
|
||||||
hash: B256,
|
block_id: BlockId,
|
||||||
) -> RpcResult<Option<Vec<RpcTransaction<Eth::NetworkTypes>>>> {
|
) -> RpcResult<Option<Vec<RpcTransaction<Eth::NetworkTypes>>>>
|
||||||
trace!(target: "rpc::eth", ?hash, "Serving eth_getEvmSystemTxsByBlockHash");
|
where
|
||||||
match self.get_evm_system_txs_by_block_number(Some(BlockId::Hash(hash.into()))).await {
|
jsonrpsee_types::ErrorObject<'static>: From<<Eth as EthApiTypes>::Error>,
|
||||||
Ok(Some(txs)) => Ok(Some(txs)),
|
{
|
||||||
// hl-node returns none if the block is not found
|
if let Some(block) = self.eth_api.recovered_block(block_id).await? {
|
||||||
_ => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the system transactions for a given block number, or the latest block if no block
|
|
||||||
/// number is provided. Semi-compliance with the `eth_getSystemTxsByBlockNumber` RPC method
|
|
||||||
/// introduced by hl-node. https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/json-rpc
|
|
||||||
///
|
|
||||||
/// NOTE: Method name differs from hl-node because we retrieve transaction data from EVM
|
|
||||||
/// (signature recovery for 'from' address, EVM hash calculation) rather than HyperCore.
|
|
||||||
async fn get_evm_system_txs_by_block_number(
|
|
||||||
&self,
|
|
||||||
id: Option<BlockId>,
|
|
||||||
) -> RpcResult<Option<Vec<RpcTransaction<Eth::NetworkTypes>>>> {
|
|
||||||
trace!(target: "rpc::eth", ?id, "Serving eth_getEvmSystemTxsByBlockNumber");
|
|
||||||
|
|
||||||
if let Some(block) = self.eth_api.recovered_block(id.unwrap_or_default()).await? {
|
|
||||||
let block_hash = block.hash();
|
let block_hash = block.hash();
|
||||||
let block_number = block.number();
|
let block_number = block.number();
|
||||||
let base_fee_per_gas = block.base_fee_per_gas();
|
let base_fee_per_gas = block.base_fee_per_gas();
|
||||||
@ -172,39 +141,19 @@ where
|
|||||||
.collect();
|
.collect();
|
||||||
Ok(Some(system_txs))
|
Ok(Some(system_txs))
|
||||||
} else {
|
} else {
|
||||||
// hl-node returns an error if the block is not found
|
Ok(None)
|
||||||
Err(ErrorObject::owned(
|
|
||||||
INTERNAL_ERROR_CODE,
|
|
||||||
format!("invalid block height: {id:?}"),
|
|
||||||
Some(()),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the receipts for the system transactions for a given block hash.
|
async fn get_system_txs_receipts_by_block_id(
|
||||||
async fn get_evm_system_txs_receipts_by_block_hash(
|
|
||||||
&self,
|
&self,
|
||||||
hash: B256,
|
block_id: BlockId,
|
||||||
) -> RpcResult<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
|
) -> RpcResult<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>>
|
||||||
trace!(target: "rpc::eth", ?hash, "Serving eth_getEvmSystemTxsReceiptsByBlockHash");
|
where
|
||||||
match self
|
jsonrpsee_types::ErrorObject<'static>: From<<Eth as EthApiTypes>::Error>,
|
||||||
.get_evm_system_txs_receipts_by_block_number(Some(BlockId::Hash(hash.into())))
|
{
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(Some(receipts)) => Ok(Some(receipts)),
|
|
||||||
_ => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the receipts for the system transactions for a given block number, or the latest
|
|
||||||
/// block if no block
|
|
||||||
async fn get_evm_system_txs_receipts_by_block_number(
|
|
||||||
&self,
|
|
||||||
block_id: Option<BlockId>,
|
|
||||||
) -> RpcResult<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
|
|
||||||
trace!(target: "rpc::eth", ?block_id, "Serving eth_getEvmSystemTxsReceiptsByBlockNumber");
|
|
||||||
if let Some((block, receipts)) =
|
if let Some((block, receipts)) =
|
||||||
EthBlocks::load_block_and_receipts(&self.eth_api, block_id.unwrap_or_default()).await?
|
EthBlocks::load_block_and_receipts(&self.eth_api, block_id).await?
|
||||||
{
|
{
|
||||||
let block_number = block.number;
|
let block_number = block.number;
|
||||||
let base_fee = block.base_fee_per_gas;
|
let base_fee = block.base_fee_per_gas;
|
||||||
@ -249,11 +198,87 @@ where
|
|||||||
let receipts = self.eth_api.tx_resp_builder().convert_receipts(inputs)?;
|
let receipts = self.eth_api.tx_resp_builder().convert_receipts(inputs)?;
|
||||||
Ok(Some(receipts))
|
Ok(Some(receipts))
|
||||||
} else {
|
} else {
|
||||||
Err(ErrorObject::owned(
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<Eth: EthWrapper>
|
||||||
|
EthSystemTransactionApiServer<RpcTransaction<Eth::NetworkTypes>, RpcReceipt<Eth::NetworkTypes>>
|
||||||
|
for HlSystemTransactionExt<Eth>
|
||||||
|
where
|
||||||
|
jsonrpsee_types::ErrorObject<'static>: From<<Eth as EthApiTypes>::Error>,
|
||||||
|
{
|
||||||
|
/// Returns the system transactions for a given block hash.
|
||||||
|
/// Semi-compliance with the `eth_getSystemTxsByBlockHash` RPC method introduced by hl-node.
|
||||||
|
/// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/json-rpc
|
||||||
|
///
|
||||||
|
/// NOTE: Method name differs from hl-node because we retrieve transaction data from EVM
|
||||||
|
/// (signature recovery for 'from' address, EVM hash calculation) rather than HyperCore.
|
||||||
|
async fn get_evm_system_txs_by_block_hash(
|
||||||
|
&self,
|
||||||
|
hash: B256,
|
||||||
|
) -> RpcResult<Option<Vec<RpcTransaction<Eth::NetworkTypes>>>> {
|
||||||
|
trace!(target: "rpc::eth", ?hash, "Serving eth_getEvmSystemTxsByBlockHash");
|
||||||
|
match self.get_system_txs_by_block_id(BlockId::Hash(hash.into())).await {
|
||||||
|
Ok(txs) => Ok(txs),
|
||||||
|
// hl-node returns none if the block is not found
|
||||||
|
Err(_) => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the system transactions for a given block number, or the latest block if no block
|
||||||
|
/// number is provided. Semi-compliance with the `eth_getSystemTxsByBlockNumber` RPC method
|
||||||
|
/// introduced by hl-node. https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/json-rpc
|
||||||
|
///
|
||||||
|
/// NOTE: Method name differs from hl-node because we retrieve transaction data from EVM
|
||||||
|
/// (signature recovery for 'from' address, EVM hash calculation) rather than HyperCore.
|
||||||
|
async fn get_evm_system_txs_by_block_number(
|
||||||
|
&self,
|
||||||
|
id: Option<BlockId>,
|
||||||
|
) -> RpcResult<Option<Vec<RpcTransaction<Eth::NetworkTypes>>>> {
|
||||||
|
trace!(target: "rpc::eth", ?id, "Serving eth_getEvmSystemTxsByBlockNumber");
|
||||||
|
match self.get_system_txs_by_block_id(id.unwrap_or_default()).await? {
|
||||||
|
Some(txs) => Ok(Some(txs)),
|
||||||
|
None => {
|
||||||
|
// hl-node returns an error if the block is not found
|
||||||
|
Err(ErrorObject::owned(
|
||||||
|
INTERNAL_ERROR_CODE,
|
||||||
|
format!("invalid block height: {id:?}"),
|
||||||
|
Some(()),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the receipts for the system transactions for a given block hash.
|
||||||
|
async fn get_evm_system_txs_receipts_by_block_hash(
|
||||||
|
&self,
|
||||||
|
hash: B256,
|
||||||
|
) -> RpcResult<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
|
||||||
|
trace!(target: "rpc::eth", ?hash, "Serving eth_getEvmSystemTxsReceiptsByBlockHash");
|
||||||
|
match self.get_system_txs_receipts_by_block_id(BlockId::Hash(hash.into())).await {
|
||||||
|
Ok(receipts) => Ok(receipts),
|
||||||
|
// hl-node returns none if the block is not found
|
||||||
|
Err(_) => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the receipts for the system transactions for a given block number, or the latest
|
||||||
|
/// block if no block
|
||||||
|
async fn get_evm_system_txs_receipts_by_block_number(
|
||||||
|
&self,
|
||||||
|
block_id: Option<BlockId>,
|
||||||
|
) -> RpcResult<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
|
||||||
|
trace!(target: "rpc::eth", ?block_id, "Serving eth_getEvmSystemTxsReceiptsByBlockNumber");
|
||||||
|
match self.get_system_txs_receipts_by_block_id(block_id.unwrap_or_default()).await? {
|
||||||
|
Some(receipts) => Ok(Some(receipts)),
|
||||||
|
None => Err(ErrorObject::owned(
|
||||||
INTERNAL_ERROR_CODE,
|
INTERNAL_ERROR_CODE,
|
||||||
format!("invalid block height: {block_id:?}"),
|
format!("invalid block height: {block_id:?}"),
|
||||||
Some(()),
|
Some(()),
|
||||||
))
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user