-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathorder.js
183 lines (159 loc) · 3.88 KB
/
order.js
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
// GLOBALS //
const blessed = require("blessed");
const { colorScheme } = require("../constants");
const spacing = { side: 4, top: 5 };
const transactionStrings = {
label: "Trade BTC",
hint: "Select an option and enter an amount."
};
// UTILITIES //
function isValidInput(n) {
// Order amount must be numeric
if (n === "") {
return false;
}
// Order amount must be positive
let num = parseFloat(n);
return !isNaN(num) && isFinite(num) && num > 0;
}
// MAIN //
module.exports.createOrderScreen = (screen, callback) => {
let transactionForm = null;
transactionForm = blessed.form({
parent: screen,
keys: true,
type: "overlay",
top: "center",
left: "center",
width: 45,
height: 14,
bg: colorScheme.background,
color: "white"
});
let label = blessed.box({
parent: transactionForm,
top: 1,
left: 18,
width: 16,
height: 1,
content: transactionStrings.label,
style: { bg: colorScheme.background, fg: "white", bold: true },
tags: true
});
let hint = blessed.box({
parent: transactionForm,
top: 2,
left: 1,
width: 42,
height: 1,
shrink: true,
content: transactionStrings.hint,
style: { bg: colorScheme.background, fg: "white" },
tags: true
});
// Buy/Sell Options
var radioSet = blessed.radioset({
parent: transactionForm,
left: "center",
width: 28,
top: 3,
shrink: true,
padding: 1,
style: { bg: colorScheme.background /*fg: 'red'*/ }
});
var buyRadio = blessed.radiobutton({
parent: radioSet,
mouse: true,
keys: true,
shrink: true,
style: { bg: colorScheme.background },
height: 1,
left: 0,
top: 0,
name: "Buy BTC",
content: "Buy BTC"
});
var sellRadio = blessed.radiobutton({
parent: radioSet,
mouse: true,
keys: true,
shrink: true,
style: { bg: colorScheme.background },
height: 1,
left: 14,
top: 0,
name: "Sell BTC",
content: "Sell BTC"
});
// Input Field
var amountEntryBox = blessed.textbox({
parent: transactionForm,
label: " {bold}{blue-fg}BTC Amount{/bold}{/blue-fg} ",
tags: true,
keys: true,
inputOnFocus: true,
left: spacing.side,
top: 6,
border: { type: "line" },
width: 37,
height: 3,
style: {
focus: { border: { fg: colorScheme.textFieldBorderFocused } },
border: { fg: colorScheme.textFieldBorderUnfocused }
}
});
// CTA
let buttonPadding = { left: 4, right: 4, top: 1, bottom: 1 };
var submit = blessed.button({
parent: transactionForm,
mouse: true,
keys: true,
shrink: true,
right: spacing.side,
bottom: 1,
padding: buttonPadding,
name: "submit",
content: "submit",
style: {
bg: colorScheme.confirmLight,
fg: "black",
focus: { bg: colorScheme.confirmDark, fg: "black" },
hover: { bg: colorScheme.confirmDark, fg: "black" }
}
});
var cancel = blessed.button({
parent: transactionForm,
mouse: true,
keys: true,
shrink: true,
left: spacing.side,
bottom: 1,
padding: buttonPadding,
name: "cancel",
content: "cancel",
style: {
bg: colorScheme.cancelLight,
fg: "black",
focus: { bg: colorScheme.cancelDark, fg: "black" },
hover: { bg: colorScheme.cancelDark, fg: "black" }
}
});
// Event Handlers
submit.on("press", () => {
let amount = amountEntryBox.content;
let type = buyRadio.checked ? "BUY" : "SELL";
if (isValidInput(amount)) {
callback(amount, type);
transactionForm.destroy();
} else {
amountEntryBox.setValue("");
amountEntryBox.style.border.fg = "red";
}
});
cancel.on("press", () => transactionForm.destroy());
screen.on("tab", text => transactionForm.focusNext());
screen.key(["escape"], (ch, key) => transactionForm.destroy());
buyRadio.check();
transactionForm.focus();
screen.render();
};