與 HyperCore 交互
讀取預編譯合約
測試網 EVM 提供讀取預編譯合約,允許查詢 HyperCore 資訊。預編譯合約地址從 0x0000000000000000000000000000000000000800 開始,提供查詢永續倉位、現貨餘額、金庫權益、質押委託、預言機價格以及 L1 區塊號等資訊的方法。
這些值保證與建構 EVM 區塊時的最新 HyperCore 狀態一致。
隨附一個描述讀取預編譯合約的 Solidity 文件 L1Read.sol。例如,以下呼叫查詢測試網上的第三個永續預言機價格:
cast call 0x0000000000000000000000000000000000000807 0x0000000000000000000000000000000000000000000000000000000000000003 --rpc-url https://rpc.hyperliquid-testnet.xyz/evm
要轉換爲浮點數,永續合約需將返回的價格除以 10^(6 - szDecimals),現貨需除以 10^(8 - base asset szDecimals)。
以無效輸入(例如無效資產或金庫地址)呼叫預編譯合約會返回錯誤,並消耗傳入預編譯呼叫幀的全部 Gas。預編譯合約的 Gas 成本爲 2000 + 65 * (input_len + output_len)。
CoreWriter 合約
在 0x3333333333333333333333333333333333333333 處有一個系統合約,用於從 HyperEVM 向 HyperCore 發送交易。它先消耗 \~25,000 Gas,再發出一條由 HyperCore 作爲操作處理的日誌。實踐中,一次基本呼叫的 Gas 用量約爲 \~47000。隨附寫入系統合約的 Solidity 文件 CoreWriter.sol。
操作編碼細節
- 字節 1:編碼版本
- 目前僅支持版本
1,但它支持在保持向後相容的同時進行未來升級。 - 字節 2-4:操作 ID
- 這三個字節按大端序無符號整數解碼時,表示該操作的唯一標識符。
- 其餘字節:操作編碼
- 其餘字節構成操作特定的數據,始終是一系列 Solidity 類型的原始 ABI 編碼
爲防止利用 HyperEVM 繞過 L1 記憶體池獲得任何潛在的延遲優勢,從 CoreWriter 發送的訂單操作和金庫劃轉會在鏈上延遲幾秒執行。這對用戶體驗沒有明顯影響,因爲最終用戶至少要等待一個小區塊確認。這些鏈上延遲的操作在 L1 瀏覽器中出現兩次:第一次是入隊,第二次是 HyperCore 執行。
| 操作 ID | 操作 | 欄位 | Solidity 類型 | 說明 |
|---|---|---|---|---|
| 1 | 限價訂單 | (asset, isBuy, limitPx, sz, reduceOnly, encodedTif, cloid) | (uint32, bool, uint64, uint64, bool, uint8, uint128) | Tif 編碼:1 爲 Alo,2 爲 Gtc,3 爲 Ioc。Cloid 編碼:0 表示無 cloid,否則使用該數字作爲 cloid。limitPx 和 sz 應按 10^8 * 人類可讀值發送 |
| 2 | 金庫劃轉 | (vault, isDeposit, usd) | (address, bool, uint64) | |
| 3 | 代幣委託 | (validator, wei, isUndelegate) | (address, uint64, bool) | |
| 4 | 質押充值 | wei | uint64 | |
| 5 | 質押提幣 | wei | uint64 | |
| 6 | 現貨發送 | (destination, token, wei) | (address, uint64, uint64) | |
| 7 | USD 類劃轉 | (ntl, toPerp) | (uint64, bool) | |
| 8 | 完成 EVM 合約 | (token, encodedFinalizeEvmContractVariant, createNonce) | (uint64, uint8, uint64) | encodedFinalizeEvmContractVariant:1 爲 Create,2 爲 FirstStorageSlot,3 爲 CustomStorageSlot。若爲 Create 變體,則使用 createNonce 輸入參數。 |
| 9 | 添加 API 錢包 | (API wallet address, API wallet name) | (address, string) | 如果 API 錢包名稱爲空,則它成爲主 API 錢包 / agent |
| 10 | 按 oid 撤單 | (asset, oid) | (uint32, uint64) | |
| 11 | 按 cloid 撤單 | (asset, cloid) | (uint32, uint128) | |
| 12 | 批准 builder 費用 | (maxFeeRate, builder address) | (uint64, address) | maxFeeRate 以 decibps 爲單位。要批准 0.01% 的 builder 費用,maxFreeRate 應爲 10。 |
| 13 | 劃轉資產 | (destination, subAccount, source_dex, destination_dex, token, wei) | (address, address, uint32, uint32, uint64, uint64) | 如果 subAccount 不是零地址,則從 subAccount 劃轉。現貨的 source_dex 或 destination_dex 指定爲 uint32::MAX。 |
| 15 | 借貸操作 | (encodedOperation, token, wei) | (uint8, uint64, uint64) | encodedOperation:0 爲 Supply,1 爲 Withdraw。若 wei 爲 0,則最大程度執行該操作,例如從儲備中提取全部餘額。 |
| 16 | 設定抽象 | (user, abstraction) | (address, uint8) | abstraction:1 爲 disabled,2 爲 unifiedAccount,3 爲 portfolioMargin。user 可以是主用戶或子帳戶。 |
| 17 | 結果操作 | (encodedOperation, question, outcome, wei) | (uint8, uint32, uint32, uint64) | encodedOperation |
下面是一個示例合約,它代表自己的合約地址在 HyperCore 上發送操作,同時演示了在 Solidity 中構造編碼操作的一種方式。
contract CoreWriterCaller {
function sendUsdClassTransfer(uint64 ntl, bool toPerp) external {
bytes memory encodedAction = abi.encode(ntl, toPerp);
bytes memory data = new bytes(4 + encodedAction.length);
data[0] = 0x01;
data[1] = 0x00;
data[2] = 0x00;
data[3] = 0x07;
for (uint256 i = 0; i < encodedAction.length; i++) {
data[4 + i] = encodedAction[i];
}
CoreWriter(0x3333333333333333333333333333333333333333).sendRawAction(data);
}
}
\ 祝建構順利。歡迎任何反饋。
{% file src="/files/4dRObNCsfvnrcSVeJXgc" %}
{% file src="/files/Nsv6L7FugL44GNDvyDq2" %}