Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions scripts/make-slots.js
Original file line number Diff line number Diff line change
@@ -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 };