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

Add command to get unsigned and unprocessed transactions - Closes#600 #647

Merged
merged 19 commits into from
Nov 14, 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
14 changes: 11 additions & 3 deletions docs/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,25 @@ ARGUMENTS
IDS Comma-separated transaction ID(s) to get information about.

OPTIONS
-j, --[no-]json Prints output in JSON format. You can change the default behaviour in your config.json file.
-j, --[no-]json Prints output in JSON format. You can change the default behaviour in your
config.json file.

--[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.
-s, --state=unsigned|unprocessed Get transactions based on a given state. Possible values for the state are
'unsigned' and 'unprocessed'.
Examples:
- --state=unsigned
- --state=unprocessed

--[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 transaction information from the blockchain.

EXAMPLES
transaction:get 10041151099734832021
transaction:get 10041151099734832021,1260076503909567890
transaction:get 10041151099734832021,1260076503909567890 --state=unprocessed
```

## `lisk transaction:sign [TRANSACTION]`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/account/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { query } from '../../utils/query';

export default class GetCommand extends BaseCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { query } from '../../utils/query';

export default class GetCommand extends BaseCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delegate/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { query } from '../../utils/query';

export default class GetCommand extends BaseCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delegate/voters.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { query } from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';

const MAXIMUM_LIMIT = 100;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delegate/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import getAPIClient from '../../utils/api';
import query from '../../utils/query';
import { query } from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';

const MAXIMUM_LIMIT = 100;
Expand Down
45 changes: 42 additions & 3 deletions src/commands/transaction/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,39 @@
* 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 { query, handleResponse } from '../../utils/query';

const TRANSACTION_STATES = ['unsigned', 'unprocessed'];

const stateFlag = {
char: 's',
options: TRANSACTION_STATES,
description: `Get transactions based on a given state. Possible values for the state are 'unsigned' and 'unprocessed'.
Examples:
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
- --state=unsigned
- --state=unprocessed
`,
};

const queryNode = async (client, txnState, parameters) =>
Promise.all(
parameters.map(param =>
client
.getTransactions(txnState, param.query)
.then(res =>
handleResponse('node/transactions', res, param.placeholder),
),
),
);

export default class GetCommand extends BaseCommand {
async run() {
const { args: { ids } } = this.parse(GetCommand);
const { args: { ids }, flags: { state: txnState } } = this.parse(
GetCommand,
);
const req = ids.map(id => ({
query: {
limit: 1,
Expand All @@ -30,9 +56,20 @@ export default class GetCommand extends BaseCommand {
message: 'Transaction not found.',
},
}));

const client = getAPIClient(this.userConfig.api);

if (txnState === 'unsigned') {
const results = await queryNode(client.node, txnState, req);
return this.print(results);
}
if (txnState === 'unprocessed') {
const results = await queryNode(client.node, txnState, req);
return this.print(results);
}
const results = await query(client, 'transactions', req);
this.print(results);

return this.print(results);
}
}

Expand All @@ -47,6 +84,7 @@ GetCommand.args = [

GetCommand.flags = {
...BaseCommand.flags,
state: flagParser.string(stateFlag),
};

GetCommand.description = `
Expand All @@ -56,4 +94,5 @@ Gets transaction information from the blockchain.
GetCommand.examples = [
'transaction:get 10041151099734832021',
'transaction:get 10041151099734832021,1260076503909567890',
'transaction:get 10041151099734832021,1260076503909567890 --state=unprocessed',
];
4 changes: 2 additions & 2 deletions src/utils/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
*/

const handleResponse = (endpoint, res, placeholder) => {
export const handleResponse = (endpoint, res, placeholder) => {
// Get endpoints with 2xx status code should always return with data key.
if (!res.data) {
throw new Error('No data was returned.');
Expand All @@ -31,7 +31,7 @@ const handleResponse = (endpoint, res, placeholder) => {
return res.data;
};

export default async (client, endpoint, parameters) =>
export const query = async (client, endpoint, parameters) =>
Array.isArray(parameters)
? Promise.all(
parameters.map(param =>
Expand Down
80 changes: 44 additions & 36 deletions test/commands/account/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { expect, test } from '@oclif/test';
import * as config from '../../../src/utils/config';
import * as print from '../../../src/utils/print';
import * as api from '../../../src/utils/api';
import * as query from '../../../src/utils/query';
import * as queryHandler from '../../../src/utils/query';

describe('account:get command', () => {
const endpoint = 'accounts';
Expand Down Expand Up @@ -53,23 +53,27 @@ describe('account:get command', () => {
];

setupTest()
.stub(query, 'default', sandbox.stub().resolves(queryResult))
.stub(queryHandler, 'query', sandbox.stub().resolves(queryResult))
.stdout()
.command(['account:get', address])
.it('should get an account info and display as an object', () => {
expect(api.default).to.be.calledWithExactly(apiConfig);
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [
{
query: {
limit: 1,
address,
},
placeholder: {
address,
message: 'Address not found.',
expect(queryHandler.query).to.be.calledWithExactly(
apiClientStub,
endpoint,
[
{
query: {
limit: 1,
address,
},
placeholder: {
address,
message: 'Address not found.',
},
},
},
]);
],
);
return expect(printMethodStub).to.be.calledWithExactly(queryResult);
});
});
Expand All @@ -89,45 +93,49 @@ describe('account:get command', () => {
];

setupTest()
.stub(query, 'default', sandbox.stub().resolves(queryResult))
.stub(queryHandler, 'query', sandbox.stub().resolves(queryResult))
.stdout()
.command(['account:get', addresses.join(',')])
.it('should get accounts info and display as an array', () => {
expect(api.default).to.be.calledWithExactly(apiConfig);
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [
{
query: {
limit: 1,
address: addresses[0],
},
placeholder: {
address: addresses[0],
message: 'Address not found.',
},
},
{
query: {
limit: 1,
address: addresses[1],
expect(queryHandler.query).to.be.calledWithExactly(
apiClientStub,
endpoint,
[
{
query: {
limit: 1,
address: addresses[0],
},
placeholder: {
address: addresses[0],
message: 'Address not found.',
},
},
placeholder: {
address: addresses[1],
message: 'Address not found.',
{
query: {
limit: 1,
address: addresses[1],
},
placeholder: {
address: addresses[1],
message: 'Address not found.',
},
},
},
]);
],
);
return expect(printMethodStub).to.be.calledWithExactly(queryResult);
});

setupTest()
.stub(query, 'default', sandbox.stub().resolves(queryResult))
.stub(queryHandler, 'query', sandbox.stub().resolves(queryResult))
.stdout()
.command(['account:get', addressesWithEmpty.join(',')])
.it(
'should get accounts info only using non-empty args and display as an array',
() => {
expect(api.default).to.be.calledWithExactly(apiConfig);
expect(query.default).to.be.calledWithExactly(
expect(queryHandler.query).to.be.calledWithExactly(
apiClientStub,
endpoint,
[
Expand Down
Loading