Skip to content

Commit 4881e16

Browse files
Merge pull request #9 from ElementsProject/nan-bug-fix
Bug Fix: NaN Numbers #4
2 parents 4327792 + 0aae1d7 commit 4881e16

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

Diff for: apps/frontend/src/store/AppContext.tsx

+32-9
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ import { isCompatibleVersion, sortDescByKey } from '../utilities/data-formatters
1111
import logger from '../services/logger.service';
1212
import { AppContextType } from '../types/app-context.type';
1313
import { ApplicationConfiguration, FiatConfig, WalletConnect } from '../types/app-config.type';
14-
import { BkprTransaction, Fund, FundChannel, FundOutput, Invoice, ListBitcoinTransactions, ListInvoices, ListPayments, ListPeers, NodeFeeRate, NodeInfo, Payment, Peer } from '../types/lightning-wallet.type';
14+
import { BkprTransaction, Fund, FundChannel, FundOutput, Invoice, ListBitcoinTransactions, ListInvoices, ListPayments, ListOffers, ListPeers, NodeFeeRate, NodeInfo, Payment, Peer } from '../types/lightning-wallet.type';
1515

16-
const aggregateChannels = (peers: Peer[]) => {
16+
const aggregateChannels = (peers: Peer[], currentVersion: string) => {
1717
const aggregatedChannels: any = { activeChannels: [], pendingChannels: [], inactiveChannels: [] };
1818
peers?.forEach((peer: Peer) => {
1919
if (peer.channels && peer.channels.length > 0) {
2020
peer.channels.map(channel => {
2121
channel.connected = peer.connected || false;
2222
channel.node_alias = peer.alias || peer.id?.substring(0,20) || '';
23-
channel.satoshi_to_us = Math.floor((channel.msatoshi_to_us || channel.to_us_msat || 0) / SATS_MSAT);
24-
channel.satoshi_total = Math.floor((channel.msatoshi_total || channel.total_msat || 0) / SATS_MSAT);
25-
channel.satoshi_to_them = Math.floor(((channel.msatoshi_total || channel.total_msat || 0) - (channel.msatoshi_to_us || channel.to_us_msat || 0)) / SATS_MSAT);
23+
if (isCompatibleVersion(currentVersion, '23.02')) {
24+
channel.satoshi_to_us = Math.floor((channel.msatoshi_to_us || channel.to_us_msat || 0) / SATS_MSAT);
25+
channel.satoshi_total = Math.floor((channel.msatoshi_total || channel.total_msat || 0) / SATS_MSAT);
26+
channel.satoshi_to_them = Math.floor(((channel.msatoshi_total || channel.total_msat || 0) - (channel.msatoshi_to_us || channel.to_us_msat || 0)) / SATS_MSAT);
27+
} else {
28+
channel.satoshi_to_us = Math.floor((channel.msatoshi_to_us || 0) / SATS_MSAT);
29+
channel.satoshi_total = Math.floor((channel.msatoshi_total || 0) / SATS_MSAT);
30+
channel.satoshi_to_them = Math.floor(((channel.msatoshi_total || 0) - (channel.msatoshi_to_us || 0)) / SATS_MSAT);
31+
}
32+
2633
if (channel.state === 'CHANNELD_NORMAL') {
2734
if (channel.connected) {
2835
channel.current_state = 'ACTIVE';
@@ -164,13 +171,13 @@ const calculateBalances = (listFunds: Fund) => {
164171
return walletBalances;
165172
};
166173

167-
const filterOnChainTransactions = (events: BkprTransaction[], state) => {
174+
const filterOnChainTransactions = (events: BkprTransaction[], currentVersion: string) => {
168175
if (!events) {
169176
return [];
170177
} else {
171178
return events.reduce((acc: any[], event, i) => {
172179
if (event.account === 'wallet' && (event.tag === 'deposit' || event.tag === 'withdrawal')) {
173-
if (isCompatibleVersion(state.nodeInfo.version, '23.02')) {
180+
if (isCompatibleVersion(currentVersion, '23.02')) {
174181
event.credit_msat = event.credit_msat || 0;
175182
event.debit_msat = event.debit_msat || 0;
176183
} else {
@@ -202,6 +209,7 @@ const AppContext = React.createContext<AppContextType>({
202209
listChannels: {isLoading: true, activeChannels: [], pendingChannels: [], inactiveChannels: []},
203210
listInvoices: {isLoading: true, invoices: []},
204211
listPayments: {isLoading: true, payments: []},
212+
listOffers: {isLoading: true, offers: []},
205213
listLightningTransactions: {isLoading: true, clnTransactions: []},
206214
listBitcoinTransactions: {isLoading: true, btcTransactions: []},
207215
walletBalances: {isLoading: true, clnLocalBalance: 0, clnRemoteBalance: 0, clnPendingBalance: 0, clnInactiveBalance: 0, btcSpendableBalance: 0, btcReservedBalance: 0},
@@ -216,6 +224,7 @@ const AppContext = React.createContext<AppContextType>({
216224
setListPeers: (peersList: ListPeers) => {},
217225
setListInvoices: (invoicesList: ListInvoices) => {},
218226
setListPayments: (paymentsList: ListPayments) => {},
227+
setListOffers: (offersList: ListOffers) => {},
219228
setListBitcoinTransactions: (transactionsList: ListBitcoinTransactions) => {},
220229
setStore: (storeData) => {},
221230
clearStore: () => {}
@@ -234,6 +243,7 @@ const defaultAppState = {
234243
listChannels: {isLoading: true, activeChannels: [], pendingChannels: [], inactiveChannels: []},
235244
listInvoices: {isLoading: true, invoices: []},
236245
listPayments: {isLoading: true, payments: []},
246+
listOffers: {isLoading: true, offers: []},
237247
listLightningTransactions: {isLoading: true, clnTransactions: []},
238248
listBitcoinTransactions: {isLoading: true, btcTransactions: []},
239249
walletBalances: {isLoading: true, clnLocalBalance: 0, clnRemoteBalance: 0, clnPendingBalance: 0, clnInactiveBalance: 0, btcSpendableBalance: 0, btcReservedBalance: 0}
@@ -256,6 +266,7 @@ const appReducer = (state, action) => {
256266
};
257267

258268
case ApplicationActions.SET_WALLET_CONNECT:
269+
action.payload.TOR_DOMAIN_NAME = action.payload.TOR_HOST?.replace('https://', '').replace('http://', '');
259270
return {
260271
...state,
261272
walletConnect: action.payload
@@ -294,7 +305,7 @@ const appReducer = (state, action) => {
294305
};
295306

296307
case ApplicationActions.SET_LIST_PEERS:
297-
let filteredChannels = aggregateChannels(action.payload.peers);
308+
let filteredChannels = aggregateChannels(action.payload.peers, state.nodeInfo.version);
298309
return {
299310
...state,
300311
listChannels: { ...filteredChannels, isLoading: false, error: action.payload.error },
@@ -338,9 +349,15 @@ const appReducer = (state, action) => {
338349
listPayments: {...action.payload, payments: sortedPayments}
339350
};
340351

352+
case ApplicationActions.SET_LIST_OFFERS:
353+
return {
354+
...state,
355+
listOffers: action.payload
356+
};
357+
341358
case ApplicationActions.SET_LIST_BITCOIN_TRANSACTIONS:
342359
const sortedTransactions = action.payload.events?.sort((t1: BkprTransaction, t2: BkprTransaction) => ((t1.timestamp && t2.timestamp && t1.timestamp > t2.timestamp) ? -1 : 1));
343-
const filteredTransactions = filterOnChainTransactions(sortedTransactions, state);
360+
const filteredTransactions = filterOnChainTransactions(sortedTransactions, state.nodeInfo.version);
344361
return {
345362
...state,
346363
listBitcoinTransactions: { isLoading: false, error: action.payload.error, btcTransactions: filteredTransactions },
@@ -404,6 +421,10 @@ const AppProvider: React.PropsWithChildren<any> = (props) => {
404421
dispatchApplicationAction({ type: ApplicationActions.SET_LIST_SEND_PAYS, payload: list });
405422
};
406423

424+
const setListOffersHandler = (list: ListOffers) => {
425+
dispatchApplicationAction({ type: ApplicationActions.SET_LIST_OFFERS, payload: list });
426+
};
427+
407428
const setListBitcoinTransactionsHandler = (list: any) => {
408429
dispatchApplicationAction({ type: ApplicationActions.SET_LIST_BITCOIN_TRANSACTIONS, payload: list });
409430
};
@@ -429,6 +450,7 @@ const AppProvider: React.PropsWithChildren<any> = (props) => {
429450
listChannels: applicationState.listChannels,
430451
listInvoices: applicationState.listInvoices,
431452
listPayments: applicationState.listPayments,
453+
listOffers: applicationState.listOffers,
432454
listLightningTransactions: applicationState.listLightningTransactions,
433455
listBitcoinTransactions: applicationState.listBitcoinTransactions,
434456
walletBalances: applicationState.walletBalances,
@@ -443,6 +465,7 @@ const AppProvider: React.PropsWithChildren<any> = (props) => {
443465
setListPeers: setListPeersHandler,
444466
setListInvoices: setListInvoicesHandler,
445467
setListPayments: setListPaymentsHandler,
468+
setListOffers: setListOffersHandler,
446469
setListBitcoinTransactions: setListBitcoinTransactionsHandler,
447470
setStore: setContextStore,
448471
clearStore: clearContextHandler

Diff for: apps/frontend/src/utilities/data-formatters.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ export const formatCurrencyType = (num: number, shorten: boolean, returnFormat:
3535
(shorten ? Math.floor((num / 1000)) : parseFloat(num.toString())) // number format
3636
};
3737

38-
export const formatCurrency = (num: number, fromUnit: Units, toUnit: Units = Units.SATS, shorten: boolean = false, numDecimalPlaces: number = 5, returnFormat: string = 'string') => {
38+
export const formatCurrency = (num: any, fromUnit: Units, toUnit: Units = Units.SATS, shorten: boolean = false, numDecimalPlaces: number = 5, returnFormat: string = 'string') => {
3939
if (typeof num === 'undefined') { num = 0; }
40+
if (num && typeof num === 'string' && num.includes('msat')) { num = num.substring(0, (num.length - 4)); }
4041
switch (fromUnit) {
4142
case Units.MSATS:
4243
return toUnit === Units.BTC ? ConvertMSatsToBTC(num, numDecimalPlaces) : toUnit === Units.SATS ? ConvertMSatsToSats(num, numDecimalPlaces, returnFormat) : formatCurrencyType(num, shorten, returnFormat);

0 commit comments

Comments
 (0)