Skip to content

Commit

Permalink
updated file structure for balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Aug 23, 2024
1 parent 7d052e6 commit 0ec5803
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
35 changes: 20 additions & 15 deletions src/balance/command.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
12 changes: 12 additions & 0 deletions src/balance/interaction.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
18 changes: 18 additions & 0 deletions src/balance/main.ts
Original file line number Diff line number Diff line change
@@ -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`
}
}
14 changes: 2 additions & 12 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 2 additions & 4 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 0ec5803

Please sign in to comment.