Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/action/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ChannelAction {
async connectAndOpen() {
try {
const { channel, settings } = this._store;
const amount = toSatoshis(channel.amount, settings.unit);
const amount = toSatoshis(channel.amount, settings);
if (!channel.pubkeyAtHost.includes('@')) {
return this._notification.display({ msg: 'Please enter pubkey@host' });
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class InvoiceAction {
try {
const { invoice, settings } = this._store;
const response = await this._grpc.sendCommand('addInvoice', {
value: toSatoshis(invoice.amount, settings.unit),
value: toSatoshis(invoice.amount, settings),
memo: invoice.note,
});
invoice.encoded = response.payment_request;
Expand Down
6 changes: 3 additions & 3 deletions src/action/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PaymentAction {
const request = await this._grpc.sendCommand('decodePayReq', {
pay_req: invoice.replace(PREFIX_URI, ''),
});
payment.amount = toAmount(parseSat(request.num_satoshis), settings.unit);
payment.amount = toAmount(parseSat(request.num_satoshis), settings);
payment.note = request.description;
await this.estimateLightningFee({
destination: request.destination,
Expand All @@ -89,7 +89,7 @@ class PaymentAction {
amt: satAmt,
num_routes: 1,
});
payment.fee = toAmount(parseSat(routes[0].total_fees), settings.unit);
payment.fee = toAmount(parseSat(routes[0].total_fees), settings);
} catch (err) {
log.error(`Estimating lightning fee failed!`, err);
}
Expand All @@ -100,7 +100,7 @@ class PaymentAction {
const { payment, settings } = this._store;
await this._grpc.sendCommand('sendCoins', {
addr: payment.address,
amount: toSatoshis(payment.amount, settings.unit),
amount: toSatoshis(payment.amount, settings),
});
this._nav.goPayBitcoinDone();
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/computed/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const ComputedPayment = store => {
paymentFeeLabel: computed(() => toLabel(store.payment.fee, store.settings)),
paymentTotalLabel: computed(() => {
const { payment, settings } = store;
const satAmount = toSatoshis(payment.amount, settings.unit);
const satFee = toSatoshis(payment.fee, settings.unit);
const satAmount = toSatoshis(payment.amount, settings);
const satFee = toSatoshis(payment.fee, settings);
return toAmountLabel(satAmount + satFee, settings);
}),
});
Expand Down
8 changes: 4 additions & 4 deletions src/computed/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { UNITS, FIATS } from '../config';
const ComputedSetting = store => {
extendObservable(store, {
selectedUnitLabel: computed(() => getUnitLabel(store.settings.unit)),
selectedFiatLabel: computed(() => FIATS[store.settings.fiat].display),
selectedFiatLabel: computed(() => FIATS[store.settings.fiat].displayLong),
satUnitLabel: computed(() => getUnitLabel('sat')),
bitUnitLabel: computed(() => getUnitLabel('bit')),
btcUnitLabel: computed(() => getUnitLabel('btc')),
usdFiatLabel: computed(() => FIATS['usd'].display),
eurFiatLabel: computed(() => FIATS['eur'].display),
gbpFiatLabel: computed(() => FIATS['gbp'].display),
usdFiatLabel: computed(() => FIATS['usd'].displayLong),
eurFiatLabel: computed(() => FIATS['eur'].displayLong),
gbpFiatLabel: computed(() => FIATS['gbp'].displayLong),
});
};

Expand Down
6 changes: 5 additions & 1 deletion src/computed/wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computed, extendObservable } from 'mobx';
import { toAmountLabel } from '../helper';
import { UNITS } from '../config';
import { UNITS, FIATS } from '../config';

const ComputedWallet = store => {
extendObservable(store, {
Expand All @@ -13,6 +13,10 @@ const ComputedWallet = store => {
channelBalanceLabel: computed(() =>
toAmountLabel(store.channelBalanceSatoshis, store.settings)
),
unitFiatLabel: computed(() => {
const { displayFiat, unit, fiat } = store.settings;
return displayFiat ? FIATS[fiat].display : UNITS[unit].display;
}),
unitLabel: computed(() => {
const { settings } = store;
return !settings.displayFiat ? UNITS[settings.unit].display : null;
Expand Down
6 changes: 3 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ module.exports.UNITS = {
btc: { display: 'BTC', displayLong: 'Bitcoin', denominator: 100000000 },
};
module.exports.FIATS = {
usd: { display: 'US Dollar' },
eur: { display: 'Euro' },
gbp: { display: 'British Pound' },
usd: { display: '$', displayLong: 'US Dollar' },
eur: { display: '€', displayLong: 'Euro' },
gbp: { display: '£', displayLong: 'British Pound' },
};
module.exports.DEFAULT_UNIT = 'btc';
module.exports.DEFAULT_FIAT = 'usd';
52 changes: 30 additions & 22 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,45 @@ export const parseSat = satoshis => {
};

/**
* Convert a string formatted BTC amount to satoshis
* @param {string} amount The amount e.g. '0.0001'
* @param {string} unit The BTC unit e.g. 'btc' or 'bit'
* @return {number} The satoshis as an integer
* Convert a string formatted btc/fiat amount to satoshis
* @param {string} amount The amount e.g. '0.0001'
* @param {Object} settings Contains the current exchange rate
* @return {number} The satoshis as an integer
*/
export const toSatoshis = (amount, unit) => {
export const toSatoshis = (amount, settings) => {
if (
typeof amount !== 'string' ||
!/^[0-9]*[.]?[0-9]*$/.test(amount) ||
!UNITS[unit]
!settings ||
typeof settings.displayFiat !== 'boolean'
) {
throw new Error('Missing args!');
throw new Error('Invalid input!');
}
if (settings.displayFiat) {
const rate = settings.exchangeRate[settings.fiat] || 0;
return Math.round(Number(amount) * rate * UNITS.btc.denominator);
} else {
return Math.round(Number(amount) * UNITS[settings.unit].denominator);
}
return Math.round(Number(amount) * UNITS[unit].denominator);
};

/**
* Convert satoshis to a BTC values than can set as a text input value
* @param {number} satoshis The value as a string or number
* @param {string} unit The BTC unit e.g. 'btc' or 'bit'
* @param {Object} settings Contains the current exchange rate
* @return {string} The amount formatted as '0.0001'
*/
export const toAmount = (satoshis, unit) => {
if (!Number.isInteger(satoshis) || !UNITS[unit]) {
export const toAmount = (satoshis, settings) => {
if (
!Number.isInteger(satoshis) ||
!settings ||
typeof settings.displayFiat !== 'boolean'
) {
throw new Error('Invalid input!');
}
const num = satoshis / UNITS[unit].denominator;
const num = settings.displayFiat
? calculateExchangeRate(satoshis, settings)
: satoshis / UNITS[settings.unit].denominator;
return num.toLocaleString('en-US', {
useGrouping: false,
maximumFractionDigits: 8,
Expand All @@ -103,8 +115,7 @@ export const calculateExchangeRate = (satoshis, settings) => {
throw new Error('Invalid input!');
}
const rate = settings.exchangeRate[settings.fiat] || 0;
const balance = satoshis / rate / UNITS.btc.denominator;
return formatFiat(balance, settings.fiat);
return satoshis / rate / UNITS.btc.denominator;
};

/**
Expand All @@ -122,22 +133,19 @@ export const toAmountLabel = (satoshis, settings) => {
throw new Error('Invalid input!');
}
return settings.displayFiat
? calculateExchangeRate(satoshis, settings)
: formatNumber(toAmount(satoshis, settings.unit));
? formatFiat(calculateExchangeRate(satoshis, settings), settings.fiat)
: formatNumber(toAmount(satoshis, settings));
};

/**
* Convert a string formatted BTC amount either to fiat or the selected BTC unit.
* Convert a string formatted btc/fiat amount either to fiat or the selected BTC unit.
* The output should be used throughout the UI for value labels.
* @param {string} amount The amount e.g. '0.0001'
* @param {string} amount The amount e.g. '0.0001'
* @param {Object} settings Contains the current exchange rate
* @return {string} The corresponding value label
*/
export const toLabel = (amount, settings) => {
if (!settings) {
throw new Error('Missing args!');
}
const satoshis = toSatoshis(amount, settings.unit);
const satoshis = toSatoshis(amount, settings);
return toAmountLabel(satoshis, settings);
};

Expand Down
4 changes: 3 additions & 1 deletion src/view/channel-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const ChannelCreateView = ({ store, nav, channel }) => (
onChangeText={amount => channel.setAmount({ amount })}
onSubmitEditing={() => channel.connectAndOpen()}
/>
<BalanceLabelUnit style={styles.unit}>{store.unit}</BalanceLabelUnit>
<BalanceLabelUnit style={styles.unit}>
{store.unitFiatLabel}
</BalanceLabelUnit>
</BalanceLabel>
<FormStretcher>
<InputField
Expand Down
4 changes: 3 additions & 1 deletion src/view/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const InvoiceView = ({ store, nav, invoice }) => (
onChangeText={amount => invoice.setAmount({ amount })}
onSubmitEditing={() => invoice.generateUri()}
/>
<BalanceLabelUnit style={styles.unit}>{store.unit}</BalanceLabelUnit>
<BalanceLabelUnit style={styles.unit}>
{store.unitFiatLabel}
</BalanceLabelUnit>
</BalanceLabel>
<FormStretcher>
<InputField
Expand Down
2 changes: 1 addition & 1 deletion src/view/pay-bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const PayBitcoinView = ({ store, nav, payment }) => (
onSubmitEditing={() => nav.goPayBitcoinConfirm()}
/>
<BalanceLabelUnit style={styles.unit}>
{store.unit}
{store.unitFiatLabel}
</BalanceLabelUnit>
</BalanceLabel>
<InputField
Expand Down
4 changes: 2 additions & 2 deletions test/unit/computed/invoice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ describe('Computed Invoice Unit Tests', () => {
it('should format fiat amount', () => {
store.settings.displayFiat = true;
store.settings.exchangeRate.usd = 0.00014503;
store.invoice.amount = '0.1001';
store.invoice.amount = '1.10';
ComputedInvoice(store);
expect(store.invoiceAmountLabel, 'to match', /690[,.]20/);
expect(store.invoiceAmountLabel, 'to match', /1[,.]10/);
});
});
});
10 changes: 5 additions & 5 deletions test/unit/computed/payment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ describe('Computed Payment Unit Tests', () => {
it('should calculate fiat total', () => {
store.settings.displayFiat = true;
store.settings.exchangeRate.usd = 0.00014503;
store.payment.fee = '0.0001';
store.payment.amount = '0.1';
store.payment.fee = '0.10';
store.payment.amount = '1.00';
ComputedPayment(store);
expect(store.paymentAmountLabel, 'to match', /689[,.]51/);
expect(store.paymentFeeLabel, 'to match', /0[,.]69/);
expect(store.paymentTotalLabel, 'to match', /690[,.]20/);
expect(store.paymentAmountLabel, 'to match', /1[,.]00/);
expect(store.paymentFeeLabel, 'to match', /0[,.]10/);
expect(store.paymentTotalLabel, 'to match', /1[,.]10/);
});

it('should ignore fee if blank', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/computed/wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('Computed Wallet Unit Tests', () => {
expect(store.walletAddressUri, 'to equal', '');
expect(store.balanceLabel, 'to equal', '0');
expect(store.channelBalanceLabel, 'to equal', '0');
expect(store.unitFiatLabel, 'to equal', 'BTC');
expect(store.unitLabel, 'to equal', 'BTC');
expect(store.unit, 'to equal', 'BTC');
});

it('should generate valid wallet address uri', () => {
Expand All @@ -35,6 +37,7 @@ describe('Computed Wallet Unit Tests', () => {
ComputedWallet(store);
expect(store.balanceLabel, 'to match', /6[,.]895[,.]13/);
expect(store.channelBalanceLabel, 'to match', /0[,.]69/);
expect(store.unitFiatLabel, 'to equal', '$');
expect(store.unitLabel, 'to equal', null);
});

Expand All @@ -50,6 +53,7 @@ describe('Computed Wallet Unit Tests', () => {
/^1{1}[,.]0{3}[,.]0{3}[,.]0{1}1{1}$/
);
expect(store.channelBalanceLabel, 'to equal', '100');
expect(store.unitFiatLabel, 'to equal', 'bits');
expect(store.unitLabel, 'to equal', 'bits');
});
});
Expand Down
Loading