Skip to content

Commit

Permalink
WIP: QA file-restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Oct 1, 2024
1 parent f3412d2 commit 148b6f8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/account/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function entropyAccountCommand () {
.addCommand(entropyAccountImport())
.addCommand(entropyAccountList())
.addCommand(entropyAccountRegister())
// .addCommand(entropyAccountAlias())
// IDEA: support aliases for remote accounts (those we don't have seeds for)
// this would make transfers safer/ easier from CLI
}

function entropyAccountCreate () {
Expand Down Expand Up @@ -84,9 +87,9 @@ function entropyAccountList () {
function entropyAccountRegister () {
return new Command('register')
.description('Register an entropy account with a program')
.addOption(passwordOption())
.addOption(endpointOption())
.addOption(accountOption())
.addOption(endpointOption())
.addOption(passwordOption())
// Removing these options for now until we update the design to accept program configs
// .addOption(
// new Option(
Expand All @@ -101,11 +104,16 @@ function entropyAccountRegister () {
// )
// )
.action(async (opts) => {
console.log('here 0')
console.log(opts)
// NOTE: loadEntropy throws if it can't find opts.account
const entropy: Entropy = await loadEntropy(opts.account, opts.endpoint)
const accountService = new EntropyAccount(entropy, opts.endpoint)

console.log('here 1')

const verifyingKey = await accountService.register()
console.log('here 2')
await addVerifyingKeyToAccountAndSelect(verifyingKey, opts.account)

cliWrite(verifyingKey)
Expand Down
19 changes: 13 additions & 6 deletions src/balance/command.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { Command } from "commander";
import Entropy from "@entropyxyz/sdk";
import { cliWrite, endpointOption, loadEntropy, passwordOption } from "src/common/utils-cli";

import { EntropyBalance } from "./main";
import { cliWrite, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli";
import { findAccountByAddressOrName } from "../common/utils";
import * as config from "../config";

export function entropyBalanceCommand () {
const balanceCommand = new Command('balance')
balanceCommand
.description('Command to retrieive the balance of an account on the Entropy Network')
.argument('address', 'Account address whose balance you want to query')
.addOption(passwordOption())
.argument('account <address|name>', 'Account address whose balance you want to query')
.addOption(endpointOption())
.action(async (address, opts) => {
const entropy: Entropy = await loadEntropy(address, opts.endpoint)
.addOption(passwordOption())
.action(async (account, opts) => {
const entropy: Entropy = await loadEntropy(account, opts.endpoint)
const BalanceService = new EntropyBalance(entropy, opts.endpoint)

const { accounts } = await config.get()
const address = findAccountByAddressOrName(accounts, account)?.address

const balance = await BalanceService.getBalance(address)
cliWrite(`${balance.toLocaleString('en-US')} BITS`)
process.exit(0)
})

return balanceCommand
}
7 changes: 4 additions & 3 deletions src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function getConfigOrNull () {

export function endpointOption () {
return new Option(
'-e, --endpoint <endpoint>',
'-e, --endpoint <url>',
[
'Runs entropy with the given endpoint and ignores network endpoints in config.',
'Can also be given a stored endpoint name from config eg: `entropy --endpoint test-net`.'
Expand All @@ -44,16 +44,17 @@ export function endpointOption () {

export function passwordOption (description?: string) {
return new Option(
'-p, --password <password>',
'-p, --password',
description || 'Password for the account'
)
.hideHelp() // TEMP
}

export function accountOption () {
const storedConfig = getConfigOrNull()

return new Option(
'-a, --account <accountAddressOrName>',
'-a, --account <address|name>',
[
'Sets the account for the session.',
'Defaults to the last set account (or the first account if one has not been set before).'
Expand Down
12 changes: 9 additions & 3 deletions src/config/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const PREFIX = 'data:application/UI8A;base64,'
// was a UInt8Array, but is stored as base64

export function serialize (config) {
export function serialize (config: object) {
return JSON.stringify(config, replacer, 2)
}

export function deserialize (config) {
return JSON.parse(config, reviver)
export function deserialize (config: string) {
try {
return JSON.parse(config, reviver)
} catch (err) {
console.log('broken config:', config)
// WIP here: nothing being passed in?!!
throw err
}
}

function replacer (_key: string, value: any) {
Expand Down
4 changes: 2 additions & 2 deletions src/sign/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export function entropySignCommand () {
const signCommand = new Command('sign')
.description('Sign a message using the Entropy network. Output is a JSON { verifyingKey, signature }')
.argument('msg', 'Message you would like to sign (string)')
.addOption(passwordOption('Password for the source account (if required)'))
.addOption(endpointOption())
.addOption(accountOption())
.addOption(endpointOption())
.addOption(passwordOption('Password for the source account (if required)'))
// .addOption(
// new Option(
// '-r, --raw',
Expand Down
21 changes: 16 additions & 5 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { Command } from "commander"
import { accountOption, endpointOption, loadEntropy, passwordOption } from "src/common/utils-cli"
import { accountOption, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli"
import { EntropyTransfer } from "./main"

export function entropyTransferCommand () {
const transferCommand = new Command('tranfer')
const transferCommand = new Command('transfer')
transferCommand
.description('Transfer funds between two Entropy accounts.') // TODO: name the output
.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())
.argument('amount', 'Amount of funds to be moved (in "tokens")')
.addOption(accountOption())
.addOption(endpointOption())
.addOption(passwordOption('Password for the source account (if required)'))
.action(async (destination, amount, opts) => {
console.log({ destination, amount, opts })

// TODO: destination as <name|address> ?
const entropy = await loadEntropy(opts.account, opts.endpoint)
.catch(err => {
// WIP here. SOMETHING is wrecking the config upstream
console.error("loadEntropy failed", err)
throw err
})

const transferService = new EntropyTransfer(entropy, opts.endpoint)

await transferService.transfer(destination, amount)

// cliWrite(??) // TODO: write the output
process.exit(0)
})
Expand Down
53 changes: 53 additions & 0 deletions tests/cli.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#! /usr/bin/bash

ENTROPY_ENDPOINT=ws://127.0.0.1:9944

rm ~/.config/entropy-cryptography/entropy-cli.json
# backup config
# mv ~/.config/entropy-cryptography/entropy-cli{.json,.backup.json}

print () {
COLOR='\033[0;35m'
RESET='\033[0m'
echo ""
echo -e "${COLOR}> $1${RESET}"
}

print "// ACCOUNT /////////////////////////////////////////////////"

# Errors (correct, but messy?)
# print "account ls:"
# entropy account ls | jq

print "account create"
entropy account create naynay | jq

print "account import"
entropy account import faucet 0x358f394d157e31be23313a1500f5e2c8871e514e530a35aa5c05334be7a39ba6 | jq

print "account list"
entropy account list | jq



print "// BALANCE ///////////////////////////////////////////////// "

print "balance naynay"
entropy balance naynay

print "balance 5CqJyjALDFz4sKjQgK8NXBQGHCWAiV63xXn2Dye393Y6Vghz"
# entropy balance faucet
entropy balance 5CqJyjALDFz4sKjQgK8NXBQGHCWAiV63xXn2Dye393Y6Vghz



print "// TRANSFER ////////////////////////////////////////////////"

print "entropy transfer"
NAYNAY_ADDRESS=`entropy account ls | jq --raw-output ".[0].address"`
# NOTE: --raw-output is needed to drop the quotes
entropy transfer -a faucet ${NAYNAY_ADDRESS} 2.5
entropy balance naynay

# restore config
# mv ~/.config/entropy-cryptography/entropy-cli{.backup.json,.json}

0 comments on commit 148b6f8

Please sign in to comment.