forked from umee-network/umee
/
genesis.go
80 lines (71 loc) · 2.4 KB
/
genesis.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mokitanetwork/katana/x/oracle/types"
)
// IterateAllHistoricPrices iterates over all historic prices.
// Iterator stops when exhausting the source, or when the handler returns `true`.
func (k Keeper) IterateAllHistoricPrices(
ctx sdk.Context,
handler func(types.Price) bool,
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixHistoricPrice)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var decProto sdk.DecProto
k.cdc.MustUnmarshal(iter.Value(), &decProto)
denom, blockNum := types.ParseDenomAndBlockFromKey(iter.Key(), types.KeyPrefixHistoricPrice)
historicPrice := types.Price{
ExchangeRateTuple: types.ExchangeRateTuple{ExchangeRate: decProto.Dec, Denom: denom},
BlockNum: blockNum,
}
if handler(historicPrice) {
break
}
}
}
// IterateAllMedianPrices iterates over all median prices.
// Iterator stops when exhausting the source, or when the handler returns `true`.
func (k Keeper) IterateAllMedianPrices(
ctx sdk.Context,
handler func(types.Price) bool,
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixMedian)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var decProto sdk.DecProto
k.cdc.MustUnmarshal(iter.Value(), &decProto)
denom, blockNum := types.ParseDenomAndBlockFromKey(iter.Key(), types.KeyPrefixMedian)
median := types.Price{
ExchangeRateTuple: types.ExchangeRateTuple{ExchangeRate: decProto.Dec, Denom: denom},
BlockNum: blockNum,
}
if handler(median) {
break
}
}
}
// IterateAllMedianDeviationPrices iterates over all median deviation prices.
// Iterator stops when exhausting the source, or when the handler returns `true`.
func (k Keeper) IterateAllMedianDeviationPrices(
ctx sdk.Context,
handler func(types.Price) bool,
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixMedianDeviation)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var decProto sdk.DecProto
k.cdc.MustUnmarshal(iter.Value(), &decProto)
denom, blockNum := types.ParseDenomAndBlockFromKey(iter.Key(), types.KeyPrefixMedianDeviation)
medianDeviation := types.Price{
ExchangeRateTuple: types.ExchangeRateTuple{ExchangeRate: decProto.Dec, Denom: denom},
BlockNum: blockNum,
}
if handler(medianDeviation) {
break
}
}
}