implement tx for unlock-all-funds msg

master
michael 2022-09-08 23:39:28 +00:00
parent 42bb8112ad
commit 25df2fbd0a
1 changed files with 29 additions and 2 deletions

View File

@ -2,16 +2,43 @@ package keeper
import ( import (
"context" "context"
"errors"
"fmt"
"math/big"
"colinear/x/colinearcore/types" "colinear/x/colinearcore/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
) )
func (k msgServer) UnlockAllFunds(goCtx context.Context, msg *types.MsgUnlockAllFunds) (*types.MsgUnlockAllFundsResponse, error) { func (k msgServer) UnlockAllFunds(goCtx context.Context, msg *types.MsgUnlockAllFunds) (*types.MsgUnlockAllFundsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx) ctx := sdk.UnwrapSDKContext(goCtx)
// TODO: Handling the message lockedUsers, ok := k.Keeper.GetLockedUsers(ctx)
_ = 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 return &types.MsgUnlockAllFundsResponse{}, nil
} }