UNPKG

@holographxyz/cli

Version:
96 lines (95 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const inquirer = tslib_1.__importStar(require("inquirer")); const core_1 = require("@oclif/core"); const color_1 = tslib_1.__importDefault(require("@oclif/color")); const bignumber_1 = require("@ethersproject/bignumber"); const units_1 = require("@ethersproject/units"); const Table = require('cli-table3'); const core_chain_service_1 = tslib_1.__importDefault(require("../../services/core-chain-service")); const operator_chain_service_1 = tslib_1.__importDefault(require("../../services/operator-chain-service")); const config_1 = require("../../utils/config"); const network_monitor_1 = require("../../utils/network-monitor"); /** * Unbond * Description: Unbond an operator from pod. */ class Unbond extends core_1.Command { static description = 'Un-bond an operator from a pod'; static examples = ['$ <%= config.bin %> <%= command.id %>']; networkMonitor; async getBondInfoFromNetwork(networkOption) { const network = networkOption.value; core_1.CliUx.ux.action.start(`Loading ${networkOption.name} network RPC provider`); await this.networkMonitor.run(true); core_1.CliUx.ux.action.stop(); // Setup the contracts and chain services const coreChainService = new core_chain_service_1.default(network, this.networkMonitor); await coreChainService.initialize(); const operatorContract = await coreChainService.getOperator(); const operatorChainService = new operator_chain_service_1.default(network, this.networkMonitor, operatorContract); const operator = operatorChainService.operator; const bondedAmount = await operator.getBondedAmount(coreChainService.wallet.address); return { networkOption, bondedAmount, operatorChainService, }; } async run() { this.log('Loading user configurations...'); const { userWallet, configFile, supportedNetworksOptions } = await (0, config_1.ensureConfigFileIsValid)(this.config.configDir, undefined, true); this.networkMonitor = new network_monitor_1.NetworkMonitor({ parent: this, configFile: configFile, networks: supportedNetworksOptions.map(networkOption => networkOption.value), debug: this.debug, userWallet: userWallet, verbose: false, }); const networksBondInfo = []; // instantiate const table = new Table({ head: ['Network', 'Status', 'Bonded Amount'], }); for (const networkOption of supportedNetworksOptions) { const info = await this.getBondInfoFromNetwork(networkOption); networksBondInfo.push(info); const status = info.bondedAmount.gt(bignumber_1.BigNumber.from('0')) ? 'BONDED' : 'UNBONDED'; table.push([info.networkOption.name, status, (0, units_1.formatUnits)(info.bondedAmount, 'ether')]); } this.log(table.toString()); const possibleNetworksToUnbond = networksBondInfo.filter(info => info.bondedAmount.gt(bignumber_1.BigNumber.from('0'))); if (possibleNetworksToUnbond.length === 0) { this.exit(); } const prompt = await inquirer.prompt([ { type: 'checkbox', name: 'networks', message: 'Which networks do you want to unbond?', choices: possibleNetworksToUnbond.map(networkInfo => networkInfo.networkOption), validate: async (input) => { if (input.length > 0) { return true; } return 'Please select at least 1 network. Use the arrow keys and space-bar to select.'; }, }, ]); const providedNetworks = prompt.networks; const selectedNetworksToUnbond = possibleNetworksToUnbond.filter(networkInfo => providedNetworks.includes(networkInfo.networkOption.value)); for (const networkToUnbond of selectedNetworksToUnbond) { this.log(`Unbonding from network: ${networkToUnbond.networkOption.name}`); const unbondReceipt = await networkToUnbond.operatorChainService.unbondUtilityToken(); if (unbondReceipt === null) { this.log(color_1.default.red(`Could not confirm the success of unbonding transaction.`)); this.exit(); } this.log('Successfully unbonded. Exiting...'); this.exit(); } } } exports.default = Unbond;