-
Notifications
You must be signed in to change notification settings - Fork 170
/
price_listener.go
71 lines (60 loc) · 1.98 KB
/
price_listener.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
package grpc
import (
"fmt"
"github.com/umee-network/umee/v6/client"
sdkclient "github.com/umee-network/umee/v6/sdkclient"
oracletypes "github.com/umee-network/umee/v6/x/oracle/types"
)
func listenForPrices(
umee client.Client,
params oracletypes.Params,
chainHeight *sdkclient.ChainHeightListener,
) (*PriceStore, error) {
priceStore := NewPriceStore()
// Wait until the beginning of a median period
var beginningHeight int64
for {
beginningHeight = <-chainHeight.HeightChanged
if isPeriodFirstBlock(beginningHeight, params.MedianStampPeriod) {
break
}
}
// Record each historic stamp when the chain should be recording them
for i := 0; i < int(params.MedianStampPeriod); i++ {
height := <-chainHeight.HeightChanged
if isPeriodFirstBlock(height, params.HistoricStampPeriod) {
exchangeRates, err := umee.QueryExchangeRates()
fmt.Printf("block %d stamp: %+v\n", height, exchangeRates)
if err != nil {
return nil, err
}
for _, rate := range exchangeRates {
priceStore.addStamp(rate.Denom, rate.Amount)
}
}
}
medians, err := umee.QueryMedians()
if err != nil {
return nil, err
}
expectedNumMedians := int(params.MaximumMedianStamps) * len(params.AcceptList)
if len(medians) != expectedNumMedians {
return nil, fmt.Errorf("amount of medians %d does not match the expected amount %d", len(medians), expectedNumMedians)
}
// Saves the last median for each denom
for _, median := range medians {
priceStore.medians[median.ExchangeRateTuple.Denom] = median.ExchangeRateTuple.ExchangeRate
}
medianDeviations, err := umee.QueryMedianDeviations()
if err != nil {
return nil, err
}
// Saves the last median deviation for each denom
for _, medianDeviation := range medianDeviations {
priceStore.medianDeviations[medianDeviation.ExchangeRateTuple.Denom] = medianDeviation.ExchangeRateTuple.ExchangeRate
}
return priceStore, nil
}
func isPeriodFirstBlock(height int64, blocksPerPeriod uint64) bool {
return uint64(height)%blocksPerPeriod == 0
}