Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 82 additions & 36 deletions bin/aecli-inspect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#!/usr/bin/env node
// # æternity CLI `inspect` file
//
// This script initialize all `inspect` commands
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
Expand All @@ -18,41 +14,91 @@
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
// We'll use `commander` for parsing options
//
// Also we need `esm` package to handle `ES imports`
const program = require('commander')

require = require('esm')(module/*, options */) // use to handle es6 import/export
const utils = require('./utils/index')
const { Inspect } = require('./commands')
import { Command } from 'commander'
import { TxBuilder } from '@aeternity/aepp-sdk'
import { HASH_TYPES, NODE_URL, NODE_INTERNAL_URL } from './utils/constant'
import { initChain } from './utils/cli'
import {
print, printBlock, printBlockTransactions,
printName, printOracle, printQueries,
printTransaction, printUnderscored,
} from './utils/print'
import { getBlock, updateNameStatus, validateName } from './utils/helpers'

// ## Initialize `options`
program
.option('-u --url [hostname]', 'Node to connect to', utils.constant.NODE_URL)
.option('--internalUrl [internal]', 'Node to connect to(internal)', utils.constant.NODE_INTERNAL_URL)
export default new Command('inspect')
.arguments('<identifier>')
.description('Get information about an entity by its identifier')
.on('--help', () => console.log([
'',
'You can use this command to get info about account, block, transaction or name.',
'',
'Examples:',
' `aecli inspect testName.test` --> get info about AENS `name`',
' `aecli inspect ak_134defawsgf34gfq4f` --> get info about `account`',
' `aecli inspect kh_134defawsgf34gfq4f` --> get info about `key block` by block `hash`',
' `aecli inspect mh_134defawsgf34gfq4f` --> get info about `micro block` by block `hash`',
' `aecli inspect 1234` --> get info about `block` by block `height`',
' `aecli inspect th_asfwegfj34234t34t` --> get info about `transaction` by transaction `hash`',
].join('\n')))
.option('-u --url [hostname]', 'Node to connect to', NODE_URL)
.option('--internalUrl [internal]', 'Node to connect to (internal)', NODE_INTERNAL_URL)
.option('-f --force', 'Ignore node version compatibility check')
.option('--json', 'Print result in json format')
.action(async (identifier, { json, ...options }) => {
const pref = identifier.split('_')[0]

// ## Initialize `inspect` command
//
// You can use this command to get info about account, block, transaction or name
//
// Example: `aecli inspect testName.test` --> get info about AENS `name`
//
// Example: `aecli inspect ak_134defawsgf34gfq4f` --> get info about `account`
//
// Example: `aecli inspect kh_134defawsgf34gfq4f` --> get info about `key block` by block `hash`
//
// Example: `aecli inspect mh_134defawsgf34gfq4f` --> get info about `micro block` by block `hash`
//
// Example: `aecli inspect 1234` --> get info about `block` by block `height`
//
// Example: `aecli inspect th_asfwegfj34234t34t` --> get info about `transaction` by transaction `hash`
program
.arguments('<hash>')
.description('Hash or Name to inspect (eg: ak_..., mk_..., name.test)')
.action(async (hash, cmd) => Inspect.inspect(hash, cmd))
if (pref === HASH_TYPES.rawTransaction) {
checkPref(identifier, HASH_TYPES.rawTransaction)
const { tx, txType: type } = TxBuilder.unpackTx(identifier)
if (json) print({ tx: tx, type })
else {
printUnderscored('Tx Type', type)
Object.entries(tx).forEach(entry => printUnderscored(...entry))
}
return
}

// Parse arguments
program.parse(process.argv)
const client = await initChain(options)

if (+identifier) {
printBlock(await client.api.getKeyBlockByHeight(+identifier), json)
return
}

switch (pref) {
case HASH_TYPES.block:
case HASH_TYPES.micro_block:
printBlock(await getBlock(identifier)(client), json)
break
case HASH_TYPES.account:
const { nonce } = await client.api.getAccountByPubkey(identifier)
const balance = await client.balance(identifier)
const transactions = (await client.api.getPendingAccountTransactionsByPubkey(identifier)).transactions
if (json) {
print({ identifier, balance, nonce, transactions })
} else {
printUnderscored('Account ID', identifier)
printUnderscored('Account balance', balance)
printUnderscored('Account nonce', nonce)
print('Account Transactions: ')
printBlockTransactions(transactions)
}
break
case HASH_TYPES.transaction:
printTransaction(await client.tx(identifier), json)
break
case HASH_TYPES.contract:
printTransaction(await client.api.getContract(identifier), json)
break
case HASH_TYPES.oracle:
printOracle(await client.getOracle(identifier), json)
const { oracleQueries } = await client.getOracleQueries(identifier)
if (oracleQueries) printQueries(oracleQueries, json)
break
default:
validateName(identifier)
printName(await updateNameStatus(identifier)(client), json)
break
}
})
23 changes: 16 additions & 7 deletions bin/aecli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
'use strict'
// We'll use `commander` for parsing options
// Also we need `esm` package to handle `ES imports`
const program = require('commander')

// We need `esm` package to handle `ES imports`
require = require('esm')(module/*, options */) // use to handle es6 import/export
// We'll use `commander` for parsing options
const { program, Command } = require('commander')
const utils = require('./utils/index')
const inspect = require('./aecli-inspect').default

// Array of child command's
const EXECUTABLE_CMD = [
{ name: 'chain', desc: 'Interact with the blockchain' },
{ name: 'inspect', desc: 'Get information on transactions, blocks,...' },
inspect,
{ name: 'account', desc: 'Handle wallet operations' },
{ name: 'contract', desc: 'Compile contracts' },
{ name: 'name', desc: 'AENS system' },
Expand All @@ -50,7 +50,16 @@ program
.action((cmd) => utils.print.printConfig(cmd))

// ## Initialize `child` command's
EXECUTABLE_CMD.forEach(({ name, desc }) => program.command(name, desc));
EXECUTABLE_CMD.forEach(command => command instanceof Command
? program.addCommand(command)
: program.command(command.name, command.desc));

// Parse arguments
program.parse(process.argv)
(async () => {
try {
await program.parseAsync(process.argv)
} catch (error) {
console.error(error.message)
process.exit(1)
}
})()
1 change: 0 additions & 1 deletion bin/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { Chain } from './chain'
export { Contract } from './contract'
export { Inspect } from './inspect'
export { Account } from './account'
export { AENS } from './aens'
export { Transaction } from './transaction'
Expand Down
Loading