-
Notifications
You must be signed in to change notification settings - Fork 386
/
provider_aggregator.go
51 lines (41 loc) · 1.42 KB
/
provider_aggregator.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
package abci
import (
"sync"
"github.com/cosmos/sdk-tutorials/tutorials/oracle/base/x/oracle/keeper"
)
// Provider defines an interface for fetching prices and candles for a given set
// of currency pairs. The provider is presumed to be a trusted source of prices.
type Provider interface {
GetTickerPrices(...keeper.CurrencyPair) (map[string]keeper.TickerPrice, error)
}
// ProviderAggregator is a simple aggregator for provider prices and candles.
// It is thread-safe since it is assumed to be called concurrently in price
// fetching goroutines, i.e. ExtendVote.
type ProviderAggregator struct {
mtx sync.Mutex
providerPrices keeper.AggregatedProviderPrices
}
func NewProviderAggregator() *ProviderAggregator {
return &ProviderAggregator{
providerPrices: make(keeper.AggregatedProviderPrices),
}
}
func (p *ProviderAggregator) SetProviderTickerPricesAndCandles(
providerName string,
prices map[string]keeper.TickerPrice,
pair keeper.CurrencyPair,
) bool {
p.mtx.Lock()
defer p.mtx.Unlock()
// set prices and candles for this provider if we haven't seen it before
if _, ok := p.providerPrices[providerName]; !ok {
p.providerPrices[providerName] = make(map[string]keeper.TickerPrice)
}
// set price for provider/base (e.g. Binance -> ATOM -> 11.98)
tp, pricesOk := prices[pair.String()]
if pricesOk {
p.providerPrices[providerName][pair.Base] = tp
}
// return true if we set at least one price
return pricesOk
}