▶️JSON RPC Call using ETH getStorage

Task Details : Make a JSON RPC call using eth_getStorageAt() to get the first storage variable (slot #0) of any deployed smart contract

  1. Within the scripts folder, create a file called getStorage.js

  2. Let's write our getStorage.js script

const hre = require("hardhat");
const { ethers } = require("ethers");

const getStorageValue = async (rpcURL, contractAddress, slotNumber) => {
  const provider = new ethers.getDefaultProvider(rpcURL); // initial ether rpc

  try {
    const storageValue = await provider.getStorage(contractAddress, slotNumber);
    console.log(`\nResponse:`);
    console.log(`Storage Value at Slot ${slotNumber}: 0x${storageValue}`);
  } catch (error) {
    console.error("Error:", error);
  }
};

async function main() {
  const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a"; // contract address
  const slotNumber = 0; // slot number
  const rpcURL = hre.network.config.url; // get rpc from hardhat.config.js

  let printLogs = {
    contract: contractAddress,
    slotNumber: slotNumber,
    rpcURL: rpcURL,
  };

  console.log(printLogs);

  await getStorageValue(rpcURL, contractAddress, slotNumber);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
  1. Execute the following command in your terminal to run the getStorage script using the Swisstronik network

npx hardhat run scripts/getStorage.js --network swisstronik
  1. Execute the following command in your terminal to run the getStorage script using the ETH Sepolia network

npx hardhat run scripts/getStorage.js --network sepolia
  1. Execute the following command in your terminal to run the getStorage script using the Polygon Mumbai network

npx hardhat run scripts/getStorage.js --network mumbai
  1. Upon successful execution, your terminal should display the latest message you've defined in the contract 🎉

The output should like this

01. Provide an explanation of the value retrieved from the RPC call. ✅ Swisstronik Network: ✔ Value: 0xc73e7f645a2bf1365a0903afa03a2cb5029ba989df7844b0fe7751b1ba918ea4 ✔ Explanation: This hexadecimal string represents the raw data stored in the contract's first storage slot, which corresponds to the private message variable. The value 0xc73e7f645a2bf1365a0903afa03a2cb5029ba989df7844b0fe7751b1ba918ea4 encodes the current message stored in the contract. This value can change when someone calls the setMessage() function to update the message. ✅ Mumbai Network and Sepolia Network: ✔ Value: 0x0000000000000000000000000000000000000000000000000000000000000000 ✔ Explanation: These values are also hexadecimal strings, but they consist entirely of zeros. This suggests that either no meaningful data was stored in the first storage slot of the contract on these networks. Essentially, it indicates that there is no message stored in the contract on these networks, or the message is an empty string. 02. What does this retrieved value represent? Is there any difference if this RPC call is made on the Sepolia or Mumbai networks? ✅ Swisstronik Network, you are seeing the encoded or hashed representation of the current message stored in the contract's first storage slot. ✅ Mumbai and Sepolia Networks, you are seeing all zeros in the first storage slot, indicating an empty message or no message has been set on these networks for the given contract.

Last updated

Was this helpful?