diff --git a/src/cli.ts b/src/cli.ts index 2e98e9db..fbb4d1d6 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -2,15 +2,14 @@ /* NOTE: calling this file entropy.ts helps commander parse process.argv */ import { Command, Option } from 'commander' -import launchTui from './tui' -import { EntropyTuiOptions } from './types' - -import { cliListAccounts } from './flows/manage-accounts/cli' import Entropy from '@entropyxyz/sdk' -import { TransferCommand } from './transfer/command' +import { cliListAccounts } from './flows/manage-accounts/cli' +import { currentAccountAddressOption, endpointOption, loadEntropy, cliWrite } from './common/utils-cli' +import { entropyTransferCommand } from './transfer/command' import { entropySignCommand } from './signing/command' -import { currentAccountAddressOption, endpointOption, loadEntropy, passwordOption, cliWrite } from './common/utils-cli' import { entropyBalanceCommand } from './balance/command' +import { EntropyTuiOptions } from './types' +import launchTui from './tui' const program = new Command() // Array of restructured commands to make it easier to migrate them to the new "flow" @@ -63,20 +62,7 @@ program.command('list') entropyBalanceCommand(entropy, program) /* Transfer */ -program.command('transfer') - .description('Transfer funds between two Entropy accounts.') // TODO: name the output - .argument('source', 'Account address funds will be drawn from') - .argument('destination', 'Account address funds will be sent to') - .argument('amount', 'Amount of funds to be moved') - .addOption(passwordOption('Password for the source account (if required)')) - .addOption(endpointOption()) - .addOption(currentAccountAddressOption()) - .action(async (_source, destination, amount, opts) => { - const transferCommand = new TransferCommand(entropy, opts.endpoint) - await transferCommand.sendTransfer(destination, amount) - // cliWrite(??) // TODO: write the output - process.exit(0) - }) +entropyTransferCommand(entropy, program) /* Sign */ entropySignCommand(entropy, program) diff --git a/src/transfer/command.ts b/src/transfer/command.ts index d61adb3a..a6dccdfa 100644 --- a/src/transfer/command.ts +++ b/src/transfer/command.ts @@ -1,32 +1,21 @@ -import Entropy from "@entropyxyz/sdk"; -import { EntropyBase } from "../common/entropy-base"; -import { setupProgress } from "../common/progress"; -import * as TransferUtils from './utils' -import inquirer from "inquirer"; +import Entropy from "@entropyxyz/sdk" +import { Command } from "commander" +import { currentAccountAddressOption, endpointOption, passwordOption } from "src/common/utils-cli" +import { EntropyTransfer } from "./main" -const FLOW_CONTEXT = 'ENTROPY_TRANSFER' - -export class TransferCommand extends EntropyBase { - constructor (entropy: Entropy, endpoint: string) { - super(entropy, endpoint, FLOW_CONTEXT) - } - - public async askQuestions () { - return inquirer.prompt(TransferUtils.transferInputQuestions) - } - - public async sendTransfer (toAddress: string, amount: string) { - const { start: startProgress, stop: stopProgress } = setupProgress('Transferring Funds') - - const formattedAmount = BigInt(parseInt(amount) * 1e10) - startProgress() - try { - const transferStatus = await TransferUtils.transfer(this.entropy, { from: this.entropy.keyring.accounts.registration.pair, to: toAddress, amount: formattedAmount }) - if (transferStatus.isFinalized) return stopProgress() - } catch (error) { - this.logger.error('There was an issue sending this transfer', error) - stopProgress() - throw error - } - } +export async function entropyTransferCommand (entropy: Entropy, rootCommand: Command) { + rootCommand.command('transfer') + .description('Transfer funds between two Entropy accounts.') // TODO: name the output + .argument('source', 'Account address funds will be drawn from') + .argument('destination', 'Account address funds will be sent to') + .argument('amount', 'Amount of funds to be moved') + .addOption(passwordOption('Password for the source account (if required)')) + .addOption(endpointOption()) + .addOption(currentAccountAddressOption()) + .action(async (_source, destination, amount, opts) => { + const transferCommand = new EntropyTransfer(entropy, opts.endpoint) + await transferCommand.sendTransfer(destination, amount) + // cliWrite(??) // TODO: write the output + process.exit(0) + }) } \ No newline at end of file diff --git a/src/transfer/interaction.ts b/src/transfer/interaction.ts new file mode 100644 index 00000000..90b29c02 --- /dev/null +++ b/src/transfer/interaction.ts @@ -0,0 +1,14 @@ +import inquirer from "inquirer" +import { print } from "../common/utils" +import { EntropyTransfer } from "./main" +import { transferInputQuestions } from "./utils" + +export async function entropyTransfer (entropy, endpoint) { + const transferCommand = new EntropyTransfer(entropy, endpoint) + const { amount, recipientAddress } = await inquirer.prompt(transferInputQuestions) + await transferCommand.sendTransfer(recipientAddress, amount) + print('') + print(`Transaction successful: Sent ${amount} to ${recipientAddress}`) + print('') + print('Press enter to return to main menu') +} \ No newline at end of file diff --git a/src/transfer/main.ts b/src/transfer/main.ts new file mode 100644 index 00000000..9399a1e8 --- /dev/null +++ b/src/transfer/main.ts @@ -0,0 +1,27 @@ +import Entropy from "@entropyxyz/sdk"; +import { EntropyBase } from "../common/entropy-base"; +import { setupProgress } from "../common/progress"; +import * as TransferUtils from './utils' + +const FLOW_CONTEXT = 'ENTROPY_TRANSFER' + +export class EntropyTransfer extends EntropyBase { + constructor (entropy: Entropy, endpoint: string) { + super(entropy, endpoint, FLOW_CONTEXT) + } + + public async sendTransfer (toAddress: string, amount: string) { + const { start: startProgress, stop: stopProgress } = setupProgress('Transferring Funds') + + const formattedAmount = BigInt(parseInt(amount) * 1e10) + startProgress() + try { + const transferStatus = await TransferUtils.transfer(this.entropy, { from: this.entropy.keyring.accounts.registration.pair, to: toAddress, amount: formattedAmount }) + if (transferStatus.isFinalized) return stopProgress() + } catch (error) { + this.logger.error('There was an issue sending this transfer', error) + stopProgress() + throw error + } + } +} \ No newline at end of file diff --git a/src/tui.ts b/src/tui.ts index ada3d66c..efc371ae 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 { TransferCommand } from './transfer/command' -import { entropySign } from './signing/interaction' import { loadEntropy } from './common/utils-cli' +import { entropySign } from './signing/interaction' import { entropyBalance } from './balance/interaction' +import { entropyTransfer } from './transfer/interaction' let shouldInit = true @@ -95,13 +95,7 @@ async function main (entropy: Entropy, choices, options, logger: EntropyLogger) } case "Transfer": { try { - const transferCommand = new TransferCommand(entropy, options.endpoint) - const { amount, recipientAddress } = await transferCommand.askQuestions() - await transferCommand.sendTransfer(recipientAddress, amount) - print('') - print(`Transaction successful: Sent ${amount} to ${recipientAddress}`) - print('') - print('Press enter to return to main menu') + await entropyTransfer(entropy, options.endpoint) } catch (error) { console.error('There was an error sending the transfer', error) }