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