<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rishidar D</title>
    <description>The latest articles on DEV Community by Rishidar D (@rishidar_d_976031705a50f9).</description>
    <link>https://dev.to/rishidar_d_976031705a50f9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3848375%2F179a17c6-8dfb-4abb-9206-745e7e5e95e5.jpg</url>
      <title>DEV Community: Rishidar D</title>
      <link>https://dev.to/rishidar_d_976031705a50f9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rishidar_d_976031705a50f9"/>
    <language>en</language>
    <item>
      <title>Storage packing in Solidity: reading packed slots from the outside</title>
      <dc:creator>Rishidar D</dc:creator>
      <pubDate>Sun, 29 Nov 2026 13:04:04 +0000</pubDate>
      <link>https://dev.to/rishidar_d_976031705a50f9/storage-packing-in-solidity-reading-packed-slots-from-the-outside-nib</link>
      <guid>https://dev.to/rishidar_d_976031705a50f9/storage-packing-in-solidity-reading-packed-slots-from-the-outside-nib</guid>
      <description>&lt;p&gt;Solidity packs small state variables into a single 32-byte slot. That is cheap on gas, and it quietly breaks every tool that assumes one variable means one slot: indexers, debuggers, and anyone reproducing a bug from a transaction trace.&lt;/p&gt;

&lt;p&gt;Here is the smallest honest demo I could write. Two &lt;code&gt;uint128&lt;/code&gt;s land at &lt;code&gt;slot 0&lt;/code&gt; offsets 0 and 16. A &lt;code&gt;uint256&lt;/code&gt; gets its own slot. The mapping/array data starts at &lt;code&gt;keccak256(abi.encode(key, slot))&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract PackedVault {
    uint128 public low;   // slot 0, offset 0
    uint128 public high;  // slot 0, offset 16
    uint256 public total; // slot 1
    mapping(address =&amp;gt; uint256) public balanceOf; // data at keccak256(abi.encode(user, uint256(2)))
    uint256[] public history; // data at keccak256(abi.encode(uint256(3)))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading it back from the outside is one &lt;code&gt;eth_getStorageAt&lt;/code&gt; per slot plus a local slice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cast storage &lt;span class="nv"&gt;$VAULT&lt;/span&gt; 0        &lt;span class="c"&gt;# low  |  high, packed into one word&lt;/span&gt;
cast storage &lt;span class="nv"&gt;$VAULT&lt;/span&gt; 1        &lt;span class="c"&gt;# total, alone&lt;/span&gt;
cast keccak &lt;span class="nv"&gt;$(&lt;/span&gt;cast abi-encode &lt;span class="s2"&gt;"f(address,uint256)"&lt;/span&gt; &lt;span class="nv"&gt;$USER&lt;/span&gt; 2&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="c"&gt;# balanceOf($USER) slot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Decoding slot 0 gives &lt;code&gt;low&lt;/code&gt; in the low 16 bytes and &lt;code&gt;high&lt;/code&gt; in the high 16. Forgetting the offset is the classic bug: a naive reader reports &lt;code&gt;low&lt;/code&gt; as the whole word and silently drops &lt;code&gt;high&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two rules that keep this honest:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Order matters.&lt;/strong&gt; Packing depends on declaration order and types; a refactor that reorders fields moves every value. Treat the layout as an ABI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only signed/unsigned integers and bytesN pack.&lt;/strong&gt; Structs and arrays start a new slot, and a mapping is always a hash, never an inline value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The harness (Foundry, 12 tests including a fuzz test over the packed pair) is at &lt;a href="https://github.com/Rishidar-lab/slotseer" rel="noopener noreferrer"&gt;github.com/Rishidar-lab/slotseer&lt;/a&gt;. It dumps raw slots on anvil so you can see the layout instead of guessing it.&lt;/p&gt;

&lt;p&gt;If you index contracts for a living, assume packing until the ABI proves otherwise.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>web3</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
