-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyDeals.js
230 lines (194 loc) · 8.47 KB
/
myDeals.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
var myDealsPage = function(){
//$("#my_deals_div").show();
$("#before_load").hide();
$("#after_load").show();
var currency = etc ? "ETC" : "ETH";
var accounts = web3.eth.accounts;
// $('#secret_address').append($('<option>', {value: -1, text: "Select your secret account"}));
//$('#public_address').append($('<option>', {value: -1, text: "Select your public account"}));
for( var i = 0 ; i < accounts.length ; i++ ){
//$('#secret_address').append($('<option>', {value: i, text: accounts[i].toString()}));
$('#public_address').append($('<option>', {value: i, text: accounts[i].toString()}));
}
//$('#secret_address').val(-1);
//$('#public_address').val(0);
//$('#secret_address').on('change', selectAccountChange);
//$('#public_address').on('change', selectAccountChange);
updateDisplay();
//updateUnsoldSellsOfUser();
};
var waitForEventConifrmFunc = function(event, tx, callback) {
event.watch(function(err, result) {
if (err) {
callback(false);
return handleError(err);
}
if (result.transactionHash === tx) callback(true);
event.stopWatching(function(){});
});
};
function sell(){
var sender = getPublicAccount();
$('#sell_button').html("Waiting for confirmation");
$('#sell_button').prop('disabled', true);
globalContractInstance.sell({from: sender, value: 30000000000000000},
function(err, result) {
if (err) return handleError(err);
waitForEventConifrmFunc(globalContractInstance.Sell({seller: sender}),
result,
function(args) {
$('#sell_button').html("Sell");
$('#sell_button').prop('disabled', false);
if (args) alertStatus("sell tx completed");
else alertStatus("sell failed");
});
});
}
function buy(){
var sender = getPublicAccount();
$('#buy_button').html("Waiting for confirmation");
$('#buy_button').prop('disabled', true);
globalContractInstance.buy({from: sender, value: 30000000000000000},
function(err, result) {
if (err) return handleError(err);
waitForEventConifrmFunc(globalContractInstance.Buy({buyer: sender}),
result,
function(args) {
$('#buy_button').html("Buy");
$('#buy_button').prop('disabled', false);
if (args) alertStatus("buy tx completed");
else alertStatus("buy failed");
});
});
}
function return_chametz(){
var sender = getPublicAccount();
$('#return_button').html("Waiting for confirmation");
$('#return_button').prop('disabled', true);
globalContractInstance.returnChametz({from: sender},
function(err, result) {
if (err) return handleError(err);
waitForEventConifrmFunc(globalContractInstance.ReturnChametz({buyer: sender}),
result,
function(args) {
$('#return_button').html("Return Chametz");
$('#return_button').prop('disabled', false);
if (args) alertStatus("return tx completed");
else alertStatus("return tx failed");
});
});
}
function cancel_sell(){
var sender = getPublicAccount();
$('#cancel_button').html("Waiting for confirmation");
$('#cancel_button').prop('disabled', true);
globalContractInstance.cancelSell({from: sender},
function(err, result) {
if (err) return handleError(err);
waitForEventConifrmFunc(globalContractInstance.CancelSell({seller: sender}),
result,
function(args) {
$('#cancel_button').html("Cancel sell");
$('#cancel_button').prop('disabled', false);
if (args) alertStatus("cancel sell tx completed");
else alertStatus("cancel tx failed");
});
});
}
var updateUnsoldSellsOfUser = function( ) {
var sender = getPublicAccount();
globalContractInstance.sellerNumOpenDeals(sender,
function(err, result) {
if( parseInt( result.toString() ) > 0 ) {
$('#sell_label').html( " (" + result.toString() + " of your units are still waiting for a buyer)" );
$('#cancel_label').html( " (" + result.toString() + " units)" );
$('#cancel_button').prop('disabled', false);
}
else {
$('#sell_label').html( " " );
$('#cancel_label').html( " " );
$('#cancel_button').prop('disabled', true);
}
});
};
var updateNumBuysOfUser = function( ) {
var sender = getPublicAccount();
globalContractInstance.buyerNumDeals(sender,
function(err, result) {
if( parseInt( result.toString() ) > 0 ) {
$('#buy_label').html( " (you bought " + result.toString() + " Chametz units)" );
$('#return_label').html( " (" + result.toString() + " units)" );
}
else {
$('#buy_label').html( " " );
$('#return_label').html( " " );
}
});
};
var numOpenDeals = function() {
globalContractInstance.numChametzForSale(
function(err, result) {
$('#units_for_sale_label').html( result.toString() + " Chametz units are currently available for sale" );
if( parseInt( result.toString() ) == 0 ) {
$('#buy_button').prop('disabled', true);
}
else{
$('#buy_button').prop('disabled', false);
}
//alert(result);
});
};
var getFormattedTime = function( unix_timestamp ) {
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var days = parseInt(unix_timestamp / (24 * 3600));
var hours = parseInt(unix_timestamp / (3600)) % 60;
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
return days.toString() + " days and " + formattedTime + " hours";
};
var timesTillPassover = function() {
globalContractInstance.passoverStartTime(
function(err, result) {
var passoverStart = parseInt(result);
var currentTime = getEpoc();
$('#return_button').prop('disabled', true);
if( passoverStart > currentTime ) {
$('#time_label').html( getFormattedTime(passoverStart-currentTime) + " until Passover begins");
$('#cancel_button').prop('disabled', true);
}
else {
$('#sell_button').prop('disabled', true);
$('#buy_button').prop('disabled', true);
globalContractInstance.passoverEndTime(
function(err, result) {
var passoverEnd = parseInt(result);
var currentTime = getEpoc();
if( passoverEnd > currentTime ) {
$('#time_label').html( getFormattedTime(passoverEnd-currentTime) + " until Passover ends");
}
else {
$('#time_label').html( "Passover ended");
$('#return_button').prop('disabled', false);
}
});
}
});
};
var updateDisplay = function() {
updateUnsoldSellsOfUser();
updateNumBuysOfUser();
numOpenDeals();
timesTillPassover();
setTimeout( updateDisplay, 1000 );
}
// num unsold deals
// num bought deals
// time to cancel
// time to return
// time left to buy/sell
// num open deals