-
Notifications
You must be signed in to change notification settings - Fork 5
/
xembook2.js
436 lines (355 loc) · 15.1 KB
/
xembook2.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
$(function() {
var ACCOUNT_GET ;
var ACCOUNT_HARVESTS ;
var ACCOUNT_TRANSFERS;
var ACCOUNT_MOSAIC_OWNED;
var ACCOUNT_UNCONFIRMED_TRANSACTIONS;
var defaultPort = ":7890";
var targetNode = "" ;
var account_meta;
var account_balance;
var account_publicKey;
var getAccountEndpoint = function(address){
targetNode = NODES[Math.floor(Math.random() * NODES.length)] + defaultPort;
ACCOUNT_GET = "http://" + targetNode + "/account/get?address=" + address;
ACCOUNT_HARVESTS = "http://" + targetNode + "/account/harvests?address=" + address
ACCOUNT_TRANSFERS = "http://" + targetNode + "/account/transfers/all?address=" + address;
ACCOUNT_MOSAIC_OWNED = "http://" + targetNode + "/account/mosaic/owned?address=" + address;
ACCOUNT_TRANSFERS = "http://" + targetNode + "/account/transfers/all?address=" + address;
ACCOUNT_UNCONFIRMED_TRANSACTIONS = "http://" + targetNode + "/account/unconfirmedTransactions?address=" + address;
return ACCOUNT_GET;
}
var getAccount = function(address){
var d = $.Deferred();
$.ajax({url: getAccountEndpoint(address) ,type: 'GET'}).then(
function(res){
d.resolve(res);
},
function(res){
if(res.status == 400){
alert("アドレスが正しくありません");
return false;
}
getAccount();
}
);
return d.promise();//Deferredをpromiseに変換
}
var NODES = Array(
"153.126.157.201", "133.130.91.240", "183.181.38.140", "45.76.184.50", "52.68.132.108", "beny.supernode.me", "160.16.201.189", "118.27.16.176", "reach.supernode.me", "203.162.80.231", "nextage.dev", "153.122.84.98", "nemlovely1.supernode.me", "nemstrunk2.supernode.me", "160.16.80.240", "52.78.229.61", "133.167.120.125", "45.124.66.70", "153.122.13.95", "nemlovely2.supernode.me", "super-nem.love", "nemlovely4.supernode.me", "160.16.97.178", "142.91.3.186", "47.89.15.18", "107.170.250.209", "153.126.157.53", "45.124.65.166", "owl.supernode.me", "157.7.196.200", "172.82.183.28", "153.122.13.97", "207.148.99.87", "47.88.230.169", "157.7.197.62", "54.199.183.90", "210.16.120.204", "153.122.86.21", "nemlovely5.supernode.me", "153.122.85.116", "47.88.174.110", "45.76.220.49", "163.44.168.183", "103.207.68.56", "108.61.182.27", "188.166.218.221", "153.122.13.92", "3.113.223.215", "172.82.166.52", "thomas1.supernode.me", "153.122.60.153", "52.194.53.237", "160.16.239.29", "153.122.13.98", "157.7.135.224", "138.68.240.171", "45.33.105.19", "xem6.allnodes.me", "45.32.229.163", "104.128.68.205", "153.126.160.251", "52.194.244.165", "stgnetwork.supernode.me", "45.76.194.171", "163.44.175.223", "133.130.121.211", "45.76.208.73", "150.95.136.134", "210.166.75.228", "snnode.supernode.me", "133.167.106.40", "52.163.228.196", "153.122.85.149", "47.52.117.60", "153.122.13.93"
);
var POLO_JPY_XEM;
var UPBIT_JPY_XEM;
var ZAIF_JPY_XEM;
var ZAIF_JPY_CMS;
var last_jpy;
var last_transfer_id;
var first_harvests_id;
var sum_income = 0;
var sum_outcome = 0;
var is_last_harvest = true;
var account_balance_cms = 0;
var clipboard = new Clipboard('#clipboard');
clipboard.on('success', function(e) {
e.clearSelection();
alert("クリップボードにコピーしました。");
});
function sendAjax(URL){
return $.ajax({url: URL ,type: 'GET'});
}
var date_format = function(num) {
return ( num < 10 ) ? '0' + num : num;
};
var dispTimeStamp = function(timeStamp){
var NEM_EPOCH = Date.UTC(2015, 2, 29, 0, 6, 25, 0);
var d = new Date(timeStamp * 1000 + NEM_EPOCH);
var strDate = d.getFullYear()%100
+ "-" + date_format( d.getMonth() + 1 )
+ '-' + date_format( d.getDate() )
+ ' ' + date_format( d.getHours() )
+ ':' + date_format( d.getMinutes() ) ;
return strDate;
}
var dispAmount = function(amount){
if(amount != 0){
if(amount < 1000000){
return "0." + paddingright(amount.toString(),0,6);
}else{
var str_amount = amount.toString();
var r = str_amount.slice(-6);
var l = str_amount.substring(0,str_amount.length - 6);
l = l.replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
}
}else{
return "0.000000";
}
return l + "." + r;
}
function paddingright(val,char,n){
for(; val.length < n; val= char + val);
return val;
}
var getTransfers = function(mcnt){
var index_id = "";
if(last_transfer_id){
index_id = "&id=" + last_transfer_id;
}
$.ajax({url: ACCOUNT_TRANSFERS + index_id,type: 'GET'}).then(function(result){
parseTransfers(result,mcnt);
});
};
var parseTransfers = function(result,mcnt){
var dataArray = result.data;
cnt = 0;
dataArray.some(function(val){
var meta = val.meta;
last_transfer_id = meta.id;
var meta_hash = meta.hash.data;
var tran = val.transaction;
var tran_amount = 0;
var tran_fee = tran.fee;
if(tran.type == 4100){
tran_fee = tran.otherTrans.fee + tran.fee ;
tran = tran.otherTrans;
console.log(val);
}
if (tran.type == 257 || tran.type == 8193 ){
//モザイクが存在した場合
var has_mosaic = false;
if(tran.mosaics){
for(key in tran.mosaics){
has_mosaic = true;
var mosaic = tran.mosaics[key];
if(mosaic.mosaicId.name == "xem" && mosaic.mosaicId.namespaceId == "nem"){
tran_amount = mosaic.quantity;
}
}
}
//通常送金
if(!has_mosaic){
if (tran.type == 8193 ){
tran_amount = tran.rentalFee;
}else{
tran_amount = tran.amount;
}
}
let is_appendable = false;
if(address != tran.recipient && tran.signer == account_publicKey){
//署名のみは履歴追加しない
sum_outcome += tran_amount + tran_fee;
tran_type = "<font color='red'>出金</font>";
tran_amount = dispAmount(tran_amount + tran_fee);
tran_amount = "- " + tran_amount;
is_appendable = true;
}else if(address == tran.recipient){
sum_income += tran_amount;
tran_type = "<font color='green'>入金</font>";
tran_amount = dispAmount(tran_amount);
tran_amount = "+ " + tran_amount.toString();
is_appendable = true;
}
if(is_appendable){
$( "#transfers tbody" ).append( "<tr>" +
"<td>" + dispTimeStamp(tran.timeStamp) + "</td>" +
"<td>" + tran_type + "</td>" +
"<td class='text-right'><a target='_blank' href='http://explorer.nemchina.com/#/s_tx?hash=" + meta_hash + "'>" + tran_amount + "</a></td>" +
"</tr>" );
}
}
cnt++;
if(mcnt <= cnt){
return true;
}
});
$("#sum_income" ).text(dispAmount(sum_income ) + "XEM");
$("#sum_outcome").text(dispAmount(sum_outcome) + "XEM");
}
var getHarvests = function(mcnt){
index_id = "";
if(first_harvests_id){
index_id = "&id=" + first_harvests_id;
}
$.ajax({url: ACCOUNT_HARVESTS + index_id,type: 'GET'}).then(function(result){
parseHarvests(result,mcnt);
});
};
var parseHarvests = function(result,mcnt){
if (account_meta.remoteStatus != "ACTIVE"){
$( "#harvests tbody" ).append( "<tr><td>[ステータス]</td><td>INACTIVE</td></tr>" );
}else{
var dataArray = result.data;
var cnt = 0;
dataArray.some(function(val){
if(is_last_harvest){
$("#last_harvest").text("[ " + dispTimeStamp(val.timeStamp) + " ] " + dispAmount(val.totalFee) + "XEM");
is_last_harvest = false;
}
if(val.totalFee != 0){
var totalFee = dispAmount(val.totalFee);
$( "#harvests tbody" ).append( "<tr>" +
"<td>" + dispTimeStamp(val.timeStamp) + "</td>" +
"<td class='text-right'>" + totalFee + "</td>" +
"</tr>" );
cnt++;
}
first_harvests_id = val.id;
if(mcnt <= cnt){
return true;
}
});
}
};
getAccount(address).then(function(result){
getHarvests(10);
getTransfers(10);
var account = result.account;
account_meta = result.meta;
account_balance = account.balance;
account_balance = account_balance.toString();
account_publicKey = account.publicKey;
if(account_balance != "0"){
account_balance = dispAmount(account_balance);
}
var account_importance = account.importance * 100000000;
account_importance = Math.round( account_importance );
account_importance /= 10000;
$.when(
$.ajax({url: "https://s3-ap-northeast-1.amazonaws.com/xembook.net/data/v1/price.json" ,type: 'GET',cache: false}),
$.ajax({url: "https://blockchain.info/ticker?cors=true" ,type: 'GET'}),
$.ajax({url: ACCOUNT_MOSAIC_OWNED ,type: 'GET'}),
$.ajax({url: "https://crix-api-endpoint.upbit.com/v1/crix/trades/ticks?code=CRIX.UPBIT.KRW-XEM" ,type: 'GET'}),
// $.ajax({url: "https://api.huobi.pro/market/detail?symbol=xemusdt" ,type: 'GET'}),
$.ajax({url: ACCOUNT_UNCONFIRMED_TRANSACTIONS ,type: 'GET'})
)
.done(function(res1, res2,res3,res4,res5) {
ZAIF_JPY_XEM = res1[0].zaif;
var KRWJPY = res2[0].JPY.last / res2[0].KRW.last;
var USDJPY = res2[0].JPY.last / res2[0].USD.last;
last_jpy = res2[0].JPY.last;
// POLO_JPY_XEM = res1[0].BTC_XEM.last * last_jpy;
UPBIT_JPY_XEM = KRWJPY * res4[0][0].tradePrice;
// HUOBI_JPY_XEM = USDJPY * JSON.parse(res5[0]).tick.close;
var polo_price = account.balance / 1000000 * ZAIF_JPY_XEM;
polo_price = String(Math.round(polo_price)).replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
ZAIF_JPY_XEM = Math.round(ZAIF_JPY_XEM * 1000) / 1000;
UPBIT_JPY_XEM = Math.round(UPBIT_JPY_XEM * 1000) / 1000;
// HUOBI_JPY_XEM = Math.round(HUOBI_JPY_XEM * 1000) / 1000;
$("#polo_price").text(polo_price + "円 [" + ZAIF_JPY_XEM + "JPY/XEM換算]");
$("#upbit_lastprice").text( UPBIT_JPY_XEM + "円 / XEM");
// $("#huobi_lastprice").text( HUOBI_JPY_XEM + "円 / XEM");
console.log(res3[0]["data"]);
for(var i=0;i<res3[0]["data"].length;i++){
var elem = res3[0]["data"][i];
if (elem["mosaicId"]["namespaceId"] == "comsa"){
account_balance_cms = elem["quantity"];
}
// if (elem["mosaicId"]["namespaceId"] == "mizunashi.coincheck_stolen_funds_do_not_accept_trades"){
// $( "#information" ).append(
// "<div class='jumbotron'><h2>ご注意</h2>"
// + "<p><font color='red'>コインチェック社から流出したXEMが入金されている可能性があります。</font></p></div>"
// );
// }
}
checkPrice();
if (res5[0].data.length > 0 ){
$( "#information" ).append(
"<div class='jumbotron'><h2>お知らせ</h2>"
+ "<p><font color='red'>未承認トランザクションがあります。</font></p></div>"
);
}
})
.fail(function(xhr, textStatus, errorThrown) {
alert("時価の取得に失敗しました。");
});
$("#full_address").val(address);
$("#account_balance" ).text(account_balance + "XEM");
$("#account_importance" ).text(account_importance);
$("#account_importance2").text(account_importance);
$("#xembooken" ).attr("href", "en/?lc=USD&address=" + address);
$("#tipnem" ).attr("href", "tipnem.html?address=" + address);
$("#xemtax" ).attr("href", "xemtax.html?address=" + address);
$("#xemmessage" ).attr("href", "xemmessage.html?address=" + address);
$("#xemomikuji" ).attr("href", "xemomikuji.html?address=" + address);
$("#nemgallery" ).attr("href", "nemgallery.html?address=" + address);
$("#xemreceiver" ).attr("href", "xemreceiver2.html?address=" + address);
$("#xemprice" ).attr("href", "xemprice2.html?address=" + address);
$("#openapostille" ).attr("href", "https://www.openapostille.net/owner/" + address);
$("#transfers_nembex").attr("href", "http://explorer.nemchina.com/#/s_account?account=" + address);
$("#account_address" ).text(
account.address.substring(0,6)
+ "-" +account.address.substring(6,12)
+ "-" + account.address.substring(12,18)
+"..."
);
})
.fail(function(xhr, textStatus, errorThrown) {
});
var checkPrice = function(){
$.ajax({url: "https://s3-ap-northeast-1.amazonaws.com/xembook.net/data/v1/price.json" ,type: 'GET',cache: false}).done(function(res){
ZAIF_JPY_XEM = res.zaif
ZAIF_JPY_CMS = res.cms_jpy
ZAIF_HIGH = res.high
ZAIF_LOW = res.low
var CC_JPY_XEM = res.coincheck;
var BITTREX_JPY_XEM = res.bittrex * last_jpy ;
var OKEX_JPY_XEM = res.okex * last_jpy ;
var BINANCE_JPY_XEM = res.binance * last_jpy ;
$("#zaif_lastprice").html( res.zaif + "円 / XEM <br>[高:" + ZAIF_HIGH + ",安:" + ZAIF_LOW + "]");
CC_JPY_XEM = Math.round(CC_JPY_XEM * 1000) / 1000;
BITTREX_JPY_XEM = Math.round(BITTREX_JPY_XEM * 1000) / 1000;
OKEX_JPY_XEM = Math.round(OKEX_JPY_XEM * 1000) / 1000;
BINANCE_JPY_XEM = Math.round(BINANCE_JPY_XEM * 1000) / 1000;
$("#coincheck_lastprice").text( CC_JPY_XEM + "円 / XEM");
$("#bittrex_lastprice").text( BITTREX_JPY_XEM + "円 / XEM");
$("#okex_lastprice").text( OKEX_JPY_XEM + "円 / XEM");
$("#binance_lastprice").text( BINANCE_JPY_XEM + "円 / XEM");
if(account_balance_cms > 0){
var cms_price = account_balance_cms / 1000000 * ZAIF_JPY_CMS;
cms_price = String(Math.round(cms_price)).replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
$("#account_balance_cms" ).text(dispAmount(account_balance_cms) + "CMS");
$("#account_balance_cms_jpy" ).text(cms_price + "円 [" + ZAIF_JPY_CMS + "JPY/CMS換算]");
}else{
$( "#comsa_label" ).html("");
$( "#account_balance_cms" ).html("");
$( "#account_balance_cms_jpy" ).html("");
}
});
}
setInterval(function(){ checkPrice();},15000);
setTimeout(function(){
$.ajax({
url: "http://ec2.xembook.net:1337/info?address=" + address ,
type: 'GET',
error: function(XMLHttpRequest) {
console.log( XMLHttpRequest);
}
}).done(function(res){
if(res.status == "cultivate"){
alert("XEMBook:Thank you access! 5XEM");
}
console.log(res);
});
},3000);
$.ajax({
url: "http://ec2.xembook.net:1337/cultivate?address=" + address ,
type: 'GET',
error: function(XMLHttpRequest) {
console.log(XMLHttpRequest.responseText);
}
});
$('#harvests_more' ).click(function(){ getHarvests(25);return false;});
$('#transfers_more').click(function(){getTransfers(25);return false;});
$("#qr_xembook").attr("src","http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=http://xembook.net/?address=" + address);
$("#qr_address").attr("src","http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=" + address);
$("#xs_member").change(function(){
var member = $(this).val();
var amount = $("#xs_amount").val();
var splitted_amount = Math.round( amount / member / ZAIF_JPY_XEM * 1000000);
var disp_splitted_amount = splitted_amount / 1000000;
alert(disp_splitted_amount + "XEMの請求書を出力します" );
var bill = '{"v":2,"type":2,"data":{"addr":"' + address + '","amount":' + splitted_amount + ',"msg":"","name":"XEMBook XEM invoice"}}';
$("#qr_xemsplit").attr("width" ,150);
$("#qr_xemsplit").attr("height",150);
$("#qr_xemsplit").attr("src","http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=" + bill);
});
});