-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
221 lines (186 loc) · 4.78 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"errors"
"flag"
"log"
"os"
"time"
ui "github.com/gizak/termui"
kk "github.com/solipsis/go-keepkey/pkg/keepkey"
ss "github.com/solipsis/shapeshift"
)
// ui state
type state int
const (
loading state = iota
encounteredError
selection
exchange
setup
)
var activeState = loading
var Log *log.Logger
var ssAPIKey = "14e1754a594e6f6d234f0867c3884040e0e5d74776ba1e82b4c019147fb625d8343681f12a5c59d804a7fd27140eff83d521091d69c2173ed5916a2b270a1fd1"
// ui elements
var (
loadingScreen *LoadingScreen
errorScreen *ErrorScreen
selectScreen *PairSelectorScreen
exchangeScreen *ExchangeScreen
inputScreen *InputScreen
setupScreen *SetupScreen
header *Header
)
var kkMode = flag.Bool("kk", false, "keepkey mode")
var kkDevice *kk.Keepkey
func main() {
flag.Parse()
// debug logging
f, err := os.OpenFile("debugLog", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
Log = log.New(f, "", 0)
Log.SetOutput(f)
// start ui thread
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
// Begin by loading the selection screen
header = newHeader(DefaultHeaderConfig)
activeState = activeState.transitionLoading("Loading...")
draw(0)
activeState = activeState.transitionSelect()
draw(0)
// Loop until ui exits
listenForEvents()
ui.Loop()
}
// Screen drawing state machine
func draw(t int) {
Log.Println("Current State: ", activeState)
ui.Render(header.draw()...)
switch activeState {
case loading:
ui.Render(loadingScreen.Buffers()...)
case encounteredError:
ui.Render(errorScreen.Buffers()...)
case selection:
ui.Render(selectScreen.Buffers()...)
case setup:
ui.Render(setupScreen.Buffers()...)
case exchange:
// Delays are to ensure QR buffer gets flushed as it
// is drawn separately from the rest of the ui elements
ui.Render(exchangeScreen.Buffers()...)
time.Sleep(100 * time.Millisecond)
exchangeScreen.DrawQR()
}
}
// State transitions
func (s *state) transitionLoading(text string) state {
loadingScreen = NewLoadingScreen(text)
ui.Clear()
return loading
}
func (s *state) transitionError(err error) state {
errorScreen = NewErrorScreen(err.Error())
ui.Clear()
return encounteredError
}
func (s *state) transitionSelect() state {
selectScreen = NewPairSelectorScreen(DefaultSelectLayout)
selectScreen.Init()
ui.Clear()
return selection
}
func (s *state) transitionSetup(precise bool) state {
Log.Println("selectStats", selectScreen.stats)
setupScreen = newSetupScreen(precise, selectScreen.stats)
ui.Clear()
return setup
}
func (s *state) transitionExchange() state {
precise := selectScreen.isPreciseOrder()
// Parse the amount if this is a precise order
var amount float64
if precise {
amt, err := setupScreen.amount()
if err != nil {
return s.transitionError(errors.New("Invalid order amount"))
}
amount = amt
}
// create the order and submit to ShapeShift
shift := &ss.New{
ToAddress: setupScreen.receiveAddress(),
FromAddress: setupScreen.returnAddress(),
Amount: amount,
Pair: selectScreen.activePair(),
ApiKey: ssAPIKey,
}
nshift, err := newShift(shift)
if err != nil {
return s.transitionError(err)
}
// if we have just transitioned to this page
// set up timer to update the time remaining
if precise {
ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
if activeState == exchange {
ui.Render(exchangeScreen.Buffers()...)
}
}
}()
}
exchangeScreen = NewExchangeScreen(nshift, precise)
ui.Clear()
return exchange
}
func listenForEvents() {
anyKey := []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "<Up>", "<Down>", "<Left>", "<Right>", "<Space>", "<Backspace>", "<Delete>", "C-8>",
}
// Subscribe to keyboard event listeners
ui.Handle("<Enter>", func(e ui.Event) {
// TODO: move this logic into their respective screens
switch activeState {
case selection:
selectScreen.jankDrawToggle = true
activeState = activeState.transitionSetup(selectScreen.isPreciseOrder())
case encounteredError:
ui.StopLoop()
case setup:
activeState = activeState.transitionExchange()
}
draw(0)
})
ui.Handle(anyKey, func(e ui.Event) {
switch activeState {
case selection:
selectScreen.Handle(e.ID)
case setup:
setupScreen.Handle(e.ID)
}
draw(0)
})
ui.Handle("q", func(e ui.Event) {
switch activeState {
case setup:
setupScreen.Handle(e.ID)
default:
ui.StopLoop()
}
draw(0)
})
// Redraw if user resizes gui
ui.Handle("<Resize>", func(e ui.Event) {
draw(0)
})
}