Skip to content

Commit

Permalink
feat[contracts]: ChugSplash tooling to generate complete action bundl…
Browse files Browse the repository at this point in the history
…es from config files (#749)

* wip: Start chugsplash hardhat tooling

* docs: add some comments

* style: break storage slot compute line into two lines

* test: Add tests for hardhat tooling

* fix: use stricter env type
  • Loading branch information
smartcontracts authored and gakonst committed May 20, 2021
1 parent 8bd63e5 commit 1cad865
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/contracts/src/chugsplash/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* External Imports */
/* Imports: External */
import { fromHexString, toHexString } from '@eth-optimism/core-utils'
import { ethers } from 'ethers'
import MerkleTree from 'merkletreejs'
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/chugsplash/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Handlebars from 'handlebars'
import { ethers } from 'ethers'

type SolidityVariable =
| boolean
| string
| number
| Array<SolidityVariable>
Expand Down
56 changes: 56 additions & 0 deletions packages/contracts/src/chugsplash/hardhat-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Imports: External */
import { HardhatRuntimeEnvironment } from 'hardhat/types'

/* Imports: Internal */
import { computeStorageSlots, getStorageLayout } from './storage'
import { ChugSplashConfig, parseChugSplashConfig } from './config'
import {
ChugSplashAction,
ChugSplashActionBundle,
getChugSplashActionBundle,
} from './actions'

/**
* Generates a ChugSplash action bundle from a config file.
* @param hre Hardhat runtime environment, used to load artifacts + storage layouts.
* @param config Config file to convert into a bundle.
* @param env Environment variables to inject into the config file.
* @returns Action bundle generated from the parsed config file.
*/
export const makeActionBundleFromConfig = async (
hre: HardhatRuntimeEnvironment,
config: ChugSplashConfig,
env: {
[key: string]: string | number | boolean
} = {}
): Promise<ChugSplashActionBundle> => {
// Parse the config to replace any template variables.
const parsed = parseChugSplashConfig(config, env)

const actions: ChugSplashAction[] = []
for (const [contractName, contractConfig] of Object.entries(
parsed.contracts
)) {
const artifact = hre.artifacts.readArtifactSync(contractConfig.source)
const storageLayout = await getStorageLayout(hre, contractConfig.source)

// Add a SET_CODE action for each contract first.
actions.push({
target: contractConfig.address,
code: artifact.deployedBytecode,
})

// Add SET_STORAGE actions for each storage slot that we want to modify.
const slots = computeStorageSlots(storageLayout, contractConfig.variables)
for (const slot of slots) {
actions.push({
target: contractConfig.address,
key: slot.key,
value: slot.val,
})
}
}

// Generate a bundle from the list of actions.
return getChugSplashActionBundle(actions)
}
1 change: 1 addition & 0 deletions packages/contracts/src/chugsplash/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './actions'
export * from './config'
export * from './storage'
export * from './hardhat-tools'
Loading

0 comments on commit 1cad865

Please sign in to comment.