forked from bnb-chain/bep3-deputy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (136 loc) · 4.47 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/binance-chain/go-sdk/common/types"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/binance-chain/bep3-deputy/admin"
"github.com/binance-chain/bep3-deputy/common"
"github.com/binance-chain/bep3-deputy/deputy"
"github.com/binance-chain/bep3-deputy/executor/bnb"
"github.com/binance-chain/bep3-deputy/executor/eth"
"github.com/binance-chain/bep3-deputy/observer"
"github.com/binance-chain/bep3-deputy/store"
"github.com/binance-chain/bep3-deputy/util"
)
const flagConfigType = "config-type"
const flagConfigAwsRegion = "aws-region"
const flagConfigAwsSecretKey = "aws-secret-key"
const flagConfigPath = "config-path"
const flagBnbNetwork = "bnb-network"
const ConfigTypeLocal = "local"
const ConfigTypeAws = "aws"
func printUsage() {
fmt.Print("usage: ./deputy --bnb-network [0 for testnet, 1 for mainnet] --config-type [aws or local] --config-path config_file_path --aws-region region --aws-secret-key secret key\n")
}
func ensureDir(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0700)
if err != nil {
return fmt.Errorf("could not create directory %v. %v", dir, err)
}
}
return nil
}
func initFlags() {
flag.String(flagConfigPath, "", "config file path")
flag.String(flagConfigType, "", "config type, local or aws")
flag.String(flagConfigAwsRegion, "", "aws s3 region")
flag.String(flagConfigAwsSecretKey, "", "aws s3 secret key")
flag.Int(flagBnbNetwork, int(types.TestNetwork), "binance chain network type")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}
func main() {
initFlags()
bnbNetwork := viper.GetInt(flagBnbNetwork)
if bnbNetwork != int(types.TestNetwork) && bnbNetwork != int(types.ProdNetwork) {
printUsage()
return
}
// we set binance chain network type first because we need to parse binance chain address from config
types.Network = types.ChainNetwork(bnbNetwork)
configType := viper.GetString(flagConfigType)
if configType == "" {
printUsage()
return
}
if configType != ConfigTypeAws && configType != ConfigTypeLocal {
printUsage()
return
}
var config *util.Config
// get config from aws s3
if configType == ConfigTypeAws {
awsSecretKey := viper.GetString(flagConfigAwsSecretKey)
if awsSecretKey == "" {
printUsage()
return
}
awsRegion := viper.GetString(flagConfigAwsRegion)
if awsRegion == "" {
printUsage()
return
}
configContent, err := util.GetSecret(awsSecretKey, awsRegion)
if err != nil {
fmt.Printf("get aws config error, err=%s", err.Error())
return
}
config = util.ParseConfigFromJson(configContent)
} else {
configFilePath := viper.GetString(flagConfigPath)
if configFilePath == "" {
printUsage()
return
}
config = util.ParseConfigFromFile(configFilePath)
}
config.Validate()
util.InitLogger(*config.LogConfig)
if config.InstrumentationConfig.Prometheus && config.InstrumentationConfig.PrometheusListenAddr != "" {
http.Handle("/metrics", promhttp.Handler())
go func() {
if err := http.ListenAndServe(config.InstrumentationConfig.PrometheusListenAddr, nil); err != http.ErrServerClosed {
util.Logger.Error("Prometheus HTTP server ListenAndServe, err: %v", err)
}
}()
util.MustRegisterMetrics()
}
if config.DBConfig.Dialect == common.DBDialectSqlite3 {
err := ensureDir(filepath.Dir(config.DBConfig.DBPath))
if err != nil {
panic(err.Error())
}
}
db, err := gorm.Open(config.DBConfig.Dialect, config.DBConfig.DBPath)
if err != nil {
panic(fmt.Sprintf("open db error, err=%s", err.Error()))
}
defer db.Close()
// init db if tables do not exist
store.InitTables(db)
bnbExecutor := bnb.NewExecutor(config.BnbConfig.RpcAddr, types.ChainNetwork(bnbNetwork), config.BnbConfig)
var ethExecutor common.Executor
if config.EthConfig.SwapType == common.EthSwapTypeEth {
ethExecutor = eth.NewEthExecutor(config.EthConfig.Provider, config.EthConfig.SwapContractAddr, config)
} else {
ethExecutor = eth.NewErc20Executor(config.EthConfig.Provider, config.EthConfig.SwapContractAddr, config)
}
dp := deputy.NewDeputy(db, config, bnbExecutor, ethExecutor)
dp.Start()
ob := observer.NewObserver(db, config, bnbExecutor, ethExecutor)
ob.Start()
adm := admin.NewAdmin(config, dp)
go adm.Serve()
select {}
}