|
| 1 | +import * as hre from 'hardhat'; |
| 2 | +import { Contract, ContractFactory, ContractFunction, Transaction } from 'ethers'; |
| 3 | +import promptSync from 'prompt-sync'; |
| 4 | + |
| 5 | +import { EnvironmentInfo, loadEnvironmentInfo } from './environment'; |
| 6 | +import { newContractFactory, waitForInput } from './helper-functions'; |
| 7 | +import { newWalletOptions, WalletOptions } from './wallet-options'; |
| 8 | + |
| 9 | +/** |
| 10 | + * GrantExecutorRole grants the `EXECUTOR` role to a given address. This function can only |
| 11 | + * be invoked by the wallet with the `DEFAULT_ADMIN_ROLE` role. |
| 12 | + **/ |
| 13 | +async function grantExecutorRole(): Promise<EnvironmentInfo> { |
| 14 | + const env = loadEnvironmentInfo(hre.network.name); |
| 15 | + const { network, multiCallDeployContractAddress } = env; |
| 16 | + const prompt = promptSync(); |
| 17 | + |
| 18 | + // Setup wallet with default admin role |
| 19 | + const wallets: WalletOptions = await newWalletOptions(env); |
| 20 | + |
| 21 | + // Attach to contract |
| 22 | + const contractName = "MultiCallDeploy"; |
| 23 | + const contractFactory: ContractFactory = await newContractFactory(wallets.getWallet(), contractName); |
| 24 | + |
| 25 | + console.log(`[${network}] Confirm contract address ${multiCallDeployContractAddress} ...`); |
| 26 | + const multiCallDeploy: Contract = await contractFactory.attach(multiCallDeployContractAddress); |
| 27 | + |
| 28 | + // Obtain the executor role reference |
| 29 | + const executorRole = await multiCallDeploy.EXECUTOR_ROLE(); |
| 30 | + console.log(`[${network}] Executor role ${executorRole}`); |
| 31 | + |
| 32 | + const newAddress = prompt(`[${network}] New address to be added to the executor role: `); |
| 33 | + console.log(`[${network}] Confirm new address ${newAddress} ...`); |
| 34 | + |
| 35 | + await waitForInput(); |
| 36 | + |
| 37 | + // Only grant the role if the wallet does not already have access to this to this role. |
| 38 | + const isExecutor = await multiCallDeploy.hasRole(executorRole, newAddress.trim()); |
| 39 | + if (!isExecutor) { |
| 40 | + const tx = await multiCallDeploy.grantExecutorRole(newAddress.trim()); |
| 41 | + await tx.wait(); |
| 42 | + console.log(`[${network}] Executor role granted to ${newAddress}`); |
| 43 | + } else { |
| 44 | + console.log(`[${network}] ${newAddress} already has the executor role`); |
| 45 | + } |
| 46 | + |
| 47 | + return env; |
| 48 | +} |
| 49 | + |
| 50 | +// Call primary function |
| 51 | +grantExecutorRole() |
| 52 | + .then((env: EnvironmentInfo) => { |
| 53 | + console.log(`[${env.network}] Grant successful...`); |
| 54 | + process.exit(0); |
| 55 | + }) |
| 56 | + .catch(err => { |
| 57 | + console.error(err.message); |
| 58 | + process.exit(1); |
| 59 | + }); |
0 commit comments