/
msg_server_unstake_application.go
57 lines (47 loc) · 1.87 KB
/
msg_server_unstake_application.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
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/application/types"
)
// TODO(#73): Determine if an application needs an unbonding period after unstaking.
func (k msgServer) UnstakeApplication(
ctx context.Context,
msg *types.MsgUnstakeApplication,
) (*types.MsgUnstakeApplicationResponse, error) {
isSuccessful := false
defer telemetry.EventSuccessCounter(
"unstake_application",
telemetry.DefaultCounterFn,
func() bool { return isSuccessful },
)
logger := k.Logger().With("method", "UnstakeApplication")
logger.Info(fmt.Sprintf("About to unstake application with msg: %v", msg))
// Check if the application already exists or not
var err error
foundApp, isAppFound := k.GetApplication(ctx, msg.Address)
if !isAppFound {
logger.Info(fmt.Sprintf("Application not found. Cannot unstake address %s", msg.Address))
return nil, types.ErrAppNotFound
}
logger.Info(fmt.Sprintf("Application found. Unstaking application for address %s", msg.Address))
// Retrieve the address of the application
appAddress, 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 application pool back to the application
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, appAddress, []sdk.Coin{*foundApp.Stake})
if err != nil {
logger.Error(fmt.Sprintf("could not send %v coins from %s module to %s account due to %v", foundApp.Stake, appAddress, types.ModuleName, err))
return nil, err
}
// Update the Application in the store
k.RemoveApplication(ctx, appAddress.String())
logger.Info(fmt.Sprintf("Successfully removed the application: %+v", foundApp))
isSuccessful = true
return &types.MsgUnstakeApplicationResponse{}, nil
}