Unverified Commit 0f2947d8 authored by Valentine Wallace's avatar Valentine Wallace Committed by Tankred Hase

Simplify max on-chain payment logic.

parent 725a5de4
......@@ -131,27 +131,11 @@ class PaymentAction {
* @return {Promise<undefined>}
*/
async setMax() {
const timeout = setTimeout(() => {
return this._notification.display({
type: 'error',
msg: 'Setting max payment amount timed out!',
});
}, PAYMENT_TIMEOUT);
const { payment, balanceSatoshis, settings } = this._store;
let feeSat;
let amtSat = Math.floor(0.99 * balanceSatoshis);
while (!feeSat) {
try {
feeSat = await this._getFeeEstimateSat({
addr: payment.address,
amtSat,
});
} catch (err) {
amtSat = Math.floor(0.99 * amtSat);
} finally {
clearTimeout(timeout);
}
}
let amtSat = Math.floor(0.98 * balanceSatoshis);
payment.amount = toAmount(amtSat, settings);
await this.estimateFee();
amtSat = balanceSatoshis - toSatoshis(payment.fee, settings);
payment.amount = toAmount(amtSat, settings);
payment.sendAll = true;
}
......@@ -232,21 +216,12 @@ class PaymentAction {
*/
async estimateFee() {
const { payment, settings } = this._store;
const amtSat = toSatoshis(payment.amount, settings);
const feeSat = await this._getFeeEstimateSat({
addr: payment.address,
amtSat,
});
payment.fee = toAmount(feeSat, settings);
}
async _getFeeEstimateSat({ addr, amtSat }) {
const AddrToAmount = {};
AddrToAmount[addr] = amtSat;
AddrToAmount[payment.address] = toSatoshis(payment.amount, settings);
const { feeSat } = await this._grpc.sendCommand('estimateFee', {
AddrToAmount,
});
return feeSat;
payment.fee = toAmount(feeSat, settings);
}
/**
......
......@@ -303,22 +303,14 @@ describe('Action Payments Unit Tests', () => {
});
describe('setMax()', () => {
it('should set the payment amount to ~99% of the wallet balance', async () => {
it('should set the payment amount to the wallet balance minus fee', async () => {
store.payment.address = 'some-address';
store.balanceSatoshis = 100000;
grpc.sendCommand.onSecondCall().resolves({ feeSat: 100 });
grpc.sendCommand.resolves({ feeSat: 100 });
await payment.setMax();
expect(store.payment.amount, 'to match', /^0[,.]0{3}9{1}8{1}0{1}1{1}$/);
expect(store.payment.amount, 'to match', /^0[,.]0{3}9{3}$/);
expect(store.payment.sendAll, 'to be true');
});
it('should display error notification on timeout', async () => {
grpc.sendCommand.callsFake(() => nap(50));
payment.setMax();
await nap(10);
expect(store.payment.amount, 'to be', '');
expect(store.payment.sendAll, 'to be false');
});
});
describe('payBitcoin()', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment