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

Commit 4e64148

Browse files
valentinewallacetanx
authored andcommitted
Incorporate sendAll logic into payment actions.
1. If the user edits the payment amount, reset sendAll to false. 2. Factor out _sendPayment into a helper and set the payment amount to zero if sendAll is true.
1 parent 64d8bf8 commit 4e64148

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/action/payment.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class PaymentAction {
122122
*/
123123
setAmount({ amount }) {
124124
this._store.payment.amount = amount;
125+
this._store.payment.sendAll = false;
125126
}
126127

127128
/**
@@ -139,6 +140,7 @@ class PaymentAction {
139140
if (await this.decodeInvoice({ invoice: this._store.payment.address })) {
140141
this._nav.goPayLightningConfirm();
141142
} else if (isAddress(this._store.payment.address)) {
143+
this._store.payment.sendAll = false;
142144
this._nav.goPayBitcoin();
143145
} else {
144146
this._notification.display({ msg: 'Invalid invoice or address' });
@@ -238,11 +240,7 @@ class PaymentAction {
238240
}, PAYMENT_TIMEOUT);
239241
this._nav.goWait();
240242
try {
241-
const { payment, settings } = this._store;
242-
await this._grpc.sendCommand('sendCoins', {
243-
addr: payment.address,
244-
amount: toSatoshis(payment.amount, settings),
245-
});
243+
await this._sendPayment();
246244
this._nav.goPayBitcoinDone();
247245
} catch (err) {
248246
this._nav.goPayBitcoinConfirm();
@@ -252,6 +250,16 @@ class PaymentAction {
252250
}
253251
}
254252

253+
async _sendPayment() {
254+
const { payment, settings } = this._store;
255+
let amount = payment.sendAll ? 0 : toSatoshis(payment.amount, settings);
256+
await this._grpc.sendCommand('sendCoins', {
257+
addr: payment.address,
258+
amount,
259+
sendAll: payment.sendAll,
260+
});
261+
}
262+
255263
/**
256264
* Send the amount specified in the invoice as a lightning transaction and
257265
* display the wait screen while the payment confirms.

test/unit/action/payment.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ describe('Action Payments Unit Tests', () => {
209209
payment.setAmount({ amount: 'some-amount' });
210210
expect(store.payment.amount, 'to equal', 'some-amount');
211211
});
212+
213+
it('should reset sendAll if set', () => {
214+
store.payment.sendAll = true;
215+
payment.setAmount({ amount: 'some-amount' });
216+
expect(store.payment.amount, 'to equal', 'some-amount');
217+
expect(store.payment.sendAll, 'to be false');
218+
});
212219
});
213220

214221
describe('checkType()', () => {
@@ -305,6 +312,23 @@ describe('Action Payments Unit Tests', () => {
305312
expect(grpc.sendCommand, 'was called with', 'sendCoins', {
306313
addr: 'some-address',
307314
amount: 1000,
315+
sendAll: false,
316+
});
317+
expect(nav.goPayBitcoinDone, 'was called once');
318+
expect(notification.display, 'was not called');
319+
});
320+
321+
it('should set amount to 0 on sendAll', async () => {
322+
store.payment.sendAll = true;
323+
store.payment.amount = '0.00001';
324+
store.payment.address = 'some-address';
325+
grpc.sendCommand.withArgs('sendCoins').resolves();
326+
await payment.payBitcoin();
327+
expect(nav.goWait, 'was called once');
328+
expect(grpc.sendCommand, 'was called with', 'sendCoins', {
329+
addr: 'some-address',
330+
amount: 0,
331+
sendAll: true,
308332
});
309333
expect(nav.goPayBitcoinDone, 'was called once');
310334
expect(notification.display, 'was not called');

0 commit comments

Comments
 (0)