Skip to content

Commit

Permalink
feat(CLI): Refactor constant(Use constant from sdk). Refactor error h…
Browse files Browse the repository at this point in the history
…andling in AENS module. Add `pre-claim` command. Refactor claim command
  • Loading branch information
nduchak committed Oct 9, 2019
1 parent 28f7883 commit 7038629
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 81 deletions.
4 changes: 2 additions & 2 deletions bin/aecli-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const { Account } = require('./commands')

// ## Initialize `options`
program
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.NODE_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.NODE_INTERNAL_URL)
.option('-P, --password [password]', 'Wallet Password')
.option('-f --force', 'Ignore epoch version compatibility check')
.option('--json', 'Print result in json format')
Expand Down
4 changes: 2 additions & 2 deletions bin/aecli-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const { Chain } = require('./commands')

// # Initialize `options`
program
.option('-u --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('--internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.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)
.option('-L --limit [playlimit]', 'Limit for play command', utils.constant.PLAY_LIMIT)
.option('-f --force', 'Ignore node version compatibility check')
.option('--json', 'Print result in json format')
Expand Down
4 changes: 2 additions & 2 deletions bin/aecli-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const { Contract } = require('./commands')

// ## Initialize `options`
program
.option('-u --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('--internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.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)
.option('--compilerUrl [compilerUrl]', 'Compiler URL', utils.constant.COMPILER_URL)
.option('-f --force', 'Ignore node version compatibility check')
.option('--json', 'Print result in json format')
Expand Down
6 changes: 3 additions & 3 deletions bin/aecli-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const { Inspect } = require('./commands')

// ## Initialize `options`
program
.option('-u --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('--internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.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)
.option('-f --force', 'Ignore node version compatibility check')
.option('--json', 'Print result in json format')

Expand All @@ -52,7 +52,7 @@ program
program
.arguments('<hash>')
.description('Hash or Name to inspect (eg: ak_..., mk_..., name.test)')
.action(async (hash, cmd) => await Inspect.inspect(hash, cmd))
.action(async (hash, cmd) => Inspect.inspect(hash, cmd))

// Parse arguments or show `help` if argument's is empty
program.parse(process.argv)
Expand Down
77 changes: 74 additions & 3 deletions bin/aecli-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,81 @@ const { AENS } = require('./commands')

// ## Initialize `options`
program
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.NODE_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.NODE_INTERNAL_URL)
.option('--ttl [ttl]', 'Override the ttl that the transaction is going to be sent with', utils.constant.TX_TTL)
.option('--fee [fee]', 'Override the fee that the transaction is going to be sent with')
.option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('-f --force', 'Ignore node version compatibility check')
.option('--json', 'Print result in json format')
.option('--json', 'Print result in json format', utils.constant.OUTPUT_JSON)


