Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.
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
3 changes: 2 additions & 1 deletion src/action/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* call the corresponding GRPC apis for channel management.
*/

import { toSatoshis, poll } from '../helper';
import { toSatoshis, poll, getTimeTilAvailable } from '../helper';
import * as log from './log';

class ChannelAction {
Expand Down Expand Up @@ -160,6 +160,7 @@ class ChannelAction {
limboBalance: pfcc.limboBalance,
maturityHeight: pfcc.maturityHeight,
blocksTilMaturity: pfcc.blocksTilMaturity,
timeTilAvailable: getTimeTilAvailable(pfcc.blocksTilMaturity),
status: 'pending-force-closing',
}));
const wccs = response.waitingCloseChannels.map(wcc => ({
Expand Down
17 changes: 17 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ export const toLabel = (amount, settings) => {
return toAmountLabel(satoshis, settings);
};

/**
* Convert a number of blocks to an amount of time in the format "X days and Y
* hours" assuming 10 minutes per block.
* @param {number} numBlocks The number of blocks to convert.
* @return {string} The amount of time the blocks is equivalent to.
*/
export const getTimeTilAvailable = numBlocks => {
if (!Number.isInteger(numBlocks)) {
throw new Error('Invalid input!');
}
const days = Math.floor(numBlocks / (24 * 6));
const hours = Math.floor((numBlocks % (24 * 6)) / 6);
const daysString = days === 1 ? 'day' : 'days';
const hoursString = hours === 1 ? 'hour' : 'hours';
return `${days} ${daysString} and ${hours} ${hoursString}`;
};

/**
* Split '_' separated words and convert to uppercase
* @param {string} value The input string
Expand Down
5 changes: 5 additions & 0 deletions src/view/channel-detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const ChannelDetailView = ({ store, nav }) => (
<DetailField name="Status">
{store.selectedChannel.statusLabel}
</DetailField>
{store.selectedChannel.timeTilAvailable ? (
<DetailField name="Time Til Available">
{store.selectedChannel.timeTilAvailable}
</DetailField>
) : null}
<DetailField name="Capacity">
{store.selectedChannel.capacityLabel} {store.unitLabel}
</DetailField>
Expand Down
10 changes: 9 additions & 1 deletion test/unit/action/channel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ describe('Action Channels Unit Tests', () => {
grpc.sendCommand.withArgs('pendingChannels').resolves({
pendingOpenChannels: [{ channel: { ...pendingChannel } }],
pendingClosingChannels: [{ channel: { ...pendingChannel } }],
pendingForceClosingChannels: [{ channel: { ...pendingChannel } }],
pendingForceClosingChannels: [
{
channel: { ...pendingChannel },
blocksTilMaturity: 463,
},
],
waitingCloseChannels: [{ channel: { ...pendingChannel } }],
totalLimboBalance: 1,
});
Expand All @@ -156,6 +161,9 @@ describe('Action Channels Unit Tests', () => {
remotePubkey: 'some-key',
fundingTxId: 'FFFF',
});
expect(store.pendingChannels[2], 'to satisfy', {
timeTilAvailable: '3 days and 5 hours',
});
});

it('should log error on failure', async () => {
Expand Down
28 changes: 28 additions & 0 deletions test/unit/helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,32 @@ describe('Helpers Unit Tests', () => {
);
});
});

describe('getTimeTilAvailable()', () => {
it('should format blocks to human-readable time', () => {
const numBlocks = 463;
const time = helpers.getTimeTilAvailable(numBlocks);
expect(time, 'to equal', '3 days and 5 hours');
});

it('should error on undefined', () => {
expect(
helpers.getTimeTilAvailable.bind(undefined),
'to throw',
/Invalid/
);
});

it('should work for 1 day and 1 hour', () => {
const numBlocks = 150;
const time = helpers.getTimeTilAvailable(numBlocks);
expect(time, 'to equal', '1 day and 1 hour');
});

it('should work for 0', () => {
const numBlocks = 0;
const time = helpers.getTimeTilAvailable(numBlocks);
expect(time, 'to equal', '0 days and 0 hours');
});
});
});