From 0ec58035ab5569f085a73609443dcdbc1ef56364 Mon Sep 17 00:00:00 2001 From: Nayyir Jutha Date: Fri, 23 Aug 2024 13:45:18 -0400 Subject: [PATCH] updated file structure for balance --- src/balance/command.ts | 35 ++++++++++++++++++++--------------- src/balance/interaction.ts | 12 ++++++++++++ src/balance/main.ts | 18 ++++++++++++++++++ src/cli.ts | 14 ++------------ src/tui.ts | 6 ++---- 5 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/balance/interaction.ts create mode 100644 src/balance/main.ts diff --git a/src/balance/command.ts b/src/balance/command.ts index b53a03e6..34f23c44 100644 --- a/src/balance/command.ts +++ b/src/balance/command.ts @@ -1,18 +1,23 @@ -import Entropy from "@entropyxyz/sdk" -import { EntropyBase } from "../common/entropy-base" -import * as BalanceUtils from "./utils" +import Entropy from "@entropyxyz/sdk"; +import { Command } from "commander"; +import { cliWrite, endpointOption, passwordOption } from "src/common/utils-cli"; +import { EntropyBalance } from "./main"; -const FLOW_CONTEXT = 'ENTROPY-BALANCE' -export class BalanceCommand extends EntropyBase { - constructor (entropy: Entropy, endpoint: string) { - super(entropy, endpoint, FLOW_CONTEXT) - } +export async function entropyBalanceCommand (entropy: Entropy, rootCommand: Command) { + const programCommand = rootCommand.command('balance') + .description('Commands to retrieive account balances on the Entropy Network') + entropyAccountBalance(entropy, programCommand) +} - public async getBalance (address: string) { - const balance = await BalanceUtils.getBalance(this.entropy, address) - - this.logger.log(`Current balance of ${address}: ${balance}`, `${BalanceCommand.name}`) - - return `${balance.toLocaleString('en-US')} BITS` - } +async function entropyAccountBalance (entropy: Entropy, programCommand: Command) { + programCommand + .argument('address', 'Account address whose balance you want to query') + .addOption(passwordOption()) + .addOption(endpointOption()) + .action(async (address, opts) => { + const balanceCommand = new EntropyBalance(entropy, opts.endpoint) + const balance = await balanceCommand.getBalance(address) + cliWrite(balance) + process.exit(0) + }) } \ No newline at end of file diff --git a/src/balance/interaction.ts b/src/balance/interaction.ts new file mode 100644 index 00000000..a8b51c3e --- /dev/null +++ b/src/balance/interaction.ts @@ -0,0 +1,12 @@ +import { print } from "src/common/utils" +import { EntropyBalance } from "./main" + +export async function entropyBalance (entropy, endpoint, storedConfig) { + try { + const entropyBalance = new EntropyBalance(entropy, endpoint) + const balanceString = await entropyBalance.getBalance(storedConfig.selectedAccount) + print(`Address ${storedConfig.selectedAccount} has a balance of: ${balanceString}`) + } catch (error) { + console.error('There was an error retrieving balance', error) + } +} \ No newline at end of file diff --git a/src/balance/main.ts b/src/balance/main.ts new file mode 100644 index 00000000..399c7db3 --- /dev/null +++ b/src/balance/main.ts @@ -0,0 +1,18 @@ +import Entropy from "@entropyxyz/sdk" +import { EntropyBase } from "../common/entropy-base" +import * as BalanceUtils from "./utils" + +const FLOW_CONTEXT = 'ENTROPY-BALANCE' +export class EntropyBalance extends EntropyBase { + constructor (entropy: Entropy, endpoint: string) { + super(entropy, endpoint, FLOW_CONTEXT) + } + + public async getBalance (address: string) { + const balance = await BalanceUtils.getBalance(this.entropy, address) + + this.logger.log(`Current balance of ${address}: ${balance}`, EntropyBalance.name) + + return `${balance.toLocaleString('en-US')} BITS` + } +} \ No newline at end of file diff --git a/src/cli.ts b/src/cli.ts index 772219d1..2e98e9db 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,10 +7,10 @@ import { EntropyTuiOptions } from './types' import { cliListAccounts } from './flows/manage-accounts/cli' import Entropy from '@entropyxyz/sdk' -import { BalanceCommand } from './balance/command' import { TransferCommand } from './transfer/command' import { entropySignCommand } from './signing/command' import { currentAccountAddressOption, endpointOption, loadEntropy, passwordOption, cliWrite } from './common/utils-cli' +import { entropyBalanceCommand } from './balance/command' const program = new Command() // Array of restructured commands to make it easier to migrate them to the new "flow" @@ -60,17 +60,7 @@ program.command('list') }) /* balance */ -program.command('balance') - .description('Get the balance of an Entropy account. Output is a number') - .argument('address', 'Account address whose balance you want to query') - .addOption(passwordOption()) - .addOption(endpointOption()) - .action(async (address, opts) => { - const balanceCommand = new BalanceCommand(entropy, opts.endpoint) - const balance = await balanceCommand.getBalance(address) - cliWrite(balance) - process.exit(0) - }) +entropyBalanceCommand(entropy, program) /* Transfer */ program.command('transfer') diff --git a/src/tui.ts b/src/tui.ts index 16fdf74b..ada3d66c 100644 --- a/src/tui.ts +++ b/src/tui.ts @@ -6,10 +6,10 @@ import { EntropyTuiOptions } from './types' import { logo } from './common/ascii' import { print } from './common/utils' import { EntropyLogger } from './common/logger' -import { BalanceCommand } from './balance/command' import { TransferCommand } from './transfer/command' import { entropySign } from './signing/interaction' import { loadEntropy } from './common/utils-cli' +import { entropyBalance } from './balance/interaction' let shouldInit = true @@ -87,9 +87,7 @@ async function main (entropy: Entropy, choices, options, logger: EntropyLogger) switch (answers.choice) { case "Balance": { try { - const balanceCommand = new BalanceCommand(entropy, options.endpoint) - const balanceString = await balanceCommand.getBalance(storedConfig.selectedAccount) - print(`Address ${storedConfig.selectedAccount} has a balance of: ${balanceString}`) + await entropyBalance(entropy, options.endpoint, storedConfig) } catch (error) { console.error('There was an error retrieving balance', error) }