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::{address, data, json, quantity, small, tag};
6use crate::client::evm::rpc::{BlockTag, CallRequest, LogFilter, Query};
7use crate::client::evm::u256::{B256, U256};
8use crate::utils::{Address, Json, hex};
9
10fn query<T>(method: &'static str, params: Vec<Json>, read: fn(&str) -> Result<T, String>) -> Query<T> {
11    Query { method, params, read }
12}
13
14/// `eth_chainId`.
15#[must_use]
16pub fn chain_id() -> Query<u64> {
17    query("eth_chainId", Vec::new(), small)
18}
19
20/// `eth_blockNumber`.
21#[must_use]
22pub fn block_number() -> Query<u64> {
23    query("eth_blockNumber", Vec::new(), small)
24}
25
26/// `eth_getBalance`: wei of the chain's coin.
27#[must_use]
28pub fn balance(owner: Address, block: BlockTag) -> Query<U256> {
29    query("eth_getBalance", vec![address(owner), tag(block)], json)
30}
31
32/// `eth_getTransactionCount`: the next nonce, counting the pool at `Pending`.
33#[must_use]
34pub fn transaction_count(owner: Address, block: BlockTag) -> Query<u64> {
35    query("eth_getTransactionCount", vec![address(owner), tag(block)], small)
36}
37
38/// `eth_gasPrice`: on HyperEVM, the next small block's base fee.
39#[must_use]
40pub fn gas_price() -> Query<U256> {
41    query("eth_gasPrice", Vec::new(), json)
42}
43
44/// `eth_maxPriorityFeePerGas`: on HyperEVM, always zero for now.
45#[must_use]
46pub fn max_priority_fee() -> Query<U256> {
47    query("eth_maxPriorityFeePerGas", Vec::new(), json)
48}
49
50/// `eth_feeHistory` over `blocks` blocks up to `newest`, with the tip at
51/// each whole percentile in `percentiles`, 0 to 100, rising.
52#[must_use]
53pub fn fee_history(blocks: u64, newest: BlockTag, percentiles: &[u8]) -> Query<FeeHistory> {
54    let percentiles = percentiles.iter().map(|percent| Json::Number(percent.to_string())).collect();
55    query("eth_feeHistory", vec![quantity(U256::from_u64(blocks)), tag(newest), Json::Array(percentiles)], json)
56}
57
58/// `eth_estimateGas`, a call or (with no `to`) a creation.
59#[must_use]
60pub fn estimate_gas(call: &CallRequest) -> Query<u64> {
61    query("eth_estimateGas", vec![call.json()], small)
62}
63
64/// `eth_call`: what the call returns, as bytes.
65#[must_use]
66pub fn call(call: &CallRequest, block: BlockTag) -> Query<Vec<u8>> {
67    query("eth_call", vec![call.json(), tag(block)], data)
68}
69
70/// `eth_sendRawTransaction`: the hash the node took it under.
71#[must_use]
72pub fn send_raw_transaction(raw: &[u8]) -> Query<B256> {
73    query("eth_sendRawTransaction", vec![Json::String(hex::encode_prefixed(raw))], json)
74}
75
76/// `eth_getTransactionReceipt`: `None` until it is mined.
77#[must_use]
78pub fn receipt(hash: B256) -> Query<Option<Receipt>> {
79    query("eth_getTransactionReceipt", vec![Json::String(hash.to_hex())], json)
80}
81
82/// `eth_getTransactionByHash`: `None` for a hash the node does not know.
83#[must_use]
84pub fn transaction(hash: B256) -> Query<Option<Transaction>> {
85    query("eth_getTransactionByHash", vec![Json::String(hash.to_hex())], json)
86}
87
88/// `eth_getBlockByNumber`, with whole transactions when `full`.
89#[must_use]
90pub fn block(block: BlockTag, full: bool) -> Query<Option<Block>> {
91    query("eth_getBlockByNumber", vec![tag(block), Json::Bool(full)], json)
92}
93
94/// `eth_getCode`: empty for an account that is not a contract.
95#[must_use]
96pub fn code(at: Address, block: BlockTag) -> Query<Vec<u8>> {
97    query("eth_getCode", vec![address(at), tag(block)], data)
98}
99
100/// `eth_getStorageAt`: one storage slot's word.
101#[must_use]
102pub fn storage_at(at: Address, slot: U256, block: BlockTag) -> Query<B256> {
103    query("eth_getStorageAt", vec![address(at), quantity(slot), tag(block)], json)
104}
105
106/// `eth_getLogs`.
107#[must_use]
108pub fn logs(filter: &LogFilter) -> Query<Vec<Log>> {
109    query("eth_getLogs", vec![filter.json()], json)
110}
111
112/// `eth_bigBlockGasPrice`, HyperEVM's: the next big block's base fee.
113#[must_use]
114pub fn big_block_gas_price() -> Query<U256> {
115    query("eth_bigBlockGasPrice", Vec::new(), json)
116}
117
118/// `eth_usingBigBlocks`, HyperEVM's: whether the address's transactions
119/// go to the big blocks (`evmUserModify` on HyperCore sets it).
120#[must_use]
121pub fn using_big_blocks(owner: Address) -> Query<bool> {
122    query("eth_usingBigBlocks", vec![address(owner)], json)
123}