-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
165 lines (142 loc) · 3.73 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
package main
import (
"auctionBidder/cli"
"auctionBidder/client"
"auctionBidder/utils"
"auctionBidder/web3"
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
func main() {
var err error
defer func() {
if err != nil {
spew.Dump(err)
}
}()
setEnv()
err = checkEnv()
if err != nil {
logrus.Error(err)
return
}
err = utils.InitDb()
if err != nil {
logrus.Error(err)
return
}
_, err = startBot()
if err != nil {
logrus.Error(err)
return
}
return
}
func startBot() (bot *cli.BidderBot, err error) {
privateKey := os.Getenv("PRIVATE_KEY")
maxSlippage, _ := decimal.NewFromString(os.Getenv("MAX_SLIPPAGE"))
ethereumNodeUrl := os.Getenv("ETHEREUM_NODE_URL")
minOrderValueUSD, _ := decimal.NewFromString(os.Getenv("MIN_ORDER_VALUE_USD"))
profitMargin, _ := decimal.NewFromString(os.Getenv("PROFIT_MARGIN"))
markets := os.Getenv("MARKETS")
gasPriceLevel := os.Getenv("GAS_PRICE_LEVEL")
gasPriceTipsInGwei := 5
switch gasPriceLevel {
case "fast":
gasPriceTipsInGwei = 0
case "super-fast":
gasPriceTipsInGwei = 10
case "flash-boy":
gasPriceTipsInGwei = 25
}
ddexClient, err := client.NewDdexClient(privateKey)
if err != nil {
return
}
bidderClient, err := client.NewBidderClient(privateKey, ddexClient.Assets, ddexClient.Markets)
if err != nil {
return
}
web3Client := web3.NewWeb3(ethereumNodeUrl)
bot = &cli.BidderBot{
bidderClient,
ddexClient,
web3Client.NewBlockChannel(),
maxSlippage,
minOrderValueUSD,
gasPriceTipsInGwei,
markets,
profitMargin,
}
go bot.Run()
err = cli.StartGui()
return
}
func setEnv() {
os.Setenv("CONFIGPATH", "/workingDir/config.json")
os.Setenv("SQLITEPATH", "/workingDir/auctionBidderSqlite")
os.Setenv("LOGPATH", "/workingDir")
os.Setenv("CHAIN_ID", "1")
os.Setenv("HYDRO_CONTRACT_ADDRESS", "0x241e82C79452F51fbfc89Fac6d912e021dB1a3B7")
os.Setenv("DDEX_URL", "https://api.ddex.io/v4")
// ropsten
if os.Getenv("NETWORK") == "ropsten" {
os.Setenv("CHAIN_ID", "3")
os.Setenv("HYDRO_CONTRACT_ADDRESS", "0x06898143DF04616a8A8F9614deb3B99Ba12b3096")
os.Setenv("DDEX_URL", "https://bfd-ropsten-59c1702d-api.intra.ddex.io/v4/")
}
loadEnv(os.Getenv("CONFIGPATH"))
}
func loadEnv(filePath string) (err error) {
jsonFile, err := os.Open(filePath)
defer jsonFile.Close()
if err != nil {
return
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var envs map[string]string
json.Unmarshal(byteValue, &envs)
for key, value := range envs {
os.Setenv(key, value)
}
return
}
func checkEnv() (err error) {
requiredEnvDefaultValue := map[string]string{
"PRIVATE_KEY": "B7A0C9D2786FC4DD080EA5D619D36771AEB0C8C26C290AFD3451B92BA2B7BC2C",
"MAX_SLIPPAGE": "0.05",
"ETHEREUM_NODE_URL": "https://mainnet.infura.io/v3/37851992caeb4289aa749112fe798621",
"MIN_ORDER_VALUE_USD": "100",
"MARKETS": "ETH-USDT,ETH-DAI",
"PROFIT_MARGIN": "0.01",
"GAS_PRICE_LEVEL": "fast",
}
if os.Getenv("NETWORK") == "ropsten" {
requiredEnvDefaultValue["ETHEREUM_NODE_URL"] = "https://ropsten.infura.io/v3/37851992caeb4289aa749112fe798621"
requiredEnvDefaultValue["MARKETS"] = "ETH-USDT6,TETH-DAI"
}
for envName, defaultValue := range requiredEnvDefaultValue {
if os.Getenv(envName) == "" {
fmt.Printf("Enter %s(default %s):", envName, defaultValue)
var input string
fmt.Scanln(&input)
if input == "" {
input = defaultValue
}
os.Setenv(envName, input)
}
requiredEnvDefaultValue[envName] = os.Getenv(envName)
}
f, err := os.OpenFile(os.Getenv("CONFIGPATH"), os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if err == nil {
envToWrite, _ := json.MarshalIndent(requiredEnvDefaultValue, "", " ")
f.Write(envToWrite)
}
return
}