Skip to content

Commit

Permalink
fix(pg,demo): update demo package to reflect latest getContract fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
sam-goldman committed Feb 3, 2023
1 parent 7121460 commit fd98872
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-fans-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@chugsplash/demo': patch
'@chugsplash/plugins': patch
---

Update demo package to reflect latest `getContract` function
2 changes: 1 addition & 1 deletion packages/demo/chugsplash/hello-chugsplash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UserChugSplashConfig } from '@chugsplash/core'

const config: UserChugSplashConfig = {
options: {
projectName: 'Test Refactor',
projectName: 'Hello ChugSplash',
},
contracts: {
MyFirstContract: {
Expand Down
5 changes: 4 additions & 1 deletion packages/demo/test/HelloChugSplash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ describe('HelloChugSplash', () => {
// You must reset your ChugSplash deployments to their initial state here
await chugsplash.reset()

MyFirstContract = await chugsplash.getContract('MyFirstContract')
MyFirstContract = await chugsplash.getContract(
'Hello ChugSplash',
'MyFirstContract'
)
})

it('initializes correctly', async () => {
Expand Down
49 changes: 27 additions & 22 deletions packages/plugins/src/hardhat/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,53 +104,58 @@ export const deployAllChugSplashConfigs = async (

export const getContract = async (
hre: HardhatRuntimeEnvironment,
provider: ethers.providers.JsonRpcProvider,
referenceName: string,
projectName: string
projectName: string,
referenceName: string
): Promise<ethers.Contract> => {
if ((await getChainId(provider)) !== 31337) {
if ((await getChainId(hre.ethers.provider)) !== 31337) {
throw new Error('Only the Hardhat Network is currently supported.')
}
const configsWithFileNames: {
userConfig: UserChugSplashConfig
configFileName: string
}[] = await fetchFilesRecursively(hre.config.paths.chugsplash)
const userConfigs: UserChugSplashConfig[] = fetchFilesRecursively(
hre.config.paths.chugsplash
)
.filter((configFileName) => {
return !isEmptyChugSplashConfig(configFileName)
})
.map((configFileName) => {
const userConfig = readUserChugSplashConfig(configFileName)
return { configFileName, userConfig }
return readUserChugSplashConfig(configFileName)
})
.filter(({ userConfig }) => {
.filter((userCfg) => {
return (
Object.keys(userConfig.contracts).includes(referenceName) &&
userConfig.options.projectName === projectName
Object.keys(userCfg.contracts).includes(referenceName) &&
userCfg.options.projectName === projectName
)
})

// TODO: Make function `getContract(projectName, referenceName)` and change this error message.
if (configsWithFileNames.length < 1) {
throw new Error(`Cannot find a config file containing ${referenceName}.`)
if (userConfigs.length === 0) {
throw new Error(
`Cannot find a project named "${projectName}" that contains the reference name "${referenceName}".`
)
}

if (userConfigs.length > 1) {
throw new Error(
`Multiple projects named "${projectName}" contain the reference name "${referenceName}"\n` +
`Please merge these projects or change one of the project names.`
)
}

const { userConfig: userCfg } = configsWithFileNames[0]
const userConfig = userConfigs[0]

const proxyAddress =
userCfg.contracts[referenceName].externalProxy ||
getDefaultProxyAddress(userCfg.options.projectName, referenceName)
userConfig.contracts[referenceName].externalProxy ||
getDefaultProxyAddress(userConfig.options.projectName, referenceName)
if ((await isContractDeployed(proxyAddress, hre.ethers.provider)) === false) {
throw new Error(`You must first deploy ${referenceName}.`)
throw new Error(`The proxy for ${referenceName} has not been deployed.`)
}

const Proxy = new ethers.Contract(
proxyAddress,
new ethers.utils.Interface(
hre.artifacts.readArtifactSync(
userCfg.contracts[referenceName].contract
userConfig.contracts[referenceName].contract
).abi
),
provider.getSigner()
hre.ethers.provider.getSigner()
)

return Proxy
Expand Down
15 changes: 5 additions & 10 deletions packages/plugins/src/hardhat/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ declare module 'hardhat/types/runtime' {
chugsplash: {
reset: () => Promise<void>
getContract: (
name: string,
projectName: string
projectName: string,
referenceName: string
) => Promise<ethers.Contract>
}
}
Expand All @@ -52,15 +52,10 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => {
await resetChugSplashDeployments(hre)
},
getContract: async (
name: string,
projectName: string
projectName: string,
referenceName: string
): Promise<ethers.Contract> => {
const contract = await getContract(
hre,
hre.ethers.provider,
name,
projectName
)
const contract = await getContract(hre, projectName, referenceName)
return contract
},
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/src/sample-project/sample-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('HelloChugSplash', () => {
// You must reset your ChugSplash deployments to their initial state here
await chugsplash.reset()
MyFirstContract = await chugsplash.getContract('MyFirstContract')
MyFirstContract = await chugsplash.getContract('Hello ChugSplash', 'MyFirstContract')
})
it('initializes correctly', async () => {
Expand All @@ -34,7 +34,7 @@ describe('HelloChugSplash', () => {
// You must reset your ChugSplash deployments to their initial state here
await chugsplash.reset()
MyFirstContract = await chugsplash.getContract('MyFirstContract')
MyFirstContract = await chugsplash.getContract('Hello ChugSplash', 'MyFirstContract')
})
it('initializes correctly', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/test/Storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe('Storage', () => {
// Reset to the initial deployment state
await chugsplash.reset()

MyStorage = await chugsplash.getContract('MyStorage', 'My First Project')
MyStorage = await chugsplash.getContract('My First Project', 'MyStorage')
MySimpleStorage = await chugsplash.getContract(
'MySimpleStorage',
'My First Project'
'My First Project',
'MySimpleStorage'
)
})

Expand Down

0 comments on commit fd98872

Please sign in to comment.