Skip to main content

df_papertrade_core/client/
mod.rs

1//! The [`Client`] an app trades through, and what it stands on.
2//!
3//! - [`config`]: the venue endpoints, the clock and randomness.
4//! - [`relayer`]: relayer requests as values, their answers, and the sender.
5//! - [`chain`]: contract reads on HyperEVM.
6//! - [`deployment`]: contract addresses, the relayer origin and the EIP-712 domains.
7//! - [`error`]: why a client call failed.
8//!
9//! The client's reads are here too. Its writes live with their domains: the session key in
10//! [`crate::account::session`], trades and staking in [`crate::trade::writes`], deposits in
11//! [`crate::transfer::deposit`].
12
13pub mod chain;
14pub mod config;
15pub mod deployment;
16pub mod error;
17mod reads;
18pub mod relayer;
19
20pub use config::Config;
21pub use error::Error;
22pub use reads::Quote;
23pub use relayer::{Accepted, Call, Relayer, RelayerError};
24
25use df_hyperliquid_core::client::Http;
26use df_hyperliquid_core::client::evm::Rpc;
27
28/// Papertrade client over one transport. Cheap to clone when the transport is.
29#[derive(Clone, Debug)]
30pub struct Client<H> {
31    http: H,
32    config: Config,
33}
34
35impl<H: Http + Clone> Client<H> {
36    pub fn new(http: H, config: Config) -> Self {
37        Client { http, config }
38    }
39
40    #[must_use]
41    pub const fn config(&self) -> &Config {
42        &self.config
43    }
44
45    #[must_use]
46    pub const fn http(&self) -> &H {
47        &self.http
48    }
49
50    pub(crate) fn now_ms(&self) -> u64 {
51        (self.config.now_ms)()
52    }
53
54    pub(crate) fn random<const N: usize>(&self) -> Result<[u8; N], Error> {
55        let mut bytes = [0u8; N];
56        (self.config.random)(&mut bytes).map_err(Error::Random)?;
57        Ok(bytes)
58    }
59
60    #[must_use]
61    pub fn relayer(&self) -> Relayer<H> {
62        Relayer::at(self.http.clone(), self.config.relayer.clone())
63    }
64
65    pub(crate) fn rpc(&self) -> Rpc<H> {
66        Rpc::new(self.http.clone(), self.config.rpc.clone())
67    }
68}