Skip to content

Instantly share code, notes, and snippets.

@daopunk
Created June 30, 2022 21:42
Show Gist options
  • Select an option

  • Save daopunk/064478fac1a98a8fbf38188ec0c785a4 to your computer and use it in GitHub Desktop.

Select an option

Save daopunk/064478fac1a98a8fbf38188ec0c785a4 to your computer and use it in GitHub Desktop.
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":
return getBytePackedVar(itemSlot, contractAddress, 0, 32);
case "number":
return getUint256(itemSlot, contractAddress);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment