Skip to content

Commit

Permalink
feat(Account): Spend by name. Extend account spend command with abi…
Browse files Browse the repository at this point in the history
…lity to put recipient address or name (#117)
  • Loading branch information
nduchak committed Oct 16, 2019
1 parent 8279579 commit 043d1a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
6 changes: 4 additions & 2 deletions bin/aecli-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ program
//
// Example: `aecli account spend ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 100 --password testpassword`
//
// Example: `aecli account spend ./myWalletKeyFile aensAccountName.chain 100 --password testpassword`
//
// You can set transaction `ttl(Time to leave)`. If not set use default.
//
// Example: `aecli account spend ./myWalletKeyFile ak_1241rioefwj23f2wfdsfsdsdfsasdf 100 --password testpassword --ttl 20` --> this tx will leave for 20 blocks
program
.command('spend <wallet_path> <receiver> <amount>')
.command('spend <wallet_path> <receiverIdOrName> <amount>')
.option('--networkId [networkId]', 'Network id (default: ae_mainnet)')
.option('--payload [payload]', 'Transaction payload.', '')
.option('-F, --fee [fee]', 'Spend transaction fee.')
.option('-T, --ttl [ttl]', 'Validity of the spend transaction in number of blocks (default forever)', utils.constant.TX_TTL)
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.action(async (walletPath, receiver, amount, ...arguments) => await Account.spend(walletPath, receiver, amount, utils.cli.getCmdFromArguments(arguments)))
.action(async (walletPath, receiverIdOrName, amount, ...arguments) => await Account.spend(walletPath, receiverIdOrName, amount, utils.cli.getCmdFromArguments(arguments)))


// ## Initialize `transfer` command
Expand Down
45 changes: 23 additions & 22 deletions bin/commands/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,24 @@ async function sign (walletPath, tx, options) {
printUnderscored('Unsigned', tx)
printUnderscored('Signed', signedTx)
}
process.exit(0)
exit()
})
} catch (e) {
printError(e.message)
exit(1)
}
}

// ## `Spend` function
// this function allow you to `send` token's to another `account`
async function spend (walletPath, receiver, amount, options) {
let { ttl, json, nonce, fee, payload = '' } = options
ttl = parseInt(ttl)
nonce = parseInt(nonce)
fee = parseInt(fee)
async function spend (walletPath, receiverNameOrAddress, amount, options) {
const { ttl, json, nonce, fee, payload = '' } = options
try {
checkPref(receiver, HASH_TYPES.account)
// Get `keyPair` by `walletPath`, decrypt using password and initialize `Ae` client with this `keyPair`
const client = await initClientByWalletFile(walletPath, options)

await handleApiError(async () => {
let tx = await client.spend(amount, receiver, { ttl, nonce, payload, fee })
let tx = await client.spend(amount, receiverNameOrAddress, { ttl, nonce, payload, fee })
// if waitMined false
if (typeof tx !== 'object') {
tx = await client.tx(tx)
Expand All @@ -82,20 +79,18 @@ async function spend (walletPath, receiver, amount, options) {
json
? print({ tx })
: printTransaction(tx, json)
process.exit(0)
exit()
})
} catch (e) {
printError(e.message)
exit(1)
}
}

// ## `Transfer` function
// this function allow you to `send` % of balance to another `account`
async function transferFunds (walletPath, receiver, percentage, options) {
let { ttl, json, nonce, fee, payload = '', excludeFee } = options
ttl = parseInt(ttl)
nonce = parseInt(nonce)
fee = parseInt(fee)
const { ttl, json, nonce, fee, payload = '', excludeFee } = options
percentage = parseFloat(percentage)
try {
checkPref(receiver, HASH_TYPES.account)
Expand All @@ -115,10 +110,11 @@ async function transferFunds (walletPath, receiver, percentage, options) {
} else {
printTransaction(tx, json)
}
process.exit(0)
exit()
})
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand All @@ -141,11 +137,12 @@ async function getBalance (walletPath, options) {
printUnderscored('ID', address)
printUnderscored('Nonce', nonce)
}
exit(0)
exit()
}
)
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand Down Expand Up @@ -175,11 +172,12 @@ async function getAddress (walletPath, options) {
}
}
}
exit(0)
exit(1)
}
)
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand All @@ -205,11 +203,12 @@ async function getAccountNonce (walletPath, options) {
printUnderscored('Nonce', nonce - 1)
printUnderscored('Next Nonce', nonce)
}
process.exit(0)
process.exit()
}
)
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand All @@ -227,9 +226,10 @@ async function createSecureWallet (walletPath, { output, password, overwrite, js
printUnderscored('Address', publicKey)
printUnderscored('Path', path)
}
process.exit(0)
exit()
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand All @@ -247,9 +247,10 @@ async function createSecureWalletByPrivKey (walletPath, priv, { output, password
printUnderscored('Address', publicKey)
printUnderscored('Path', path)
}
process.exit(0)
exit()
} catch (e) {
printError(e.message)
exit(1)
}
}

Expand All @@ -273,12 +274,12 @@ async function generateKeyPairs (count = 1, { forcePrompt, json }) {
})
}
} else {
process.exit(0)
exit()
}
process.exit(0)
exit()
} catch (e) {
printError(e.message)
process.exit(1)
exit(1)
}
}

Expand Down
15 changes: 14 additions & 1 deletion test/cli/aens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function randomString (len, charSet) {

describe('CLI AENS Module', function () {
configure(this)
const { publicKey } = generateKeyPair()
let wallet
let nameAuctionsSupported
let name
Expand Down Expand Up @@ -103,7 +104,6 @@ describe('CLI AENS Module', function () {
nameResult.status.should.equal('CLAIMED')
})
it('Update Name', async () => {
const { publicKey } = generateKeyPair()
const updateTx = JSON.parse(await execute(['name', 'update', WALLET_NAME, '--password', 'test', name2, publicKey, '--json']))
const nameResult = JSON.parse(await execute(['inspect', name2, '--json']))

Expand All @@ -112,6 +112,19 @@ describe('CLI AENS Module', function () {
isUpdatedNode.should.be.equal(true)
nameResult.status.should.equal('CLAIMED')
})
it('Fail spend by name on invalid input', async () => {
const amount = 100000009
const error = await execute(['account', 'spend', WALLET_NAME, '--password', 'test', 'sdasdaasdas', amount, '--json'])
error.indexOf('AENS: Invalid name domain').should.not.be.equal(-1)
})
it('Spend by name', async () => {
const amount = 100000009
const spendTx = JSON.parse(await execute(['account', 'spend', WALLET_NAME, '--password', 'test', name2, amount, '--json']))
const nameObject = await wallet.aensQuery(name2)
spendTx.tx.tx.recipientId.should.be.equal(nameObject.id)
const balance = await wallet.getBalance(publicKey)
balance.should.be.equal(`${amount}`)
})
it('Revoke Name', async () => {
const revoke = JSON.parse(await execute(['name', 'revoke', WALLET_NAME, '--password', 'test', name2, '--json']))
const nameResult = JSON.parse(await execute(['inspect', name2, '--json']))
Expand Down

0 comments on commit 043d1a2

Please sign in to comment.