Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Add delegate:votes and voters - Closes #168 #636

Merged
merged 6 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/delegate.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Commands relating to Lisk delegates.

* [`lisk delegate:get USERNAMES`](#lisk-delegate-get-usernames)
* [`lisk delegate:voters USERNAMES`](#lisk-delegate-voters-usernames)
* [`lisk delegate:votes USERNAMES`](#lisk-delegate-votes-usernames)

## `lisk delegate:get USERNAMES`

Expand All @@ -28,3 +30,65 @@ EXAMPLES
delegate:get lightcurve
delegate:get lightcurve,4miners.net
```

## `delegate:voters USERNAMES`

Gets voters information for given delegate(s) from the blockchain.

```
USAGE
$ lisk delegate:voters USERNAMES

ARGUMENTS
USERNAMES Comma-separated username(s) to get information about.

OPTIONS
--limit Limits the returned voters array by specified integer amount. Maximum is 100.

--offset Offsets the returned voters array by specified integer amount.

--sort Sorts the returned voters array. Sort type must be one of `publicKey:asc`, `publicKey:desc`, `balance:asc`, `balance:desc`, `username:asc` or `username:desc`.

--[no-]pretty Prints JSON in pretty format rather than condensed. Has no effect if the output is set to table. You
can change the default behaviour in your config.json file.


DESCRIPTION
Gets voters information for given delegate(s) from the blockchain.

EXAMPLES
delegate:voters lightcurve
delegate:voters lightcurve,4miners.net
delegate:voters lightcurve,4miners.net --limit 20 --offset 5 --sort publicKey:asc --pretty
```

## `delegate:votes USERNAMES`

Gets votes information for given delegate(s) from the blockchain.

```
USAGE
$ lisk delegate:votes USERNAMES

ARGUMENTS
USERNAMES Comma-separated username(s) to get information about.

OPTIONS
--limit Limits the returned voters array by specified integer amount. Maximum is 100.

--offset Offsets the returned voters array by specified integer amount.

--sort Sorts the returned voters array. Sort type must be one of `balance:asc`, `balance:desc`, `username:asc` or `username:desc`.

--[no-]pretty Prints JSON in pretty format rather than condensed. Has no effect if the output is set to table. You
can change the default behaviour in your config.json file.


DESCRIPTION
Gets voters information for given delegate(s) from the blockchain.

EXAMPLES
delegate:votes lightcurve
delegate:votes lightcurve,4miners.net
delegate:votes lightcurve,4miners.net --limit 20 --offset 5 --sort balance:asc --pretty
```
116 changes: 116 additions & 0 deletions src/commands/delegate/voters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* LiskHQ/lisk-commander
* Copyright © 2017–2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';

const MAXIMUM_LIMIT = 100;

const processFlagInputs = (limitStr, offsetStr, sortStr) => {
const limit = parseInt(limitStr, 10);
const offset = parseInt(offsetStr, 10);
const sort = sortStr ? sortStr.trim() : undefined;
if (limitStr !== limit.toString() || !Number.isInteger(limit) || limit <= 0) {
throw new Error('Limit must be an integer and greater than 0');
}
if (limit && limit > MAXIMUM_LIMIT) {
throw new Error(`Maximum limit amount is ${MAXIMUM_LIMIT}`);
}
if (
offsetStr !== offset.toString() ||
!Number.isInteger(offset) ||
offset < 0
) {
throw new Error('Offset must be an integer and greater than or equal to 0');
}
if (!SORT_FIELDS.includes(sort)) {
throw new Error(`Sort must be one of: ${SORT_FIELDS.join(', ')}`);
}

return {
limit,
offset,
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
sort,
};
};

export default class VotersCommand extends BaseCommand {
async run() {
const {
args: { usernames },
flags: { limit: limitStr, offset: offsetStr, sort: sortStr },
} = this.parse(VotersCommand);

const { limit, offset, sort } = processFlagInputs(
limitStr,
offsetStr,
sortStr,
);

const req = usernames.map(username => ({
query: {
username,
limit: limit || 10,
offset: offset || 0,
sort: sort || 'balance:desc',
},
placeholder: {
username,
message: 'Delegate not found.',
},
}));
const client = getAPIClient(this.userConfig.api);
const results = await query(client, 'voters', req);
this.print(results);
}
}

VotersCommand.args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
parse: input => input.split(',').filter(Boolean),
},
];

VotersCommand.flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: 'balance:desc',
}),
};

VotersCommand.description = `
Gets voters information for given delegate(s) from the blockchain.
`;

VotersCommand.examples = [
'delegate:voters lightcurve',
'delegate:voters lightcurve,4miners.net',
'delegate:voters lightcurve,4miners.net --limit 20 --offset 5 --sort publicKey:asc --pretty',
];
120 changes: 120 additions & 0 deletions src/commands/delegate/votes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* LiskHQ/lisk-commander
* Copyright © 2017–2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';

const MAXIMUM_LIMIT = 100;

const VOTES_SORT_FIELDS = SORT_FIELDS.filter(
field => !field.includes('publicKey'),
);

const processFlagInputs = (limitStr, offsetStr, sortStr) => {
const limit = parseInt(limitStr, 10);
const offset = parseInt(offsetStr, 10);
const sort = sortStr ? sortStr.trim() : undefined;
if (limitStr !== limit.toString() || !Number.isInteger(limit) || limit <= 0) {
throw new Error('Limit must be an integer and greater than 0');
}
if (limit && limit > MAXIMUM_LIMIT) {
throw new Error(`Maximum limit amount is ${MAXIMUM_LIMIT}`);
}
if (
offsetStr !== offset.toString() ||
!Number.isInteger(offset) ||
offset < 0
) {
throw new Error('Offset must be an integer and greater than or equal to 0');
}
if (!VOTES_SORT_FIELDS.includes(sort)) {
throw new Error(`Sort must be one of: ${VOTES_SORT_FIELDS.join(', ')}`);
}

return {
limit,
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
offset,
sort,
};
};

export default class VotesCommand extends BaseCommand {
async run() {
const {
args: { usernames },
flags: { limit: limitStr, offset: offsetStr, sort: sortStr },
} = this.parse(VotesCommand);

const { limit, offset, sort } = processFlagInputs(
limitStr,
offsetStr,
sortStr,
);

const req = usernames.map(username => ({
query: {
username,
limit: limit || 10,
offset: offset || 0,
sort: sort || 'balance:desc',
},
placeholder: {
username,
message: 'Delegate not found.',
},
}));
const client = getAPIClient(this.userConfig.api);
const results = await query(client, 'votes', req);
this.print(results);
}
}

VotesCommand.args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
parse: input => input.split(',').filter(Boolean),
},
];

VotesCommand.flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: 'balance:desc',
}),
};

VotesCommand.description = `
Gets votes information for given delegate(s) from the blockchain.
`;

VotesCommand.examples = [
'delegate:votes lightcurve',
'delegate:votes lightcurve,4miners.net',
'delegate:votes lightcurve,4miners.net --limit 20 --offset 5 --sort balance:asc --pretty',
];
9 changes: 9 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ export const NETHASHES = {
test: constants.TESTNET_NETHASH,
beta: constants.BETANET_NETHASH,
};

export const SORT_FIELDS = [
'publicKey:asc',
'publicKey:desc',
'balance:asc',
'balance:desc',
'username:asc',
'username:desc',
];
Loading