Skip to content

Instantly share code, notes, and snippets.

@amarachiugwu
Created October 18, 2022 15:54
Show Gist options
  • Select an option

  • Save amarachiugwu/f957fd1dbd784ab6b5815ee01354ee13 to your computer and use it in GitHub Desktop.

Select an option

Save amarachiugwu/f957fd1dbd784ab6b5815ee01354ee13 to your computer and use it in GitHub Desktop.
retrieve the data stored to storage by line 10 below
contract DataStore{
uint256[50] _____gap_____;
struct Data{
uint256[] numbers;
bool in__;
bytes32 key;
}
constructor() public{
returnDataSlot().key=keccak256('YEP FOUND IT');
}
function returnDataSlot() internal view returns(Data storage d){
assembly{
d.slot:=13
}
}
}
// d is binded to slot 13
// therefore the storage slot for members of the slot starts from slot 13
// uint256[] numbers is stored at storage 13 (this slot holds the length of the array)
// bool in__ stored at slot 13
// bytes32 key ocupies slot 15 since its 32 bytes and can't be pack with bool of bytes1 above
import { ethers } from "hardhat";
async function main() {
const DataStore = await ethers.getContractFactory("DataStore");
const dataStore = await DataStore.deploy();
await dataStore.deployed();
console.log(`contract deployed to ${dataStore.address}`);
const addr = dataStore.address;
let tx_ = await ethers.provider.getStorageAt(addr, 15);
console.log(`storage at 4: ${tx_}`);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment