-
Notifications
You must be signed in to change notification settings - Fork 200
/
keeper.go
357 lines (300 loc) · 10.4 KB
/
keeper.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package keeper
import (
"fmt"
"sort"
"time"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/pricefeed/types"
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
) Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// SetPrice updates the posted price for a specific oracle
func (k Keeper) SetPrice(
ctx sdk.Context,
oracle sdk.AccAddress,
token0 string,
token1 string,
price sdk.Dec,
expiry time.Time,
) (types.PostedPrice, error) {
// If the posted price expires before the current block, it is invalid.
if expiry.Before(ctx.BlockTime()) {
return types.PostedPrice{}, types.ErrExpired
}
// TODO: test this behavior when setting the inverse pair
pairName := common.RawPoolNameFromDenoms([]string{token0, token1})
pairID := common.PoolNameFromDenoms([]string{token0, token1})
if (pairName != pairID) && (!price.Equal(sdk.ZeroDec())) {
price = sdk.OneDec().Quo(price)
}
_, err := k.GetOracle(ctx, pairID, oracle)
if err != nil {
return types.PostedPrice{}, err
}
newRawPrice := types.NewPostedPrice(pairID, oracle, price, expiry)
// Emit an event containing the oracle's new price
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeOracleUpdatedPrice,
sdk.NewAttribute(types.AttributePairID, pairID),
sdk.NewAttribute(types.AttributeOracle, oracle.String()),
sdk.NewAttribute(types.AttributePairPrice, price.String()),
sdk.NewAttribute(types.AttributeExpiry, expiry.UTC().String()),
),
)
// Sets the raw price for a single oracle instead of an array of all oracle's raw prices
store := ctx.KVStore(k.storeKey)
store.Set(types.RawPriceKey(pairID, oracle), k.cdc.MustMarshal(&newRawPrice))
return newRawPrice, nil
}
// SetCurrentPrices updates the price of an asset to the median of all valid oracle inputs
func (k Keeper) SetCurrentPrices(ctx sdk.Context, token0 string, token1 string) error {
assetPair := common.AssetPair{Token0: token0, Token1: token1}
pairID := assetPair.Name()
tokens := common.DenomsFromPoolName(pairID)
token0, token1 = tokens[0], tokens[1]
_, ok := k.GetPair(ctx, pairID)
if !ok {
return sdkerrors.Wrap(types.ErrInvalidPair, pairID)
}
// store current price
validPrevPrice := true
prevPrice, err := k.GetCurrentPrice(ctx, token0, token1)
if err != nil {
validPrevPrice = false
}
postedPrices := k.GetRawPrices(ctx, pairID)
var notExpiredPrices []types.CurrentPrice
// filter out expired prices
for _, post := range postedPrices {
if post.Expiry.After(ctx.BlockTime()) {
notExpiredPrices = append(
notExpiredPrices,
types.NewCurrentPrice(token0, token1, post.Price))
}
}
if len(notExpiredPrices) == 0 {
// NOTE: The current price stored will continue storing the most recent (expired)
// price if this is not set.
// This zero's out the current price stored value for that market and ensures
// that CDP methods that GetCurrentPrice will return error.
k.setCurrentPrice(ctx, pairID, types.CurrentPrice{})
return types.ErrNoValidPrice
}
medianPrice := k.CalculateMedianPrice(notExpiredPrices)
// check case that market price was not set in genesis
if validPrevPrice && !medianPrice.Equal(prevPrice.Price) {
// only emit event if price has changed
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypePairPriceUpdated,
sdk.NewAttribute(types.AttributePairID, pairID),
sdk.NewAttribute(types.AttributePairPrice, medianPrice.String()),
),
)
}
currentPrice := types.NewCurrentPrice(token0, token1, medianPrice)
k.setCurrentPrice(ctx, pairID, currentPrice)
// Update the TWA prices
err = k.updateTWAPPrice(ctx, pairID)
if err != nil {
return err
}
return nil
}
func (k Keeper) setCurrentPrice(ctx sdk.Context, pairID string, currentPrice types.CurrentPrice) {
store := ctx.KVStore(k.storeKey)
store.Set(types.CurrentPriceKey(pairID), k.cdc.MustMarshal(¤tPrice))
}
/* updateTWAPPrice updates the twap price for a token0, token1 pair
We use the blocktime to update the twap price.
Calculation is done as follow:
$$P_{TWAP} = \frac {\sum {P_j \times Bh_j }}{\sum{Bh_j}} $$
With
P_j: current posted price for the pair of tokens
Bh_j: current block timestamp
*/
func (k Keeper) updateTWAPPrice(ctx sdk.Context, pairID string) error {
tokens := common.DenomsFromPoolName(pairID)
token0, token1 := tokens[0], tokens[1]
currentPrice, err := k.GetCurrentPrice(ctx, token0, token1)
if err != nil {
return err
}
currentTWAP, err := k.GetCurrentTWAPPrice(ctx, token0, token1)
// Err there means no twap price have been set yet for this pair
if err != nil {
currentTWAP = types.CurrentTWAP{
PairID: pairID,
Numerator: sdk.MustNewDecFromStr("0"),
Denominator: sdk.NewInt(0),
Price: sdk.MustNewDecFromStr("0"),
}
}
blockUnixTime := sdk.NewInt(ctx.BlockTime().Unix())
newDenominator := currentTWAP.Denominator.Add(blockUnixTime)
newNumerator := currentTWAP.Numerator.Add(currentPrice.Price.Mul(sdk.NewDecFromInt(blockUnixTime)))
newTWAP := types.CurrentTWAP{
PairID: pairID,
Numerator: newNumerator,
Denominator: newDenominator,
Price: newNumerator.Quo(sdk.NewDecFromInt(newDenominator)),
}
store := ctx.KVStore(k.storeKey)
store.Set(types.CurrentTWAPPriceKey("twap-"+pairID), k.cdc.MustMarshal(&newTWAP))
return nil
}
// CalculateMedianPrice calculates the median prices for the input prices.
func (k Keeper) CalculateMedianPrice(prices []types.CurrentPrice) sdk.Dec {
l := len(prices)
if l == 1 {
// Return immediately if there's only one price
return prices[0].Price
}
// sort the prices
sort.Slice(prices, func(i, j int) bool {
return prices[i].Price.LT(prices[j].Price)
})
// for even numbers of prices, the median is calculated as the mean of the two middle prices
if l%2 == 0 {
median := k.calculateMeanPrice(prices[l/2-1], prices[l/2])
return median
}
// for odd numbers of prices, return the middle element
return prices[l/2].Price
}
func (k Keeper) calculateMeanPrice(priceA, priceB types.CurrentPrice) sdk.Dec {
sum := priceA.Price.Add(priceB.Price)
mean := sum.Quo(sdk.NewDec(2))
return mean
}
// GetCurrentPrice fetches the current median price of all oracles for a specific market
func (k Keeper) GetCurrentPrice(ctx sdk.Context, token0 string, token1 string,
) (currPrice types.CurrentPrice, err error) {
assetPair := common.AssetPair{Token0: token0, Token1: token1}
pairID := assetPair.Name()
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.CurrentPriceKey(pairID))
if bz == nil {
return types.CurrentPrice{}, types.ErrNoValidPrice
}
var price types.CurrentPrice
k.cdc.MustUnmarshal(bz, &price)
if price.Price.Equal(sdk.ZeroDec()) {
return types.CurrentPrice{}, types.ErrNoValidPrice
}
if !assetPair.IsProperOrder() {
// Return the inverse price if the tokens are not in "proper" order.
inversePrice := sdk.OneDec().Quo(price.Price)
return types.NewCurrentPrice(
/* token0 */ token1,
/* token1 */ token0,
/* price */ inversePrice), nil
}
return price, nil
}
// GetCurrentTWAPPrice fetches the current median price of all oracles for a specific market
func (k Keeper) GetCurrentTWAPPrice(ctx sdk.Context, token0 string, token1 string) (currPrice types.CurrentTWAP, err error) {
// Ensure we still have valid prices
_, err = k.GetCurrentPrice(ctx, token0, token1)
if err != nil {
return types.CurrentTWAP{}, types.ErrNoValidPrice
}
assetPair := common.AssetPair{Token0: token0, Token1: token1}
pairID := assetPair.Name()
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.CurrentTWAPPriceKey("twap-" + pairID))
if bz == nil {
return types.CurrentTWAP{}, types.ErrNoValidTWAP
}
var price types.CurrentTWAP
k.cdc.MustUnmarshal(bz, &price)
if price.Price.IsZero() {
return types.CurrentTWAP{}, types.ErrNoValidPrice
}
if !assetPair.IsProperOrder() {
// Return the inverse price if the tokens are not in "proper" order.
inversePrice := sdk.OneDec().Quo(price.Price)
return types.NewCurrentTWAP(
/* token0 */ token1,
/* token1 */ token0,
/* numerator */ price.Numerator,
/* denominator */ price.Denominator,
/* price */ inversePrice), nil
}
return price, nil
}
// IterateCurrentPrices iterates over all current price objects in the store and performs a callback function
func (k Keeper) IterateCurrentPrices(ctx sdk.Context, cb func(cp types.CurrentPrice) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.CurrentPricePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var cp types.CurrentPrice
k.cdc.MustUnmarshal(iterator.Value(), &cp)
if cb(cp) {
break
}
}
}
// GetCurrentPrices returns all current price objects from the store
func (k Keeper) GetCurrentPrices(ctx sdk.Context) types.CurrentPrices {
var cps types.CurrentPrices
k.IterateCurrentPrices(ctx, func(cp types.CurrentPrice) (stop bool) {
cps = append(cps, cp)
return false
})
return cps
}
// GetRawPrices fetches the set of all prices posted by oracles for an asset
func (k Keeper) GetRawPrices(ctx sdk.Context, marketId string) types.PostedPrices {
var pps types.PostedPrices
k.IterateRawPricesByPair(ctx, marketId, func(pp types.PostedPrice) (stop bool) {
pps = append(pps, pp)
return false
})
return pps
}
// IterateRawPrices iterates over all raw prices in the store and performs a callback function
func (k Keeper) IterateRawPricesByPair(ctx sdk.Context, marketId string, cb func(record types.PostedPrice) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.RawPriceIteratorKey((marketId)))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var record types.PostedPrice
k.cdc.MustUnmarshal(iterator.Value(), &record)
if cb(record) {
break
}
}
}