From c2ffd69ae1fb7530f4d533cf24e38539388aaf35 Mon Sep 17 00:00:00 2001 From: j75689 Date: Thu, 23 Nov 2023 16:06:39 +0800 Subject: [PATCH] fix: reuse codes --- plugins/tokens/plugin.go | 10 +++++++--- plugins/tokens/swap/handler.go | 12 ++++++++---- plugins/tokens/swap/keeper.go | 26 -------------------------- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/plugins/tokens/plugin.go b/plugins/tokens/plugin.go index 6f70502bc..a34dd41ee 100644 --- a/plugins/tokens/plugin.go +++ b/plugins/tokens/plugin.go @@ -92,9 +92,13 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap var automaticSwap swap.AtomicSwap swapKeeper.CDC().MustUnmarshalBinaryBare(iterator.Value(), &automaticSwap) swapID := iterator.Key()[len(swap.HashKey):] - err := swapKeeper.Refound(ctx, swapID, &automaticSwap) - if err != nil { - logger.Error("Refound error", "error", err) + result := swap.HandleClaimHashTimerLockedTransferAfterBCFusion(ctx, swapKeeper, swap.ClaimHTLTMsg{ + From: automaticSwap.From, + SwapID: swapID, + RandomNumber: automaticSwap.RandomNumber, + }) + if !result.IsOK() { + logger.Error("Refound error", "swapId", swapID) continue } i++ diff --git a/plugins/tokens/swap/handler.go b/plugins/tokens/swap/handler.go index 384fa824b..0cc912f70 100644 --- a/plugins/tokens/swap/handler.go +++ b/plugins/tokens/swap/handler.go @@ -22,7 +22,7 @@ func NewHandler(kp Keeper) sdk.Handler { } return handleDepositHashTimerLockedTransfer(ctx, kp, msg) case ClaimHTLTMsg: - return handleClaimHashTimerLockedTransfer(ctx, kp, msg) + return handleClaimHashTimerLockedTransfer(ctx, kp, msg, false) case RefundHTLTMsg: return handleRefundHashTimerLockedTransfer(ctx, kp, msg) default: @@ -107,7 +107,11 @@ func handleDepositHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg Deposi } -func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg) sdk.Result { +func HandleClaimHashTimerLockedTransferAfterBCFusion(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg) sdk.Result { + return handleClaimHashTimerLockedTransfer(ctx, kp, msg, true) +} + +func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg, isBCFusionRefund bool) sdk.Result { swap := kp.GetSwap(ctx, msg.SwapID) if swap == nil { return ErrNonExistSwapID(fmt.Sprintf("No matched swap with swapID %v", msg.SwapID)).Result() @@ -115,11 +119,11 @@ func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTL if swap.Status != Open { return ErrUnexpectedSwapStatus(fmt.Sprintf("Expected swap status is Open, actually it is %s", swap.Status.String())).Result() } - if swap.ExpireHeight <= ctx.BlockHeight() { + if !isBCFusionRefund && swap.ExpireHeight <= ctx.BlockHeight() { return ErrClaimExpiredSwap(fmt.Sprintf("Current block height is %d, the swap expire height(%d) is passed", ctx.BlockHeight(), swap.ExpireHeight)).Result() } - if !bytes.Equal(CalculateRandomHash(msg.RandomNumber, swap.Timestamp), swap.RandomNumberHash) { + if !isBCFusionRefund && !bytes.Equal(CalculateRandomHash(msg.RandomNumber, swap.Timestamp), swap.RandomNumberHash) { return ErrMismatchedRandomNumber("Mismatched random number").Result() } diff --git a/plugins/tokens/swap/keeper.go b/plugins/tokens/swap/keeper.go index 44616848c..060f77473 100644 --- a/plugins/tokens/swap/keeper.go +++ b/plugins/tokens/swap/keeper.go @@ -169,29 +169,3 @@ func (kp *Keeper) getIndex(ctx sdk.Context) int64 { } return int64(binary.BigEndian.Uint64(bz)) } - -func (kp *Keeper) Refound(ctx sdk.Context, swapID SwapBytes, swap *AtomicSwap) error { - if swap.Status != Open { - return fmt.Errorf(fmt.Sprint("Invalid swap status", "status", swap.Status)) - } - if !swap.OutAmount.IsZero() { - _, err := kp.ck.SendCoins(ctx, AtomicSwapCoinsAccAddr, swap.From, swap.OutAmount) - if err != nil { - return fmt.Errorf(fmt.Sprint("Failed to send coins", "sender", AtomicSwapCoinsAccAddr.String(), "recipient", swap.From.String(), "amount", swap.OutAmount.String(), "err", err.Error())) - } - } - if !swap.InAmount.IsZero() { - _, err := kp.ck.SendCoins(ctx, AtomicSwapCoinsAccAddr, swap.To, swap.InAmount) - if err != nil { - return fmt.Errorf(fmt.Sprint("Failed to send coins", "sender", AtomicSwapCoinsAccAddr.String(), "recipient", swap.To.String(), "amount", swap.InAmount.String(), "err", err.Error())) - } - } - - swap.Status = Completed - swap.ClosedTime = ctx.BlockHeader().Time.Unix() - err := kp.CloseSwap(ctx, swapID, swap) - if err != nil { - return fmt.Errorf(fmt.Sprint("Failed to close swap", "err", err.Error())) - } - return nil -}