/
msg_server_unstake_gateway.go
64 lines (52 loc) · 1.98 KB
/
msg_server_unstake_gateway.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
54
55
56
57
58
59
60
61
62
63
64
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/gateway/types"
)
// TODO_TECHDEBT(#49): Add un-delegation from delegated apps
// TODO(#73): Determine if a gateway needs an unbonding period after unstaking.
func (k msgServer) UnstakeGateway(
goCtx context.Context,
msg *types.MsgUnstakeGateway,
) (*types.MsgUnstakeGatewayResponse, error) {
isSuccessful := false
defer telemetry.EventSuccessCounter(
"unstake_gateway",
telemetry.DefaultCounterFn,
func() bool { return isSuccessful },
)
ctx := sdk.UnwrapSDKContext(goCtx)
logger := k.Logger().With("method", "UnstakeGateway")
logger.Info(fmt.Sprintf("About to unstake gateway with msg: %v", msg))
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
// Check if the gateway already exists or not
var err error
gateway, isGatewayFound := k.GetGateway(ctx, msg.Address)
if !isGatewayFound {
logger.Info(fmt.Sprintf("Gateway not found. Cannot unstake address %s", msg.Address))
return nil, types.ErrGatewayNotFound
}
logger.Info(fmt.Sprintf("Gateway found. Unstaking gateway for address %s", msg.Address))
// Retrieve the address of the gateway
gatewayAddress, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
logger.Error(fmt.Sprintf("could not parse address %s", msg.Address))
return nil, err
}
// Send the coins from the gateway pool back to the gateway
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, gatewayAddress, []sdk.Coin{*gateway.Stake})
if err != nil {
logger.Error(fmt.Sprintf("could not send %v coins from %s module to %s account due to %v", gateway.Stake, gatewayAddress, types.ModuleName, err))
return nil, err
}
// Update the Gateway in the store
k.RemoveGateway(ctx, gatewayAddress.String())
logger.Info(fmt.Sprintf("Successfully removed the gateway: %+v", gateway))
isSuccessful = true
return &types.MsgUnstakeGatewayResponse{}, nil
}