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

Commit 149bbe2

Browse files
authored
Merge pull request #464 from lightninglabs/fiat-confirm
Fiat confirm
2 parents 20a9984 + d752ee2 commit 149bbe2

File tree

9 files changed

+118
-16
lines changed

9 files changed

+118
-16
lines changed

src/computed/invoice.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { computed, extendObservable } from 'mobx';
2-
import { formatNumber } from '../helper';
2+
import { toLabel } from '../helper';
33

44
const ComputedInvoice = store => {
55
extendObservable(store, {
6-
invoiceAmountLabel: computed(() => formatNumber(store.invoice.amount)),
6+
invoiceAmountLabel: computed(() =>
7+
toLabel(store.invoice.amount, store.settings)
8+
),
79
});
810
};
911

src/computed/payment.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { computed, extendObservable } from 'mobx';
2-
import { formatNumber } from '../helper';
2+
import { toSatoshis, toAmountLabel, toLabel } from '../helper';
33

44
const ComputedPayment = store => {
55
extendObservable(store, {
6-
paymentAmountLabel: computed(() => formatNumber(store.payment.amount)),
7-
paymentFeeLabel: computed(() => formatNumber(store.payment.fee)),
6+
paymentAmountLabel: computed(() =>
7+
toLabel(store.payment.amount, store.settings)
8+
),
9+
paymentFeeLabel: computed(() => toLabel(store.payment.fee, store.settings)),
810
paymentTotalLabel: computed(() => {
9-
const { payment } = store;
10-
return formatNumber(Number(payment.amount) + Number(payment.fee));
11+
const { payment, settings } = store;
12+
const satAmount = toSatoshis(payment.amount, settings.unit);
13+
const satFee = toSatoshis(payment.fee, settings.unit);
14+
return toAmountLabel(satAmount + satFee, settings);
1115
}),
1216
});
1317
};

src/helper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ export const toAmountLabel = (satoshis, settings) => {
126126
: formatNumber(toAmount(satoshis, settings.unit));
127127
};
128128

