-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider_manager.go
124 lines (111 loc) · 3.27 KB
/
provider_manager.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
114
115
116
117
118
119
120
121
122
123
124
package provider
import (
"context"
"fmt"
"strings"
"time"
"github.com/terra-money/oracle-feeder-go/config"
"github.com/terra-money/oracle-feeder-go/pkg/types"
)
type ProviderManager struct {
config *config.Config
providers map[string]Provider
}
func NewProviderManager(config *config.Config, stopCh <-chan struct{}) *ProviderManager {
providers := make(map[string]Provider)
for _, exchange := range config.ProviderPriority {
providerConfig := config.Providers[exchange]
provider, err := NewProvider(exchange, &providerConfig, stopCh)
if err != nil {
fmt.Printf("Exchange %s connection ERROR %s \n", exchange, err)
} else {
providers[exchange] = provider
}
}
return &ProviderManager{
config: config,
providers: providers,
}
}
func (m *ProviderManager) GetPrices(ctx context.Context) *types.PricesResponse {
// exchange -> base -> price
prices := make(map[string]map[string]types.PriceByPair)
for exchange, provider := range m.providers {
prices[exchange] = provider.GetPrices()
}
priceByCoin := averagePriceByCoin(averagePriceByPair(prices))
var pricesOfCoins []types.PriceOfCoin
now := uint64(time.Now().UnixMilli())
for coin, price := range priceByCoin {
pricesOfCoins = append(pricesOfCoins, types.PriceOfCoin{
Denom: coin,
Price: price,
Timestamp: now,
})
}
resp := &types.PricesResponse{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Prices: pricesOfCoins,
}
return resp
}
func (m *ProviderManager) GetPrice(ctx context.Context, denom string) *types.PriceResponse {
r := m.GetPrices(ctx)
for _, price := range r.Prices {
if strings.EqualFold(price.Denom, denom) {
return &types.PriceResponse{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Price: price,
}
}
}
return nil
}
// Calculate average price for each pair.
//
// Returns map of pair -> price.
func averagePriceByPair(prices map[string]map[string]types.PriceByPair) map[string]types.PriceByPair {
pairSum := make(map[string]float64)
pairCount := make(map[string]float64)
for _, priceByPair := range prices {
for _, price := range priceByPair {
pair := fmt.Sprintf("%s/%s", price.Base, price.Quote)
pairSum[pair] += price.Price
pairCount[pair] += 1.0
}
}
averagedPrices := make(map[string]types.PriceByPair)
for pair, count := range pairCount {
arr := strings.Split(pair, "/")
base := arr[0]
quote := arr[1]
averagedPrices[pair] = types.PriceByPair{
Base: base,
Quote: quote,
Price: pairSum[pair] / count,
Timestamp: uint64(time.Now().UnixMilli()),
}
}
return averagedPrices
}
func averagePriceByCoin(prices map[string]types.PriceByPair) map[string]float64 {
coinSum := make(map[string]float64)
coinCount := make(map[string]float64)
for _, priceByPair := range prices {
if priceByPair.Quote == "USD" {
coinSum[priceByPair.Base] += priceByPair.Price
coinCount[priceByPair.Base] += 1.0
} else {
quoteUSD := fmt.Sprintf("%s/USD", priceByPair.Quote)
if quotePrice, ok := prices[quoteUSD]; ok && quotePrice.Price > 0.0 {
coinSum[priceByPair.Base] += priceByPair.Price * quotePrice.Price
coinCount[priceByPair.Base] += 1.0
}
}
}
priceByCoin := make(map[string]float64)
for coin, count := range coinCount {
priceByCoin[coin] = coinSum[coin] / count
}
return priceByCoin
}