Skip to content

Commit

Permalink
#20 Add get trading wallet asset balance command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreafspeziale committed Feb 22, 2019
1 parent e7308ad commit d01a6d6
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cli/tws/get-balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const logger = require('../../logger')

const { getTradingWalletBalanceCommand } = require('../../commands/commandList')

module.exports = {
describe: getTradingWalletBalanceCommand.constructor.description,
builder: getTradingWalletBalanceCommand.getBuilderArgsDetails(),
handler: async argv => logger.show(await getTradingWalletBalanceCommand.executeAsync(argv)),
}
110 changes: 110 additions & 0 deletions src/commands/GetBalanceCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const ABaseCommand = require('./ABaseCommand')
const CommandArg = require('../models/CommandArg')

/**
* Class representing GetBalanceCommand. */
class GetBalanceCommand extends ABaseCommand {
/**
* Create a GetBalanceCommand controller.
* @param {Object} logger The logger helper.
* @param {Object} tradingWalletService The tradingWallet service.
* @param {Object} getBalanceCommandValidator The getBalanceCommand validator.
* @param {Object} privateKeyService The privateKeyService.
* @param {Object} privateKeyValidator The privateKeyValidator.
* @throws {TypeError} If some required property is missing.
*/
constructor(logger, tradingWalletService, getBalanceCommandValidator,
privateKeyService, privateKeyValidator) {
super(logger, privateKeyService, privateKeyValidator)

if (!tradingWalletService) {
const errorMessage = `Invalid "tradingWalletService" value: ${tradingWalletService}`
throw new TypeError(errorMessage)
}
this.tradingWalletService = tradingWalletService

if (!getBalanceCommandValidator) {
const errorMessage = `Invalid "getBalanceCommandValidator" value: ${getBalanceCommandValidator}`
throw new TypeError(errorMessage)
}
this.getBalanceCommandValidator = getBalanceCommandValidator

this.builderArgs = this.constructor.setBuilderArgs()

this.synopsis = this.setSynopsis()
}

static get description() {
return 'It gets the trading wallet asset balance.'
}

setSynopsis() {
let synopsis = 'get-balance '
this.builderArgs.forEach((item) => {
synopsis += item.getReprForSynopsys()
})
return synopsis
}

/**
* It set the builder args necessary to set args cli command.
*/
static setBuilderArgs() {
const fromArg = new CommandArg('from',
'string', 'f', 'The from address.', 1, true)
const toArg = new CommandArg('to',
'string', 't', 'The to address.', 1, true)
const tokenArg = new CommandArg('token',
'string', 'tk', 'The token address.', 1, true)
const draftArg = new CommandArg('draft',
'boolean', 'd', 'If set, it returns the TransactionObjectDraft.', 0, false, false)
return [fromArg, toArg, tokenArg, draftArg]
}

/**
* It gets the args props to be injected in yargs cli command.
*/
getBuilderArgsDetails() {
const builderArgsDetails = {}
this.builderArgs.forEach((item) => {
builderArgsDetails[item.name] = item.getArgProps()
})
return builderArgsDetails
}

/**
* 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.
*/
async doValidateAsync({ from, to, token, draft }) {
const params = this.getBalanceCommandValidator
.getBalance({ from, to, token, draft })
return params
}

/**
* It executes the command after the validation step.
*
* @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. If set to true it shows the TransactionObjectDraft.
*/
async doExecuteAsync({ from, to, token, draft }) {
if (draft) {
return this.tradingWalletService.transactionBuilder.buildAssetBalanceTransactionDraft(from, to, token)
}

const result = await this.tradingWalletService
.getAssetBalanceAsync(from, token, to)
return result
}
}

module.exports = GetBalanceCommand
18 changes: 18 additions & 0 deletions src/validators/GetTradingWalletBalanceCommandValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const customJoiValidator = require('../utils/customJoiValidator')
const BaseValidator = require('./BaseValidator')

const withdrawSchema = customJoiValidator.object()
.keys({
from: customJoiValidator.address().ethereum().required(),
to: customJoiValidator.address().ethereum().required(),
token: customJoiValidator.address().ethereum().required(),
draft: customJoiValidator.boolean(),
})

class GetTradingWalletBalanceCommandValidator extends BaseValidator {
getBalance(getBalanceData) {
return this.constructor.validate(getBalanceData, withdrawSchema)
}
}

module.exports = GetTradingWalletBalanceCommandValidator

0 comments on commit d01a6d6

Please sign in to comment.