2022-09-08 16:29:08 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-08 16:39:28 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2022-09-08 16:29:08 -07:00
|
|
|
|
|
|
|
"colinear/x/colinearcore/types"
|
2022-09-08 16:39:28 -07:00
|
|
|
|
2022-09-08 16:29:08 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k msgServer) UnlockAllFunds(goCtx context.Context, msg *types.MsgUnlockAllFunds) (*types.MsgUnlockAllFundsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
2022-09-08 16:39:28 -07:00
|
|
|
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)
|
2022-09-08 16:29:08 -07:00
|
|
|
|
|
|
|
return &types.MsgUnlockAllFundsResponse{}, nil
|
|
|
|
}
|