// ## Initialize `pre-claim` command
//
// You can use this command to `pre-claim` AENS name
//
// Example: `aecli name pre-claim ./myWalletKeyFile --password testpass testname.aet`
//
// This command build and send `pre-claim` transaction.
// And wait until it will be mined. You can force waiting by using `--waitMined false` option. Default: true
//
// You can use `--ttl` to pre-set transaction `time to leave`
program
.command('pre-claim <wallet_path> <name>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.description('Pre-Claim a domain name')
.action(async (walletPath, name, ...arguments) => await AENS.preClaim(walletPath, name, utils.cli.getCmdFromArguments(arguments)))


// ## Initialize `claim` command
//
// You can use this command to `claim` AENS name. Name must end on `.test`.
//
// Example: `aecli name claim ./myWalletKeyFile --password testpass testname.test`
//
// This command send `pre-claim` transaction, wait until one block was mined, after that sent `claim` and `update` transaction's
//
// You can use `--nameTtl` and `--ttl` to pre-set transaction and name `time to leave`
program
.command('claim <wallet_path> <name> <salt>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.option('--nameFee [nameFee]', 'Wait until transaction will be mined', utils.constant.NAME_FEE)
.description('Claim a domain name')
.action(async (walletPath, name, salt, ...arguments) => await AENS.claim(walletPath, name, salt, utils.cli.getCmdFromArguments(arguments)))

// ## Initialize `revoke` command
//
// You can use this command to `destroy` AENS name.
//
// Example: `aecli name revoke ./myWalletKeyFile --password testpass testname.test`
program
.command('revoke <wallet_path> <name>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.description('Revoke a domain name')
.action(async (walletPath, name, ...arguments) => await AENS.revokeName(walletPath, name, utils.cli.getCmdFromArguments(arguments)))

// ## Initialize `transfer` command
//
// You can use this command to `transfer` AENS name to another account.
//
// Example: `aecli name transfer ./myWalletKeyFile --password testpass testname.test ak_qqwemjgflewgkj349gjdslksd`
program
.command('transfer <wallet_path> <name> <address>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.description('Transfer a name to another account')
.action(async (walletPath, name, address, ...arguments) => await AENS.transferName(walletPath, name, address, utils.cli.getCmdFromArguments(arguments)))

// ## Initialize `claim` command
//
// You can use this command to `update` pointer of AENS name.
//
// Example: `aecli name update ./myWalletKeyFile --password testpass testname.test ak_qwe23dffasfgdesag323`
program
.command('update <wallet_path> <name> <address>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.description('Update a name pointer')
.action(async (walletPath, name, address, ...arguments) => await AENS.updateName(walletPath, name, address, utils.cli.getCmdFromArguments(arguments)))


// ## Initialize `lookup` command
//
Expand Down
4 changes: 2 additions & 2 deletions bin/aecli-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const { Transaction } = require('./commands')

// ## Initialize `options`
program
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.EPOCH_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.EPOCH_INTERNAL_URL)
.option('-u, --url [hostname]', 'Node to connect to', utils.constant.NODE_URL)
.option('-U, --internalUrl [internal]', 'Node to connect to(internal)', utils.constant.NODE_INTERNAL_URL)
// .option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.option('--fee [fee]', 'Override the fee that the transaction is going to be sent with')
.option('--ttl [fee]', 'Override the ttl that the transaction is going to be sent with', utils.constant.TX_TTL)
Expand Down
68 changes: 44 additions & 24 deletions bin/commands/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { initChain, initClientByWalletFile } from '../utils/cli'
import { printError, print, printUnderscored, printName } from '../utils/print'
import { exit, initChain, initClientByWalletFile } from '../utils/cli'
import { printError, print, printUnderscored, printName, printTransaction } from '../utils/print'
import { handleApiError } from '../utils/errors'
import { isAvailable, updateNameStatus, validateName } from '../utils/helpers'

// ## Claim `name` function
async function claim (walletPath, domain, options) {
// Parse options(`ttl`, `nameTtl`)
const ttl = parseInt(options.ttl)
const nameTtl = parseInt(options.nameTtl)
async function preClaim (walletPath, domain, options) {
const { ttl, fee, nonce, waitMined, json } = options

try {
// Validate `name`(check if `name` end on `.test`)
validateName(domain)
Expand All @@ -41,29 +40,49 @@ async function claim (walletPath, domain, options) {
const name = await updateNameStatus(domain)(client)
if (!isAvailable(name)) {
print('Domain not available')
process.exit(1)
exit(1)
}
// Create `pre-claim` transaction
printTransaction(
await client.aensPreclaim(domain, { ttl, fee, nonce, waitMined }),
json
)
exit()
})
} catch (e) {
printError(e.message)
exit(1)
}
}

// Create `preclaimName` transaction
const { salt, height } = await client.aensPreclaim(domain, { nameTtl, ttl })
print('Pre-Claimed')
// ## Claim `name` function
async function claim (walletPath, domain, salt, options) {
const { ttl, fee, nonce, waitMined, json, nameFee } = options
try {
// Validate `name`
validateName(domain)

// Wait for next block and create `claimName` transaction
await client.aensClaim(domain, salt, (height + 1), { nameTtl, ttl }).catch(async e => console.log(await e.verifyTx()))
print('Claimed')
// Get `keyPair` by `walletPath`, decrypt using password and initialize `Ae` client with this `keyPair`
const client = await initClientByWalletFile(walletPath, options)

// Update `name` pointer
const { id } = await updateNameStatus(domain)(client)
const { hash } = await client.aensUpdate(id, await client.address(), { nameTtl, ttl })
print('Updated')
await handleApiError(async () => {
// Check if that `name' available
const name = await updateNameStatus(domain)(client)
if (!isAvailable(name)) {
print('Domain not available')
exit(1)
}

print(`Name ${domain} claimed`)
printUnderscored('Transaction hash', hash)
process.exit(0)
// Wait for next block and create `claimName` transaction
printTransaction(
await client.aensClaim(domain, salt, { nonce, ttl, fee, waitMined, nameFee }),
json
)
exit()
})
} catch (e) {
printError(e.message)
process.exit(1)
exit(1)
}
}

Expand All @@ -86,7 +105,7 @@ async function transferName (walletPath, domain, address, options) {
// Check if that `name` is unavailable and we can transfer it
const name = await updateNameStatus(domain)(client)
if (isAvailable(name)) {
print(`Domain is available, nothing to transfer`)
print('Domain is available, nothing to transfer')
process.exit(1)
}

Expand Down Expand Up @@ -152,7 +171,7 @@ async function revokeName (walletPath, domain, options) {
// Check if `name` is unavailable and we can revoke it
const name = await updateNameStatus(domain)(client)
if (isAvailable(name)) {
print(`Domain is available, nothing to revoke`)
print('Domain is available, nothing to revoke')
process.exit(1)
}

Expand Down Expand Up @@ -185,11 +204,12 @@ async function lookUp (domain, options) {
})
} catch (e) {
printError(e.message)
process.exit(1)
process.exit(0)
}
}

export const AENS = {
preClaim,
revokeName,
updateName,
claim,
Expand Down
34 changes: 20 additions & 14 deletions bin/commands/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,26 @@ async function version (options) {
// Call `getStatus` API and print it
await handleApiError(async () => {
const status = await client.api.getStatus()
const { consensusProtocolVersion } = client.getNodeInfo()
if (json) {
print(status)
process.exit(0)
}
printUnderscored(`Difficulty`, status.difficulty)
printUnderscored(`Node version`, status.nodeVersion)
printUnderscored(`Node revision`, status.nodeRevision)
printUnderscored(`Genesis hash`, status.genesisKeyBlockHash)
printUnderscored(`Network ID`, status.networkId)
printUnderscored(`Listening`, status.listening)
printUnderscored(`Peer count`, status.peerCount)
printUnderscored(`Pending transactions count`, status.pendingTransactionsCount)
printUnderscored(`Solutions`, status.solutions)
printUnderscored(`Syncing`, status.syncing)
const FORKS = {
3: 'Fortuna',
4: 'Lima'
}
printUnderscored('Difficulty', status.difficulty)
printUnderscored('Node version', status.nodeVersion)
printUnderscored('Consensus protocol version', `${FORKS[consensusProtocolVersion]}(${consensusProtocolVersion})`)
printUnderscored('Node revision', status.nodeRevision)
printUnderscored('Genesis hash', status.genesisKeyBlockHash)
printUnderscored('Network ID', status.networkId)
printUnderscored('Listening', status.listening)
printUnderscored('Peer count', status.peerCount)
printUnderscored('Pending transactions count', status.pendingTransactionsCount)
printUnderscored('Solutions', status.solutions)
printUnderscored('Syncing', status.syncing)
})
} catch (e) {
printError(e.message)
Expand All @@ -67,7 +73,7 @@ async function getNetworkId (options) {
print({ networkId })
process.exit(0)
}
printUnderscored(`Network ID`, networkId)
printUnderscored('Network ID', networkId)
})
} catch (e) {
printError(e.message)
Expand Down Expand Up @@ -148,7 +154,7 @@ function playWithLimit (limit, blockHash) {
return async (client, json) => {
if (!limit) return

let block = await getBlock(blockHash)(client)
const block = await getBlock(blockHash)(client)

setTimeout(async () => {
printBlock(block, json)
Expand All @@ -160,7 +166,7 @@ function playWithLimit (limit, blockHash) {
// # Play by `height`
function playWithHeight (height, blockHash) {
return async (client, json) => {
let block = await getBlock(blockHash)(client)
const block = await getBlock(blockHash)(client)
if (parseInt(block.height) < height) return

setTimeout(async () => {
Expand All @@ -172,7 +178,7 @@ function playWithHeight (height, blockHash) {

// ## Send 'transaction' to the chain
async function broadcast (signedTx, options) {
let { json, waitMined, verify } = options
const { json, waitMined, verify } = options
try {
// Initialize `Ae`
const client = await initChain(options)
Expand Down
4 changes: 2 additions & 2 deletions bin/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function decodeData (data, type, options) {
if (options.json) {
print(JSON.stringify({ decodedData }))
} else {
print(`Contract bytecode:`)
print('Contract bytecode:')
print(decodedData)
}
})
Expand Down Expand Up @@ -117,7 +117,7 @@ export async function decodeCallData (data, options) {
if (options.json) {
print(JSON.stringify({ decoded }))
} else {
print(`Decoded Call Data:`)
print('Decoded Call Data:')
print(decoded)
}
})
Expand Down
19 changes: 13 additions & 6 deletions bin/utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function getCmdFromArguments (args) {
}

// Create `Ae` client
export async function initClient ({ url, keypair, internalUrl, compilerUrl, force: forceCompatibility, native: nativeMode = true, networkId }) {
return Ae({ url, process, keypair, internalUrl, compilerUrl, forceCompatibility, nativeMode, networkId })
export async function initClient ({ url, keypair, internalUrl, compilerUrl, force: forceCompatibility, native: nativeMode = true, networkId, accounts }) {
return Ae({ url, process, keypair, internalUrl, compilerUrl, forceCompatibility, nativeMode, networkId, accounts })
}
// Create `TxBuilder` client
export async function initTxBuilder ({ url, internalUrl, force: forceCompatibility, native: nativeMode = true, showWarning = true }) {
Expand All @@ -61,14 +61,17 @@ export async function initCompiler ({ url, internalUrl, compilerUrl }) {
//
// We use `getWalletByPathAndDecrypt` from `utils/account` to get `keypair` from file
export async function initClientByWalletFile (walletPath, options, returnKeyPair = false) {
const { password, privateKey, accountOnly = false, networkId } = options
const { password, privateKey, accountOnly = false, networkId, debug = true } = options

const keypair = await getWalletByPathAndDecrypt(walletPath, { password, privateKey })
const accounts = [Account(R.merge(options, { keypair, networkId }))]

const client = accountOnly
? await Account(R.merge(options, { keypair, networkId }))
: await initClient(R.merge(options, { keypair }))
if (returnKeyPair)
? accounts[0]
: await initClient(R.merge(options, { accounts, debug }))
if (returnKeyPair) {
return { client, keypair }
}
return client
}

Expand All @@ -81,3 +84,7 @@ export function initExecCommands (program) {
export function isExecCommand (cmd, execCommands) {
return execCommands.find(({ name }) => cmd === name)
}

export function exit (error = 0) {
process.exit(error)
}

0 comments on commit 7038629

Please sign in to comment.