Skip to content

Instantly share code, notes, and snippets.

View daopunk's full-sized avatar
👽

daopunk daopunk

👽
View GitHub Profile
@daopunk
daopunk / readEthStorageNestedMapping.js
Last active June 30, 2022 22:10
getMappingItem to access nested mapping elements in struct in Ethereum state with ethers.js
async function getNestedMappingStruct(slot, contractAddress, key, item, nestedKey) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const paddedKey = utils.hexZeroPad(key, 32);
const itemSlot1 = utils.keccak256(paddedKey + paddedSlot.slice(2));
const itemSlot = BigNumber.from(itemSlot1).add(item).toHexString();
const paddednestedKey = utils.hexZeroPad(nestedKey, 32);
const itemNestedSlot = utils.keccak256(paddednestedKey + itemSlot.slice(2));
return getUint256(itemNestedSlot, contractAddress);
}
@daopunk
daopunk / readEthStorageMappingStruct.js
Created June 30, 2022 21:42
getMappingItem to access mapping elements in struct in Ethereum state with ethers.js
async function getMappingStruct(slot, contractAddress, key, item, type) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const paddedKey = utils.hexZeroPad(key, 32);
const itemSlot1 = utils.keccak256(paddedKey + paddedSlot.slice(2));
const itemSlot = BigNumber.from(itemSlot1).add(item).toHexString();
switch (type) {
case "string":
return await getShortStr(itemSlot, contractAddress);
case "bytes":
@daopunk
daopunk / readEthStorageMappingItem.js
Created June 30, 2022 21:41
getMappingItem to access mapping elements in Ethereum state with ethers.js
async function getMappingItem(slot, contractAddress, key) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const paddedKey = utils.hexZeroPad(key, 32);
const itemSlot = utils.keccak256(paddedKey + paddedSlot.slice(2));
return await getUint256(itemSlot, contractAddress);
}
@daopunk
daopunk / readEthStorageArrayItem.js
Created June 30, 2022 21:35
getArrayItem to access array items in Ethereum state with ethers.js
async function getArrayItem(slot, contractAddress, item, byteSize) {
const hashedSlot = utils.keccak256(utils.hexZeroPad(slot, 32));
const itemsPerSlot = 32 / byteSize;
let itemPos = item;
for (let s=1; s<item; s++) {
if (item >= itemsPerSlot) {
itemPos - itemsPerSlot;
}
}
@daopunk
daopunk / readEthStorageBytePacked.js
Created June 30, 2022 21:17
getBytePackedVar to access variables packed into 32 byte slot in Ethereum state with ethers.js
async function getBytePackedVar(slot, contractAddress, byteShift, byteSize) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const storageLocation = await ethers.provider.getStorageAt(contractAddress, paddedSlot);
let result = "";
let altByteSize = 0;
let altByteShift = 0;
let check = false;
if (byteSize <= 6) {
return BigNumber.from(storageLocation).shr(byteShift * 4).mask(byteSize * 4 * 2).toNumber().toString(16);
@daopunk
daopunk / StorageEx.sol
Last active June 30, 2022 20:56
A storage example solidity smart contract for reference to Medium article: https://medium.com/@cryptoAnarchy
contract StorageEx {
enum Vote { Absent, Yes, No }
// slot 0x0 Big Endian (first byte stored first)
bytes2 a = 0xAce0;
bytes4 b = 0xBebeAce0;
bytes4 c = 0xCafeBebe;
bytes6 d = 0xDebeCafeBebe;
bytes8 e = 0xEbeeDebeCafeBebe;
bytes8 f = 0xFebeeAceCafeBebe;
@daopunk
daopunk / readEthStorageShortString.js
Last active June 30, 2022 20:49
getShortStr to access <=32 byte strings in Ethereum state with ethers.js
async function getShortStr(slot, contractAddress) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const storageLocation = await ethers.provider.getStorageAt(contractAddress, paddedSlot);
const storageValue = BigNumber.from(storageLocation);
const stringData = utils.toUtf8String(
storageValue.and(MaxUint256.sub(255)).toHexString()
);
return stringData.replace(/\x00/g, '');
}
@daopunk
daopunk / readEthStorageEthersConstants.js
Created June 30, 2022 20:20
Reusable ethers.js methods for cleaner code
const { ethers } = require('hardhat');
require('dotenv').config();
// ethers methods
const utils = ethers.utils;
const BigNumber = ethers.BigNumber;
const MaxUint256 = ethers.constants.MaxUint256
@daopunk
daopunk / readEthStorageNumber.js
Created June 30, 2022 20:19
getUint256 function to access 32 byte numbers in Ethereum state with ethers.js
async function getUint256(slot, contractAddress) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const storageLocation = await ethers.provider.getStorageAt(contractAddress, paddedSlot);
const storageValue = BigNumber.from(storageLocation);
return storageValue;
}
@daopunk
daopunk / readEthStorageLongString.js
Last active June 30, 2022 20:50
getLongStr to access >32 byte strings in Ethereum state with ethers.js
async function getLongStr(slot, contractAddress) {
const paddedSlot = utils.hexZeroPad(slot, 32);
const storageReference = await ethers.provider.getStorageAt(contractAddress, paddedSlot);
const baseSlot = utils.keccak256(paddedSlot);
const sLength = BigNumber.from(storageReference).shr(1).toNumber();
const totalSlots = Math.ceil(sLength / 32);
let storageLocation = BigNumber.from(baseSlot).toHexString();
let str = "";