Skip to content

Commit

Permalink
fix: reuse codes
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Nov 23, 2023
1 parent 6fcbb7b commit c2ffd69
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
10 changes: 7 additions & 3 deletions plugins/tokens/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
12 changes: 8 additions & 4 deletions plugins/tokens/swap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -107,19 +107,23 @@ 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()
}
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()
}

Expand Down
26 changes: 0 additions & 26 deletions plugins/tokens/swap/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c2ffd69

Please sign in to comment.