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 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); | |
| } |
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": |
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 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); | |
| } |
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 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; | |
| } | |
| } |
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 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); |
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
| 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; |
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 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, ''); | |
| } |
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
| const { ethers } = require('hardhat'); | |
| require('dotenv').config(); | |
| // ethers methods | |
| const utils = ethers.utils; | |
| const BigNumber = ethers.BigNumber; | |
| const MaxUint256 = ethers.constants.MaxUint256 |
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 getUint256(slot, contractAddress) { | |
| const paddedSlot = utils.hexZeroPad(slot, 32); | |
| const storageLocation = await ethers.provider.getStorageAt(contractAddress, paddedSlot); | |
| const storageValue = BigNumber.from(storageLocation); | |
| return storageValue; | |
| } |
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 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 = ""; |
NewerOlder