Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion: display rate in sats when it's the unit selected #1388

Merged
merged 5 commits into from
Mar 19, 2023
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
8 changes: 6 additions & 2 deletions components/Conversion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export default class Conversion extends React.Component<
{showRate && (
<>
<Text style={{ color: themeColor('secondaryText') }}>
{` | ${getRate()}`}
{` | ${getRate(
this.props.UnitsStore.units === 'sats'
)}`}
</Text>
</>
)}
Expand Down Expand Up @@ -150,7 +152,9 @@ export default class Conversion extends React.Component<
{showRate && (
<>
<Text style={{ color: themeColor('secondaryText') }}>
{` | ${getRate()}`}
{` | ${getRate(
this.props.UnitsStore.units === 'sats'
)}`}
</Text>
</>
)}
Expand Down
4 changes: 2 additions & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@
"views.OpenChannel.nodePubkey": "Node pubkey",
"views.OpenChannel.host": "Host",
"views.OpenChannel.hostPort": "Hostname:Port",
"views.OpenChannel.localAmt": "Local amount (in satoshis)",
"views.OpenChannel.localAmt": "Local amount",
"views.OpenChannel.amtExample": "20000 (min)",
"views.OpenChannel.numConf": "Number of Confirmations",
"views.OpenChannel.satsPerByte": "Satoshis per byte",
Expand All @@ -367,7 +367,7 @@
"views.Payment.path": "Path",
"views.PaymentRequest.title": "Lightning Invoice",
"views.PaymentRequest.error": "Error loading invoice",
"views.PaymentRequest.customAmt": "Custom Amount (in satoshis)",
"views.PaymentRequest.customAmt": "Custom Amount",
"views.PaymentRequest.payDefault": "Pay default amount",
"views.PaymentRequest.payCustom": "Pay custom amount",
"views.PaymentRequest.feeEstimate": "Fee Estimate",
Expand Down
24 changes: 22 additions & 2 deletions stores/FiatStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { action, observable } from 'mobx';
import ReactNativeBlobUtil from 'react-native-blob-util';
import BigNumber from 'bignumber.js';

import SettingsStore from './SettingsStore';
import { SATS_PER_BTC } from './UnitsStore';

