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

Commit 1e31262

Browse files
committed
Implement fee selection in payment action
1 parent 967e6cb commit 1e31262

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

src/action/payment.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ import {
77
PREFIX_REGEX,
88
PAYMENT_TIMEOUT,
99
POLL_STORE_TIMEOUT,
10-
SEND_TARGET_CONF,
10+
LOW_TARGET_CONF,
11+
MED_TARGET_CONF,
12+
HIGH_TARGET_CONF,
1113
} from '../config';
12-
import { toSatoshis, toAmount, isLnUri, isAddress, nap } from '../helper';
14+
import {
15+
toSatoshis,
16+
toAmount,
17+
toAmountLabel,
18+
isLnUri,
19+
isAddress,
20+
nap,
21+
} from '../helper';
1322
import * as log from './log';
1423

1524
class PaymentAction {
@@ -105,6 +114,7 @@ class PaymentAction {
105114
init() {
106115
this._store.payment.address = '';
107116
this._store.payment.amount = '';
117+
this._store.payment.targetConf = MED_TARGET_CONF;
108118
this._store.payment.fee = '';
109119
this._store.payment.note = '';
110120
this._store.payment.useScanner = false;
@@ -223,14 +233,40 @@ class PaymentAction {
223233
* @return {Promise<undefined>}
224234
*/
225235
async estimateFee() {
236+
const { payment } = this._store;
237+
payment.feeEstimates = [];
238+
await this._fetchEstimate(LOW_TARGET_CONF, 'Low');
239+
await this._fetchEstimate(MED_TARGET_CONF, 'Med');
240+
await this._fetchEstimate(HIGH_TARGET_CONF, 'High');
241+
payment.fee = payment.feeEstimates[1].fee;
242+
}
243+
244+
async _fetchEstimate(targetConf, prio) {
226245
const { payment, settings } = this._store;
227246
const AddrToAmount = {};
228247
AddrToAmount[payment.address] = toSatoshis(payment.amount, settings);
229-
const { feeSat } = await this._grpc.sendCommand('estimateFee', {
230-
AddrToAmount,
231-
targetConf: SEND_TARGET_CONF,
248+
const { feeSat, feerateSatPerByte } = await this._grpc.sendCommand(
249+
'estimateFee',
250+
{
251+
AddrToAmount,
252+
targetConf,
253+
}
254+
);
255+
payment.feeEstimates.push({
256+
targetConf,
257+
prio,
258+
satPerByte: feerateSatPerByte,
259+
fee: toAmount(feeSat, settings),
260+
feeLabel: toAmountLabel(feeSat, settings),
232261
});
233-
payment.fee = toAmount(feeSat, settings);
262+
}
263+
264+
setTargetConf({ targetConf }) {
265+
const { payment } = this._store;
266+
payment.targetConf = targetConf;
267+
payment.fee = payment.feeEstimates.find(
268+
e => e.targetConf === targetConf
269+
).fee;
234270
}
235271

236272
/**
@@ -286,7 +322,7 @@ class PaymentAction {
286322
await this._grpc.sendCommand('sendCoins', {
287323
addr: payment.address,
288324
amount,
289-
targetConf: SEND_TARGET_CONF,
325+
targetConf: payment.targetConf,
290326
sendAll: payment.sendAll,
291327
});
292328
}

src/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module.exports.PREFIX_URI = `${prefixName}:`;
2424
module.exports.PREFIX_REGEX = /^[a-zA-Z]*:/;
2525

2626
module.exports.DEFAULT_ROUTE = 'Welcome';
27-
module.exports.SEND_TARGET_CONF = 6;
27+
module.exports.LOW_TARGET_CONF = 26;
28+
module.exports.MED_TARGET_CONF = 16;
29+
module.exports.HIGH_TARGET_CONF = 4;
2830
module.exports.PIN_LENGTH = 6;
2931
module.exports.MIN_PASSWORD_LENGTH = 8;
3032
module.exports.STRONG_PASSWORD_LENGTH = 12;

src/store.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import ComputedPayment from './computed/payment';
1414
import ComputedNotification from './computed/notification';
1515
import ComputedSetting from './computed/setting';
1616
import ComputedSeed from './computed/seed';
17-
import { DEFAULT_ROUTE, DEFAULT_UNIT, DEFAULT_FIAT } from './config';
17+
import {
18+
DEFAULT_ROUTE,
19+
DEFAULT_UNIT,
20+
DEFAULT_FIAT,
21+
MED_TARGET_CONF,
22+
} from './config';
1823

1924
export class Store {
2025
constructor() {
@@ -69,6 +74,8 @@ export class Store {
6974
payment: {
7075
address: '',
7176
amount: '',
77+
targetConf: MED_TARGET_CONF,
78+
feeEstimates: [],
7279
fee: '',
7380
note: '',
7481
sendAll: false,

0 commit comments

Comments
 (0)