Skip to content

Commit

Permalink
updated file structure for transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Aug 23, 2024
1 parent 0ec5803 commit 0adff48
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 59 deletions.
26 changes: 6 additions & 20 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 19 additions & 30 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
14 changes: 14 additions & 0 deletions src/transfer/interaction.ts
Original file line number Diff line number Diff line change
@@ -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')
}
27 changes: 27 additions & 0 deletions src/transfer/main.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
}
12 changes: 3 additions & 9 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 { 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

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

0 comments on commit 0adff48

Please sign in to comment.