-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#20 Add get trading wallet asset balance command implementation
- Loading branch information
1 parent
6dce67b
commit 95899ab
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |