Skip to main content

df_hyperliquid_core/client/evm/rpc/
eth.rs

1//! Every method, as a [`Query`]: `eth_` and HyperEVM's own, each with its
2//! params and how its answer reads.
3
4use crate::client::evm::records::{Block, FeeHistory, Log, Receipt, Transaction};
5use crate::client::evm::rpc::query::{
6    address, data, decimal, hash, index, json, quantity, small, small_or_null, sync_state, tag,
7};
8use crate::client::evm::rpc::{BlockTag, CallRequest, LogFilter, Query};
9use crate::client::evm::u256::{B256, U256};
10use crate::utils::{Address, Json, hex};
11
12fn query<T>(method: &'static str, params: Vec<Json>, read: fn(&str) -> Result<T, String>) -> Query<T> {
13    Query { method, params, read }
14}
15
16/// `eth_chainId`.
17#[must_use]
18pub fn chain_id() -> Query<u64> {
19    query("eth_chainId", Vec::new(), small)
20}
21
22/// `eth_blockNumber`.
23#[must_use]
24pub fn block_number() -> Query<u64> {
25    query("eth_blockNumber", Vec::new(), small)
26}
27
28/// `eth_getBalance`: wei of the chain's coin.
29#[must_use]
30pub fn balance(owner: Address, block: BlockTag) -> Query<U256> {
31    query("eth_getBalance", vec![address(owner), tag(block)], json)
32}
33
34/// `eth_getTransactionCount`: the next nonce, counting the pool at `Pending`.
35#[must_use]
36pub fn transaction_count(owner: Address, block: BlockTag) -> Query<u64> {
37    query("eth_getTransactionCount", vec![address(owner), tag(block)], small)
38}
39
40/// `eth_gasPrice`: on HyperEVM, the next small block's base fee.
41#[must_use]
42pub fn gas_price() -> Query<U256> {
43    query("eth_gasPrice", Vec::new(), json)
44}
45
46/// `eth_maxPriorityFeePerGas`: on HyperEVM, always zero for now.
47#[must_use]
48pub fn max_priority_fee() -> Query<U256> {
49    query("eth_maxPriorityFeePerGas", Vec::new(), json)
50}
51
52/// `eth_feeHistory` over `blocks` blocks up to `newest`, with the tip at
53/// each whole percentile in `percentiles`, 0 to 100, rising.
54#[must_use]
55pub fn fee_history(blocks: u64, newest: BlockTag, percentiles: &[u8]) -> Query<FeeHistory> {
56    let percentiles = percentiles.iter().map(|percent| Json::Number(percent.to_string())).collect();
57    query("eth_feeHistory", vec![quantity(U256::from_u64(blocks)), tag(newest), Json::Array(percentiles)], json)
58}
59
60/// `eth_estimateGas`, a call or (with no `to`) a creation.
61#[must_use]
62pub fn estimate_gas(call: &CallRequest) -> Query<u64> {
63    query("eth_estimateGas", vec![call.json()], small)
64}
65
66/// `eth_call`: what the call returns, as bytes.
67#[must_use]
68pub fn call(call: &CallRequest, block: BlockTag) -> Query<Vec<u8>> {
69    query("eth_call", vec![call.json(), tag(block)], data)
70}
71
72/// `eth_sendRawTransaction`: the hash the node took it under.
73#[must_use]
74pub fn send_raw_transaction(raw: &[u8]) -> Query<B256> {
75    query("eth_sendRawTransaction", vec![Json::String(hex::encode_prefixed(raw))], json)
76}
77
78/// `eth_getTransactionReceipt`: `None` until it is mined.
79#[must_use]
80pub fn receipt(hash: B256) -> Query<Option<Receipt>> {
81    query("eth_getTransactionReceipt", vec![Json::String(hash.to_hex())], json)
82}
83
84/// `eth_getTransactionByHash`: `None` for a hash the node does not know.
85#[must_use]
86pub fn transaction(hash: B256) -> Query<Option<Transaction>> {
87    query("eth_getTransactionByHash", vec![Json::String(hash.to_hex())], json)
88}
89
90/// `eth_getBlockByNumber`, with whole transactions when `full`.
91#[must_use]
92pub fn block(block: BlockTag, full: bool) -> Query<Option<Block>> {
93    query("eth_getBlockByNumber", vec![tag(block), Json::Bool(full)], json)
94}
95
96/// `eth_getCode`: empty for an account that is not a contract.
97#[must_use]
98pub fn code(at: Address, block: BlockTag) -> Query<Vec<u8>> {
99    query("eth_getCode", vec![address(at), tag(block)], data)
100}
101
102/// `eth_getStorageAt`: one storage slot's word.
103#[must_use]
104pub fn storage_at(at: Address, slot: U256, block: BlockTag) -> Query<B256> {
105    query("eth_getStorageAt", vec![address(at), quantity(slot), tag(block)], json)
106}
107
108/// `eth_getLogs`.
109#[must_use]
110pub fn logs(filter: &LogFilter) -> Query<Vec<Log>> {
111    query("eth_getLogs", vec![filter.json()], json)
112}
113
114/// `eth_bigBlockGasPrice`, HyperEVM's: the next big block's base fee.
115#[must_use]
116pub fn big_block_gas_price() -> Query<U256> {
117    query("eth_bigBlockGasPrice", Vec::new(), json)
118}
119
120/// `eth_usingBigBlocks`, HyperEVM's: whether the address's transactions
121/// go to the big blocks (`evmUserModify` on HyperCore sets it).
122#[must_use]
123pub fn using_big_blocks(owner: Address) -> Query<bool> {
124    query("eth_usingBigBlocks", vec![address(owner)], json)
125}
126
127/// `net_version`: the network id, which on HyperEVM is the chain id (999 on mainnet).
128#[must_use]
129pub fn net_version() -> Query<u64> {
130    query("net_version", Vec::new(), decimal)
131}
132
133/// `web3_clientVersion`: the node's name for itself (`hyperliquid evm Mainnet`).
134#[must_use]
135pub fn client_version() -> Query<String> {
136    query("web3_clientVersion", Vec::new(), json)
137}
138
139/// `eth_syncing`: whether the node is still catching up. HyperEVM's node always answers `false`.
140#[must_use]
141pub fn syncing() -> Query<bool> {
142    query("eth_syncing", Vec::new(), sync_state)
143}
144
145/// `eth_getBlockByHash`, with whole transactions when `full`: `None` for a hash the node does not
146/// know.
147#[must_use]
148pub fn block_by_hash(block: B256, full: bool) -> Query<Option<Block>> {
149    query("eth_getBlockByHash", vec![hash(block), Json::Bool(full)], json)
150}
151
152/// `eth_getBlockReceipts`: every receipt of the block, in order, not counting HyperEVM's system
153/// transactions. A block past the head is refused.
154#[must_use]
155pub fn block_receipts(block: BlockTag) -> Query<Option<Vec<Receipt>>> {
156    query("eth_getBlockReceipts", vec![tag(block)], json)
157}
158
159/// [`block_receipts`] of the block with this hash; `None` for a hash the node does not know.
160#[must_use]
161pub fn block_receipts_by_hash(block: B256) -> Query<Option<Vec<Receipt>>> {
162    query("eth_getBlockReceipts", vec![hash(block)], json)
163}
164
165/// `eth_getBlockTransactionCountByNumber`: how many transactions the block holds, not counting
166/// HyperEVM's system transactions. A block past the head is refused.
167#[must_use]
168pub fn block_transaction_count(block: BlockTag) -> Query<Option<u64>> {
169    query("eth_getBlockTransactionCountByNumber", vec![tag(block)], small_or_null)
170}
171
172/// `eth_getBlockTransactionCountByHash`: [`block_transaction_count`] of the block with this hash;
173/// `None` for a hash the node does not know.
174#[must_use]
175pub fn block_transaction_count_by_hash(block: B256) -> Query<Option<u64>> {
176    query("eth_getBlockTransactionCountByHash", vec![hash(block)], small_or_null)
177}
178
179/// `eth_getTransactionByBlockNumberAndIndex`: the block's transaction at `position`, counting
180/// from 0; `None` past the last.
181#[must_use]
182pub fn transaction_by_block_and_index(block: BlockTag, position: u64) -> Query<Option<Transaction>> {
183    query("eth_getTransactionByBlockNumberAndIndex", vec![tag(block), index(position)], json)
184}
185
186/// `eth_getTransactionByBlockHashAndIndex`: [`transaction_by_block_and_index`] in the block with
187/// this hash.
188#[must_use]
189pub fn transaction_by_block_hash_and_index(block: B256, position: u64) -> Query<Option<Transaction>> {
190    query("eth_getTransactionByBlockHashAndIndex", vec![hash(block), index(position)], json)
191}
192
193/// `eth_getSystemTxsByBlockNumber`, HyperEVM's: the system transactions of the block, the ones
194/// HyperCore makes when it sends a token to HyperEVM. Each is a legacy transaction from the token's
195/// system address, with no gas price and a zero signature. A block past the head is refused.
196#[must_use]
197pub fn system_transactions(block: BlockTag) -> Query<Option<Vec<Transaction>>> {
198    query("eth_getSystemTxsByBlockNumber", vec![tag(block)], json)
199}
200
201/// `eth_getSystemTxsByBlockHash`, HyperEVM's: [`system_transactions`] of the block with this hash;
202/// `None` for a hash the node does not know.
203#[must_use]
204pub fn system_transactions_by_hash(block: B256) -> Query<Option<Vec<Transaction>>> {
205    query("eth_getSystemTxsByBlockHash", vec![hash(block)], json)
206}