Skip to content

Commit

Permalink
fix(pg): add a chugsplash-cancel hardhat task to cancel active bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Nov 17, 2022
1 parent dcea9b7 commit 8478b24
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-colts-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/plugins': patch
---

Add a chugsplash-cancel Hardhat task to cancel active bundles
87 changes: 87 additions & 0 deletions packages/plugins/src/hardhat/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
displayDeploymentTable,
getChugSplashManagerProxyAddress,
getChugSplashManager,
getProjectOwnerAddress,
} from '@chugsplash/core'
import {
ChugSplashManagerABI,
Expand Down Expand Up @@ -87,6 +88,7 @@ export const TASK_TEST_REMOTE_EXECUTION = 'test-remote-execution'
export const TASK_FUND = 'fund'
export const TASK_CHUGSPLASH_APPROVE = 'chugsplash-approve'
export const TASK_CHUGSPLASH_STATUS = 'chugsplash-status'
export const TASK_CHUGSPLASH_CANCEL = 'chugsplash-cancel'

export const bundleLocalSubtask = async (args: {
parsedConfig: ChugSplashConfig
Expand Down Expand Up @@ -1202,3 +1204,88 @@ task(TASK_RUN)
await runSuper(args)
}
)

export const chugsplashCancelTask = async (
args: {
configPath: string
silent: boolean
},
hre: HardhatRuntimeEnvironment
) => {
const { configPath, silent } = args

const provider = hre.ethers.provider
const signer = provider.getSigner()
const parsedConfig = loadParsedChugSplashConfig(configPath)
const projectName = parsedConfig.options.projectName

const spinner = ora({ isSilent: silent })
spinner.start(`Cancelliing ${projectName} on ${hre.network.name}.`)

if (!(await isProjectRegistered(signer, projectName))) {
errorProjectNotRegistered(
await getChainId(provider),
hre.network.name,
configPath
)
}

const projectOwnerAddress = await getProjectOwnerAddress(
provider,
projectName
)
if (projectOwnerAddress !== (await signer.getAddress())) {
throw new Error(`Project is owned by: ${projectOwnerAddress}.
You attempted to cancel the project using the address: ${await signer.getAddress()}`)
}

// Get the bundle info by calling the commit subtask locally (which doesn't publish anything to
// IPFS).
const { bundleId } = await chugsplashCommitSubtask(
{
parsedConfig,
ipfsUrl: '',
commitToIpfs: false,
noCompile: true,
},
hre
)

const ChugSplashManager = getChugSplashManager(signer, projectName)

const bundleState: ChugSplashBundleState = await ChugSplashManager.bundles(
bundleId
)

if (bundleState.status !== ChugSplashBundleStatus.APPROVED) {
throw new Error(
`Project is not active. Current project state: ${
ChugSplashBundleStatus[bundleState.status]
}`
)
}

await (await ChugSplashManager.cancelActiveChugSplashBundle()).wait()

spinner.succeed(`Cancelled ${projectName} on ${hre.network.name}.`)
spinner.start(`Refunding the project owner...`)

const prevOwnerBalance = await signer.getBalance()
await (await ChugSplashManager.withdrawOwnerETH()).wait()
const refund = (await signer.getBalance()).sub(prevOwnerBalance)

spinner.succeed(
`Refunded ${ethers.utils.formatEther(
refund
)} ETH to the project owner, ${await signer.getAddress()}.`
)
}

task(TASK_CHUGSPLASH_CANCEL)
.setDescription('Cancel an active ChugSplash project.')
.addFlag('silent', "Hide all of ChugSplash's output")
.addPositionalParam(
'configPath',
'Path to the ChugSplash config file to cancel'
)
.setAction(chugsplashCancelTask)

0 comments on commit 8478b24

Please sign in to comment.