Skip to content

Commit

Permalink
#20 Add optional privateKeyFilePath arg to get trading wallet balance…
Browse files Browse the repository at this point in the history
… command
  • Loading branch information
andreafspeziale committed Feb 25, 2019
1 parent 6a52cc9 commit e269982
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/commands/GetBalanceCommand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('lodash')

const ABaseCommand = require('./ABaseCommand')
const CommandArg = require('../models/CommandArg')

Expand Down Expand Up @@ -56,9 +58,11 @@ class GetBalanceCommand extends ABaseCommand {
'string', 't', 'The to address.', 1, true)
const tokenArg = new CommandArg('token',
'string', 'tk', 'The token address.', 1, true)
const privateKeyFilePathArg = new CommandArg('private-key-path',
'string', 'prv', 'The private key file path.', 1, false)
const draftArg = new CommandArg('draft',
'boolean', 'd', 'If set, it returns the TransactionObjectDraft.', 0, false, false)
return [fromArg, toArg, tokenArg, draftArg]
return [fromArg, toArg, tokenArg, privateKeyFilePathArg, draftArg]
}

/**
Expand All @@ -76,14 +80,15 @@ class GetBalanceCommand extends ABaseCommand {
* It validates the input parameters in order to execute the command.
*
* @param {Object} params
* @param {String} params.from The personal wallet address (EOA).
* @param {String} params.to The trading wallet address.
* @param {String} params.token The token wallet address.
* @param {String} params.draft The draft parameter.
* @param {String} params.from The personal wallet address (EOA).
* @param {String} params.to The trading wallet address.
* @param {String} params.token The token wallet address.
* @param {String} params.privateKeyFilePath The EOA private key.
* @param {String} params.draft The draft parameter.
*/
async doValidateAsync({ from, to, token, draft }) {
async doValidateAsync({ from, to, token, privateKeyFilePath, draft }) {
const params = this.getBalanceCommandValidator
.getBalance({ from, to, token, draft })
.getBalance({ from, to, token, privateKeyFilePath, draft })
return params
}

Expand All @@ -94,15 +99,22 @@ class GetBalanceCommand extends ABaseCommand {
* @param {String} params.from The personal wallet address (EOA).
* @param {String} params.to The trading wallet address.
* @param {String} params.token The token wallet address.
* @param {String} params.privateKeyFilePath The EOA private key.
* @param {String} params.draft The draft. If set to true it shows the TransactionObjectDraft.
*/
async doExecuteAsync({ from, to, token, draft }) {
async doExecuteAsync({ from, to, token, privateKeyFilePath, draft }) {
let personalWalletAddressRetrived = _.cloneDeep(from)
if (privateKeyFilePath) {
const privateKey = await this.extractPrivateKey(privateKeyFilePath)
personalWalletAddressRetrived = this.getAddressFromPrivateKey(from, privateKey)
}

if (draft) {
return this.tradingWalletService.transactionBuilder.buildAssetBalanceTransactionDraft(from, to, token)
}

const result = await this.tradingWalletService
.getAssetBalanceAsync(from, token, to)
.getAssetBalanceAsync(personalWalletAddressRetrived, token, to)
return result
}
}
Expand Down
1 change: 1 addition & 0 deletions src/validators/GetTradingWalletBalanceCommandValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const withdrawSchema = customJoiValidator.object()
from: customJoiValidator.address().ethereum().required(),
to: customJoiValidator.address().ethereum().required(),
token: customJoiValidator.address().ethereum().required(),
privateKeyFilePath: customJoiValidator.path().existFile(),
draft: customJoiValidator.boolean(),
})

Expand Down

0 comments on commit e269982

Please sign in to comment.