129+
/**
130+
* Convert a string formatted BTC amount either to fiat or the selected BTC unit.
131+
* The output should be used throughout the UI for value labels.
132+
* @param {string} amount The amount e.g. '0.0001'
133+
* @param {Object} settings Contains the current exchange rate
134+
* @return {string} The corresponding value label
135+
*/
136+
export const toLabel = (amount, settings) => {
137+
if (!settings) {
138+
throw new Error('Missing args!');
139+
}
140+
const satoshis = toSatoshis(amount, settings.unit);
141+
return toAmountLabel(satoshis, settings);
142+
};
143+
129144
/**
130145
* Split '-' separated words and convert to uppercase
131146
* @param {string} value The input string

src/view/invoice-qr.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ const InvoiceQRView = ({ store, nav, invoice }) => (
6666
<BalanceLabelNumeral style={styles.numeral}>
6767
{store.invoiceAmountLabel}
6868
</BalanceLabelNumeral>
69-
<BalanceLabelUnit style={styles.unit}>{store.unit}</BalanceLabelUnit>
69+
<BalanceLabelUnit style={styles.unit}>
70+
{store.unitLabel}
71+
</BalanceLabelUnit>
7072
</BalanceLabel>
7173
<NamedField name="Note">{store.invoice.note}</NamedField>
7274
<QRCode style={styles.qrcode}>{store.invoice.uri}</QRCode>

src/view/pay-bitcoin-confirm.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ const PayBitcoinConfirmView = ({ store, nav, payment }) => (
6464
{store.paymentAmountLabel}
6565
</BalanceLabelNumeral>
6666
<BalanceLabelUnit style={styles.unit}>
67-
{store.unit}
67+
{store.unitLabel}
6868
</BalanceLabelUnit>
6969
</BalanceLabel>
7070
<NamedField name="Fee">
71-
{store.paymentFeeLabel} {store.unit}
71+
{store.paymentFeeLabel} {store.unitLabel}
7272
</NamedField>
7373
<NamedField name="Total" style={styles.totalLbl}>
74-
{store.paymentTotalLabel} {store.unit}
74+
{store.paymentTotalLabel} {store.unitLabel}
7575
</NamedField>
7676
</FormStretcher>
7777
<PillButton

src/view/pay-lightning-confirm.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ const PayLightningConfirmView = ({ store, nav, payment }) => (
5959
{store.paymentAmountLabel}
6060
</BalanceLabelNumeral>
6161
<BalanceLabelUnit style={styles.unit}>
62-
{store.unit}
62+
{store.unitLabel}
6363
</BalanceLabelUnit>
6464
</BalanceLabel>
6565
<NamedField name="Fee">
66-
{store.paymentFeeLabel} {store.unit}
66+
{store.paymentFeeLabel} {store.unitLabel}
6767
</NamedField>
6868
<NamedField name="Total" style={styles.totalLbl}>
69-
{store.paymentTotalLabel} {store.unit}
69+
{store.paymentTotalLabel} {store.unitLabel}
7070
</NamedField>
7171
</FormStretcher>
7272
<PillButton

test/unit/computed/invoice.spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ describe('Computed Invoice Unit Tests', () => {
1414
expect(store.invoiceAmountLabel, 'to equal', '0');
1515
});
1616

17-
it('should format amount', () => {
17+
it('should format btc amount', () => {
1818
store.invoice.amount = '0.1001';
1919
ComputedInvoice(store);
2020
expect(store.invoiceAmountLabel, 'to match', /^0[,.]1{1}0{2}1{1}$/);
2121
});
22+
23+
it('should format fiat amount', () => {
24+
store.settings.displayFiat = true;
25+
store.settings.exchangeRate.usd = 0.00014503;
26+
store.invoice.amount = '0.1001';
27+
ComputedInvoice(store);
28+
expect(store.invoiceAmountLabel, 'to match', /690[,.]20/);
29+
});
2230
});
2331
});

test/unit/computed/payment.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Computed Payment Unit Tests', () => {
1616
expect(store.paymentTotalLabel, 'to equal', '0');
1717
});
1818

19-
it('should calculate total', () => {
19+
it('should calculate btc total', () => {
2020
store.payment.fee = '0.0001';
2121
store.payment.amount = '0.1';
2222
ComputedPayment(store);
@@ -25,6 +25,17 @@ describe('Computed Payment Unit Tests', () => {
2525
expect(store.paymentTotalLabel, 'to match', /^0[,.]1{1}0{2}1{1}$/);
2626
});
2727

28+
it('should calculate fiat total', () => {
29+
store.settings.displayFiat = true;
30+
store.settings.exchangeRate.usd = 0.00014503;
31+
store.payment.fee = '0.0001';
32+
store.payment.amount = '0.1';
33+
ComputedPayment(store);
34+
expect(store.paymentAmountLabel, 'to match', /689[,.]51/);
35+
expect(store.paymentFeeLabel, 'to match', /0[,.]69/);
36+
expect(store.paymentTotalLabel, 'to match', /690[,.]20/);
37+
});
38+
2839
it('should ignore fee if blank', () => {
2940
store.payment.fee = '';
3041
store.payment.amount = '0.1';

test/unit/helper.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,66 @@ describe('Helpers Unit Tests', () => {
450450
});
451451
});
452452

453+
describe('toLabel()', () => {
454+
let settings;
455+
456+
beforeEach(() => {
457+
settings = {
458+
unit: 'btc',
459+
fiat: 'usd',
460+
exchangeRate: { usd: 0.00014503 },
461+
displayFiat: true,
462+
};
463+
});
464+
465+
it('should throw error if amount is undefined', () => {
466+
expect(
467+
helpers.toLabel.bind(null, undefined, settings),
468+
'to throw',
469+
/Missing/
470+
);
471+
});
472+
473+
it('should throw error if amount is null', () => {
474+
expect(helpers.toLabel.bind(null, null, settings), 'to throw', /Missing/);
475+
});
476+
477+
it('should throw error if amount is number', () => {
478+
expect(helpers.toLabel.bind(null, 0.1, settings), 'to throw', /Missing/);
479+
});
480+
481+
it('should throw error if amount is separated with a comma', () => {
482+
expect(
483+
helpers.toLabel.bind(null, '0,1', settings),
484+
'to throw',
485+
/Missing/
486+
);
487+
});
488+
489+
it('should throw error if unit is undefined', () => {
490+
expect(
491+
helpers.toLabel.bind(null, '100', undefined),
492+
'to throw',
493+
/Missing/
494+
);
495+
});
496+
497+
it('should be 0 for empty amount', () => {
498+
const num = helpers.toLabel('', settings);
499+
expect(num, 'to match', /0{1}[,.]0{2}/);
500+
});
501+
502+
it('should work for string input', () => {
503+
const num = helpers.toLabel('0.10', settings);
504+
expect(num, 'to match', /689[,.]51/);
505+
});
506+
it('should format a number value', () => {
507+
settings.displayFiat = false;
508+
const lbl = helpers.toLabel('0.10', settings);
509+
expect(lbl, 'to match', /0[,.]1/);
510+
});
511+
});
512+
453513
describe('toCaps()', () => {
454514
it('should work for undefined', () => {
455515
const caps = helpers.toCaps(undefined);

0 commit comments

Comments
 (0)