df_hyperliquid_core/client/evm/rpc/
eth.rs1use 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#[must_use]
16pub fn chain_id() -> Query<u64> {
17 query("eth_chainId", Vec::new(), small)
18}
19
20#[must_use]
22pub fn block_number() -> Query<u64> {
23 query("eth_blockNumber", Vec::new(), small)
24}
25
26#[must_use]
28pub fn balance(owner: Address, block: BlockTag) -> Query<U256> {
29 query("eth_getBalance", vec![address(owner), tag(block)], json)
30}
31
32#[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#[must_use]
40pub fn gas_price() -> Query<U256> {
41 query("eth_gasPrice", Vec::new(), json)
42}
43
44#[must_use]
46pub fn max_priority_fee() -> Query<U256> {
47 query("eth_maxPriorityFeePerGas", Vec::new(), json)
48}
49
50#[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#[must_use]
60pub fn estimate_gas(call: &CallRequest) -> Query<u64> {
61 query("eth_estimateGas", vec![call.json()], small)
62}
63
64#[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#[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#[must_use]
78pub fn receipt(hash: B256) -> Query<Option<Receipt>> {
79 query("eth_getTransactionReceipt", vec![Json::String(hash.to_hex())], json)
80}
81
82#[must_use]
84pub fn transaction(hash: B256) -> Query<Option<Transaction>> {
85 query("eth_getTransactionByHash", vec![Json::String(hash.to_hex())], json)
86}
87
88#[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#[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#[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#[must_use]
108pub fn logs(filter: &LogFilter) -> Query<Vec<Log>> {
109 query("eth_getLogs", vec![filter.json()], json)
110}
111
112#[must_use]
114pub fn big_block_gas_price() -> Query<U256> {
115 query("eth_bigBlockGasPrice", Vec::new(), json)
116}
117
118#[must_use]
121pub fn using_big_blocks(owner: Address) -> Query<bool> {
122 query("eth_usingBigBlocks", vec![address(owner)], json)
123}