UNPKG

@holographxyz/cli

Version:
100 lines (99 loc) 5.21 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 networks_1 = require("@holographxyz/networks"); const core_chain_service_1 = tslib_1.__importDefault(require("../../services/core-chain-service")); const token_chain_service_1 = tslib_1.__importDefault(require("../../services/token-chain-service")); const faucet_service_1 = tslib_1.__importDefault(require("../../services/faucet-service")); const network_monitor_1 = require("../../utils/network-monitor"); const config_1 = require("../../utils/config"); const validation_1 = require("../../utils/validation"); class Faucet extends core_1.Command { static description = 'Request Testnet HLG from a faucet.'; static examples = ['$ <%= config.bin %> <%= command.id %> --network="goerli"']; static flags = { ...network_monitor_1.networkFlag, }; networkMonitor; async run() { const { flags } = await this.parse(Faucet); let prompt; prompt = await inquirer.prompt([ { name: 'continue', message: 'Would you like to request $HLG tokens?', type: 'confirm', default: false, }, ]); if (!prompt.continue) { this.log('Exiting...'); this.exit(); } this.log('Loading user configurations...'); const { userWallet, configFile, supportedNetworksOptions } = await (0, config_1.ensureConfigFileIsValid)(this.config.configDir, undefined, true); const network = await (0, validation_1.checkOptionFlag)(supportedNetworksOptions, flags.network, 'Select the network to request tokens on'); this.log(`Joining network: ${networks_1.networks[network].shortKey}`); this.networkMonitor = new network_monitor_1.NetworkMonitor({ parent: this, configFile, networks: [network], debug: this.debug, userWallet, verbose: false, }); core_1.CliUx.ux.action.start('Loading 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 tokenContract = await coreChainService.getUtilityToken(); const faucetContract = await coreChainService.getFaucet(); const tokenChainService = new token_chain_service_1.default(network, this.networkMonitor, tokenContract); const faucetService = new faucet_service_1.default(network, this.networkMonitor, faucetContract); const currentHlgBalance = bignumber_1.BigNumber.from(await tokenChainService.balanceOf(coreChainService.wallet.address)); this.log(`Current $HLG balance: ${(0, units_1.formatUnits)(currentHlgBalance, 'ether')}`); const faucetInfo = await faucetService.getFaucetInfo(coreChainService.wallet.address); if (faucetInfo.isAllowedToWithdraw === false) { this.log(color_1.default.red(`You are not allowed to withdraw from the faucet on ${networks_1.networks[network].shortKey}. Please wait 24 hours since your last withdrawal.\n` + `Current cooldown time is ${faucetInfo.cooldown} seconds`)); this.exit(); } const faucetFee = await faucetService.getFaucetFee(coreChainService.wallet.address); if (faucetFee.hasEnoughBalance === false) { this.log(color_1.default.red(`You do not have enough ${networks_1.networks[network].tokenSymbol} to pay for withdrawal of $HLG from the faucet.\n` + `Please deposit more ${networks_1.networks[network].tokenSymbol} and try again.`)); this.exit(); } this.log(`Requesting $HLG tokens from faucet...`); prompt = await inquirer.prompt([ { name: 'continue', message: `You are about to withdraw ${faucetInfo.amount} $HLG from the faucet.\n` + `The gas cost will be approximately ${faucetFee.fee} ${networks_1.networks[network].tokenSymbol}. Continue?`, type: 'confirm', default: false, }, ]); if (!prompt.continue) { this.log('Exiting...'); this.exit(); } this.log('Sending request for tokens'); const receipt = await faucetService.requestTokens(); if (receipt === null) { this.log(color_1.default.red(`Could not confirm the success of transaction.`)); } else { this.log(color_1.default.green(`Request for tokens on ${networks_1.networks[network].shortKey} has been granted. You can return to request more tokens in 24 hours. Enjoy! 🤑`)); } this.exit(); } } exports.default = Faucet;