Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 907942c

Browse files
authored
Merge pull request #678 from lightninglabs/success-ntfn-invoices
Success ntfn invoices
2 parents 659d257 + 4e09142 commit 907942c

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

src/action/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const grpc = new GrpcAction(store, ipc);
3636
export const notify = new NotificationAction(store, nav);
3737
export const wallet = new WalletAction(store, grpc, db, nav, notify);
3838
export const info = new InfoAction(store, grpc, nav, notify);
39-
export const transaction = new TransactionAction(store, grpc, nav);
39+
export const transaction = new TransactionAction(store, grpc, nav, notify);
4040
export const channel = new ChannelAction(store, grpc, nav, notify);
4141
export const invoice = new InvoiceAction(store, grpc, nav, notify, Clipboard);
4242
export const payment = new PaymentAction(store, grpc, nav, notify);

src/action/transaction.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import * as log from './log';
77
import { parseDate, parseSat, toHex } from '../helper';
88

99
class TransactionAction {
10-
constructor(store, grpc, nav) {
10+
constructor(store, grpc, nav, notification) {
1111
this._store = store;
1212
this._grpc = grpc;
1313
this._nav = nav;
14+
this._notification = notification;
1415
}
1516

1617
/**
@@ -133,12 +134,29 @@ class TransactionAction {
133134
async subscribeInvoices() {
134135
const stream = this._grpc.sendStreamCommand('subscribeInvoices');
135136
await new Promise((resolve, reject) => {
136-
stream.on('data', () => this.update());
137+
stream.on('data', invoice => this._receiveInvoice(invoice));
137138
stream.on('end', resolve);
138139
stream.on('error', reject);
139140
stream.on('status', status => log.info(`Invoices update: ${status}`));
140141
});
141142
}
143+
144+
//
145+
// Helper functions
146+
//
147+
148+
async _receiveInvoice(invoice) {
149+
await this.update();
150+
if (!invoice.settled) return;
151+
const { computedTransactions, unitLabel } = this._store;
152+
let inv = computedTransactions.find(tx => tx.id === toHex(invoice.r_hash));
153+
this._notification.display({
154+
type: 'success',
155+
msg: `Invoice success: received ${inv.amountLabel} ${unitLabel}`,
156+
handler: () => this.select({ item: inv }),
157+
handlerLbl: 'View details',
158+
});
159+
}
142160
}
143161

144162
export default TransactionAction;

stories/screen-story.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ sinon.stub(wallet, 'checkSeed');
6767
sinon.stub(wallet, 'checkNewPassword');
6868
sinon.stub(wallet, 'checkPassword');
6969
sinon.stub(wallet, 'getExchangeRate');
70-
const transaction = new TransactionAction(store, grpc, nav);
70+
const transaction = new TransactionAction(store, grpc, nav, notify);
7171
sinon.stub(transaction, 'update');
7272
const invoice = new InvoiceAction(store, grpc, nav, notify, Clipboard);
7373
sinon.stub(invoice, 'generateUri');

test/integration/action/action-integration.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Action Integration Tests', function() {
149149
grpc1 = new GrpcAction(store1, ipc1);
150150
info1 = new InfoAction(store1, grpc1, nav1, notify1);
151151
wallet1 = new WalletAction(store1, grpc1, db1, nav1, notify1);
152-
transactions1 = new TransactionAction(store1, grpc1, nav1);
152+
transactions1 = new TransactionAction(store1, grpc1, nav1, notify1);
153153
channels1 = new ChannelAction(store1, grpc1, nav1, notify1);
154154
invoice1 = new InvoiceAction(store1, grpc1, nav1, notify1);
155155
payments1 = new PaymentAction(store1, grpc1, nav1, notify1);
@@ -161,7 +161,7 @@ describe('Action Integration Tests', function() {
161161
grpc2 = new GrpcAction(store2, ipc2);
162162
info2 = new InfoAction(store2, grpc2, nav2, notify2);
163163
wallet2 = new WalletAction(store2, grpc2, db2, nav2, notify2);
164-
transactions2 = new TransactionAction(store2, grpc2, nav2);
164+
transactions2 = new TransactionAction(store2, grpc2, nav2, notify2);
165165
channels2 = new ChannelAction(store2, grpc2, nav2, notify2);
166166
invoice2 = new InvoiceAction(store2, grpc2, nav2, notify2);
167167
payments2 = new PaymentAction(store2, grpc2, nav2, notify2);

test/unit/action/transaction.spec.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { Store } from '../../../src/store';
22
import GrpcAction from '../../../src/action/grpc';
33
import TransactionAction from '../../../src/action/transaction';
44
import NavAction from '../../../src/action/nav';
5+
import NotificationAction from '../../../src/action/notification';
56
import * as logger from '../../../src/action/log';
67

78
describe('Action Transactions Unit Tests', () => {
89
let store;
910
let sandbox;
1011
let grpc;
1112
let nav;
13+
let notification;
1214
let transaction;
1315

1416
beforeEach(() => {
@@ -18,7 +20,8 @@ describe('Action Transactions Unit Tests', () => {
1820
require('../../../src/config').RETRY_DELAY = 1;
1921
grpc = sinon.createStubInstance(GrpcAction);
2022
nav = sinon.createStubInstance(NavAction);
21-
transaction = new TransactionAction(store, grpc, nav);
23+
notification = sinon.createStubInstance(NotificationAction);
24+
transaction = new TransactionAction(store, grpc, nav, notification);
2225
});
2326

2427
afterEach(() => {
@@ -210,13 +213,38 @@ describe('Action Transactions Unit Tests', () => {
210213
});
211214

212215
it('should update invoices on data event', async () => {
213-
onStub.withArgs('data').yields();
216+
onStub.withArgs('data').yields({});
217+
onStub.withArgs('end').yields();
218+
grpc.sendStreamCommand
219+
.withArgs('subscribeInvoices')
220+
.returns({ on: onStub });
221+
await transaction.subscribeInvoices();
222+
expect(transaction.update, 'was called once');
223+
});
224+
225+
it('should notify the user on settled invoice', async () => {
226+
store.computedTransactions = [{ id: 'cdab' }];
227+
onStub.withArgs('data').yields({
228+
settled: true,
229+
r_hash: Buffer.from('cdab', 'hex'),
230+
});
231+
onStub.withArgs('end').yields();
232+
grpc.sendStreamCommand
233+
.withArgs('subscribeInvoices')
234+
.returns({ on: onStub });
235+
await transaction.subscribeInvoices();
236+
expect(notification.display, 'was called once');
237+
});
238+
239+
it('should not notify the user on an unsettled invoice', async () => {
240+
onStub.withArgs('data').yields({ settled: false });
214241
onStub.withArgs('end').yields();
215242
grpc.sendStreamCommand
216243
.withArgs('subscribeInvoices')
217244
.returns({ on: onStub });
218245
await transaction.subscribeInvoices();
219246
expect(transaction.update, 'was called once');
247+
expect(notification.display, 'was not called');
220248
});
221249

222250
it('should reject in case of error', async () => {

0 commit comments

Comments
 (0)