Skip to content

Commit

Permalink
Merge branch 'naynay/file-restructure' into naynay/signing-restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Aug 19, 2024
2 parents 0e040ec + 038051d commit f07dcda
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 109 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- new: './src/balance' - new file structure for our CLI/TUI flows
- new: './src/balance/command.ts' - main entry file for balance command for tui/cli
- new: './src/balance/utils.ts' - utilities and helper methods for all things balance
- new: './src/transfer' - new file structure for our CLI/TUI flows
- new: './src/transfer/command.ts' - main entry file for transfer command for tui/cli
- new: './src/transfer/utils.ts' - utilities and helper methods for all things transfer

### Changed

Expand All @@ -39,6 +42,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- logger to handle nested contexts for better organization of logs
- merged user + dev program folders + tests
- removed flows/balance/*.ts directory with file restructure
- removed flows/entropyTransfer/*.ts directory with file restructure


### Broke
Expand Down
15 changes: 11 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import * as config from './config'
import { EntropyTuiOptions } from './types'

import { cliListAccounts } from './flows/manage-accounts/cli'
import { cliEntropyTransfer } from './flows/entropyTransfer/cli'
import { cliSign } from './flows/sign/cli'
import { getSelectedAccount, stringify } from './common/utils'
import Entropy from '@entropyxyz/sdk'
import { initializeEntropy } from './common/initializeEntropy'
import { BalanceCommand } from './balance/command'
import { TransferCommand } from './transfer/command'

const program = new Command()
// Array of restructured commands to make it easier to migrate them to the new "flow"
Expand Down Expand Up @@ -70,7 +70,7 @@ function currentAccountAddressOption () {

let entropy: Entropy

async function loadEntropy (address: string, endpoint: string, password: string) {
export async function loadEntropy (address: string, endpoint: string, password?: string): Promise<Entropy> {
const storedConfig = config.getSync()
const selectedAccount = getSelectedAccount(storedConfig.accounts, address)

Expand All @@ -82,6 +82,12 @@ async function loadEntropy (address: string, endpoint: string, password: string)
}

entropy = await initializeEntropy({ keyMaterial: selectedAccount.data, endpoint, password })

if (!entropy?.keyring?.accounts?.registration?.pair) {
throw new Error("Signer keypair is undefined or not properly initialized.")
}

return entropy
}

/* no command */
Expand Down Expand Up @@ -147,8 +153,9 @@ program.command('transfer')
.addOption(passwordOption('Password for the source account (if required)'))
.addOption(endpointOption())
.addOption(currentAccountAddressOption())
.action(async (source, destination, amount, opts) => {
await cliEntropyTransfer({ source, destination, amount, ...opts })
.action(async (_source, destination, amount, opts) => {
const transferCommand = new TransferCommand(entropy, opts.endpoint)
await transferCommand.sendTransfer(destination, amount)
// writeOut(??) // TODO: write the output
process.exit(0)
})
Expand Down
4 changes: 2 additions & 2 deletions src/common/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export function setupProgress (label: string): { start: () => void; stop: () =>
})

const start = () => {
// 160 was found through trial and error, don't believe there is a formula to
// 150 was found through trial and error, don't believe there is a formula to
// determine the exact time it takes for the transaction to be processed and finalized
// TO-DO: Change progress bar to loading animation?
b1.start(160, 0, {
b1.start(150, 0, {
speed: "N/A"
})
// update values
Expand Down
32 changes: 0 additions & 32 deletions src/flows/entropyTransfer/cli.ts

This file was deleted.

64 changes: 0 additions & 64 deletions src/flows/entropyTransfer/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/flows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { entropyFaucet } from './entropyFaucet'
export { entropyRegister } from './register'
export { userPrograms, devPrograms } from './programs'
export { sign } from './sign'
export { entropyTransfer } from './entropyTransfer'
export { manageAccounts } from './manage-accounts'
51 changes: 51 additions & 0 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Entropy from "@entropyxyz/sdk";
import { BaseCommand } from "../common/base-command";
import { setupProgress } from "../common/progress";
import * as TransferUtils from './utils'
import inquirer from "inquirer";

const FLOW_CONTEXT = 'ENTROPY_TRANSFER'
const question = [
{
type: "input",
name: "amount",
message: "Input amount to transfer:",
default: "1",
validate: (amount) => {
if (isNaN(amount) || parseInt(amount) <= 0) {
return 'Please enter a value greater than 0'
}
return true
}
},
{
type: "input",
name: "recipientAddress",
message: "Input recipient's address:",
},
]

export class TransferCommand extends BaseCommand {
constructor (entropy: Entropy, endpoint: string) {
super(entropy, endpoint, FLOW_CONTEXT)
}

public async askQuestions () {
return inquirer.prompt(question)
}

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
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-ignore
import { Pair } from '@entropyxyz/sdk/keys'

export interface TransferOptions {
from: Pair
to: string
Expand Down
File renamed without changes.
33 changes: 29 additions & 4 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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 { loadEntropy } from './cli'

let shouldInit = true

Expand All @@ -23,7 +25,7 @@ export default function tui (entropy: Entropy, options: EntropyTuiOptions) {
'Balance': () => {},
'Register': flows.entropyRegister,
'Sign': flows.sign,
'Transfer': flows.entropyTransfer,
'Transfer': () => {},
// TODO: design programs in TUI (merge deploy+user programs)
'Deploy Program': flows.devPrograms,
'User Programs': flows.userPrograms,
Expand Down Expand Up @@ -57,6 +59,11 @@ async function main (entropy: Entropy, choices, options, logger: EntropyLogger)
storedConfig = await config.get()
}

// If the selected account changes within the TUI we need to reset the entropy instance being used
if (storedConfig.selectedAccount !== entropy.keyring.accounts.registration.address) {
entropy = await loadEntropy(storedConfig.selectedAccount, options.endpoint)
}

const answers = await inquirer.prompt([{
type: 'list',
name: 'choice',
Expand All @@ -78,11 +85,29 @@ async function main (entropy: Entropy, choices, options, logger: EntropyLogger)
logger.debug(answers)
switch (answers.choice) {
case "Balance": {
const balanceCommand = new BalanceCommand(entropy, options.endpoint)
const balanceString = await balanceCommand.getBalance(storedConfig.selectedAccount)
print(`Address ${storedConfig.selectedAccount} has a balance of: ${balanceString}`)
try {
const balanceCommand = new BalanceCommand(entropy, options.endpoint)
const balanceString = await balanceCommand.getBalance(storedConfig.selectedAccount)
print(`Address ${storedConfig.selectedAccount} has a balance of: ${balanceString}`)
} catch (error) {
console.error('There was an error retrieving balance', error)
}
break;
}
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')
} catch (error) {
console.error('There was an error sending the transfer', error)
}
break
}
default: {
const newConfigUpdates = await choices[answers.choice](storedConfig, options, logger)
if (typeof newConfigUpdates === 'string' && newConfigUpdates === 'exit') {
Expand Down
4 changes: 2 additions & 2 deletions tests/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from './testing-utils'

import { initializeEntropy } from '../src/common/initializeEntropy'
import { transfer } from '../src/flows/entropyTransfer/transfer'
import * as BalanceUtils from '../src/balance/utils'
import * as TransferUtils from '../src/transfer/utils'
import { charlieStashAddress, charlieStashSeed } from './testing-utils/constants'

const networkType = 'two-nodes'
Expand Down Expand Up @@ -62,7 +62,7 @@ test('Transfer', async (t) => {

const transferStatus = await run(
'transfer',
transfer(entropy, {
TransferUtils.transfer(entropy, {
from: charlieEntropy.keyring.accounts.registration.pair,
to: recipientAddress,
amount: BigInt(1000 * 10e10)
Expand Down

0 comments on commit f07dcda

Please sign in to comment.