Skip to main content

df_hyperliquid_core/client/evm/rpc/
call.rs

1//! [`CallRequest`]: the call object `eth_call` and `eth_estimateGas` take.
2
3use crate::client::evm::rpc::query::quantity;
4use crate::client::evm::tx::Call;
5use crate::client::evm::u256::U256;
6use crate::utils::{Address, Json, hex};
7
8/// The call object `eth_call` and `eth_estimateGas` take. No `to` estimates
9/// or runs a contract creation from `data`.
10#[derive(Debug, Clone, PartialEq, Eq, Default)]
11pub struct CallRequest {
12    pub from: Option<Address>,
13    pub to: Option<Address>,
14    pub gas: Option<u64>,
15    pub value: Option<U256>,
16    pub data: Vec<u8>,
17}
18
19impl CallRequest {
20    /// `data` sent to a contract.
21    #[must_use]
22    pub fn to(to: Address, data: Vec<u8>) -> CallRequest {
23        CallRequest { to: Some(to), data, ..CallRequest::default() }
24    }
25
26    /// What `call` does, as sent by `from`: what a transaction's gas is estimated with.
27    #[must_use]
28    pub fn of(from: Option<Address>, call: &Call) -> CallRequest {
29        CallRequest {
30            from,
31            to: call.to,
32            gas: None,
33            value: (!call.value.is_zero()).then_some(call.value),
34            data: call.data.clone(),
35        }
36    }
37
38    pub(super) fn json(&self) -> Json {
39        let mut fields = Vec::new();
40        if let Some(from) = self.from {
41            fields.push(("from".to_owned(), Json::String(from.to_hex())));
42        }
43        if let Some(to) = self.to {
44            fields.push(("to".to_owned(), Json::String(to.to_hex())));
45        }
46        if let Some(gas) = self.gas {
47            fields.push(("gas".to_owned(), quantity(U256::from_u64(gas))));
48        }
49        if let Some(value) = self.value {
50            fields.push(("value".to_owned(), quantity(value)));
51        }
52        if !self.data.is_empty() {
53            fields.push(("data".to_owned(), Json::String(hex::encode_prefixed(&self.data))));
54        }
55        Json::Object(fields)
56    }
57}