From 25df2fbd0abda45f8c7090d130aacd2bb2c7a7f0 Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Thu, 8 Sep 2022 23:39:28 +0000 Subject: [PATCH] implement tx for unlock-all-funds msg --- .../keeper/msg_server_unlock_all_funds.go | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/x/colinearcore/keeper/msg_server_unlock_all_funds.go b/x/colinearcore/keeper/msg_server_unlock_all_funds.go index 008098d..a8d9908 100644 --- a/x/colinearcore/keeper/msg_server_unlock_all_funds.go +++ b/x/colinearcore/keeper/msg_server_unlock_all_funds.go @@ -2,16 +2,43 @@ package keeper import ( "context" + "errors" + "fmt" + "math/big" "colinear/x/colinearcore/types" + sdk "github.com/cosmos/cosmos-sdk/types" ) func (k msgServer) UnlockAllFunds(goCtx context.Context, msg *types.MsgUnlockAllFunds) (*types.MsgUnlockAllFundsResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Handling the message - _ = ctx + lockedUsers, ok := k.Keeper.GetLockedUsers(ctx) + if !ok { + return nil, errors.New("unable to get locked users") + } + lockedBalStr, ok := lockedUsers.Users[msg.Creator] + if !ok { + return nil, fmt.Errorf("no locked balance found for user %s", msg.Creator) + } else { + if lockedBalStr == "0" { + return nil, fmt.Errorf("no CLR to unlock for user %s", msg.Creator) + } + } + + sendAmtBI := new(big.Int) + sendAmtBI.SetString(lockedBalStr, 10) + sendAmt := sdk.NewIntFromBigInt(sendAmtBI) + + sendCoins := sdk.NewCoins(sdk.NewCoin("uclr", sendAmt)) + + if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(msg.Creator), sendCoins); err != nil { + return nil, fmt.Errorf("failed to send %s uCLR: %s", lockedBalStr, err) + } + + delete(lockedUsers.Users, msg.Creator) + k.Keeper.SetLockedUsers(ctx, lockedUsers) return &types.MsgUnlockAllFundsResponse{}, nil }