/ 工具
■/ 工具搜尋
109 款工具
/ 合約 Storage Slot 計算器
/ 選定的工具
■合約 Storage Slot 計算器 - 免費 Web3 工具
計算 Solidity 智能合約中 mapping 與陣列的 storage slot
■ 01
甚麼是儲存槽?
在以太坊智慧合約中,儲存被組織成 32 位元組的槽。每個狀態變數佔用一個特定的槽,而 mapping 則使用依鍵與基底槽編號計算出的槽。
■ 02
運作原理是甚麼?
對於 mapping,儲存槽的計算方式為:keccak256(key . slot),其中 key 是 mapping 的鍵(填補至 32 位元組),slot 是 mapping 的基底槽編號。// 範例:計算 balances[user] 的槽 con…
■ 03
範例情境
涵蓋 ERC-20 餘額 mapping、NFT 擁有者 mapping(uint256 鍵)與巢狀 mapping,讓你能快速比較常見的輸入與輸出。
/ 相關工具
/ 程式碼
■選定工具的原始碼,與右側的即時版本並列顯示。
/ 原始碼
TypeScript
typescript
// npm install viem
import { keccak256, encodePacked, pad, toHex } from 'viem';
// Calculate storage slot for mapping
function getMappingSlot(key: string, slotNumber: number): string {
let keyBytes: `0x${string}`;
// Handle address (20 bytes) or uint256 (32 bytes)
if (key.startsWith('0x')) {