/
msg_undelegate_host.go
53 lines (39 loc) · 1.5 KB
/
msg_undelegate_host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package keeper
import (
"context"
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Stride-Labs/stride/v16/x/stakeibc/types"
)
const isUndelegateHostPreventedKey = "is-undelegate-host-prevented"
func (k msgServer) UndelegateHost(goCtx context.Context, msg *types.MsgUndelegateHost) (*types.MsgUndelegateHostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// undelegateHost is callable only if it has not yet been called and succeeded
if k.IsUndelegateHostPrevented(ctx) {
return nil, errorsmod.Wrapf(types.ErrUndelegateHostNotCallable, "")
}
// Get host zone unbonding message by summing up the unbonding records
if err := k.UndelegateHostEvmos(ctx, msg.Amount); err != nil {
return nil, fmt.Errorf("Error initiating host zone unbondings for UndelegateHostEvmos %s", err.Error())
}
// log: issuing an undelegation to Evmos
k.Logger(ctx).Info("Issuing an undelegation to Evmos")
return &types.MsgUndelegateHostResponse{}, nil
}
func (k Keeper) SetUndelegateHostPrevented(ctx sdk.Context) error {
store := ctx.KVStore(k.storeKey)
// set the key to 1 if it's not set
if !k.IsUndelegateHostPrevented(ctx) {
store.Set([]byte(isUndelegateHostPreventedKey), []byte{1})
}
return nil
}
func (k Keeper) IsUndelegateHostPrevented(ctx sdk.Context) bool {
store := ctx.KVStore(k.storeKey)
if !store.Has([]byte(isUndelegateHostPreventedKey)) {
return false
}
value := store.Get([]byte(isUndelegateHostPreventedKey))
return len(value) == 1 && value[0] == 1
}