/
distr.go
236 lines (195 loc) · 6.61 KB
/
distr.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package keeper
import (
"fmt"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/x/pool-incentives/types"
)
func (k Keeper) FundCommunityPoolFromModule(ctx sdk.Context, asset sdk.Coin) error {
err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.communityPoolName, sdk.Coins{asset})
if err != nil {
return err
}
feePool := k.distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(asset)...)
k.distrKeeper.SetFeePool(ctx, feePool)
return nil
}
// AllocateAsset allocates and distributes coin according a gauge’s proportional weight that is recorded in the record
func (k Keeper) AllocateAsset(ctx sdk.Context) error {
logger := k.Logger(ctx)
params := k.GetParams(ctx)
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
asset := k.bankKeeper.GetBalance(ctx, moduleAddr, params.MintedDenom)
if asset.Amount.IsZero() {
// when allocating asset is zero, skip execution
return nil
}
distrInfo := k.GetDistrInfo(ctx)
if distrInfo.TotalWeight.IsZero() {
// If there are no records, put the asset to the community pool
return k.FundCommunityPoolFromModule(ctx, asset)
}
assetAmountDec := asset.Amount.ToDec()
totalWeightDec := distrInfo.TotalWeight.ToDec()
for _, record := range distrInfo.Records {
allocatingAmount := assetAmountDec.Mul(record.Weight.ToDec().Quo(totalWeightDec)).TruncateInt()
// when weight is too small and no amount is allocated, just skip this to avoid zero coin send issues
if !allocatingAmount.IsPositive() {
logger.Info(fmt.Sprintf("allocating amount for (%d, %s) record is not positive", record.GaugeId, record.Weight.String()))
continue
}
if record.GaugeId == 0 { // fund community pool if gaugeId is zero
k.FundCommunityPoolFromModule(ctx, sdk.NewCoin(asset.Denom, allocatingAmount))
continue
}
coins := sdk.NewCoins(sdk.NewCoin(asset.Denom, allocatingAmount))
err := k.incentivesKeeper.AddToGaugeRewards(ctx, k.accountKeeper.GetModuleAddress(types.ModuleName), coins, record.GaugeId)
if err != nil {
return err
}
}
return nil
}
func (k Keeper) GetDistrInfo(ctx sdk.Context) types.DistrInfo {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DistrInfoKey)
if len(bz) == 0 {
distrInfo := types.DistrInfo{
TotalWeight: sdk.NewInt(0),
Records: nil,
}
bz = k.cdc.MustMarshalBinaryBare(&distrInfo)
store.Set(types.DistrInfoKey, bz)
return distrInfo
}
distrInfo := types.DistrInfo{}
k.cdc.MustUnmarshalBinaryBare(bz, &distrInfo)
return distrInfo
}
func (k Keeper) SetDistrInfo(ctx sdk.Context, distrInfo types.DistrInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&distrInfo)
store.Set(types.DistrInfoKey, bz)
}
// indexOfDistrRecordByGaugeId returns the index of the record for the specific gauge id.
// If there is no record matched to the gauge id, return -1.
func (k Keeper) indexOfDistrRecordByGaugeId(ctx sdk.Context, gaugeId uint64) int {
distrInfo := k.GetDistrInfo(ctx)
records := distrInfo.Records
for i, record := range records {
if record.GaugeId == gaugeId {
return i
}
}
return -1
}
// This is checked for no err when a proposal is made, and executed when a proposal passes
func (k Keeper) ReplaceDistrRecords(ctx sdk.Context, records ...types.DistrRecord) error {
distrInfo := k.GetDistrInfo(ctx)
lastGaugeID := uint64(0)
gaugeIdFlags := make(map[uint64]bool)
totalWeight := sdk.NewInt(0)
for _, record := range records {
if gaugeIdFlags[record.GaugeId] {
return sdkerrors.Wrapf(
types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d has duplications.",
record.GaugeId,
)
}
// Ensure records are sorted because ~AESTHETIC~
if record.GaugeId < lastGaugeID {
return sdkerrors.Wrapf(
types.ErrDistrRecordNotSorted,
"Gauge ID #%d came after Gauge ID #%d.",
record.GaugeId, lastGaugeID,
)
}
lastGaugeID = record.GaugeId
// unless GaugeID is 0 for the community pool, don't allow distribution records for gauges that don't exist
if record.GaugeId != 0 {
gauge, err := k.incentivesKeeper.GetGaugeByID(ctx, record.GaugeId)
if err != nil {
return err
}
if !gauge.IsPerpetual {
return sdkerrors.Wrapf(types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d is not perpetual.",
record.GaugeId)
}
}
gaugeIdFlags[record.GaugeId] = true
totalWeight = totalWeight.Add(record.Weight)
}
distrInfo.Records = records
distrInfo.TotalWeight = totalWeight
k.SetDistrInfo(ctx, distrInfo)
return nil
}
// This is checked for no err when a proposal is made, and executed when a proposal passes
func (k Keeper) UpdateDistrRecords(ctx sdk.Context, records ...types.DistrRecord) error {
recordsMap := make(map[uint64]types.DistrRecord)
totalWeight := sdk.NewInt(0)
for _, existingRecord := range k.GetDistrInfo(ctx).Records {
recordsMap[existingRecord.GaugeId] = existingRecord
totalWeight = totalWeight.Add(existingRecord.Weight)
}
lastGaugeID := uint64(0)
// TODO: Delete gaugeIdFlags
gaugeIdFlags := make(map[uint64]bool)
for _, record := range records {
if gaugeIdFlags[record.GaugeId] {
return sdkerrors.Wrapf(
types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d has duplications.",
record.GaugeId,
)
}
// Ensure records are sorted because ~AESTHETIC~
if record.GaugeId < lastGaugeID {
return sdkerrors.Wrapf(
types.ErrDistrRecordNotSorted,
"Gauge ID #%d came after Gauge ID #%d.",
record.GaugeId, lastGaugeID,
)
}
lastGaugeID = record.GaugeId
// unless GaugeID is 0 for the community pool, don't allow distribution records for gauges that don't exist
if record.GaugeId != 0 {
gauge, err := k.incentivesKeeper.GetGaugeByID(ctx, record.GaugeId)
if err != nil {
return err
}
if !gauge.IsPerpetual {
return sdkerrors.Wrapf(types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d is not perpetual.",
record.GaugeId)
}
}
gaugeIdFlags[record.GaugeId] = true
if val, ok := recordsMap[record.GaugeId]; ok {
totalWeight = totalWeight.Sub(val.Weight)
recordsMap[record.GaugeId] = record
totalWeight = totalWeight.Add(record.Weight)
} else {
recordsMap[record.GaugeId] = record
totalWeight = totalWeight.Add(record.Weight)
}
}
newRecords := []types.DistrRecord{}
for _, val := range recordsMap {
if !val.Weight.Equal(sdk.ZeroInt()) {
newRecords = append(newRecords, val)
}
}
sort.SliceStable(newRecords, func(i, j int) bool {
return newRecords[i].GaugeId < newRecords[j].GaugeId
})
k.SetDistrInfo(ctx, types.DistrInfo{
Records: newRecords,
TotalWeight: totalWeight,
})
return nil
}