From c1f773c1468582c4d4463135b12f39d8b99411c4 Mon Sep 17 00:00:00 2001 From: Igor Papandinas Date: Mon, 12 Feb 2024 20:57:46 +0100 Subject: [PATCH] chore: SwankyAccountCommand class created --- src/commands/account/balance.ts | 6 ++- src/commands/account/create.ts | 32 +++---------- src/commands/account/faucet.ts | 37 ++++----------- src/commands/account/swankyAccountCommands.ts | 45 +++++++++++++++++++ src/lib/substrate-api.ts | 9 ++-- 5 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 src/commands/account/swankyAccountCommands.ts diff --git a/src/commands/account/balance.ts b/src/commands/account/balance.ts index edde0881..baa538f6 100644 --- a/src/commands/account/balance.ts +++ b/src/commands/account/balance.ts @@ -19,8 +19,10 @@ export class Balance extends SwankyCommand { async run(): Promise { const { args } = await this.parse(Balance); - if(!args.alias) { - throw new InputError("Missing argument! Please provide an alias to get the balance of an account."); + if (!args.alias) { + throw new InputError( + "Missing argument! Please provide an alias account to get the balance from. Example usage: `swanky account balance `" + ); } const accountData = this.swankyConfig.accounts.find( diff --git a/src/commands/account/create.ts b/src/commands/account/create.ts index 55e31248..a9000c4a 100644 --- a/src/commands/account/create.ts +++ b/src/commands/account/create.ts @@ -1,12 +1,11 @@ import { Flags } from "@oclif/core"; import chalk from "chalk"; -import { ChainAccount, ChainApi, encrypt, resolveNetworkUrl } from "../../lib/index.js"; +import { ChainAccount, encrypt } from "../../lib/index.js"; import { AccountData } from "../../types/index.js"; import inquirer from "inquirer"; -import { SwankyCommand } from "../../lib/swankyCommand.js"; -import { ApiError } from "../../lib/errors.js"; -import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js"; -export class CreateAccount extends SwankyCommand { +import { SwankyAccountCommand } from "./swankyAccountCommands.js"; + +export class CreateAccount extends SwankyAccountCommand { static description = "Create a new dev account in config"; static flags = { @@ -87,27 +86,6 @@ export class CreateAccount extends SwankyCommand { )} stored to config` ); - const networkUrl = resolveNetworkUrl(this.swankyConfig, ""); - - const api = (await this.spinner.runCommand(async () => { - const api = await ChainApi.create(networkUrl); - await api.start(); - return api; - }, "Connecting to node")) as ChainApi; - - - await this.spinner.runCommand( - async () => { - try { - await api.faucet(accountData); - } catch (cause) { - throw new ApiError("Error transferring tokens from faucet account", { cause }); - } - }, - `Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, - `Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, - `Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, - true - ); + await this.performFaucetTransfer(accountData, true); } } diff --git a/src/commands/account/faucet.ts b/src/commands/account/faucet.ts index ced47748..2850fde8 100644 --- a/src/commands/account/faucet.ts +++ b/src/commands/account/faucet.ts @@ -1,11 +1,9 @@ import { Args } from "@oclif/core"; -import { ChainApi, resolveNetworkUrl } from "../../lib/index.js"; import { AccountData } from "../../types/index.js"; -import { SwankyCommand } from "../../lib/swankyCommand.js"; -import { ApiError, ConfigError } from "../../lib/errors.js"; -import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js"; +import { ConfigError } from "../../lib/errors.js"; +import { SwankyAccountCommand } from "./swankyAccountCommands.js"; -export class Faucet extends SwankyCommand { +export class Faucet extends SwankyAccountCommand { static description = "Transfer some tokens from faucet to an account"; static aliases = [`account:faucet`]; @@ -17,36 +15,19 @@ export class Faucet extends SwankyCommand { description: "Alias of account to be used", }), }; + async run(): Promise { const { args } = await this.parse(Faucet); - const accountData = this.swankyConfig.accounts.find( - (account: AccountData) => account.alias === args.alias - ); + const accountData = this.findAccountByAlias(args.alias); if (!accountData) { throw new ConfigError("Provided account alias not found in swanky.config.json"); } - const networkUrl = resolveNetworkUrl(this.swankyConfig, ""); - - const api = (await this.spinner.runCommand(async () => { - const api = await ChainApi.create(networkUrl); - await api.start(); - return api; - }, "Connecting to node")) as ChainApi; + await this.performFaucetTransfer(accountData); + } - await this.spinner.runCommand( - async () => { - try { - await api.faucet(accountData); - } catch (cause) { - throw new ApiError("Error transferring tokens from faucet account", { cause }); - } - }, - `Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`, - `Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`, - `Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`, - true - ); + findAccountByAlias(alias: string): AccountData | undefined { + return this.swankyConfig.accounts.find(account => account.alias === alias); } } diff --git a/src/commands/account/swankyAccountCommands.ts b/src/commands/account/swankyAccountCommands.ts new file mode 100644 index 00000000..b884f203 --- /dev/null +++ b/src/commands/account/swankyAccountCommands.ts @@ -0,0 +1,45 @@ +import { Command } from "@oclif/core"; +import chalk from "chalk"; +import { AccountData, ChainApi, resolveNetworkUrl } from "../../index.js"; +import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js"; +import { SwankyCommand } from "../../lib/swankyCommand.js"; +import { ApiError } from "../../lib/errors.js"; + +export abstract class SwankyAccountCommand extends SwankyCommand { + async performFaucetTransfer(accountData: AccountData, canBeSkipped = false) { + let api: ChainApi | null = null; + try { + api = (await this.spinner.runCommand(async () => { + const networkUrl = resolveNetworkUrl(this.swankyConfig, ""); + const api = await ChainApi.create(networkUrl); + await api.start(); + return api; + }, "Connecting to node")) as ChainApi; + + if (api) + await this.spinner.runCommand( + async () => { + if (api) await api.faucet(accountData); + }, + `Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, + `Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, + `Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`, + true + ); + } catch (cause) { + if (cause instanceof Error) { + if (cause.message.includes('ECONNREFUSED') && canBeSkipped) { + this.warn(`Unable to connect to the node. Skipping faucet transfer for ${chalk.yellowBright(accountData.alias)}.`); + } else { + throw new ApiError("Error transferring tokens from faucet account", { cause }); + } + } else { + throw new ApiError("An unknown error occurred during faucet transfer", { cause: new Error(String(cause)) }); + } + } finally { + if (api) { + await api.disconnect(); + } + } + } +} diff --git a/src/lib/substrate-api.ts b/src/lib/substrate-api.ts index 188b44b8..42bef37c 100644 --- a/src/lib/substrate-api.ts +++ b/src/lib/substrate-api.ts @@ -103,6 +103,10 @@ export class ChainApi { return this._registry; } + public async disconnect(): Promise { + await this._provider.disconnect(); + } + public async start(): Promise { const chainProperties = await this._api.rpc.system.properties(); @@ -249,7 +253,7 @@ export class ChainApi { }); } - public async faucet(accountData: AccountData) : Promise { + public async faucet(accountData: AccountData): Promise { const keyring = new Keyring({ type: KEYPAIR_TYPE }); const alicePair = keyring.addFromUri(ALICE_URI); @@ -267,9 +271,8 @@ export class ChainApi { return; } resolve(); - this._provider.disconnect(); } - }).catch(error => reject(error)).finally(() => this._provider.disconnect()); + }).catch((error) => reject(error)); }); } }