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