forked from rotki/rotki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otctrades.js
261 lines (242 loc) · 9.5 KB
/
otctrades.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var settings = require("./settings.js")();
require("./elements.js")();
require("./utils.js")();
var OTC_TRADES_TABLE = null;
var CURRENT_TRADE = {};
function update_valhtml(selector_text, val) {
$(selector_text).val(val);
$(selector_text).html(val);
}
function format (d) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>Extra Info</td>'+
'<td>'+d.notes+'</td>'+
'</tr>'+
'<tr>'+
'<td>Links:</td>'+
'<td>'+d.link+'</td>'+
'</tr>'+
'<tr>'+
'<td>Fee:</td>'+
'<td>'+d.fee+' '+d.fee_currency+'</td>'+
'</tr>'+
'</table>';
}
function edit_otc_trade(row) {
let data = row.data();
let ts = timestamp_to_date(data.timestamp);
$('#otc_timestamp').val(ts);
$('#otc_pair').val(data.pair);
$('#otc_type').val(data.type);
$('#otc_amount').val(data.amount);
$('#otc_rate').val(data.rate);
$('#otc_fee').val(data.fee);
$('#otc_fee_currency').val(data.fee_currency);
$('#otc_link').val(data.link);
$('#otc_notes').val(data.notes);
CURRENT_TRADE = {
'id': data.id,
'timestamp': ts,
'pair': data.pair,
'trade_type': data.type,
'amount': data.amount,
'rate': data.rate,
'fee': data.fee,
'fee_currency': data.fee_currency,
'link': data.link,
'notes': data.notes
};
update_valhtml('#otctradesubmit', 'Edit Trade');
}
function delete_otc_trade(row) {
let data = row.data();
client.invoke(
'delete_otctrade',
data.id,
(error, res) => {
if (error || res == null) {
showError('Error at Trade Deletion', error);
} else {
if (!res['result']) {
showError('Error at Trade Deletion', res['message']);
} else {
showInfo('Succcess', 'Trade Deleted');
reload_table_data();
}
}
});
}
function reload_table_data() {
client.invoke("query_otctrades", (error, result) => {
if (error || result == null) {
console.log("Error at querying OTC trades: " + error);
} else {
update_valhtml('#otc_timestamp', '');
update_valhtml('#otc_pair', '');
update_valhtml('#otc_amount', '');
update_valhtml('#otc_rate', '');
update_valhtml('#otc_fee', '');
update_valhtml('#otc_fee_currency', '');
update_valhtml('#otc_link', '');
update_valhtml('#otc_notes', '');
OTC_TRADES_TABLE.clear();
OTC_TRADES_TABLE.rows.add(result);
OTC_TRADES_TABLE.draw();
}
});
}
function create_otctrades_table() {
var str = '<div class="row"><div class="col-lg-12"><h1 class=page-header">OTC Trades List</h1></div></div>';
str += '<div class="row"><table id="table_otctrades"><thead><tr><th></th><th>Pair</th><th>Type</th><th>Amount</th><th>Rate</th><th>Time</th></tr/></thead><tbody id="table_otctrades_body"></tbody></table></div>';
$(str).insertAfter($('#otc_table_anchor'));
client.invoke("query_otctrades", (error, result) => {
if (error || result == null) {
console.log("Error at querying OTC trades: " + error);
} else {
for (let i = 0; i < result.length; i++) {
let str = '<tr><td></td><td></td/><td></td><td></td></tr>';
$(str).appendTo($('#table_otctrades_body'));
}
OTC_TRADES_TABLE = $('#table_otctrades').DataTable({
"data": result,
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": '',
"render": function () {
return '<i class="fa fa-plus-square" aria-hidden="true"></i>';
},
width:"15px"
},
{ "data": "pair" },
{ "data": "type" },
{ "data": "amount" },
{ "data": "rate" },
{
"render": function (data, type, row) {
if (type == 'sort') {
return data;
}
return timestamp_to_date(data);
},
"data": "timestamp"
},
],
"order": [[5, 'asc']],
drawCallback : dt_edit_drawcallback('table_otctrades', edit_otc_trade, delete_otc_trade)
});
}
});
}
function create_otctrades_ui() {
var str = '<div class="row"><div class="col-lg-12"><h1 class="page-header">OTC Trades Management</h1></div></div>';
str += '<div class="row"><div class="col-lg-12"><div class="panel panel-default"><div class="panel-heading">Register New Trade</div><div class="panel-body"></div></div></div></div>';
$('#page-wrapper').html(str);
str = form_entry('time', 'otc_timestamp', '', 'Time that the trade took place');
str += form_entry('pair', 'otc_pair', '', 'Pair for the trade. BASECURRENCY_QUOTE_CURRENCY');
str += form_select('trade type', 'otc_type', ['buy', 'sell'], '');
str += form_entry('amount', 'otc_amount', '', 'Amount bought/sold');
str += form_entry('rate', 'otc_rate', '', 'Rate of the trade');
str += form_entry('fee', 'otc_fee', '', 'Fee if any that the trade occured');
str += form_entry('fee currency', 'otc_fee_currency', '', 'Currency the fee was paid in');
str += form_entry('link', 'otc_link', '', 'A link to the trade. e.g.: in an explorer');
str += form_text('Enter additional notes', 'otc_notes', 3, '', 'Additional notes to store for the trade');
str += form_button('Add Trade', 'otctradesubmit');
str += '<div id="otc_table_anchor"></div>';
$(str).appendTo($('.panel-body'));
create_otctrades_table();
}
function add_otctrades_listeners() {
// if we are reloading the page, recreate the table
if (OTC_TRADES_TABLE) {
// first remove the 2 table divs
$('#otc_table_anchor').next('div').remove();
$('#otc_table_anchor').next('div').remove();
// and then recreate it
create_otctrades_table();
}
$('#otctradesubmit').click(function(event) {
event.preventDefault();
let otc_timestamp = $('#otc_timestamp').val();
let otc_pair = $('#otc_pair').val();
let otc_type = $('#otc_type').val();
let otc_amount = $('#otc_amount').val();
let otc_rate = $('#otc_rate').val();
let otc_fee = $('#otc_fee').val();
let otc_fee_currency = $('#otc_fee_currency').val();
let otc_link = $('#otc_link').val();
let otc_notes = $('#otc_notes').val();
let trade_id = null;
if ('id' in CURRENT_TRADE) {
trade_id = CURRENT_TRADE['id'];
}
let payload = {
'otc_id': trade_id,
'otc_timestamp': otc_timestamp,
'otc_pair': otc_pair,
'otc_type': otc_type,
'otc_amount': otc_amount,
'otc_rate': otc_rate,
'otc_fee': otc_fee,
'otc_fee_currency': otc_fee_currency,
'otc_link': otc_link,
'otc_notes': otc_notes
};
// depending on the type of button value we call different function
let command = 'add_otctrade';
if ($('#otctradesubmit').val() == 'Edit Trade') {
command = 'edit_otctrade';
}
// and now send the data to the python process
client.invoke(
command,
payload,
(error, res) => {
if (error || res == null) {
showError('Trade Addition Error', error);
} else {
if (!res['result']) {
showError('Trade Addition Error', res['message']);
} else {
showInfo('Success', 'Trade submitted');
reload_table_data();
// also make sure we are back to adding a trade
// in case we were editing
$('#otctradesubmit').val('Add Trade');
$('#otctradesubmit').html('Add Trade');
}
}
});
});
$('#otc_timestamp').datetimepicker({
format: settings.datetime_format
});
// Add event listener for opening and closing details
$('#table_otctrades tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var tdi = tr.find("i.fa");
var row = OTC_TRADES_TABLE.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
tdi.first().removeClass('fa-minus-square');
tdi.first().addClass('fa-plus-square');
}
else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
tdi.first().removeClass('fa-plus-square');
tdi.first().addClass('fa-minus-square');
}
});
}
module.exports = function() {
this.create_otctrades_ui = create_otctrades_ui;
this.add_otctrades_listeners = add_otctrades_listeners;
};