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

Commit 04a4e7f

Browse files
Merge pull request #465 from lightninglabs/fiat-amount-input
Allow input amount in fiat if selected - payment + invoice
2 parents 38100c6 + a65e3e3 commit 04a4e7f

File tree

15 files changed

+207
-85
lines changed

15 files changed

+207
-85
lines changed

src/action/channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ChannelAction {
133133
async connectAndOpen() {
134134
try {
135135
const { channel, settings } = this._store;
136-
const amount = toSatoshis(channel.amount, settings.unit);
136+
const amount = toSatoshis(channel.amount, settings);
137137
if (!channel.pubkeyAtHost.includes('@')) {
138138
return this._notification.display({ msg: 'Please enter pubkey@host' });
139139
}

src/action/invoice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class InvoiceAction {
3131
try {
3232
const { invoice, settings } = this._store;
3333
const response = await this._grpc.sendCommand('addInvoice', {
34-
value: toSatoshis(invoice.amount, settings.unit),
34+
value: toSatoshis(invoice.amount, settings),
3535
memo: invoice.note,
3636
});
3737
invoice.encoded = response.payment_request;

src/action/payment.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class PaymentAction {
6868
const request = await this._grpc.sendCommand('decodePayReq', {
6969
pay_req: invoice.replace(PREFIX_URI, ''),
7070
});
71-
payment.amount = toAmount(parseSat(request.num_satoshis), settings.unit);
71+
payment.amount = toAmount(parseSat(request.num_satoshis), settings);
7272
payment.note = request.description;
7373
await this.estimateLightningFee({
7474
destination: request.destination,
@@ -89,7 +89,7 @@ class PaymentAction {
8989
amt: satAmt,
9090
num_routes: 1,
9191
});
92-
payment.fee = toAmount(parseSat(routes[0].total_fees), settings.unit);
92+
payment.fee = toAmount(parseSat(routes[0].total_fees), settings);
9393
} catch (err) {
9494
log.error(`Estimating lightning fee failed!`, err);
9595
}
@@ -100,7 +100,7 @@ class PaymentAction {
100100
const { payment, settings } = this._store;
101101
await this._grpc.sendCommand('sendCoins', {
102102
addr: payment.address,
103-
amount: toSatoshis(payment.amount, settings.unit),
103+
amount: toSatoshis(payment.amount, settings),
104104
});
105105
this._nav.goPayBitcoinDone();
106106
} catch (err) {

src/computed/payment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const ComputedPayment = store => {
99
paymentFeeLabel: computed(() => toLabel(store.payment.fee, store.settings)),
1010
paymentTotalLabel: computed(() => {
1111
const { payment, settings } = store;
12-
const satAmount = toSatoshis(payment.amount, settings.unit);
13-
const satFee = toSatoshis(payment.fee, settings.unit);
12+
const satAmount = toSatoshis(payment.amount, settings);
13+
const satFee = toSatoshis(payment.fee, settings);
1414
return toAmountLabel(satAmount + satFee, settings);
1515
}),
1616
});

src/computed/setting.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { UNITS, FIATS } from '../config';
55
const ComputedSetting = store => {
66
extendObservable(store, {
77
selectedUnitLabel: computed(() => getUnitLabel(store.settings.unit)),
8-
selectedFiatLabel: computed(() => FIATS[store.settings.fiat].display),
8+
selectedFiatLabel: computed(() => FIATS[store.settings.fiat].displayLong),
99
satUnitLabel: computed(() => getUnitLabel('sat')),
1010
bitUnitLabel: computed(() => getUnitLabel('bit')),
1111
btcUnitLabel: computed(() => getUnitLabel('btc')),
12-
usdFiatLabel: computed(() => FIATS['usd'].display),
13-
eurFiatLabel: computed(() => FIATS['eur'].display),
14-
gbpFiatLabel: computed(() => FIATS['gbp'].display),
12+
usdFiatLabel: computed(() => FIATS['usd'].displayLong),
13+
eurFiatLabel: computed(() => FIATS['eur'].displayLong),
14+
gbpFiatLabel: computed(() => FIATS['gbp'].displayLong),
1515
});
1616
};
1717

src/computed/wallet.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { computed, extendObservable } from 'mobx';
22
import { toAmountLabel } from '../helper';
3-
import { UNITS } from '../config';
3+
import { UNITS, FIATS } from '../config';
44

55
const ComputedWallet = store => {
66
extendObservable(store, {
@@ -13,6 +13,10 @@ const ComputedWallet = store => {
1313
channelBalanceLabel: computed(() =>
1414
toAmountLabel(store.channelBalanceSatoshis, store.settings)
1515
),
16+
unitFiatLabel: computed(() => {
17+
const { displayFiat, unit, fiat } = store.settings;
18+
return displayFiat ? FIATS[fiat].display : UNITS[unit].display;
19+
}),
1620
unitLabel: computed(() => {
1721
const { settings } = store;
1822
return !settings.displayFiat ? UNITS[settings.unit].display : null;

src/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ module.exports.UNITS = {
2121
btc: { display: 'BTC', displayLong: 'Bitcoin', denominator: 100000000 },
2222
};
2323
module.exports.FIATS = {
24-
usd: { display: 'US Dollar' },
25-
eur: { display: 'Euro' },
26-
gbp: { display: 'British Pound' },
24+
usd: { display: '$', displayLong: 'US Dollar' },
25+
eur: { display: '€', displayLong: 'Euro' },
26+
gbp: { display: '£', displayLong: 'British Pound' },
2727
};
2828
module.exports.DEFAULT_UNIT = 'btc';
2929
module.exports.DEFAULT_FIAT = 'usd';

src/helper.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,45 @@ export const parseSat = satoshis => {
5656
};
5757

5858
/**
59-
* Convert a string formatted BTC amount to satoshis
60-
* @param {string} amount The amount e.g. '0.0001'
61-
* @param {string} unit The BTC unit e.g. 'btc' or 'bit'
62-
* @return {number} The satoshis as an integer
59+
* Convert a string formatted btc/fiat amount to satoshis
60+
* @param {string} amount The amount e.g. '0.0001'
61+
* @param {Object} settings Contains the current exchange rate
62+
* @return {number} The satoshis as an integer
6363
*/
64-
export const toSatoshis = (amount, unit) => {
64+
export const toSatoshis = (amount, settings) => {
6565
if (
6666
typeof amount !== 'string' ||
6767
!/^[0-9]*[.]?[0-9]*$/.test(amount) ||
68-
!UNITS[unit]
68+
!settings ||
69+
typeof settings.displayFiat !== 'boolean'
6970
) {
70-
throw new Error('Missing args!');
71+
throw new Error('Invalid input!');
72+
}
73+
if (settings.displayFiat) {
74+
const rate = settings.exchangeRate[settings.fiat] || 0;
75+
return Math.round(Number(amount) * rate * UNITS.btc.denominator);
76+
} else {
77+
return Math.round(Number(amount) * UNITS[settings.unit].denominator);
7178
}
72-
return Math.round(Number(amount) * UNITS[unit].denominator);
7379
};
7480

7581
/**
7682
* Convert satoshis to a BTC values than can set as a text input value
7783
* @param {number} satoshis The value as a string or number
78-
* @param {string} unit The BTC unit e.g. 'btc' or 'bit'
84+
* @param {Object} settings Contains the current exchange rate
7985
* @return {string} The amount formatted as '0.0001'
8086
*/
81-
export const toAmount = (satoshis, unit) => {
82-
if (!Number.isInteger(satoshis) || !UNITS[unit]) {
87+
export const toAmount = (satoshis, settings) => {
88+
if (
89+
!Number.isInteger(satoshis) ||
90+
!settings ||
91+
typeof settings.displayFiat !== 'boolean'
92+
) {
8393
throw new Error('Invalid input!');
8494
}
85-
const num = satoshis / UNITS[unit].denominator;
95+
const num = settings.displayFiat
96+
? calculateExchangeRate(satoshis, settings)
97+
: satoshis / UNITS[settings.unit].denominator;
8698
return num.toLocaleString('en-US', {
8799
useGrouping: false,
88100
maximumFractionDigits: 8,
@@ -103,8 +115,7 @@ export const calculateExchangeRate = (satoshis, settings) => {
103115
throw new Error('Invalid input!');
104116
}
105117
const rate = settings.exchangeRate[settings.fiat] || 0;
106-
const balance = satoshis / rate / UNITS.btc.denominator;
107-
return formatFiat(balance, settings.fiat);
118+
return satoshis / rate / UNITS.btc.denominator;
108119
};
109120

110121
/**
@@ -122,22 +133,19 @@ export const toAmountLabel = (satoshis, settings) => {
122133
throw new Error('Invalid input!');
123134
}
124135
return settings.displayFiat
125-
? calculateExchangeRate(satoshis, settings)
126-
: formatNumber(toAmount(satoshis, settings.unit));
136+
? formatFiat(calculateExchangeRate(satoshis, settings), settings.fiat)
137+
: formatNumber(toAmount(satoshis, settings));
127138
};
128139

129140
/**
130-
* Convert a string formatted BTC amount either to fiat or the selected BTC unit.
141+
* Convert a string formatted btc/fiat amount either to fiat or the selected BTC unit.
131142
* The output should be used throughout the UI for value labels.
132-
* @param {string} amount The amount e.g. '0.0001'
143+
* @param {string} amount The amount e.g. '0.0001'
133144
* @param {Object} settings Contains the current exchange rate
134145
* @return {string} The corresponding value label
135146
*/
136147
export const toLabel = (amount, settings) => {
137-
if (!settings) {
138-
throw new Error('Missing args!');
139-
}
140-
const satoshis = toSatoshis(amount, settings.unit);
148+
const satoshis = toSatoshis(amount, settings);
141149
return toAmountLabel(satoshis, settings);
142150
};
143151

src/view/channel-create.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ const ChannelCreateView = ({ store, nav, channel }) => (
4444
onChangeText={amount => channel.setAmount({ amount })}
4545
onSubmitEditing={() => channel.connectAndOpen()}
4646
/>
47-
<BalanceLabelUnit style={styles.unit}>{store.unit}</BalanceLabelUnit>
47+
<BalanceLabelUnit style={styles.unit}>
48+
{store.unitFiatLabel}
49+
</BalanceLabelUnit>
4850
</BalanceLabel>
4951
<FormStretcher>
5052
<InputField

src/view/invoice.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ const InvoiceView = ({ store, nav, invoice }) => (
4444
onChangeText={amount => invoice.setAmount({ amount })}
4545
onSubmitEditing={() => invoice.generateUri()}
4646
/>
47-
<BalanceLabelUnit style={styles.unit}>{store.unit}</BalanceLabelUnit>
47+
<BalanceLabelUnit style={styles.unit}>
48+
{store.unitFiatLabel}
49+
</BalanceLabelUnit>
4850
</BalanceLabel>
4951
<FormStretcher>
5052
<InputField

0 commit comments

Comments
 (0)