Skip to main content

df_hyperliquid_core/client/evm/precompiles/
table.rs

1//! The read precompiles as `L1Read.sol` declares them, in address order, and
2//! the address each sits at.
3
4use crate::utils::Address;
5
6/// One precompile as `L1Read.sol` declares it.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Spec {
10    /// The low two bytes of its address.
11    pub at: u16,
12    /// The `L1Read.sol` function that wraps it.
13    pub name: &'static str,
14    /// The arguments as `L1Read.sol` declares them, such as `address user, uint16 perp`.
15    pub args: &'static str,
16    /// The return type as `L1Read.sol` declares it, such as `(uint64 withdrawable)`.
17    pub returns: &'static str,
18}
19
20const POSITION: &str = "(int64 szi, uint64 entryNtl, int64 isolatedRawUsd, uint32 leverage, bool isIsolated)";
21
22/// Every read precompile `L1Read.sol` names, in address order.
23pub const ALL: [Spec; 21] = [
24    Spec { at: 0x800, name: "position", args: "address user, uint16 perp", returns: POSITION },
25    Spec {
26        at: 0x801,
27        name: "spotBalance",
28        args: "address user, uint64 token",
29        returns: "(uint64 total, uint64 hold, uint64 entryNtl)",
30    },
31    Spec {
32        at: 0x802,
33        name: "userVaultEquity",
34        args: "address user, address vault",
35        returns: "(uint64 equity, uint64 lockedUntilTimestamp)",
36    },
37    Spec { at: 0x803, name: "withdrawable", args: "address user", returns: "(uint64 withdrawable)" },
38    Spec {
39        at: 0x804,
40        name: "delegations",
41        args: "address user",
42        returns: "(address validator, uint64 amount, uint64 lockedUntilTimestamp)[]",
43    },
44    Spec {
45        at: 0x805,
46        name: "delegatorSummary",
47        args: "address user",
48        returns: "(uint64 delegated, uint64 undelegated, uint64 totalPendingWithdrawal, uint64 nPendingWithdrawals)",
49    },
50    Spec { at: 0x806, name: "markPx", args: "uint32 index", returns: "uint64" },
51    Spec { at: 0x807, name: "oraclePx", args: "uint32 index", returns: "uint64" },
52    Spec { at: 0x808, name: "spotPx", args: "uint32 index", returns: "uint64" },
53    Spec { at: 0x809, name: "l1BlockNumber", args: "", returns: "uint64" },
54    Spec {
55        at: 0x80a,
56        name: "perpAssetInfo",
57        args: "uint32 perp",
58        returns: "(string coin, uint32 marginTableId, uint8 szDecimals, uint8 maxLeverage, bool onlyIsolated)",
59    },
60    Spec { at: 0x80b, name: "spotInfo", args: "uint32 spot", returns: "(string name, uint64[2] tokens)" },
61    Spec {
62        at: 0x80c,
63        name: "tokenInfo",
64        args: "uint32 token",
65        returns: "(string name, uint64[] spots, uint64 deployerTradingFeeShare, address deployer, address evmContract, uint8 szDecimals, uint8 weiDecimals, int8 evmExtraWeiDecimals)",
66    },
67    Spec {
68        at: 0x80d,
69        name: "tokenSupply",
70        args: "uint32 token",
71        returns: "(uint64 maxSupply, uint64 totalSupply, uint64 circulatingSupply, uint64 futureEmissions, (address user, uint64 balance)[] nonCirculatingUserBalances)",
72    },
73    Spec { at: 0x80e, name: "bbo", args: "uint32 asset", returns: "(uint64 bid, uint64 ask)" },
74    Spec {
75        at: 0x80f,
76        name: "accountMarginSummary",
77        args: "uint32 perp_dex_index, address user",
78        returns: "(int64 accountValue, uint64 marginUsed, uint64 ntlPos, int64 rawUsd)",
79    },
80    Spec { at: 0x810, name: "coreUserExists", args: "address user", returns: "(bool exists)" },
81    Spec {
82        at: 0x811,
83        name: "borrowLendUserState",
84        args: "address user, uint64 token",
85        returns: "((uint64 basis, uint64 value) borrow, (uint64 basis, uint64 value) supply)",
86    },
87    Spec {
88        at: 0x812,
89        name: "borrowLendReserveState",
90        args: "uint64 token",
91        returns: "(uint64 borrowYearlyRateBps, uint64 supplyYearlyRateBps, uint64 balance, uint64 utilizationBps, uint64 oraclePx, uint64 ltvBps, uint64 totalSupplied, uint64 totalBorrowed)",
92    },
93    Spec { at: 0x813, name: "position2", args: "address user, uint32 perp", returns: POSITION },
94    Spec {
95        at: 0x814,
96        name: "outcomeStatus",
97        args: "uint32 outcome",
98        returns: "(uint8 status, uint64 settledValue, uint32 question)",
99    },
100];
101
102/// The precompile at `0x...<at>`, if `L1Read.sol` names one there.
103#[must_use]
104pub fn spec(at: u16) -> Option<&'static Spec> {
105    ALL.iter().find(|one| one.at == at)
106}
107
108/// `0x` and nineteen zero bytes, then the two of `at`.
109#[must_use]
110pub fn address(at: u16) -> Address {
111    let mut bytes = [0u8; 20];
112    bytes[18..].copy_from_slice(&at.to_be_bytes());
113    Address::from_bytes(bytes)
114}
115
116#[cfg(test)]
117#[path = "tests/table.rs"]
118mod tests;