Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ChugSplash config parsing via Handlebars #739

Merged
merged 3 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"@openzeppelin/contracts-upgradeable": "^3.3.0",
"@typechain/hardhat": "^1.0.1",
"ganache-core": "^2.13.2",
"glob": "^7.1.6"
"glob": "^7.1.6",
"handlebars": "^4.7.7"
},
"devDependencies": {
"@eth-optimism/hardhat-ovm": "^0.1.0",
Expand Down
94 changes: 94 additions & 0 deletions packages/contracts/src/chugsplash/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Imports: External */
import * as Handlebars from 'handlebars'
import { ethers } from 'ethers'

type SolidityVariable =
| string
| number
| Array<SolidityVariable>
| {
[name: string]: SolidityVariable
}

export interface ChugSplashConfig {
contracts: {
[name: string]: {
address: string
source: string
variables?: {
[name: string]: SolidityVariable
}
}
}
}

/**
* Parses a ChugSplash config file by replacing template values.
* @param config Unparsed config file to parse.
* @param env Environment variables to inject into the file.
* @return Parsed config file with template variables replaced.
*/
export const parseChugSplashConfig = (
config: ChugSplashConfig,
env: any = {}
): ChugSplashConfig => {
config.contracts = config.contracts || {}

const contracts = {}
for (const [contractName, contractConfig] of Object.entries(
config.contracts
)) {
// Block people from accidentally using templates in contract names.
if (contractName.includes('{') || contractName.includes('}')) {
throw new Error(
`cannot use template strings in contract names: ${contractName}`
)
}

// Block people from accidentally using templates in contract names.
if (
contractConfig.source.includes('{') ||
contractConfig.source.includes('}')
) {
throw new Error(
`cannot use template strings in contract source names: ${contractConfig.source}`
)
}

// Make sure addresses are fixed and are actually addresses.
if (!ethers.utils.isAddress(contractConfig.address)) {
throw new Error(
`contract address is not a valid address: ${contractConfig.address}`
)
}

contracts[contractName] = contractConfig.address
}

return JSON.parse(
Handlebars.compile(JSON.stringify(config))({
env: new Proxy(env, {
get: (target, prop) => {
const val = target[prop]
if (val === undefined) {
throw new Error(
`attempted to access unknown env value: ${prop as any}`
)
}
return val
},
}),
contracts: new Proxy(contracts, {
get: (target, prop) => {
const val = target[prop]
if (val === undefined) {
throw new Error(
`attempted to access unknown contract: ${prop as any}`
)
}
return val
},
}),
})
)
}
1 change: 1 addition & 0 deletions packages/contracts/src/chugsplash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config'
1 change: 1 addition & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './contract-defs'
export { getLatestStateDump, StateDump } from './contract-dumps'
export * from './contract-deployment'
export * from './predeploys'
export * from './chugsplash'
Loading