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

Commit 762a801

Browse files
Merge pull request #448 from lightninglabs/show-tx-fee
Display tx fee on lightning pay confirm view
2 parents 3e16180 + bc014dd commit 762a801

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/action/payment.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,31 @@ class PaymentAction {
7070
});
7171
payment.amount = toAmount(parseSat(request.num_satoshis), settings.unit);
7272
payment.note = request.description;
73+
await this.estimateLightningFee({
74+
destination: request.destination,
75+
satAmt: request.num_satoshis,
76+
});
7377
return true;
7478
} catch (err) {
7579
log.info(`Decoding payment request failed: ${err.message}`);
7680
return false;
7781
}
7882
}
7983

84+
async estimateLightningFee({ destination, satAmt }) {
85+
try {
86+
const { payment, settings } = this._store;
87+
const { routes } = await this._grpc.sendCommand('queryRoutes', {
88+
pub_key: destination,
89+
amt: satAmt,
90+
num_routes: 1,
91+
});
92+
payment.fee = toAmount(parseSat(routes[0].total_fees), settings.unit);
93+
} catch (err) {
94+
log.error(`Estimating lightning fee failed!`, err);
95+
}
96+
}
97+
8098
async payBitcoin() {
8199
try {
82100
const { payment, settings } = this._store;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,16 @@ describe('Action Integration Tests', function() {
344344
expect(isValid, 'to be', false);
345345
});
346346

347-
it('should decode invoice and return true', async () => {
347+
it('should decode invoice, set fee and return true', async () => {
348348
const isValid = await payments1.decodeInvoice({
349349
invoice: store2.invoice.uri,
350350
});
351351
expect(isValid, 'to be', true);
352+
expect(
353+
parseFloat(store1.payment.fee),
354+
'to be greater than or equal to',
355+
0
356+
);
352357
});
353358

354359
it('should send lightning payment from request', async () => {

test/unit/action/payment.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,48 @@ describe('Action Payments Unit Tests', () => {
139139
grpc.sendCommand.withArgs('decodePayReq').resolves({
140140
num_satoshis: '1700',
141141
description: 'foo',
142+
destination: 'bar',
142143
});
144+
grpc.sendCommand
145+
.withArgs('queryRoutes', {
146+
pub_key: 'bar',
147+
amt: '1700',
148+
num_routes: 1,
149+
})
150+
.resolves({
151+
routes: [{ total_fees: '100' }],
152+
});
143153
const isValid = await payment.decodeInvoice({ invoice: 'some-invoice' });
144154
expect(isValid, 'to be', true);
145155
expect(store.payment.amount, 'to match', /^0[,.]0{4}1{1}7{1}$/);
146156
expect(store.payment.note, 'to be', 'foo');
157+
expect(store.payment.fee, 'to match', /^0[,.]0{5}1{1}$/);
147158
});
148159

149-
it('should set response to null on error', async () => {
160+
it('should set nothing on decode error', async () => {
150161
grpc.sendCommand.withArgs('decodePayReq').rejects(new Error('Boom!'));
151162
const isValid = await payment.decodeInvoice({ invoice: 'some-invoice' });
152163
expect(isValid, 'to be', false);
153164
expect(store.payment.amount, 'to be', '');
154165
expect(store.payment.note, 'to be', '');
166+
expect(store.payment.fee, 'to be', '');
155167
expect(logger.info, 'was called once');
156168
});
169+
170+
it('should set no fee on query route error', async () => {
171+
grpc.sendCommand.withArgs('decodePayReq').resolves({
172+
num_satoshis: '1700',
173+
description: 'foo',
174+
destination: 'bar',
175+
});
176+
grpc.sendCommand.withArgs('queryRoutes').rejects(new Error('Boom!'));
177+
const isValid = await payment.decodeInvoice({ invoice: 'some-invoice' });
178+
expect(isValid, 'to be', true);
179+
expect(store.payment.amount, 'to match', /^0[,.]0{4}1{1}7{1}$/);
180+
expect(store.payment.note, 'to be', 'foo');
181+
expect(store.payment.fee, 'to be', '');
182+
expect(logger.error, 'was called once');
183+
});
157184
});
158185

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

0 commit comments

Comments
 (0)