This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
188 lines (159 loc) · 5.35 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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"
sdk "github.com/kava-labs/cosmos-sdk/types"
"github.com/kava-labs/go-sdk/client"
app "github.com/kava-labs/go-sdk/kava"
"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/executor/kava"
"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 flagKavaNetwork = "kava-network"
const ConfigTypeLocal = "local"
const ConfigTypeAws = "aws"
func printUsage() {
fmt.Print("usage: ./deputy --bnb-network [0 for testnet, 1 for mainnet] --kava-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")
flag.Int(flagKavaNetwork, int(types.TestNetwork), "kava 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)
// we set kava address prefixes first because we need to parse kava chain address from config
kavaConfig := sdk.GetConfig()
app.SetBech32AddressPrefixes(kavaConfig)
kavaConfig.Seal()
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(types.ChainNetwork(bnbNetwork), config.BnbConfig)
var otherExecutor common.Executor
switch config.ChainConfig.OtherChain {
case common.ChainEth:
if config.EthConfig.SwapType == common.EthSwapTypeEth {
otherExecutor = eth.NewEthExecutor(config.EthConfig.Provider, config.EthConfig.SwapContractAddr, config.EthConfig)
} else {
otherExecutor = eth.NewErc20Executor(config.EthConfig.Provider, config.EthConfig.SwapContractAddr, config)
}
case common.ChainKava:
kavaNetwork := viper.GetInt(flagKavaNetwork)
switch kavaNetwork {
case int(client.LocalNetwork), int(client.TestNetwork), int(client.ProdNetwork):
break
default:
printUsage()
return
}
otherExecutor = kava.NewExecutor(client.ChainNetwork(kavaNetwork), config.KavaConfig)
}
dp := deputy.NewDeputy(db, config, bnbExecutor, otherExecutor)
dp.Start()
ob := observer.NewObserver(db, config, bnbExecutor, otherExecutor)
ob.Start()
adm := admin.NewAdmin(config, dp)
go adm.Serve()
select {}
}