-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
162 lines (131 loc) · 4.4 KB
/
plan.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package keeper
import (
gogotypes "github.com/gogo/protobuf/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kogisin/cosmos-sdk-modules/x/farming/types"
)
// NewPlan sets the next plan number to a given plan interface
func (k Keeper) NewPlan(ctx sdk.Context, plan types.PlanI) types.PlanI {
if err := plan.SetId(k.GetNextPlanIDWithUpdate(ctx)); err != nil {
panic(err)
}
return plan
}
// GetPlan implements PlanI.
func (k Keeper) GetPlan(ctx sdk.Context, id uint64) (plan types.PlanI, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetPlanKey(id))
if bz == nil {
return plan, false
}
return k.decodePlan(bz), true
}
// GetAllPlans returns all plans in the Keeper.
func (k Keeper) GetAllPlans(ctx sdk.Context) (plans []types.PlanI) {
k.IterateAllPlans(ctx, func(plan types.PlanI) (stop bool) {
plans = append(plans, plan)
return false
})
return plans
}
// SetPlan implements PlanI.
func (k Keeper) SetPlan(ctx sdk.Context, plan types.PlanI) {
id := plan.GetId()
store := ctx.KVStore(k.storeKey)
bz, err := k.MarshalPlan(plan)
if err != nil {
panic(err)
}
store.Set(types.GetPlanKey(id), bz)
}
// RemovePlan removes an plan for the plan mapper store.
// NOTE: this will cause supply invariant violation if called
func (k Keeper) RemovePlan(ctx sdk.Context, plan types.PlanI) {
id := plan.GetId()
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPlanKey(id))
}
// IterateAllPlans iterates over all the stored plans and performs a callback function.
// Stops iteration when callback returns true.
func (k Keeper) IterateAllPlans(ctx sdk.Context, cb func(plan types.PlanI) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PlanKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
plan := k.decodePlan(iterator.Value())
if cb(plan) {
break
}
}
}
// GetPlansByFarmerAddrIndex reads from kvstore and return a specific Plan indexed by given farmer address
func (k Keeper) GetPlansByFarmerAddrIndex(ctx sdk.Context, farmerAcc sdk.AccAddress) (plans []types.PlanI) {
k.IteratePlansByFarmerAddr(ctx, farmerAcc, func(plan types.PlanI) bool {
plans = append(plans, plan)
return false
})
return plans
}
// IteratePlansByFarmerAddr iterates over all the stored plans and performs a callback function.
// Stops iteration when callback returns true.
func (k Keeper) IteratePlansByFarmerAddr(ctx sdk.Context, farmerAcc sdk.AccAddress, cb func(plan types.PlanI) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPlansByFarmerAddrIndexKey(farmerAcc))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
planID := gogotypes.UInt64Value{}
err := k.cdc.Unmarshal(iterator.Value(), &planID)
if err != nil {
panic(err)
}
plan, _ := k.GetPlan(ctx, planID.GetValue())
if cb(plan) {
break
}
}
}
// SetPlanIDByFarmerAddrIndex sets Index by FarmerAddr
// TODO: need to gas cost check for existing check or update everytime
func (k Keeper) SetPlanIDByFarmerAddrIndex(ctx sdk.Context, planID uint64, farmerAcc sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&gogotypes.UInt64Value{Value: planID})
store.Set(types.GetPlanByFarmerAddrIndexKey(farmerAcc, planID), b)
}
// CreateFixedAmountPlan sets fixed amount plan.
func (k Keeper) CreateFixedAmountPlan(ctx sdk.Context, msg *types.MsgCreateFixedAmountPlan, typ types.PlanType) *types.FixedAmountPlan {
nextId := k.GetNextPlanIDWithUpdate(ctx)
farmingPoolAddr := msg.GetFarmingPoolAddress()
terminationAddr := farmingPoolAddr
basePlan := types.NewBasePlan(
nextId,
typ,
farmingPoolAddr,
terminationAddr,
msg.GetStakingCoinWeights(),
msg.StartTime,
msg.EndTime,
msg.GetEpochDays(),
)
fixedPlan := types.NewFixedAmountPlan(basePlan, msg.EpochAmount)
k.SetPlan(ctx, fixedPlan)
return fixedPlan
}
// CreateRatioPlan sets ratio plan.
func (k Keeper) CreateRatioPlan(ctx sdk.Context, msg *types.MsgCreateRatioPlan, typ types.PlanType) *types.RatioPlan {
nextId := k.GetNextPlanIDWithUpdate(ctx)
farmingPoolAddr := msg.GetFarmingPoolAddress()
terminationAddr := farmingPoolAddr
basePlan := types.NewBasePlan(
nextId,
typ,
farmingPoolAddr,
terminationAddr,
msg.GetStakingCoinWeights(),
msg.StartTime,
msg.EndTime,
msg.GetEpochDays(),
)
ratioPlan := types.NewRatioPlan(basePlan, msg.EpochRatio)
k.SetPlan(ctx, ratioPlan)
return ratioPlan
}