Skip to content

Commit 0503746

Browse files
committed
fix: improve docs and tests
1 parent cd64e91 commit 0503746

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

internal/adapters/dataproviders/rootstock/pegin_contract.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ func (peginContract *peginContractImpl) RegisterPegin(params blockchain.Register
240240
return transactionReceipt, nil
241241
}
242242

243+
// Withdraw withdraws the specified amount from the LP's balance in the pegin contract.
244+
// It first performs a dry-run call to check for reverts before submitting the actual transaction.
243245
func (peginContract *peginContractImpl) Withdraw(amount *entities.Wei) error {
244246
const functionName = "withdraw"
245247
var res []any

internal/usecases/liquidity_provider/transfer_excess_to_cold_wallet.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5459
func 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.
355361
func (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.
390380
func (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.

internal/usecases/liquidity_provider/transfer_excess_to_cold_wallet_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ func TestTransferExcessToColdWalletUseCase_Run_HappyPathRskExcessWithContractFun
399399
generalProvider.On("GeneralConfiguration", ctx).Return(generalConfig)
400400

401401
stateConfig := lpEntity.StateConfiguration{
402-
LastBtcToColdWalletTransfer: &nowUnix,
403-
LastRbtcToColdWalletTransfer: &nowUnix,
402+
LastBtcToColdWalletTransfer: nowUnix,
403+
LastRbtcToColdWalletTransfer: nowUnix,
404404
}
405-
generalProvider.On("StateConfiguration", ctx).Return(stateConfig)
405+
generalProvider.On("StateConfiguration", ctx).Return(stateConfig, nil)
406406

407407
pegoutProvider.On("AvailablePegoutLiquidity", ctx).Return(btcLiquidity, nil)
408408
peginProvider.On("AvailablePeginLiquidity", ctx).Return(rbtcLiquidity, nil)
@@ -532,9 +532,9 @@ func TestTransferExcessToColdWalletUseCase_Run_RskExcess_GetContractBalanceFails
532532
},
533533
})
534534
generalProvider.On("StateConfiguration", ctx).Return(lpEntity.StateConfiguration{
535-
LastBtcToColdWalletTransfer: &nowUnix,
536-
LastRbtcToColdWalletTransfer: &nowUnix,
537-
})
535+
LastBtcToColdWalletTransfer: nowUnix,
536+
LastRbtcToColdWalletTransfer: nowUnix,
537+
}, nil)
538538
pegoutProvider.On("AvailablePegoutLiquidity", ctx).Return(btcLiquidity, nil)
539539
peginProvider.On("AvailablePeginLiquidity", ctx).Return(rbtcLiquidity, nil)
540540

@@ -626,9 +626,9 @@ func TestTransferExcessToColdWalletUseCase_Run_RskExcess_WithdrawContractFundsFa
626626
},
627627
})
628628
generalProvider.On("StateConfiguration", ctx).Return(lpEntity.StateConfiguration{
629-
LastBtcToColdWalletTransfer: &nowUnix,
630-
LastRbtcToColdWalletTransfer: &nowUnix,
631-
})
629+
LastBtcToColdWalletTransfer: nowUnix,
630+
LastRbtcToColdWalletTransfer: nowUnix,
631+
}, nil)
632632
pegoutProvider.On("AvailablePegoutLiquidity", ctx).Return(btcLiquidity, nil)
633633
peginProvider.On("AvailablePeginLiquidity", ctx).Return(rbtcLiquidity, nil)
634634

0 commit comments

Comments
 (0)