Skip to content

Commit

Permalink
fix: replace incorrect use of the getDefaultProxyAddress function
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Nov 30, 2022
1 parent 81b8995 commit c5ec8e4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
8 changes: 8 additions & 0 deletions .changeset/mean-parents-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@chugsplash/core': patch
'@chugsplash/demo': patch
'@chugsplash/executor': patch
'@chugsplash/plugins': patch
---

Replace incorrect use of the `getDefaultProxyAddress` function
22 changes: 15 additions & 7 deletions packages/core/src/config/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ChugSplashActionBundle,
makeBundleFromActions,
} from '../actions'
import { getProxyAddress } from '../utils'
import { getDefaultProxyAddress } from '../utils'
import {
UserChugSplashConfig,
UserConfigVariable,
Expand Down Expand Up @@ -88,9 +88,11 @@ export const parseChugSplashConfig = (
for (const [referenceName, contractConfig] of Object.entries(
config.contracts
)) {
// Set the proxy address to the user-defined value if it exists, otherwise set it to the default proxy
// used by ChugSplash.
contractConfig.address =
contractConfig.address ||
getProxyAddress(config.options.projectName, referenceName)
getDefaultProxyAddress(config.options.projectName, referenceName)
contracts[referenceName] = contractConfig.address
}

Expand Down Expand Up @@ -146,6 +148,7 @@ export const makeActionBundleFromConfig = async (
): Promise<ChugSplashActionBundle> => {
// Parse the config to replace any template variables.
const parsed = parseChugSplashConfig(config, env)
console.log(parsed.contracts)

const actions: ChugSplashAction[] = []
for (const [referenceName, contractConfig] of Object.entries(
Expand Down Expand Up @@ -197,19 +200,24 @@ export const parseContractReferences = (
)) {
if (isContractReference(variable)) {
const [targetReferenceName] = Object.values(variable)
if (config.contracts[targetReferenceName] === undefined) {
const targetContractConfig = config.contracts[targetReferenceName]
if (targetContractConfig === undefined) {
throw new Error(
`Could not find a contract definition for ${targetReferenceName} in the config file for
${config.options.projectName}. Please create a contract definition for ${targetReferenceName} or
remove the reference to it in the "${variableName}" variable in your contract definition for
${referenceName}.`
)
}
// Set the variable to be the user-defined proxy address if it exists, otherwise use the
// default proxy address.
config.contracts[referenceName].variables[variableName] =
getProxyAddress(
config.options.projectName,
targetReferenceName.trim()
)
targetContractConfig.address
? targetContractConfig.address
: getDefaultProxyAddress(
config.options.projectName,
targetReferenceName.trim()
)
}
}
}
Expand Down
52 changes: 26 additions & 26 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ export const writeDeploymentArtifact = (
fs.writeFileSync(artifactPath, JSON.stringify(artifact, null, '\t'))
}

export const getProxyAddress = (
/**
* Returns the address of a default proxy used by ChugSplash, which is calculated as a function of
* the projectName and the corresponding contract's reference name. Note that a default proxy will
* NOT be used if the user defines their own proxy address in the ChugSplash config via the `proxy`
* attribute.
*
* @param projectName Name of the ChugSplash project.
* @param referenceName Reference name of the contract that corresponds to the proxy.
* @returns Address of the default EIP-1967 proxy used by ChugSplash.
*/
export const getDefaultProxyAddress = (
projectName: string,
referenceName: string
): string => {
Expand All @@ -93,14 +103,10 @@ export const checkIsUpgrade = async (
provider: ethers.providers.Provider,
parsedConfig: ParsedChugSplashConfig
): Promise<boolean | string> => {
for (const referenceName of Object.keys(parsedConfig.contracts)) {
if (
await isProxyDeployed(
provider,
parsedConfig.options.projectName,
referenceName
)
) {
for (const [referenceName, contractConfig] of Object.entries(
parsedConfig.contracts
)) {
if (await isProxyDeployed(provider, contractConfig.address)) {
return referenceName
}
}
Expand All @@ -118,21 +124,17 @@ export const checkValidUpgrade = async (
address: string
}[] = []
let proxyDetected = false
for (const referenceName of Object.keys(parsedConfig.contracts)) {
if (
await isProxyDeployed(
provider,
parsedConfig.options.projectName,
referenceName
)
) {
for (const [referenceName, contractConfig] of Object.entries(
parsedConfig.contracts
)) {
if (await isProxyDeployed(provider, contractConfig.address)) {
proxyDetected = true
const proxyAddress = getProxyAddress(
parsedConfig.options.projectName,
referenceName
)

const contract = new ethers.Contract(proxyAddress, ProxyABI, provider)
const contract = new ethers.Contract(
contractConfig.address,
ProxyABI,
provider
)

const owner = await getProxyOwner(contract)
const managerProxy = await getChugSplashManagerProxyAddress(
Expand All @@ -141,7 +143,7 @@ export const checkValidUpgrade = async (
if (owner !== managerProxy) {
requiresOwnershipTransfer.push({
name: referenceName,
address: proxyAddress,
address: contractConfig.address,
})
}
}
Expand Down Expand Up @@ -174,10 +176,8 @@ npx hardhat chugsplash-transfer-ownership>

export const isProxyDeployed = async (
provider: ethers.providers.Provider,
projectName: string,
referenceName: string
proxyAddress: string
): Promise<boolean> => {
const proxyAddress = getProxyAddress(projectName, referenceName)
return (await provider.getCode(proxyAddress)) !== '0x'
}

Expand Down
4 changes: 2 additions & 2 deletions packages/demo/chugsplash/SimpleStorage.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserChugSplashConfig } from '@chugsplash/core'
import { UserChugSplashConfig } from '@chugsplash/core'

const config: UserChugSplashConfig = {
const config: UserChugSplashConfig = {
// Configuration options for the project:
options: {
projectName: 'My First Project',
Expand Down
11 changes: 4 additions & 7 deletions packages/executor/src/utils/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CanonicalChugSplashConfig,
CompilerInput,
getChugSplashManagerProxyAddress,
getProxyAddress,
parseChugSplashConfig,
getConstructorArgs,
chugsplashFetchSubtask,
Expand Down Expand Up @@ -101,7 +100,9 @@ export const verifyChugSplashConfig = async (
console.error(err)
}

for (const referenceName of Object.keys(canonicalConfig.contracts)) {
for (const [referenceName, contractConfig] of Object.entries(
canonicalConfig.contracts
)) {
const artifact = artifacts[referenceName]
const { abi, contractName, sourceName, compilerOutput } = artifact
const { constructorArgValues } = getConstructorArgs(
Expand All @@ -115,10 +116,6 @@ export const verifyChugSplashConfig = async (
const implementationAddress = await ChugSplashManager.implementations(
referenceName
)
const proxyAddress = getProxyAddress(
canonicalConfig.options.projectName,
referenceName
)

const { input, solcVersion } = canonicalConfig.inputs.find(
(compilerInput) =>
Expand Down Expand Up @@ -154,7 +151,7 @@ export const verifyChugSplashConfig = async (
await linkProxyWithImplementation(
etherscanApiEndpoints,
etherscanApiKey,
proxyAddress,
contractConfig.address,
implementationAddress,
contractName
)
Expand Down
12 changes: 3 additions & 9 deletions packages/plugins/src/hardhat/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import yesno from 'yesno'
import { ethers } from 'ethers'
import {
ParsedChugSplashConfig,
getProxyAddress,
isEmptyChugSplashConfig,
registerChugSplashProject,
ChugSplashBundleState,
Expand Down Expand Up @@ -284,18 +283,13 @@ ${configsWithFileNames.map(

const { config: cfg } = configsWithFileNames[0]

if (
(await isProxyDeployed(
hre.ethers.provider,
cfg.options.projectName,
referenceName
)) === false
) {
const proxyAddress = cfg.contracts[referenceName].address
if ((await isProxyDeployed(hre.ethers.provider, proxyAddress)) === false) {
throw new Error(`You must first deploy ${referenceName}.`)
}

const Proxy = new ethers.Contract(
getProxyAddress(cfg.options.projectName, referenceName),
proxyAddress,
new ethers.utils.Interface(
getContractArtifact(cfg.contracts[referenceName].contract).abi
),
Expand Down

0 comments on commit c5ec8e4

Please sign in to comment.