-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm3.go
130 lines (107 loc) · 3.88 KB
/
m3.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
// Copyright (c) 2015 Max Wolter
//
// This file is part of M3 - Maker Market Maker.
//
// M3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// M3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with M3. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"os"
"os/signal"
"os/user"
"path"
"strings"
"syscall"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ogier/pflag"
"github.com/awishformore/logger"
"github.com/awishformore/m3/adaptor/market"
"github.com/awishformore/m3/adaptor/wallet"
"github.com/awishformore/m3/business"
)
const (
major = 0
minor = 1
patch = 1
)
func main() {
// get current user
usr, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "FAILED TO GET CURRENT USER (%v)\n", err)
os.Exit(1)
}
// define configuration parameters
level := pflag.StringP("level", "l", "INFO", "log level")
ipc := pflag.StringP("ipc", "i", path.Join(usr.HomeDir, ".ethereum", "testnet", "geth.ipc"), "IPC endpoint for Ethereum node")
maker := pflag.StringP("maker", "m", "0x5661e7bc2403c7cc08df539e4a8e2972ec256d11", "Maker Market contract address")
proxy := pflag.StringP("proxy", "p", "0x5661e7bc2403c7cc08df539e4a8e2972ec256d12", "Trade Proxy contract address")
refresh := pflag.DurationP("refresh", "r", time.Second*14, "interval to check poll for new orders on market")
threshold := pflag.Uint64P("threshold", "t", 30000, "threshold of profit margin to execute trades")
key := pflag.StringP("key", "k", "", "private key to be used to transact with the proxy contract")
pass := pflag.StringP("pass", "p", "", "password to unlock the private key to use for transacting")
pflag.Parse()
// initialize logger
lvl := logger.ParseLevel(*level)
log := logger.New(os.Stderr, lvl)
log.Infof("M3 DAEMON v%v.%v.%v", major, minor, patch)
log.Infof("starting m3 daemon...")
// initialize connection to the geth node
conn, err := rpc.NewIPCClient(*ipc)
if err != nil {
log.Criticalf("could not initialize IPC connection: %v (%v)", ipc, err)
os.Exit(1)
}
// initialize the contract backend for our wrappers
backend := backends.NewRPCBackend(conn)
// initialize the wrapper around the maker market
mkr, err := market.NewMaker(backend, common.HexToAddress(*maker))
if err != nil {
log.Criticalf("could not initialize the market wrapper (%v)", err)
os.Exit(1)
}
// initialize transactor to authenticate actions
auth, err := bind.NewTransactor(strings.NewReader(*key), *pass)
if err != nil {
log.Criticalf("could not initialize auth transactor (%v)", err)
os.Exit(1)
}
// initialize the wrapper around our m3 wallet
pxy, err := wallet.NewM3(backend, auth, common.HexToAddress(*proxy))
if err != nil {
log.Criticalf("could not initialize wallet wrapper (%v)", err)
os.Exit(1)
}
// initialize matcher logic
matcher := business.NewMatcher(
log, mkr, pxy,
business.SetRefresh(*refresh),
business.SetThreshold(*threshold),
)
log.Infof("m3 daemon startup complete")
// wait for signal to shut down
sigc := make(chan os.Signal)
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-sigc
log.Infof("shutting down m3 daemon...")
// stop execution & free resources on relevant modules
matcher.Stop()
conn.Close()
log.Infof("m3 daemon shutdown complete")
os.Exit(0)
}