-
Notifications
You must be signed in to change notification settings - Fork 568
/
statistics.go
279 lines (221 loc) · 9.21 KB
/
statistics.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
gogotypes "github.com/cosmos/gogoproto/types"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils"
poolmanagertypes "github.com/osmosis-labs/osmosis/v25/x/poolmanager/types"
"github.com/osmosis-labs/osmosis/v25/x/protorev/types"
)
// ----------------------- Statistics Stores ----------------------- //
// GetNumberOfTrades returns the number of trades executed by the ProtoRev module
func (k Keeper) GetNumberOfTrades(ctx sdk.Context) (osmomath.Int, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNumberOfTrades)
bz := store.Get(types.KeyPrefixNumberOfTrades)
if len(bz) == 0 {
return osmomath.ZeroInt(), fmt.Errorf("no trades have been executed by the protorev module")
}
trades := osmomath.Int{}
if err := trades.Unmarshal(bz); err != nil {
return osmomath.ZeroInt(), err
}
return trades, nil
}
// IncrementNumberOfTrades increments the number of trades executed by the ProtoRev module
func (k Keeper) IncrementNumberOfTrades(ctx sdk.Context) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNumberOfTrades)
numberOfTrades, _ := k.GetNumberOfTrades(ctx)
numberOfTrades = numberOfTrades.Add(oneInt)
bz, err := numberOfTrades.Marshal()
if err != nil {
return err
}
store.Set(types.KeyPrefixNumberOfTrades, bz)
return nil
}
// GetProfitsByDenom returns the profits made by the ProtoRev module for the given denom
func (k Keeper) GetProfitsByDenom(ctx sdk.Context, denom string) (sdk.Coin, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixProfitByDenom)
key := types.GetKeyPrefixProfitByDenom(denom)
bz := store.Get(key)
if len(bz) == 0 {
return sdk.NewCoin(denom, osmomath.ZeroInt()), fmt.Errorf("no profits for denom %s", denom)
}
profits := sdk.Coin{}
if err := profits.Unmarshal(bz); err != nil {
return sdk.NewCoin(denom, osmomath.ZeroInt()), err
}
return profits, nil
}
// GetAllProfits returns all of the profits made by the ProtoRev module.
func (k Keeper) GetAllProfits(ctx sdk.Context) []sdk.Coin {
profits := make([]sdk.Coin, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixProfitByDenom)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value()
profit := sdk.Coin{}
if err := profit.Unmarshal(bz); err == nil {
profits = append(profits, profit)
}
}
return profits
}
func (k Keeper) SetCyclicArbProfitTrackerValue(ctx sdk.Context, cyclicArbProfits sdk.Coins) {
newCyclicArbProfits := poolmanagertypes.TrackedVolume{
Amount: cyclicArbProfits,
}
osmoutils.MustSet(ctx.KVStore(k.storeKey), types.KeyCyclicArbTracker, &newCyclicArbProfits)
}
func (k Keeper) GetCyclicArbProfitTrackerValue(ctx sdk.Context) (currentCyclicArbProfits sdk.Coins) {
var cyclicArbProfits poolmanagertypes.TrackedVolume
cyclicArbProfitsFound, err := osmoutils.Get(ctx.KVStore(k.storeKey), types.KeyCyclicArbTracker, &cyclicArbProfits)
if err != nil {
// We can only encounter an error if a database or serialization errors occurs, so we panic here.
// Normally this would be handled by `osmoutils.MustGet`, but since we want to specifically use `osmoutils.Get`,
// we also have to manually panic here.
panic(err)
}
// If no volume was found, we treat the existing volume as 0.
// While we can technically require volume to exist, we would need to store empty coins in state for each pool (past and present),
// which is a high storage cost to pay for a weak guardrail.
currentCyclicArbProfits = sdk.NewCoins()
if cyclicArbProfitsFound {
currentCyclicArbProfits = cyclicArbProfits.Amount
}
return currentCyclicArbProfits
}
// GetCyclicArbProfitTrackerStartHeight gets the height from which we started accounting for cyclic arb profits.
func (k Keeper) GetCyclicArbProfitTrackerStartHeight(ctx sdk.Context) int64 {
startHeight := gogotypes.Int64Value{}
osmoutils.MustGet(ctx.KVStore(k.storeKey), types.KeyCyclicArbTrackerStartHeight, &startHeight)
return startHeight.Value
}
// SetCyclicArbProfitTrackerStartHeight sets the height from which we started accounting for cyclic arb profits.
func (k Keeper) SetCyclicArbProfitTrackerStartHeight(ctx sdk.Context, startHeight int64) {
osmoutils.MustSet(ctx.KVStore(k.storeKey), types.KeyCyclicArbTrackerStartHeight, &gogotypes.Int64Value{Value: startHeight})
}
// UpdateProfitsByDenom updates the profits made by the ProtoRev module for the given denom
func (k Keeper) UpdateProfitsByDenom(ctx sdk.Context, denom string, tradeProfit osmomath.Int) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixProfitByDenom)
key := types.GetKeyPrefixProfitByDenom(denom)
profits, _ := k.GetProfitsByDenom(ctx, denom)
profits.Amount = profits.Amount.Add(tradeProfit)
bz, err := profits.Marshal()
if err != nil {
return err
}
store.Set(key, bz)
return nil
}
// GetAllRoutes returns all of the routes that the ProtoRev module has traded on
func (k Keeper) GetAllRoutes(ctx sdk.Context) ([][]uint64, error) {
routes := make([][]uint64, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixTradesByRoute)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// ignore the portion of the key that is the prefix
key := iterator.Key()[len(types.KeyPrefixTradesByRoute)+1:]
// convert the key into a route
route, err := types.CreateRouteFromKey(key)
if err != nil {
return nil, err
}
routes = append(routes, route)
}
return routes, nil
}
// GetTradesByRoute returns the number of trades executed by the ProtoRev module for the given route
func (k Keeper) GetTradesByRoute(ctx sdk.Context, route []uint64) (osmomath.Int, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTradesByRoute)
key := types.GetKeyPrefixTradesByRoute(route)
bz := store.Get(key)
if len(bz) == 0 {
return osmomath.ZeroInt(), fmt.Errorf("no trades for route %d", route)
}
trades := osmomath.Int{}
if err := trades.Unmarshal(bz); err != nil {
return osmomath.ZeroInt(), err
}
return trades, nil
}
// IncrementTradesByRoute increments the number of trades executed by the ProtoRev module for the given route
func (k Keeper) IncrementTradesByRoute(ctx sdk.Context, route []uint64) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTradesByRoute)
key := types.GetKeyPrefixTradesByRoute(route)
trades, _ := k.GetTradesByRoute(ctx, route)
trades = trades.Add(oneInt)
bz, err := trades.Marshal()
if err != nil {
return err
}
store.Set(key, bz)
return nil
}
// GetProfitsByRoute returns the profits made by the ProtoRev module for the given route and denom
func (k Keeper) GetProfitsByRoute(ctx sdk.Context, route []uint64, denom string) (sdk.Coin, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixProfitsByRoute)
key := types.GetKeyPrefixProfitsByRoute(route, denom)
bz := store.Get(key)
if len(bz) == 0 {
return sdk.NewCoin(denom, osmomath.ZeroInt()), fmt.Errorf("no profits for route %d", route)
}
profits := sdk.Coin{}
if err := profits.Unmarshal(bz); err != nil {
return sdk.NewCoin(denom, osmomath.ZeroInt()), err
}
return profits, nil
}
// GetAllProfitsByRoute returns all of the profits made by the ProtoRev module for the given route
func (k Keeper) GetAllProfitsByRoute(ctx sdk.Context, route []uint64) []sdk.Coin {
profits := make([]sdk.Coin, 0)
store := ctx.KVStore(k.storeKey)
prefix := append(types.KeyPrefixProfitsByRoute, types.GetKeyPrefixProfitsByRoute(route, "")...)
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value()
profit := sdk.Coin{}
if err := profit.Unmarshal(bz); err == nil {
profits = append(profits, profit)
}
}
return profits
}
// UpdateProfitsByRoute updates the profits made by the ProtoRev module for the given route and denom
func (k Keeper) UpdateProfitsByRoute(ctx sdk.Context, route []uint64, denom string, profit osmomath.Int) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixProfitsByRoute)
key := types.GetKeyPrefixProfitsByRoute(route, denom)
profits, _ := k.GetProfitsByRoute(ctx, route, denom)
profits.Amount = profits.Amount.Add(profit)
bz, err := profits.Marshal()
if err != nil {
return err
}
store.Set(key, bz)
return nil
}
// UpdateStatistics updates the module statistics after each trade is executed
func (k Keeper) UpdateStatistics(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes, denom string, profit osmomath.Int) error {
// Increment the number of trades executed by the ProtoRev module
if err := k.IncrementNumberOfTrades(ctx); err != nil {
return err
}
// Update the profits made by the ProtoRev module for the denom
if err := k.UpdateProfitsByDenom(ctx, denom, profit); err != nil {
return err
}
// Increment the number of times the module has executed a trade on a given route
if err := k.IncrementTradesByRoute(ctx, route.PoolIds()); err != nil {
return err
}
// Update the profits accumulated by the ProtoRev module for the given route and denom
if err := k.UpdateProfitsByRoute(ctx, route.PoolIds(), denom, profit); err != nil {
return err
}
return nil
}