From 77b27bd89ba182ef8273d36373475a68165d5c14 Mon Sep 17 00:00:00 2001 From: KaralinaVpered <162497874+KaralinaVpered@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:24:23 +0100 Subject: [PATCH] Update make-slots.js --- scripts/make-slots.js | 49 +++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/scripts/make-slots.js b/scripts/make-slots.js index 27a708f..41112b0 100644 --- a/scripts/make-slots.js +++ b/scripts/make-slots.js @@ -1,19 +1,36 @@ -const ethers = require('ethers') +// Import the ethers library +import { utils, BigNumber } from 'ethers'; +/** + * Creates a storage object from a given accounts object. + * Each account's address and balance are used to generate a storage key-value pair. + * @param {Object} accounts - An object where keys are account addresses and values are account balances. + * @returns {Object} A storage object with hexadecimal storage keys and their corresponding balances. + */ const makeStorageFromAccounts = (accounts) => { - const storage = {} - for (const [address, balance] of Object.entries(accounts)) { - const preimage = ethers.utils.hexConcat([ - ethers.utils.hexZeroPad(address, 32), - ethers.utils.hexZeroPad('0x00', 32), - ]) - const key = ethers.utils.keccak256(preimage) - const val = ethers.BigNumber.from(balance).toHexString() - storage[key] = val - } - return storage -} + const storage = {}; -module.exports = { - makeStorageFromAccounts -} + // Iterate over each account + for (const [address, balance] of Object.entries(accounts)) { + // Concatenate and pad the address with zeros + const preimage = utils.hexConcat([ + utils.hexZeroPad(address, 32), + utils.hexZeroPad('0x00', 32), + ]); + + // Generate a storage key using keccak256 + const key = utils.keccak256(preimage); + + // Convert the balance to a hexadecimal string + const val = BigNumber.from(balance).toHexString(); + + // Store the balance in the storage object with the generated key + storage[key] = val; + } + + // Return the populated storage object + return storage; +}; + +// Export the function for use in other modules +export { makeStorageFromAccounts };