Skip to content

Instantly share code, notes, and snippets.

@KaiCode2
Created February 2, 2024 07:31
Show Gist options
  • Save KaiCode2/ef31f9641e79efb51d70414159757544 to your computer and use it in GitHub Desktop.
Save KaiCode2/ef31f9641e79efb51d70414159757544 to your computer and use it in GitHub Desktop.
WIP: Deploys contracts to deterministic addresses based on their names
import { ethers, artifacts, deployments, run, getNamedAccounts, network } from 'hardhat'
import { setCode, takeSnapshot, getStorageAt, setStorageAt } from '@nomicfoundation/hardhat-network-helpers'
import { Contracts } from '@/utils'
import { Signer } from 'ethers'
import fs from 'fs'
/*
This script deploys the contracts to hardhat/anvil chain such that their addresses can be derived from the keccak256 hash of their name.
NOTE: This is still a work in progress and just work for the Issuers contract for now.
NOTE: Only 'inplace' storage slots work correctly. While the location of 'mapping' storage slot are transferred, any values stored in them currently are not.
The script does the following:
1. Snapshot the empty chain state
2. Runs hardhat deploy
3. Stores each contract's deployed bytecode and storage layout
4. Stores each storage slot of the deployed contracts
5. Restores the snapshot
6. Writes the deployments bytecode of each contracts to their expected deterministic address
7. Writes each contracts' storage slots to match the post-deployment state
*/
async function main() {
const deploymentAddress = ethers.utils.solidityKeccak256(['string'], [Contracts.issuers]).slice(0, 42)
console.log('deploymentAddress:', deploymentAddress)
const { owner, deployer, authorizer } = await getNamedAccounts()
const deployerSigner = (await ethers.getImpersonatedSigner(deployer)) as unknown as Signer
const snapshot = await takeSnapshot()
const IssuersFactory = await ethers.getContractFactory(Contracts.issuers, deployerSigner)
// 1. Use artifacts
let storageLayout: any[]
const issuersArtifact = await artifacts.readArtifact(Contracts.issuers)
const buildInfos = await artifacts.getBuildInfoPaths()
buildInfos.forEach((source, idx) => {
const artifactBuffer: Buffer = fs.readFileSync(source)
let data = JSON.parse(artifactBuffer.toString())
let contractBuildOutput = data.output?.contracts?.[issuersArtifact.sourceName]?.[issuersArtifact.contractName]
if (contractBuildOutput) {
storageLayout = contractBuildOutput.storageLayout?.storage
}
})
if (!storageLayout) {
throw new Error('Storage layout not found')
}
// 2. Use dry-run deployment
const issuersDeployment = await IssuersFactory.deploy(owner, authorizer)
const deployedIssuers = await issuersDeployment.deployed()
const initialOwner = await deployedIssuers.owner()
let storageSnapshot = await Promise.all(
storageLayout.map((storageSlot) => {
return getStorageAt(deployedIssuers.address, parseInt(storageSlot.slot)).then((slotValue) => {
return { slot: parseInt(storageSlot.slot), value: slotValue, label: storageSlot.label, type: storageSlot.type }
})
})
)
const issuersCode = await ethers.provider.getCode(deployedIssuers.address)
await setCode(deploymentAddress, issuersCode)
// 3. Reset snapshot
await snapshot.restore()
// 4. Copy code and storage to expected address
await setCode(deploymentAddress, issuersCode)
await Promise.all(
storageSnapshot.map((slot) => {
return setStorageAt(deploymentAddress, slot.slot, slot.value)
})
)
let issuers = IssuersFactory.attach(deploymentAddress)
console.log('owner: ', await issuers.owner())
console.log('original owner: ', initialOwner)
console.log('is a mathch: ', initialOwner === owner)
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment