-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
130 lines (104 loc) · 2.8 KB
/
keeper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/nebula-labs/nebula/x/launchpad/types"
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramstore paramtypes.Subspace
hooks types.LaunchpadHooks
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey sdk.StoreKey,
ps paramtypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
paramstore: ps,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetModuleAccountAddress gets the address of module account
func (k Keeper) GetModuleAccountAddress(ctx sdk.Context) sdk.AccAddress {
return k.accountKeeper.GetModuleAddress(types.ModuleName)
}
// ============ Project Helper Logic
// Get new project address
func (k Keeper) NewProjectAddress(projectID uint64) sdk.AccAddress {
key := append([]byte("project"), sdk.Uint64ToBigEndian(projectID)...)
return address.Module(types.ModuleName, key)
}
func (k Keeper) SetProjectActive(ctx sdk.Context, projectId uint64) error {
project, err := k.GetProjectById(ctx, projectId)
if err != nil {
return err
}
if project.ProjectStatus == types.PROJECT_ACTIVE {
return nil
}
if project.ProjectStatus != types.PROJECT_INIT {
return types.ErrCannotModifyProject
}
project.ProjectStatus = types.PROJECT_ACTIVE
err = k.SetProject(ctx, project)
if err != nil {
return err
}
return nil
}
func (k Keeper) SetProjectEndable(ctx sdk.Context, projectId uint64) error {
project, err := k.GetProjectById(ctx, projectId)
if err != nil {
return err
}
if project.ProjectStatus != types.PROJECT_ACTIVE {
return types.ErrCannotModifyProject
}
// check if all rm has ended
for _, any := range project.RegisteredRm {
rmItem, err := k.ParseAnyReleaseMechanism(any)
if err != nil {
return err
}
if rmItem.GetReleaseMechanismStatus() != types.RM_ENDED {
return types.ErrCannotModifyProject
}
}
project.ProjectStatus = types.PROJECT_ENDED
err = k.SetProject(ctx, project)
if err != nil {
return err
}
return nil
}
// ============ Hooks
// Set the gamm hooks.
func (k *Keeper) SetHooks(gh types.LaunchpadHooks) *Keeper {
if k.hooks != nil {
panic("cannot set gamm hooks twice")
}
k.hooks = gh
return k
}