Skip to content

Commit

Permalink
feat: add change storage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BC-A committed May 12, 2024
1 parent c59150c commit f872d05
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions contracts/Storage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// contracts/Box.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Storage {
mapping(uint256 => uint256) public globalExitRootMap;

function get(uint256 key) public view returns (uint256) {
return globalExitRootMap[key];
}

function set(uint256 key, uint256 value) public {
globalExitRootMap[key] = value;
}
}
51 changes: 51 additions & 0 deletions scripts/storage_slot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");

async function main() {
const [owner] = await ethers.getSigners();
const storage = await hre.ethers.deployContract("Storage");
console.log('Deploying storage contact...');
storage.waitForDeployment();

let key = 1
let value = 10
await storage.connect(owner).set(key, value);

let getValue = await storage.connect(owner).get(1);
console.log("value: ", getValue);

let address = await storage.getAddress()

let slotP = 0
let abi = ethers.AbiCoder.defaultAbiCoder()
// uint256 and uint256 are mapping uint256 => uint256
let abiCode = abi.encode(["uint256", "uint256"], [key, slotP])
let slotId = ethers.keccak256(abiCode)
// 0xada5013122d395ba3c54772283fb069b10426056ef8ca54750cb9bb552a59e7d
console.log("slotId: ", slotId)
const slot = await network.provider.send("eth_getStorageAt", [
address, slotId
]);

console.log("slot: ", slot)

let newValue = "0x0000000000000000000000000000000000000000000000000000000000000001"
await network.provider.send("hardhat_setStorageAt", [
address, slotId, newValue
]);

let getNewValue = await storage.connect(owner).get(1);
console.log("newValue: ", getNewValue);
}

// 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;
});

0 comments on commit f872d05

Please sign in to comment.