-
Notifications
You must be signed in to change notification settings - Fork 327
/
keeper_replace_update_distribution.go
79 lines (60 loc) · 1.66 KB
/
keeper_replace_update_distribution.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
package keeper
import (
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension/v3/x/streamer/types"
)
// This is checked for no err when a proposal is made, and executed when a proposal passes.
func (k Keeper) ReplaceDistrRecords(ctx sdk.Context, streamId uint64, records []types.DistrRecord) error {
stream, err := k.GetStreamByID(ctx, streamId)
if err != nil {
return err
}
distrInfo, err := k.NewDistrInfo(ctx, records)
if err != nil {
return err
}
stream.DistributeTo = distrInfo
err = k.setStream(ctx, stream)
if err != nil {
return err
}
return nil
}
// UpdateDistrRecords is checked for no err when a proposal is made, and executed when a proposal passes.
func (k Keeper) UpdateDistrRecords(ctx sdk.Context, streamId uint64, records []types.DistrRecord) error {
recordsMap := make(map[uint64]types.DistrRecord)
stream, err := k.GetStreamByID(ctx, streamId)
if err != nil {
return err
}
err = k.validateGauges(ctx, records)
if err != nil {
return err
}
for _, existingRecord := range stream.DistributeTo.Records {
recordsMap[existingRecord.GaugeId] = existingRecord
}
for _, record := range records {
recordsMap[record.GaugeId] = record
}
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
})
distrInfo, err := k.NewDistrInfo(ctx, newRecords)
if err != nil {
return err
}
stream.DistributeTo = distrInfo
err = k.setStream(ctx, stream)
if err != nil {
return err
}
return nil
}