Skip to content

Instantly share code, notes, and snippets.

@daopunk
Last active June 30, 2022 20:50
Show Gist options
  • Select an option

  • Save daopunk/646b35cfe220cb816e78c27b13ae017b to your computer and use it in GitHub Desktop.

Select an option

Save daopunk/646b35cfe220cb816e78c27b13ae017b to your computer and use it in GitHub Desktop.
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 = "";
for (let i=1; i <= totalSlots; i++) {
const stringDataPerSlot = await ethers.provider.getStorageAt(contractAddress, storageLocation);
str = str.concat(utils.toUtf8String(stringDataPerSlot));
storageLocation = BigNumber.from(baseSlot).add(i).toHexString();
}
return str.replace(/\x00/g, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment