@@ -8,6 +8,7 @@ import { faDollarSign } from '@fortawesome/free-solid-svg-icons';
8
8
import { isCompatibleVersion } from '../utilities/data-formatters' ;
9
9
import { BalanceSheetSQL , SatsFlowSQL , VolumeSQL } from '../sql/bookkeeper-sql' ;
10
10
import { transformToBalanceSheet , transformToSatsFlow , transformToVolumeData } from '../sql/bookkeeper-transform' ;
11
+ import { BookkeeperLandingData } from '../types/lightning-bookkeeper-landing.type' ;
11
12
12
13
let intervalID ;
13
14
let localAuthStatus : any = null ;
@@ -163,26 +164,93 @@ const useHttp = () => {
163
164
const btcDeposit = ( ) => {
164
165
return sendRequest ( false , 'post' , '/cln/call' , { 'method' : 'newaddr' , 'params' : { 'addresstype' : 'bech32' } } ) ;
165
166
} ;
167
+
168
+ const getBookkeeperLanding = async ( ) : Promise < BookkeeperLandingData > => {
169
+ const balanceSheetData = await getBalanceSheet ( TimeGranularity . MONTHLY , true , undefined , new Date ( ) ) ;
170
+ const satsFlowData = await getSatsFlow ( TimeGranularity . MONTHLY , true , undefined , new Date ( ) ) ;
171
+ const volumeData = await getVolumeData ( ) ;
172
+
173
+ const latestBalanceSheetPeriod = balanceSheetData . periods [ balanceSheetData . periods . length - 1 ] ;
174
+ const balanceInWallet = latestBalanceSheetPeriod . accounts . filter ( ( ) => "wallet" ) [ 0 ] . balance ;
175
+ const balanceInChannels = latestBalanceSheetPeriod . accounts
176
+ . filter ( account => account . account !== "wallet" )
177
+ . reduce ( ( sum , account ) => sum + account . balance , 0 ) ;
178
+ const numberOfChannels = latestBalanceSheetPeriod . accounts
179
+ . filter ( account => account . account !== "wallet" )
180
+ . reduce ( ( sum ) => sum + 1 , 0 ) ;
181
+
182
+ const latestSatsFlowPeriod = satsFlowData . periods [ satsFlowData . periods . length - 1 ] ;
183
+ const inflowsThisMonth = latestSatsFlowPeriod . inflowSat ;
184
+ const outflowsThisMonth = latestSatsFlowPeriod . outflowSat ;
185
+
186
+
187
+ return {
188
+ balanceSheetSummary : {
189
+ balanceInWallet : balanceInWallet ,
190
+ balanceInChannels : balanceInChannels ,
191
+ numberOfChannels : numberOfChannels ,
192
+ } ,
193
+ satsFlowSummary : {
194
+ inflows : inflowsThisMonth ,
195
+ outflows : outflowsThisMonth ,
196
+ } ,
197
+ volumeSummary : {
198
+ mostTrafficChannel : "best" ,
199
+ leastTrafficChannel : "worst" ,
200
+ }
201
+ } ;
202
+ } ;
166
203
167
204
/**
168
205
* Gets Balance Sheet data.
169
206
* @param timeGranularity - Group data by this time granularity.
170
- * @param startTimestamp - If specified, the starting range for the data.
171
- * @param endTimestamp - The ending range for the data.
207
+ * @param hideZeroActivityPeriods - Hide bars where balance did not change.
208
+ * @param startTimestamp - The starting range for the data if specified, else uses beginning.
209
+ * @param endTimestamp - The ending range for the data if specified, else uses now.
172
210
* @returns Returns balance data grouped in periods of the specified time granularity.
173
211
*/
174
- const getBalanceSheet = ( timeGranularity : TimeGranularity , hideZeroActivityPeriods : Boolean , startTimestamp : number , endTimestamp : number ) => {
212
+ const getBalanceSheet = ( timeGranularity : TimeGranularity , hideZeroActivityPeriods : Boolean , startDate ?: Date , endDate ?: Date ) => {
213
+ let startTimestamp = 1 ;
214
+ let endTimestamp = Math . floor ( new Date ( ) . getTime ( ) ) ;
215
+
216
+ if ( startDate != null ) {
217
+ startTimestamp = Math . floor ( startDate . getTime ( ) / 1000 ) ;
218
+ }
219
+ if ( endDate != null ) {
220
+ endTimestamp = Math . floor ( endDate . getTime ( ) / 1000 ) ;
221
+ }
175
222
return sendRequest ( false , 'post' , '/cln/call' , { 'method' : 'sql' , 'params' : [ BalanceSheetSQL ] } )
176
223
. then ( ( response ) => transformToBalanceSheet ( response . data , timeGranularity , hideZeroActivityPeriods , startTimestamp , endTimestamp ) ) ;
177
224
} ;
178
225
179
- const getSatsFlow = ( timeGranularity : TimeGranularity , hideZeroActivityPeriods : boolean , startTimestamp : number , endTimestamp : number ) => {
180
- return sendRequest ( false , 'post' , 'cln/call' , { 'method' : 'sql' , 'params' : [ SatsFlowSQL ( startTimestamp , endTimestamp ) ] } )
226
+ /**
227
+ * Gets Sats Flow data.
228
+ * @param timeGranularity - Group data by this time granularity.
229
+ * @param hideZeroActivityPeriods - Hide bars where balance did not change.
230
+ * @param startTimestamp - The starting range for the data if specified, else uses beginning.
231
+ * @param endTimestamp - The ending range for the data, else uses now.
232
+ * @returns Returns sats flow data grouped in periods of the specified time granularity.
233
+ */
234
+ const getSatsFlow = ( timeGranularity : TimeGranularity , hideZeroActivityPeriods : boolean , startDate ?: Date , endDate ?: Date ) => {
235
+ let startTimestamp = 1 ;
236
+ let endTimestamp = Math . floor ( new Date ( ) . getTime ( ) ) ;
237
+
238
+ if ( startDate != null ) {
239
+ startTimestamp = Math . floor ( startDate . getTime ( ) / 1000 ) ;
240
+ }
241
+ if ( endDate != null ) {
242
+ endTimestamp = Math . floor ( endDate . getTime ( ) / 1000 ) ;
243
+ }
244
+ return sendRequest ( false , 'post' , '/cln/call' , { 'method' : 'sql' , 'params' : [ SatsFlowSQL ( startTimestamp , endTimestamp ) ] } )
181
245
. then ( ( response ) => transformToSatsFlow ( response . data , timeGranularity , hideZeroActivityPeriods ) ) ;
182
246
} ;
183
247
248
+ /**
249
+ * Gets Volume Chart data.
250
+ * @returns Returns balance data grouped in periods of the specified time granularity.
251
+ */
184
252
const getVolumeData = ( ) => {
185
- return sendRequest ( false , 'post' , 'cln/call' , { 'method' : 'sql' , 'params' : [ VolumeSQL ] } )
253
+ return sendRequest ( false , 'post' , '/ cln/call' , { 'method' : 'sql' , 'params' : [ VolumeSQL ] } )
186
254
. then ( ( response ) => transformToVolumeData ( response . data ) ) ;
187
255
} ;
188
256
@@ -330,6 +398,7 @@ const useHttp = () => {
330
398
decodeInvoice,
331
399
fetchInvoice,
332
400
createInvoiceRune,
401
+ getBookkeeperLanding,
333
402
getBalanceSheet,
334
403
getSatsFlow,
335
404
getVolumeData,
0 commit comments