/
msg_settle_request.go
83 lines (68 loc) · 2.11 KB
/
msg_settle_request.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package keeper
import (
"context"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/spn/x/launch/types"
profiletypes "github.com/tendermint/spn/x/profile/types"
)
func (k msgServer) SettleRequest(
goCtx context.Context,
msg *types.MsgSettleRequest,
) (*types.MsgSettleRequestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain, found := k.GetChain(ctx, msg.LaunchID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrChainNotFound, "%d", msg.LaunchID)
}
if chain.LaunchTriggered {
return nil, sdkerrors.Wrapf(types.ErrTriggeredLaunch, "%d", msg.LaunchID)
}
coord, found := k.profileKeeper.GetCoordinator(ctx, chain.CoordinatorID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrChainInactive,
"the chain %d coordinator not found", chain.LaunchID)
}
if !coord.Active {
return nil, sdkerrors.Wrapf(profiletypes.ErrCoordInactive,
"the chain %d coordinator inactive", chain.LaunchID)
}
if msg.Approve && msg.Signer != coord.Address {
return nil, sdkerrors.Wrap(types.ErrNoAddressPermission, msg.Signer)
}
// first check if the request exists
request, found := k.GetRequest(ctx, msg.LaunchID, msg.RequestID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrRequestNotFound,
"request %d for chain %d not found",
msg.RequestID,
msg.LaunchID,
)
}
if request.Status != types.Request_PENDING {
return nil, sdkerrors.Wrapf(types.ErrRequestSettled,
"request %d is not pending",
msg.RequestID,
)
}
if msg.Signer != request.Creator && msg.Signer != coord.Address {
return nil, sdkerrors.Wrap(types.ErrNoAddressPermission, msg.Signer)
}
// apply request if approving and update status
if msg.Approve {
err := ApplyRequest(ctx, k.Keeper, chain, request, coord)
if err != nil {
return nil, err
}
request.Status = types.Request_APPROVED
} else {
request.Status = types.Request_REJECTED
}
k.SetRequest(ctx, request)
err := ctx.EventManager().EmitTypedEvent(&types.EventRequestSettled{
LaunchID: msg.LaunchID,
RequestID: request.RequestID,
Approved: msg.Approve,
})
return &types.MsgSettleRequestResponse{}, err
}