Skip to content

Commit

Permalink
refactor: move chugsplash into its own package (#948)
Browse files Browse the repository at this point in the history
* wip: start breaking out chugsplash

* remove old chugsplash content

* chore: add changeset

* update dependency locations

* add chugsplash dep to contracts

* add missing deps

* fix a silly typo

* correctly export everything
  • Loading branch information
smartcontracts authored May 24, 2021
1 parent 466827d commit 9c4afc1
Show file tree
Hide file tree
Showing 30 changed files with 16,576 additions and 776 deletions.
6 changes: 6 additions & 0 deletions .changeset/orange-cycles-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@eth-optimism/contracts': minor
'@eth-optimism/chugsplash': patch
---

Move chugsplash tooling into its own package
1 change: 1 addition & 0 deletions packages/chugsplash/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
1 change: 1 addition & 0 deletions packages/chugsplash/.prettierrc.json
22 changes: 22 additions & 0 deletions packages/chugsplash/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright 2020-2021 Optimism

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions packages/chugsplash/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HardhatUserConfig } from 'hardhat/config'

import '@nomiclabs/hardhat-ethers'
import '@nomiclabs/hardhat-waffle'

const config: HardhatUserConfig = {
paths: {
sources: './test/contracts',
},
solidity: {
version: '0.7.6',
settings: {
outputSelection: {
'*': {
'*': ['storageLayout'],
},
},
},
},
}

export default config
40 changes: 40 additions & 0 deletions packages/chugsplash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@eth-optimism/chugsplash",
"version": "0.1.0",
"main": "dist/index",
"files": [
"dist/**/*.js"
],
"types": "dist/index",
"author": "Optimism PBC",
"license": "MIT",
"scripts": {
"build": "tsc -p ./tsconfig.build.json",
"test": "npx hardhat test",
"lint": "yarn lint:fix && yarn lint:check",
"lint:fix": "yarn run lint:fix:typescript",
"lint:fix:typescript": "prettier --config .prettierrc.json --write \"hardhat.config.ts\" \"{src,test}/**/*.ts\"",
"lint:check": "tslint --format stylish --project .",
"clean": "rm -rf ./dist ./tsconfig.build.tsbuildinfo"
},
"dependencies": {
"@eth-optimism/core-utils": "^0.4.3",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.2.0",
"handlebars": "^4.7.7",
"hardhat": "^2.3.0",
"lodash": "^4.17.21",
"merkletreejs": "^0.2.18",
"semver": "^7.3.5"
},
"devDependencies": {
"@types/chai": "^4.2.18",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^8.2.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"mocha": "^8.4.0"
}
}
File renamed without changes.
16 changes: 16 additions & 0 deletions packages/chugsplash/src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Imports: External */
import hre from 'hardhat'

// TODO
export type ContractArtifact = any
export type BuildInfo = any

export const getContractArtifact = async (
name: string
): Promise<ContractArtifact> => {
return hre.artifacts.readArtifactSync(name)
}

export const getBuildInfo = async (name: string): Promise<BuildInfo> => {
return hre.artifacts.getBuildInfo(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import * as Handlebars from 'handlebars'
import { ethers } from 'ethers'

/* Imports: Internal */
import { computeStorageSlots, getStorageLayout } from './storage'
import {
ChugSplashAction,
ChugSplashActionBundle,
getChugSplashActionBundle,
} from './actions'
import { getContractArtifact } from './artifacts'

type SolidityVariable =
| boolean
| string
Expand Down Expand Up @@ -107,3 +116,46 @@ export const parseChugSplashConfig = (
})
)
}

/**
* Generates a ChugSplash action bundle from a config file.
* @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 (
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 = await getContractArtifact(contractConfig.source)
const storageLayout = await getStorageLayout(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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './actions'
export * from './artifacts'
export * from './config'
export * from './storage'
export * from './hardhat-tools'
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { fromHexString, remove0x } from '@eth-optimism/core-utils'
import { BigNumber, ethers } from 'ethers'
import semver from 'semver'
import { getBuildInfo, getContractArtifact } from './artifacts'

// Represents the JSON objects outputted by the Solidity compiler that describe the structure of
// state within the contract. See
Expand Down Expand Up @@ -66,13 +67,10 @@ const padHexSlotValue = (val: string, offset: number): string => {
* @return Storage layout object from the compiler output.
*/
export const getStorageLayout = async (
hre: any, //HardhatRuntimeEnvironment,
name: string
): Promise<SolidityStorageLayout> => {
const { sourceName, contractName } = hre.artifacts.readArtifactSync(name)
const buildInfo = await hre.artifacts.getBuildInfo(
`${sourceName}:${contractName}`
)
const { sourceName, contractName } = await getContractArtifact(name)
const buildInfo = await getBuildInfo(`${sourceName}:${contractName}`)
const output = buildInfo.output.contracts[sourceName][contractName]

if (!semver.satisfies(buildInfo.solcVersion, '>=0.4.x <0.9.x')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from '../setup'
import { expect } from './setup'

/* Imports: External */
import { ethers } from 'hardhat'
import hre from 'hardhat'
import { Contract } from 'ethers'
import MerkleTree from 'merkletreejs'
import { fromHexString } from '@eth-optimism/core-utils'
Expand All @@ -11,9 +11,11 @@ import {
getChugSplashActionBundle,
getActionHash,
ChugSplashAction,
} from '../../src'
} from '../src'

describe('ChugSplash action bundling', () => {
const ethers = (hre as any).ethers // as Ethers (???)

let Helper_ChugSplashMock: Contract
before(async () => {
Helper_ChugSplashMock = await (
Expand Down
Loading

0 comments on commit 9c4afc1

Please sign in to comment.