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

Commit 0f2947d

Browse files
valentinewallacetanx
authored andcommitted
Simplify max on-chain payment logic.
1 parent 725a5de commit 0f2947d

File tree

2 files changed

+9
-42
lines changed

2 files changed

+9
-42
lines changed

src/action/payment.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,11 @@ class PaymentAction {
131131
* @return {Promise<undefined>}
132132
*/
133133
async setMax() {
134-
const timeout = setTimeout(() => {
135-
return this._notification.display({
136-
type: 'error',
137-
msg: 'Setting max payment amount timed out!',
138-
});
139-
}, PAYMENT_TIMEOUT);
140134
const { payment, balanceSatoshis, settings } = this._store;
141-
let feeSat;
142-
let amtSat = Math.floor(0.99 * balanceSatoshis);
143-
while (!feeSat) {
144-
try {
145-
feeSat = await this._getFeeEstimateSat({
146-
addr: payment.address,
147-
amtSat,
148-
});
149-
} catch (err) {
150-
amtSat = Math.floor(0.99 * amtSat);
151-
} finally {
152-
clearTimeout(timeout);
153-
}
154-
}
135+
let amtSat = Math.floor(0.98 * balanceSatoshis);
136+
payment.amount = toAmount(amtSat, settings);
137+
await this.estimateFee();
138+
amtSat = balanceSatoshis - toSatoshis(payment.fee, settings);
155139
payment.amount = toAmount(amtSat, settings);
156140
payment.sendAll = true;
157141
}
@@ -232,21 +216,12 @@ class PaymentAction {
232216
*/
233217
async estimateFee() {
234218
const { payment, settings } = this._store;
235-
const amtSat = toSatoshis(payment.amount, settings);
236-
const feeSat = await this._getFeeEstimateSat({
237-
addr: payment.address,
238-
amtSat,
239-
});
240-
payment.fee = toAmount(feeSat, settings);
241-
}
242-
243-
async _getFeeEstimateSat({ addr, amtSat }) {
244219
const AddrToAmount = {};
245-
AddrToAmount[addr] = amtSat;
220+
AddrToAmount[payment.address] = toSatoshis(payment.amount, settings);
246221
const { feeSat } = await this._grpc.sendCommand('estimateFee', {
247222
AddrToAmount,
248223
});
249-
return feeSat;
224+
payment.fee = toAmount(feeSat, settings);
250225
}
251226

252227
/**

test/unit/action/payment.spec.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,14 @@ describe('Action Payments Unit Tests', () => {
303303
});
304304

305305
describe('setMax()', () => {
306-
it('should set the payment amount to ~99% of the wallet balance', async () => {
306+
it('should set the payment amount to the wallet balance minus fee', async () => {
307307
store.payment.address = 'some-address';
308308
store.balanceSatoshis = 100000;
309-
grpc.sendCommand.onSecondCall().resolves({ feeSat: 100 });
309+
grpc.sendCommand.resolves({ feeSat: 100 });
310310
await payment.setMax();
311-
expect(store.payment.amount, 'to match', /^0[,.]0{3}9{1}8{1}0{1}1{1}$/);
311+
expect(store.payment.amount, 'to match', /^0[,.]0{3}9{3}$/);
312312
expect(store.payment.sendAll, 'to be true');
313313
});
314-
315-
it('should display error notification on timeout', async () => {
316-
grpc.sendCommand.callsFake(() => nap(50));
317-
payment.setMax();
318-
await nap(10);
319-
expect(store.payment.amount, 'to be', '');
320-
expect(store.payment.sendAll, 'to be false');
321-
});
322314
});
323315

324316
describe('payBitcoin()', () => {

0 commit comments

Comments
 (0)