github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/currency/currency.go (about)

     1  package currency
     2  
     3  import "strings"
     4  
     5  type Conf struct {
     6  	Currency  string
     7  	Label     string
     8  	Symbol    string
     9  	Pairs     []string
    10  	Pools     []string
    11  	Explorers ExplorerConf
    12  	Unit      string
    13  }
    14  
    15  type ExplorerConf struct {
    16  	BaseUri        string
    17  	AddressUri     string
    18  	TransactionUri string
    19  }
    20  
    21  type ConfigMapping string
    22  
    23  const (
    24  	LabelMap    ConfigMapping = "pub:map:currency:label"
    25  	SymbolMap   ConfigMapping = "pub:map:currency:sym"
    26  	UnitMap     ConfigMapping = "pub:map:currency:unit"
    27  	ExplorerMap ConfigMapping = "pub:map:currency:explorer"
    28  	ExchangeMap ConfigMapping = "pub:list:pair:exchange"
    29  )
    30  
    31  type RawConf struct {
    32  	Mapping string
    33  	Data    interface{}
    34  }
    35  
    36  func parseLabelMap(config map[string]Conf, raw []interface{}) {
    37  	for _, rawLabel := range raw {
    38  		data := rawLabel.([]interface{})
    39  		cur := data[0].(string)
    40  		if val, ok := config[cur]; ok {
    41  			// add value
    42  			val.Label = data[1].(string)
    43  			config[cur] = val
    44  		} else {
    45  			// create new empty config instance
    46  			cfg := Conf{}
    47  			cfg.Label = data[1].(string)
    48  			cfg.Currency = cur
    49  			config[cur] = cfg
    50  		}
    51  	}
    52  }
    53  
    54  func parseSymbMap(config map[string]Conf, raw []interface{}) {
    55  	for _, rawLabel := range raw {
    56  		data := rawLabel.([]interface{})
    57  		cur := data[0].(string)
    58  		if val, ok := config[cur]; ok {
    59  			// add value
    60  			val.Symbol = data[1].(string)
    61  			config[cur] = val
    62  		} else {
    63  			// create new empty config instance
    64  			cfg := Conf{}
    65  			cfg.Symbol = data[1].(string)
    66  			cfg.Currency = cur
    67  			config[cur] = cfg
    68  		}
    69  	}
    70  }
    71  
    72  func parseUnitMap(config map[string]Conf, raw []interface{}) {
    73  	for _, rawLabel := range raw {
    74  		data := rawLabel.([]interface{})
    75  		cur := data[0].(string)
    76  		if val, ok := config[cur]; ok {
    77  			// add value
    78  			val.Unit = data[1].(string)
    79  			config[cur] = val
    80  		} else {
    81  			// create new empty config instance
    82  			cfg := Conf{}
    83  			cfg.Unit = data[1].(string)
    84  			cfg.Currency = cur
    85  			config[cur] = cfg
    86  		}
    87  	}
    88  }
    89  
    90  func parseExplorerMap(config map[string]Conf, raw []interface{}) {
    91  	for _, rawLabel := range raw {
    92  		data := rawLabel.([]interface{})
    93  		cur := data[0].(string)
    94  		explorers := data[1].([]interface{})
    95  		var cfg Conf
    96  		if val, ok := config[cur]; ok {
    97  			cfg = val
    98  		} else {
    99  			// create new empty config instance
   100  			cc := Conf{}
   101  			cc.Currency = cur
   102  			cfg = cc
   103  		}
   104  		ec := ExplorerConf{
   105  			explorers[0].(string),
   106  			explorers[1].(string),
   107  			explorers[2].(string),
   108  		}
   109  		cfg.Explorers = ec
   110  		config[cur] = cfg
   111  	}
   112  }
   113  
   114  func parseExchangeMap(config map[string]Conf, raw []interface{}) {
   115  	for _, rs := range raw {
   116  		symbol := rs.(string)
   117  		var base, quote string
   118  
   119  		if len(symbol) > 6 {
   120  			base = strings.Split(symbol, ":")[0]
   121  			quote = strings.Split(symbol, ":")[1]
   122  		} else {
   123  			base = symbol[3:]
   124  			quote = symbol[:3]
   125  		}
   126  
   127  		// append if base exists in configs
   128  		if val, ok := config[base]; ok {
   129  			val.Pairs = append(val.Pairs, symbol)
   130  			config[base] = val
   131  		}
   132  
   133  		// append if quote exists in configs
   134  		if val, ok := config[quote]; ok {
   135  			val.Pairs = append(val.Pairs, symbol)
   136  			config[quote] = val
   137  		}
   138  	}
   139  }
   140  
   141  func FromRaw(raw []RawConf) ([]Conf, error) {
   142  	configMap := make(map[string]Conf)
   143  	for _, r := range raw {
   144  		switch ConfigMapping(r.Mapping) {
   145  		case LabelMap:
   146  			data := r.Data.([]interface{})
   147  			parseLabelMap(configMap, data)
   148  		case SymbolMap:
   149  			data := r.Data.([]interface{})
   150  			parseSymbMap(configMap, data)
   151  		case UnitMap:
   152  			data := r.Data.([]interface{})
   153  			parseUnitMap(configMap, data)
   154  		case ExplorerMap:
   155  			data := r.Data.([]interface{})
   156  			parseExplorerMap(configMap, data)
   157  		case ExchangeMap:
   158  			data := r.Data.([]interface{})
   159  			parseExchangeMap(configMap, data)
   160  		}
   161  	}
   162  
   163  	// convert map to array
   164  	configs := make([]Conf, 0)
   165  	for _, v := range configMap {
   166  		configs = append(configs, v)
   167  	}
   168  
   169  	return configs, nil
   170  }