/
genesis.go
113 lines (101 loc) · 3.5 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
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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v4/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
}
}
}
// AllHistoricPrices is a helper function that collects and returns all
// median prices using the IterateAllHistoricPrices iterator
func (k Keeper) AllHistoricPrices(ctx sdk.Context) types.Prices {
prices := types.Prices{}
k.IterateAllHistoricPrices(ctx, func(median types.Price) (stop bool) {
prices = append(prices, median)
return false
})
return prices
}
// 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
}
}
}
// AllMedianPrices is a helper function that collects and returns all
// median prices using the IterateAllMedianPrices iterator
func (k Keeper) AllMedianPrices(ctx sdk.Context) types.Prices {
prices := types.Prices{}
k.IterateAllMedianPrices(ctx, func(median types.Price) (stop bool) {
prices = append(prices, median)
return false
})
return prices
}
// 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
}
}
}
// AllMedianDeviationPrices is a helper function that collects and returns
// all median prices using the IterateAllMedianDeviationPrices iterator
func (k Keeper) AllMedianDeviationPrices(ctx sdk.Context) types.Prices {
prices := types.Prices{}
k.IterateAllMedianDeviationPrices(ctx, func(median types.Price) (stop bool) {
prices = append(prices, median)
return false
})
return prices
}