-
Notifications
You must be signed in to change notification settings - Fork 40
/
payout.go
227 lines (177 loc) · 5.28 KB
/
payout.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
package keeper
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
protobuf "github.com/gogo/protobuf/types"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/subscription/types"
)
func (k *Keeper) SetPayout(ctx sdk.Context, payout types.Payout) {
var (
store = k.Store(ctx)
key = types.PayoutKey(payout.ID)
value = k.cdc.MustMarshal(&payout)
)
store.Set(key, value)
}
func (k *Keeper) HasPayout(ctx sdk.Context, id uint64) bool {
var (
store = k.Store(ctx)
key = types.PayoutKey(id)
)
return store.Has(key)
}
func (k *Keeper) GetPayout(ctx sdk.Context, id uint64) (payout types.Payout, found bool) {
var (
store = k.Store(ctx)
key = types.PayoutKey(id)
value = store.Get(key)
)
if value == nil {
return payout, false
}
k.cdc.MustUnmarshal(value, &payout)
return payout, true
}
func (k *Keeper) DeletePayout(ctx sdk.Context, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutKey(id)
)
store.Delete(key)
}
func (k *Keeper) GetPayouts(ctx sdk.Context) (items types.Payouts) {
var (
store = k.Store(ctx)
iter = sdk.KVStorePrefixIterator(store, types.PayoutKeyPrefix)
)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var item types.Payout
k.cdc.MustUnmarshal(iter.Value(), &item)
items = append(items, item)
}
return items
}
func (k *Keeper) IteratePayouts(ctx sdk.Context, fn func(index int, item types.Payout) (stop bool)) {
store := k.Store(ctx)
iter := sdk.KVStorePrefixIterator(store, types.PayoutKeyPrefix)
defer iter.Close()
for i := 0; iter.Valid(); iter.Next() {
var item types.Payout
k.cdc.MustUnmarshal(iter.Value(), &item)
if stop := fn(i, item); stop {
break
}
i++
}
}
func (k *Keeper) SetPayoutForAccount(ctx sdk.Context, addr sdk.AccAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForAccountKey(addr, id)
value = k.cdc.MustMarshal(&protobuf.BoolValue{Value: true})
)
store.Set(key, value)
}
func (k *Keeper) HashPayoutForAccount(ctx sdk.Context, addr sdk.AccAddress, id uint64) bool {
var (
store = k.Store(ctx)
key = types.PayoutForAccountKey(addr, id)
)
return store.Has(key)
}
func (k *Keeper) DeletePayoutForAccount(ctx sdk.Context, addr sdk.AccAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForAccountKey(addr, id)
)
store.Delete(key)
}
func (k *Keeper) SetPayoutForNode(ctx sdk.Context, addr hubtypes.NodeAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForNodeKey(addr, id)
value = k.cdc.MustMarshal(&protobuf.BoolValue{Value: true})
)
store.Set(key, value)
}
func (k *Keeper) HashPayoutForNode(ctx sdk.Context, addr hubtypes.NodeAddress, id uint64) bool {
var (
store = k.Store(ctx)
key = types.PayoutForNodeKey(addr, id)
)
return store.Has(key)
}
func (k *Keeper) DeletePayoutForNode(ctx sdk.Context, addr hubtypes.NodeAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForNodeKey(addr, id)
)
store.Delete(key)
}
func (k *Keeper) SetPayoutForAccountByNode(ctx sdk.Context, accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForAccountByNodeKey(accAddr, nodeAddr, id)
value = k.cdc.MustMarshal(&protobuf.BoolValue{Value: true})
)
store.Set(key, value)
}
func (k *Keeper) HashPayoutForAccountByNode(ctx sdk.Context, accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress, id uint64) bool {
var (
store = k.Store(ctx)
key = types.PayoutForAccountByNodeKey(accAddr, nodeAddr, id)
)
return store.Has(key)
}
func (k *Keeper) DeletePayoutForAccountByNode(ctx sdk.Context, accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForAccountByNodeKey(accAddr, nodeAddr, id)
)
store.Delete(key)
}
func (k *Keeper) GetLatestPayoutForAccountByNode(ctx sdk.Context, accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress) (payout types.Payout, found bool) {
store := k.Store(ctx)
iter := sdk.KVStoreReversePrefixIterator(store, types.GetPayoutForAccountByNodeKeyPrefix(accAddr, nodeAddr))
defer iter.Close()
if iter.Valid() {
payout, found = k.GetPayout(ctx, types.IDFromPayoutForAccountByNodeKey(iter.Key()))
if !found {
panic(fmt.Errorf("payout for account by node key %X does not exist", iter.Key()))
}
}
return payout, found
}
func (k *Keeper) SetPayoutForNextAt(ctx sdk.Context, at time.Time, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForNextAtKey(at, id)
value = k.cdc.MustMarshal(&protobuf.BoolValue{Value: true})
)
store.Set(key, value)
}
func (k *Keeper) DeletePayoutForNextAt(ctx sdk.Context, at time.Time, id uint64) {
var (
store = k.Store(ctx)
key = types.PayoutForNextAtKey(at, id)
)
store.Delete(key)
}
func (k *Keeper) IteratePayoutsForNextAt(ctx sdk.Context, at time.Time, fn func(index int, item types.Payout) (stop bool)) {
store := k.Store(ctx)
iter := store.Iterator(types.PayoutForNextAtKeyPrefix, sdk.PrefixEndBytes(types.GetPayoutForNextAtKeyPrefix(at)))
defer iter.Close()
for i := 0; iter.Valid(); iter.Next() {
payout, found := k.GetPayout(ctx, types.IDFromPayoutForNextAtKey(iter.Key()))
if !found {
panic(fmt.Errorf("payout for next_at key %X does not exist", iter.Key()))
}
if stop := fn(i, payout); stop {
break
}
i++
}
}