-
Notifications
You must be signed in to change notification settings - Fork 168
/
price_store.go
65 lines (58 loc) · 1.46 KB
/
price_store.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
package grpc
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v5/util/decmath"
)
type PriceStore struct {
historicStamps map[string][]sdk.Dec
medians map[string]sdk.Dec
medianDeviations map[string]sdk.Dec
}
func NewPriceStore() *PriceStore {
return &PriceStore{
historicStamps: map[string][]sdk.Dec{},
medians: map[string]sdk.Dec{},
medianDeviations: map[string]sdk.Dec{},
}
}
func (ps *PriceStore) addStamp(denom string, stamp sdk.Dec) {
if _, ok := ps.historicStamps[denom]; !ok {
ps.historicStamps[denom] = []sdk.Dec{}
}
ps.historicStamps[denom] = append(ps.historicStamps[denom], stamp)
}
func (ps *PriceStore) checkMedians() error {
for denom, stamps := range ps.historicStamps {
calcMedian, err := decmath.Median(stamps)
if err != nil {
return err
}
if !ps.medians[denom].Equal(calcMedian) {
return fmt.Errorf(
"expected %d for the %s median but got %d",
ps.medians[denom],
denom,
calcMedian,
)
}
}
return nil
}
func (ps *PriceStore) checkMedianDeviations() error {
for denom, median := range ps.medians {
calcMedianDeviation, err := decmath.MedianDeviation(median, ps.historicStamps[denom])
if err != nil {
return err
}
if !ps.medianDeviations[denom].Equal(calcMedianDeviation) {
return fmt.Errorf(
"expected %d for the %s median deviation but got %d",
ps.medianDeviations[denom],
denom,
calcMedianDeviation,
)
}
}
return nil
}