Created
June 30, 2022 21:42
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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