/
exchange.go
63 lines (55 loc) · 1.24 KB
/
exchange.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
package datasource
import (
"encoding/json"
"go/build"
"io/ioutil"
"os"
"github.com/diadata-org/diadata/pkg/dia"
)
type Source struct {
Exchanges []dia.Exchange `json:"exchanges"`
}
//InitSource Read exchange.json file and put exchange metadata in Source struct
func InitSource() (source *Source, err error) {
var (
jsonFile *os.File
fileBytes []byte
)
executionMode := os.Getenv("EXEC_MODE")
if executionMode == "production" {
jsonFile, err = os.Open("/config/exchanges.json")
} else {
gopath := os.Getenv("GOPATH")
if gopath == "" {
jsonFile, err = os.Open(build.Default.GOPATH)
} else {
jsonFile, err = os.Open(os.Getenv("GOPATH") + "/src/github.com/diadata-org/diadata/config/exchanges.json")
}
}
if err != nil {
return
}
defer func() {
cerr := jsonFile.Close()
if err == nil {
err = cerr
}
}()
fileBytes, err = ioutil.ReadAll(jsonFile)
if err != nil {
return
}
err = json.Unmarshal(fileBytes, &source)
if err != nil {
return
}
return
}
//GetExchanges Get map of exchane name and exchange metadata
func (s *Source) GetExchanges() (exchanges map[string]dia.Exchange) {
exchanges = make(map[string]dia.Exchange)
for _, exchange := range s.Exchanges {
exchanges[exchange.Name] = exchange
}
return
}