interface CurrencyDisplayRules {
symbol: string;
Expand Down Expand Up @@ -288,14 +291,19 @@ export default class FiatStore {
};

@action
public getRate = () => {
public getRate = (sats: boolean) => {
const { settings } = this.settingsStore;
const { fiat } = settings;

if (fiat && this.fiatRates.filter) {
const fiatEntry = this.fiatRates.filter(
(entry: any) => entry.code === fiat
)[0];
const rate = (fiatEntry && fiatEntry.rate) || 0;
const rate = sats
? (fiatEntry &&
new BigNumber(fiatEntry.rate).div(SATS_PER_BTC)) ||
0
: (fiatEntry && fiatEntry.rate) || 0;
const { symbol, space, rtl, separatorSwap } = this.symbolLookup(
fiatEntry && fiatEntry.code
);
Expand All @@ -305,10 +313,22 @@ export default class FiatStore {
: this.numberWithCommas(rate);

if (rtl) {
if (sats) {
return `${formattedRate}${
space ? ' ' : ''
}${symbol} sat/${fiat}`;
}

return `${formattedRate}${
space ? ' ' : ''
}${symbol} BTC/${fiat}`;
} else {
if (sats) {
return `${symbol}${
space ? ' ' : ''
}${formattedRate} sat/${fiat}`;
}

return `${symbol}${
space ? ' ' : ''
}${formattedRate} BTC/${fiat}`;
Expand Down
17 changes: 10 additions & 7 deletions views/LnurlPay/LnurlPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,27 @@ export default class LnurlPay extends React.Component<
}
toggleUnits={changeUnits}
/>
{units !== 'sats' && (
<Amount sats={satAmount} fixedUnits="sats" toggleable />
)}
{units !== 'BTC' && (
<Amount sats={satAmount} fixedUnits="BTC" toggleable />
{fiat !== 'Disabled' && units !== 'fiat' && (
<Amount sats={satAmount} fixedUnits="fiat" toggleable />
)}
{units === 'fiat' && (
{fiat !== 'Disabled' && (
<TouchableOpacity onPress={() => changeUnits()}>
<Text
style={{
...styles.text,
color: themeColor('text')
}}
>
{FiatStore.getRate()}
{FiatStore.getRate(units === 'sats')}
</Text>
</TouchableOpacity>
)}
{units !== 'sats' && (
<Amount sats={satAmount} fixedUnits="sats" toggleable />
)}
{units !== 'BTC' && (
<Amount sats={satAmount} fixedUnits="BTC" toggleable />
)}
{lnurl.commentAllowed > 0 ? (
<>
<Text
Expand Down
140 changes: 121 additions & 19 deletions views/OpenChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { inject, observer } from 'mobx-react';
import { Header, Icon } from 'react-native-elements';
import NfcManager, { NfcEvents } from 'react-native-nfc-manager';

import Amount from './../components/Amount';
import Button from './../components/Button';
import LightningIndicator from './../components/LightningIndicator';
import Screen from './../components/Screen';
Expand All @@ -31,9 +32,11 @@ import BackendUtils from './../utils/BackendUtils';
import { localeString } from './../utils/LocaleUtils';
import { themeColor } from './../utils/ThemeUtils';

import BalanceStore from './../stores/BalanceStore';
import ChannelsStore from './../stores/ChannelsStore';
import FiatStore from './../stores/FiatStore';
import SettingsStore from './../stores/SettingsStore';
import BalanceStore from './../stores/BalanceStore';
import UnitsStore, { SATS_PER_BTC } from './../stores/UnitsStore';
import UTXOsStore from './../stores/UTXOsStore';

import Scan from './../assets/images/SVG/Scan.svg';
Expand All @@ -43,7 +46,9 @@ interface OpenChannelProps {
navigation: any;
ChannelsStore: ChannelsStore;
BalanceStore: BalanceStore;
FiatStore: FiatStore;
SettingsStore: SettingsStore;
UnitsStore: UnitsStore;
UTXOsStore: UTXOsStore;
}

Expand All @@ -61,7 +66,14 @@ interface OpenChannelState {
utxoBalance: number;
}

@inject('ChannelsStore', 'SettingsStore', 'BalanceStore', 'UTXOsStore')
@inject(
'ChannelsStore',
'FiatStore',
'SettingsStore',
'BalanceStore',
'UnitsStore',
'UTXOsStore'
)
@observer
export default class OpenChannel extends React.Component<
OpenChannelProps,
Expand Down Expand Up @@ -211,6 +223,8 @@ export default class OpenChannel extends React.Component<
const {
ChannelsStore,
BalanceStore,
FiatStore,
UnitsStore,
UTXOsStore,
SettingsStore,
navigation
Expand All @@ -227,7 +241,9 @@ export default class OpenChannel extends React.Component<
scidAlias
} = this.state;
const { implementation, settings } = SettingsStore;
const { privacy } = settings;
const { fiatRates, getSymbol } = FiatStore;
const { units, changeUnits } = UnitsStore;
const { fiat, privacy } = settings;
const enableMempoolRates = privacy && privacy.enableMempoolRates;

const {
Expand All @@ -241,6 +257,34 @@ export default class OpenChannel extends React.Component<
} = ChannelsStore;
const { confirmedBlockchainBalance } = BalanceStore;

const fiatEntry =
fiat && fiatRates && fiatRates.filter
? fiatRates.filter((entry: any) => entry.code === fiat)[0]
: null;

const rate =
fiat && fiat !== 'Disabled' && fiatRates && fiatEntry
? fiatEntry.rate
: 0;

// conversion
let satAmount: string | number;
switch (units) {
case 'sats':
satAmount = local_funding_amount;
break;
case 'BTC':
satAmount = Number(local_funding_amount) * SATS_PER_BTC;
break;
case 'fiat':
satAmount = Number(
(Number(local_funding_amount.replace(/,/g, '.')) /
Number(rate)) *
Number(SATS_PER_BTC)
).toFixed(0);
break;
}

const BackButton = () => (
<Icon
name="arrow-back"
Expand Down Expand Up @@ -384,28 +428,80 @@ export default class OpenChannel extends React.Component<
</Text>
<TextInput
keyboardType="numeric"
placeholder={localeString(
'views.OpenChannel.amtExample'
)}
value={local_funding_amount}
onChangeText={(text: string) =>
this.setState({ local_funding_amount: text })
}
locked={openingChannel}
prefix={
units !== 'sats' &&
(units === 'BTC'
? '₿'
: !getSymbol().rtl
? getSymbol().symbol
: null)
}
suffix={
units === 'sats'
? units
: getSymbol().rtl &&
units === 'fiat' &&
getSymbol().symbol
}
toggleUnits={changeUnits}
/>
{local_funding_amount !== 'all' && (
<View style={{ marginBottom: 10 }}>
{fiat !== 'Disabled' && units !== 'fiat' && (
<Amount
sats={satAmount}
fixedUnits="fiat"
toggleable
/>
)}
{fiat !== 'Disabled' && (
<TouchableOpacity
onPress={() => changeUnits()}
>
<Text
style={{
color: themeColor('text')
}}
>
{FiatStore.getRate(
units === 'sats'
)}
</Text>
</TouchableOpacity>
)}
{units !== 'sats' && (
<Amount
sats={satAmount}
fixedUnits="sats"
toggleable
/>
)}
{units !== 'BTC' && (
<Amount
sats={satAmount}
fixedUnits="BTC"
toggleable
/>
)}
</View>
)}

{local_funding_amount === 'all' && (
<Text
style={{
...styles.text,
color: themeColor('text')
}}
>
{`${
utxoBalance > 0
? utxoBalance
: confirmedBlockchainBalance
} ${localeString('views.Receive.satoshis')}`}
</Text>
<View style={{ marginBottom: 20 }}>
<Amount
sats={
utxoBalance > 0
? utxoBalance
: confirmedBlockchainBalance
}
toggleable
/>
</View>
)}

<Text
Expand Down Expand Up @@ -544,7 +640,13 @@ export default class OpenChannel extends React.Component<
size: 25,
color: 'white'
}}
onPress={() => connectPeer(this.state)}
onPress={() =>
connectPeer({
...this.state,
local_funding_amount:
satAmount.toString()
})
}
/>
</View>

Expand Down
Loading