Skip to content

Commit

Permalink
fix force unlock bug and cli add (#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic authored Nov 11, 2022
1 parent a355026 commit 0c8fe28
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
50 changes: 50 additions & 0 deletions x/lockup/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetTxCmd() *cobra.Command {
NewLockTokensCmd(),
NewBeginUnlockingCmd(),
NewBeginUnlockByIDCmd(),
NewForceUnlockByIdCmd(),
)

return cmd
Expand Down Expand Up @@ -154,3 +155,52 @@ func NewBeginUnlockByIDCmd() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// NewForceUnlockByIdCmd force unlocks individual period lock by ID if proper permissions exist.
func NewForceUnlockByIdCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "force-unlock-by-id [id]",
Short: "force unlocks individual period lock by ID",
Long: "force unlocks individual period lock by ID. if no amount provided, entire lock is unlocked",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)

id, err := strconv.Atoi(args[0])
if err != nil {
return err
}

coins := sdk.Coins(nil)
amountStr, err := cmd.Flags().GetString(FlagAmount)
if err != nil {
return err
}

if amountStr != "" {
coins, err = sdk.ParseCoinsNormalized(amountStr)
if err != nil {
return err
}
}

msg := types.NewMsgForceUnlock(
clientCtx.GetFromAddress(),
uint64(id),
coins,
)

return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}

cmd.Flags().AddFlagSet(FlagSetUnlockTokens())

flags.AddTxFlagsToCmd(cmd)
return cmd
}
23 changes: 15 additions & 8 deletions x/lockup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,28 @@ func (server msgServer) ExtendLockup(goCtx context.Context, msg *types.MsgExtend
func (server msgServer) ForceUnlock(goCtx context.Context, msg *types.MsgForceUnlock) (*types.MsgForceUnlockResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

// check if message sender matches lock owner
if lock.Owner != msg.Owner {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "Sender (%s) does not match lock owner (%s)", msg.Owner, lock.Owner)
}

// check for chain parameter that the address is allowed to force unlock
forceUnlockAllowedAddresses := server.keeper.GetParams(ctx).ForceUnlockAllowedAddresses
found := false
for _, address := range forceUnlockAllowedAddresses {
if address == msg.Owner {
for _, addr := range forceUnlockAllowedAddresses {
// defense in depth, double checking the message owner and lock owner are both the same and is one of the allowed force unlock addresses
if addr == lock.Owner && addr == msg.Owner {
found = true
break
}
}
if !found {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "Sender (%s) not allowed to force unlock", msg.Owner)
}

lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "Sender (%s) not allowed to force unlock", lock.Owner)
}

// check that given lock is not superfluid staked
Expand Down

0 comments on commit 0c8fe28

Please sign in to comment.