11import { Injectable , InternalServerErrorException , OnModuleInit } from '@nestjs/common' ;
22import { BitcoinClient } from 'src/integration/blockchain/bitcoin/bitcoin-client' ;
33import { BitcoinService } from 'src/integration/blockchain/bitcoin/bitcoin.service' ;
4+ import { CitreaClient } from 'src/integration/blockchain/citrea/citrea-client' ;
45import { LndChannelDto } from 'src/integration/blockchain/lightning/dto/lnd.dto' ;
56import { LightningClient } from 'src/integration/blockchain/lightning/lightning-client' ;
67import { LightningService } from 'src/integration/blockchain/lightning/services/lightning.service' ;
@@ -12,7 +13,7 @@ import { QueueHandler } from 'src/shared/utils/queue-handler';
1213import { AssetService } from 'src/subdomains/master-data/asset/services/asset.service' ;
1314import { CoinGeckoService } from 'src/subdomains/pricing/services/coingecko.service' ;
1415import { LightningWalletTotalBalanceDto } from 'src/subdomains/user/application/dto/lightning-wallet.dto' ;
15- import { MonitoringBalanceEntity } from '../entities/monitoring-balance.entity' ;
16+ import { MonitoringBalanceEntity , MonitoringBlockchainBalance } from '../entities/monitoring-balance.entity' ;
1617import { MonitoringBalanceRepository } from '../repositories/monitoring-balance.repository' ;
1718import { MonitoringRepository } from '../repositories/monitoring.repository' ;
1819
@@ -23,6 +24,7 @@ export class MonitoringService implements OnModuleInit {
2324 private readonly bitcoinClient : BitcoinClient ;
2425 private readonly lightningClient : LightningClient ;
2526 private rootstockClient : RootstockClient ;
27+ private citreaClient : CitreaClient ;
2628
2729 private readonly processBalancesQueue : QueueHandler ;
2830
@@ -43,6 +45,7 @@ export class MonitoringService implements OnModuleInit {
4345
4446 onModuleInit ( ) {
4547 this . rootstockClient = this . evmRegistryService . getClient ( Blockchain . ROOTSTOCK ) as RootstockClient ;
48+ this . citreaClient = this . evmRegistryService . getClient ( Blockchain . CITREA ) as CitreaClient ;
4649 }
4750
4851 // --- LIGHTNING --- //
@@ -66,10 +69,7 @@ export class MonitoringService implements OnModuleInit {
6669 customerBalances : LightningWalletTotalBalanceDto [ ] ,
6770 ) : Promise < void > {
6871 try {
69- const onchainBalance = await this . getOnchainBalance ( ) ;
70- const lndOnchainBalance = await this . getLndOnchainBalance ( ) ;
71- const lightningBalance = await this . getLightningBalance ( ) ;
72- const rootstockBalance = await this . getRootstockBalance ( ) ;
72+ const blockchainBalance = await this . getBlockchainBalances ( ) ;
7373
7474 const btcAccountAsset = await this . assetService . getBtcAccountAssetOrThrow ( ) ;
7575 const btcAccountAssetId = btcAccountAsset . id ;
@@ -86,14 +86,7 @@ export class MonitoringService implements OnModuleInit {
8686
8787 const customerFiatBalances = customerBalances . filter ( ( b ) => b . assetId !== btcAccountAssetId ) ;
8888
89- await this . processBtcBalance (
90- onchainBalance ,
91- lndOnchainBalance ,
92- lightningBalance ,
93- rootstockBalance ,
94- internalBtcBalance ,
95- customerBtcBalance ,
96- ) ;
89+ await this . processBtcBalance ( blockchainBalance , internalBtcBalance , customerBtcBalance ) ;
9790 await this . processFiatBalances ( customerFiatBalances ) ;
9891 } catch ( e ) {
9992 this . logger . error ( 'Error while processing balances' , e ) ;
@@ -119,21 +112,15 @@ export class MonitoringService implements OnModuleInit {
119112 }
120113
121114 private async processBtcBalance (
122- onchainBalance : number ,
123- lndOnchainBalance : number ,
124- lightningBalance : number ,
125- rootstockBalance : number ,
115+ blockchainBalance : MonitoringBlockchainBalance ,
126116 internalBtcBalance : LightningWalletTotalBalanceDto ,
127117 customerBtcBalance : LightningWalletTotalBalanceDto ,
128118 ) {
129119 const chfPrice = await this . coinGeckoService . getPrice ( 'BTC' , 'CHF' ) ;
130120 if ( ! chfPrice . isValid ) throw new InternalServerErrorException ( `Invalid price from BTC to CHF` ) ;
131121
132122 const btcMonitoringEntity = MonitoringBalanceEntity . createAsBtcEntity (
133- onchainBalance ,
134- lndOnchainBalance ,
135- lightningBalance ,
136- rootstockBalance ,
123+ blockchainBalance ,
137124 internalBtcBalance ,
138125 customerBtcBalance ,
139126 chfPrice ,
@@ -169,20 +156,14 @@ export class MonitoringService implements OnModuleInit {
169156 return balance ;
170157 }
171158
172- private async getOnchainBalance ( ) : Promise < number > {
173- return this . bitcoinClient . getWalletBalance ( ) ;
174- }
175-
176- private async getLndOnchainBalance ( ) : Promise < number > {
177- return this . lightningClient . getLndConfirmedWalletBalance ( ) ;
178- }
179-
180- private async getLightningBalance ( ) : Promise < number > {
181- return this . lightningClient . getLndLightningBalance ( ) ;
182- }
183-
184- private async getRootstockBalance ( ) : Promise < number > {
185- return this . rootstockClient . getNativeCoinBalance ( ) ;
159+ private async getBlockchainBalances ( ) : Promise < MonitoringBlockchainBalance > {
160+ return {
161+ onchainBalance : await this . bitcoinClient . getWalletBalance ( ) ,
162+ lndOnchainBalance : await this . lightningClient . getLndConfirmedWalletBalance ( ) ,
163+ lightningBalance : await this . lightningClient . getLndLightningBalance ( ) ,
164+ rootstockBalance : await this . rootstockClient . getNativeCoinBalance ( ) ,
165+ citreaBalance : await this . citreaClient . getNativeCoinBalance ( ) ,
166+ } ;
186167 }
187168
188169 private async getChannels ( ) : Promise < LndChannelDto [ ] > {
0 commit comments