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

Add unfreeze-contracts command to celotool #2433

Merged
merged 3 commits into from
Jan 14, 2020
Merged
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
114 changes: 114 additions & 0 deletions packages/celotool/src/cmds/unfreeze_contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* tslint:disable no-console */
import { newKit } from '@celo/contractkit'
import { eqAddress } from '@celo/utils/lib/address'
import { switchToClusterFromEnv } from 'src/lib/cluster'
import { addCeloEnvMiddleware, CeloEnvArgv } from 'src/lib/env-utils'
import { portForwardAnd } from 'src/lib/port_forward'
import yargs from 'yargs'

export const command = 'unfreeze-contracts'

export const describe = 'command for unfreezing epoch rewards and the exchange'

interface UnfreezeContractsArgv extends CeloEnvArgv {
exchange: boolean
rewards: boolean
freeze: boolean
precheck: boolean
verify: boolean
}

export const builder = (argv: yargs.Argv) => {
return addCeloEnvMiddleware(argv)
.option('exchange', {
type: 'boolean',
description: 'Affect the exchange',
default: true,
})
.option('rewards', {
type: 'boolean',
description: 'Affect epoch rewards',
default: true,
})
.option('freeze', {
type: 'boolean',
description: 'Freeze contracts instead of unfreezing',
default: false,
})
.option('precheck', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we always do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want this to be an option in case it causes some kind of issue.

type: 'boolean',
description: 'Check the contract freeze status before continuing',
default: true,
})
.option('verify', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, shouldn't we always do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want this to be an option in case it causes some kind of issue.

type: 'boolean',
description: 'Verify the contract freeze status after',
default: true,
})
}

export const handler = async (argv: UnfreezeContractsArgv) => {
await switchToClusterFromEnv()

const cb = async () => {
const kit = newKit('http://localhost:8545')
const account = (await kit.web3.eth.getAccounts())[0]
console.log(`Using account: ${account}`)
kit.defaultAccount = account

const [exchange, epochRewards] = await Promise.all([
argv.exchange ? kit._web3Contracts.getExchange() : null,
argv.rewards ? kit._web3Contracts.getEpochRewards() : null,
])

for (const [name, contract] of Object.entries({ exchange, epochRewards })) {
if (contract == null) {
continue
}

if (argv.precheck) {
const frozen = await contract.methods.frozen().call()
// console.debug(`${name}.frozen = ${frozen}`)
if (argv.freeze == frozen) {
console.error(`${name} is already ${argv.freeze ? 'frozen' : 'unfrozen'}. Skipping.`)
continue
}

const freezer = await contract.methods.freezer().call()
// console.debug(`${name}.freezer = ${freezer}`)
if (!eqAddress(freezer, account)) {
console.error(`${account} cannot freeze or unfreeze ${name}. Skipping.`)
continue
}
}

if (argv.freeze) {
console.info(`Sending freeze transaction to ${name} ...`)
await contract.methods.freeze().send({ from: account })
} else {
console.info(`Sending unfreeze transaction to ${name} ...`)
await contract.methods.unfreeze().send({ from: account })
}

if (argv.verify) {
const frozen = await contract.methods.frozen().call()
// console.debug(`${name}.frozen = ${frozen}`)
if (argv.freeze != frozen) {
console.error(
`${name} is not ${argv.freeze ? 'frozen' : 'unfrozen'}. Something went wrong.`
)
continue
}
console.info(`Succesfully ${argv.freeze ? 'froze' : 'unfroze'} ${name}`)
}
}
}

try {
await portForwardAnd(argv.celoEnv, cb)
} catch (error) {
console.error(`Unable to ${argv.freeze ? 'freeze' : 'unfreeze'} contracts on ${argv.celoEnv}`)
console.error(error)
process.exit(1)
}
}