-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_exchange_market_config.go
102 lines (89 loc) · 4.07 KB
/
static_exchange_market_config.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
package constants
import (
"encoding/json"
"fmt"
"github.com/furyanprotocol/v4-chain/protocol/testutil/daemons/pricefeed/exchange_config"
"sort"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/constants/exchange_common"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
)
const (
// MinimumRequiredExchangesPerMarket is the minimum number of markets required for a market to be reliably priced
// by the pricefeed daemon. This number was chosen to supply the minimum number of prices required to
// compute an index price for a market, given exchange unavailability due to exchange geo-fencing,
// downtime, etc.
// Ok to drop this to 5 for some markets if needed, but 6 is better.
MinimumRequiredExchangesPerMarket = 6
)
// GenerateExchangeConfigJson generates human-readable exchange config json for each market based on the contents
// of an exchangeToExchangeConfig map. For the default exchange configs, pass in the
// `StaticExchangeMarketConfig` map above as the argument.
func GenerateExchangeConfigJson(
exchangeToExchangeConfig map[types.ExchangeId]*types.MutableExchangeMarketConfig,
) (
marketToExchangeConfigJson map[types.MarketId]string,
) {
// marketToExchangeConfigJson maps markets to a map of exchange, exchange market config. This
// is used to generate the exchange config json from the above map that is keyed by exchange id.
// We keep this intermediate map so that we can sort the exchange configs for each market by exchange name
// in order to make the output deterministic.
marketToExchangeMarketConfigs := make(map[types.MarketId]map[string]types.ExchangeMarketConfigJson)
// Generate the market-specific exchange config for each market, exchange.
for id, exchangeConfig := range exchangeToExchangeConfig {
// Skip config for the test exchange.
if id == exchange_common.EXCHANGE_ID_TEST_EXCHANGE {
continue
}
if id == exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE {
continue
}
for marketId, config := range exchangeConfig.MarketToMarketConfig {
marketExchangeConfigs, ok := marketToExchangeMarketConfigs[marketId]
if !ok {
marketToExchangeMarketConfigs[marketId] = map[string]types.ExchangeMarketConfigJson{}
marketExchangeConfigs = marketToExchangeMarketConfigs[marketId]
}
exchangeMarketConfigJson := types.ExchangeMarketConfigJson{
ExchangeName: id,
Ticker: config.Ticker,
Invert: config.Invert,
}
// Convert adjust-by market id to name if specified.
if config.AdjustByMarket != nil {
adjustByMarketName, ok := exchange_config.StaticMarketNames[*config.AdjustByMarket]
if !ok {
panic(fmt.Sprintf("no name for adjust-by market %v", *config.AdjustByMarket))
}
exchangeMarketConfigJson.AdjustByMarket = adjustByMarketName
}
marketExchangeConfigs[id] = exchangeMarketConfigJson
}
}
// Initialize the output map.
marketToExchangeConfigJson = make(map[types.MarketId]string, len(marketToExchangeMarketConfigs))
// Generate the output map of market to exchange config json.
for marketId, exchangeToConfigs := range marketToExchangeMarketConfigs {
// Sort output exchange configs by exchange name in order to make output deterministic.
exchangeNames := make([]string, 0, len(exchangeToConfigs))
// 1. Generate sorted list of exchange names.
for name := range exchangeToConfigs {
exchangeNames = append(exchangeNames, name)
}
sort.Strings(exchangeNames)
// 2. Generate sorted list of exchange configs by exchange name.
sortedExchangeConfigs := make([]types.ExchangeMarketConfigJson, 0, len(exchangeNames))
for _, exchangeName := range exchangeNames {
sortedExchangeConfigs = append(sortedExchangeConfigs, exchangeToConfigs[exchangeName])
}
exchangeConfigJson := types.ExchangeConfigJson{
Exchanges: sortedExchangeConfigs,
}
// 3. Generate human-readable formatted output json for the market, sorted by exchange name.
bytes, err := json.MarshalIndent(exchangeConfigJson, "", " ")
if err != nil {
panic(err)
}
marketToExchangeConfigJson[marketId] = string(bytes)
}
return marketToExchangeConfigJson
}