@@ -51,6 +51,11 @@ type TransferToColdWalletResult struct {
5151 RskResult NetworkTransferResult
5252}
5353
54+ type currentLiquidityResult struct {
55+ Btc * entities.Wei
56+ Rbtc * entities.Wei
57+ }
58+
5459func NewTransferToColdWalletResult (btcResult , rskResult NetworkTransferResult ) * TransferToColdWalletResult {
5560 return & TransferToColdWalletResult {
5661 BtcResult : btcResult ,
@@ -352,6 +357,7 @@ func (useCase *TransferExcessToColdWalletUseCase) publishRskTransferEvent(ctx co
352357 }
353358}
354359
360+ // sendTransferAlert triggers the alertSender to send an alert with the transfer details.
355361func (useCase * TransferExcessToColdWalletUseCase ) sendTransferAlert (ctx context.Context , asset string , amount * entities.Wei , txHash string , fee * entities.Wei , isTimeForcing bool ) error {
356362 reason := "threshold"
357363 if isTimeForcing {
@@ -369,22 +375,6 @@ func (useCase *TransferExcessToColdWalletUseCase) sendTransferAlert(ctx context.
369375 return useCase .alertSender .SendAlert (ctx , alerts .AlertSubjectHotToColdTransfer , body , []string {useCase .alertRecipient })
370376}
371377
372- func (useCase * TransferExcessToColdWalletUseCase ) withdrawContractFunds () {
373- lpAddress := useCase .generalProvider .RskAddress ()
374- contractBalance , err := useCase .peginContract .GetBalance (lpAddress )
375- if err != nil {
376- log .Errorf ("TransferExcessToColdWallet: failed to get pegin contract balance: %v" , err )
377- return
378- }
379- if contractBalance .Cmp (entities .NewWei (0 )) <= 0 {
380- return
381- }
382- log .Infof ("TransferExcessToColdWallet: withdrawing %s wei from pegin contract" , contractBalance .String ())
383- if err := useCase .peginContract .Withdraw (contractBalance ); err != nil {
384- log .Errorf ("TransferExcessToColdWallet: failed to withdraw from pegin contract: %v" , err )
385- }
386- }
387-
388378// handleRskTransfer orchestrates a single RBTC transfer: skips if no excess, executes the transfer,
389379// checks if the amount is economical, and publishes the corresponding event on success.
390380func (useCase * TransferExcessToColdWalletUseCase ) handleRskTransfer (ctx context.Context , excess * entities.Wei , isTimeForcingTransfer bool ) NetworkTransferResult {
@@ -425,9 +415,22 @@ func (useCase *TransferExcessToColdWalletUseCase) handleRskTransfer(ctx context.
425415 }
426416}
427417
428- type currentLiquidityResult struct {
429- Btc * entities.Wei
430- Rbtc * entities.Wei
418+ // withdrawContractFunds retrieves the LP's balance from the pegin contract and withdraws it
419+ // back to the hot wallet, ensuring contract funds are prioritized before cold wallet transfers.
420+ func (useCase * TransferExcessToColdWalletUseCase ) withdrawContractFunds () {
421+ lpAddress := useCase .generalProvider .RskAddress ()
422+ contractBalance , err := useCase .peginContract .GetBalance (lpAddress )
423+ if err != nil {
424+ log .Errorf ("TransferExcessToColdWallet: failed to get pegin contract balance: %v" , err )
425+ return
426+ }
427+ if contractBalance .Cmp (entities .NewWei (0 )) <= 0 {
428+ return
429+ }
430+ log .Infof ("TransferExcessToColdWallet: withdrawing %s wei from pegin contract" , contractBalance .String ())
431+ if err := useCase .peginContract .Withdraw (contractBalance ); err != nil {
432+ log .Errorf ("TransferExcessToColdWallet: failed to withdraw from pegin contract: %v" , err )
433+ }
431434}
432435
433436// validateColdWallet checks that both BTC and RSK cold wallet addresses are configured.
0 commit comments