forked from xembook/xembook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xemreceiver.html
388 lines (309 loc) · 11.3 KB
/
xemreceiver.html
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
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0">
<link rel="apple-touch-icon" href="icon.png">
<title>XEMReceiver</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-100421513-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<h1>XEMReceiver</h1>
<hr class="zigzag-orange-white">
<div class="container">
<div class="row">
<div class="account col-sm-4">
<div class="form">
<h2>注文情報</h2>
<dl>
<dt>注文ID</dt>
<dd><input id="cnumber" type="text" value=""></dd>
<dt>価格(JPY)</dt>
<dd><input id="jpy_price" type="text" value="0" >円</dd>
</dl>
<dl>
<dt>現在のレート</dt>
<dd><span id="last_price"></span></dd>
<dt>XEM価格(参考値)</dt>
<dd><span id="xem_price"></span> XYM</dd>
</dl>
<ul class="list-unstyled">
<li><button id="submit_price" type="button" class="btn btn-default">QRコード出力</button></li>
<li id="receiverqr"></li>
</ul>
</div>
</div>
<div class="account col-sm-4">
<h2>ファイナライズ</h2>
<dl id="node_info">
<dt>接続ノード</dt><dd><span id="node_activity"></span><span id="node_host"></span></dd>
<dt>確定済み日時</dt>
<dd><span id="finalize_range"></span></dd>
<dt>次回確定予想 [ <span id="finalize_next_time"></span> 分後 ]</dt>
<dd><span id="finalize_next_range"></span>までの注文分</dd>
</dl>
</div>
<div class="account col-sm-4">
<h2>確認</h2>
<dl>
<dd><a id="xembook" href="">XEMBookで確認</a></dd>
<dd><a target="_blank" id="account_explorer">Explorerで確認</a></dd>
</dl>
</div>
<div class="account col-sm-8">
<h2>注文状況</h2>
<table id="rollback" class="table">
<thead><tr>
<th>日時</th>
<th>送信者</th>
<th>メッセージ</th>
<th>価格</th>
</tr></thead>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="symbol-sdk-pack-1.0.1.min.js"></script>
<script src="xembook_config.js"></script>
<script>
var nodelist = NODES;
var explorer = EXPLORER;
//const nodelist = TEST_NODES;
//const explorer = TEST_EXPLORER;
</script>
<script src="xembook.js"></script>
<script>
const qr = require("/node_modules/symbol-qr-library");
async function showInfo(accountInfo){
$("#node_activity").html("<font color='green'>● </font>");
accountInfo.subscribe(async _=>{
showAccountInfo(_);
});
chainRepo.getChainInfo().subscribe(chain=>{
blockRepo.getBlockByHeight(chain.height).subscribe(block=>{
getNewInfo(block);
});
});
//ノード
nodeRepo.getNodeInfo().subscribe(node => {$("#node_host").text(node.host)});
}
function showAmountInfo(){
$.when(
// $.ajax({url: "https://blockchain.info/ticker?cors=true" ,type: 'GET'}),
$.ajax({url: "https://s3-ap-northeast-1.amazonaws.com/xembook.net/data/v2/tickers.json" ,type: 'GET',cache: false}),
)
.done(function(tickers) {
const gateio = Math.round(tickers.USDJPY * tickers.gateio * 1000) / 1000;
const zaif = tickers.zaif;
$("#last_price").text( zaif + "円 / XYM");
// xem_price = Math.ceil($("#jpy_price").val() /gateio );
xem_price = $("#jpy_price").val() /zaif ;
console.log(xem_price)
$("#xem_price").text(xem_price.toFixed(2));
});
}
function dispBlockTimeStamp(id,txInfo){
blockRepo.getBlockByHeight(txInfo.height)
.pipe(
op.delay(1000),
)
.subscribe(block => {
$(id).text(
dispTimeStamp(Number(block.timestamp.toString()),epochAdjustment)
);
},err=>{
console.log("retry");
dispBlockTimeStamp(id,txInfo);
})
}
function appendAggTx(tx){}
function appendTx(insTag,id,tx){}
function insertTxAfter(insTag,id,tx){}
function showReceiptInfo(tag,height,receipt,cnt){}
function showTxAmountInfo(id,amount,fee){}
function showAccountInfo(accountInfo){}
var lastBlockHash = "";
setInterval(function(){
// showPriceInfo(accountRepo.getAccountInfo(address));
var nodeActivity = "";
if(lastBlockHash === newBlockHash){
nodeActivity = "<font color='red'>● </font>";
}else{
nodeActivity = "<font color='green'>● </font>";
}
$("#node_activity").html(nodeActivity);
lastBlockHash = newBlockHash;
}, 60000);
var lastHeight = 0;
var lastFinalizedHeight = 0;
var lastFinalizedTimestamp = 0;
var lastFinalizedBlock;
function getAmountMark(xym){
let amountMark = "";
if(xym != undefined){
if(xym.amount.compact() === Number($("#xem_price").text()) * 1000000){
amountMark = "✅" ;
}
amountMark += " " + dispAmount(xym.amount.toString(),6).slice(0,-4) + " XYM";
}
return amountMark;
}
function getListenerInfo(listener){
/* この一行を加えると何故か動く */
listener.status(address);
listener.unconfirmedAdded(address)
.pipe(
op.filter(tx=> address.plain() === tx.recipientAddress.plain())
)
.subscribe(tx=>{
// const xym = tx.mosaics.find(x=>x.id.toHex() === currencyId);
const xyms = tx.mosaics.filter(item=> [currencyNamespaceId,currencyId].includes(item.id.toHex()));
for(const xym of xyms){
const amountMark = getAmountMark(xym);
console.log("未承認" + tx.message.payload.slice(0,14));
console.log(tx);
let unconfirmedMark = "未承認";
if (tx.maxFee.compact() / tx.size < 100){
unconfirmedMark = "🐢 " + unconfirmedMark;
}
$("#rollback").prepend("<tr>"
+ "<td id=" + tx.transactionInfo.hash + ">" + unconfirmedMark + "</td>"
+ "<td><span id='statehash" + tx.transactionInfo.hash + "'>● </span>" + tx.signer.address.pretty().slice(0,6) + "..." + tx.signer.address.pretty().slice(-3) + "</td>"
+ "<td>" + tx.message.payload.slice(0,14) + "</td>"
+ "<td>" + amountMark + "</td>"
+ "</tr>"
);
$("#statehash" + tx.transactionInfo.hash).css("color","gray");
$("#statehash" + tx.transactionInfo.hash).addClass("unconfirmed");
}
})
listener.confirmed(address)
.pipe(
op.filter(tx=> address.plain() === tx.recipientAddress.plain())
)
.subscribe(async tx=>{
if(tx.type === nem.TransactionType.TRANSFER){
const txId = tx.transactionInfo.hash;
console.log("承認済"+tx.message.payload.slice(0,14));
if($("#" + tx.transactionInfo.hash).length){
//承認日付に変更
$("#" + tx.transactionInfo.hash).html(
"<a target='_blank' href='" + explorer + "/transactions/" + tx.transactionInfo.hash + "'>"
+ "<span id='date" + txId + "'></span></a>");
}else{
//接続ノードが未承認を検知できずに承認が発生した場合(超低手数料での送金が承認された場合)
const xyms = tx.mosaics.filter(item=> [currencyNamespaceId,currencyId].includes(item.id.toHex()));
for(const xym of xyms){
const amountMark = getAmountMark(xym);
$("#rollback").prepend("<tr>"
+ "<td id=" + tx.transactionInfo.hash + "><a target='_blank' href='" + explorer + "/transactions/" + tx.transactionInfo.hash + "'>"+ "<span id='date" + txId + "'></span></a></td>"
+ "<td><span id='statehash" + tx.transactionInfo.hash + "'>● </span>" + tx.signer.address.pretty().slice(0,6) + "..." + tx.signer.address.pretty().slice(-3) + "</td>"
+ "<td>" + tx.message.payload.slice(0,14) + "</td>"
+ "<td>" + amountMark + "</td>"
+ "</tr>"
);
}
}
$("#statehash" + tx.transactionInfo.hash).addClass("state" + tx.transactionInfo.height); //高さ情報クラスを追加(ファイナライズ・ロールバック制御)
$("#statehash" + tx.transactionInfo.hash).removeClass("unconfirmed"); //高さ情報クラスを追加(ファイナライズ・ロールバック制御)
$("#statehash" + tx.transactionInfo.hash).css("color","brown");
dispBlockTimeStamp("#date"+ txId,tx.transactionInfo);
}
})
listener.newBlock().subscribe(async block=>{
newBlockHash = block.hash; //活性チェック用
getNewInfo(block);
});
listener.finalizedBlock().subscribe(async block=>{
fintime = (new Date()).getTime();
lastFinalizedTimestamp = (new Date()).getTime();
console.log(fintime - lastFinalizedTimestamp);
console.log((fintime - lastFinalizedTimestamp) / 1000 / 60);
lastFinalizedTimestamp = fintime;
console.log("listener finalized block");
for (let h = lastFinalizedHeight + 1; h <= block.height.compact(); h++){
console.log("finalize:" + h);
$(".state" + h).each((i,o)=>{
console.log(o);
$(o).css("color","green");
$(o).removeClass("state" + h);
});
}
lastFinalizedHeight = block.height.compact();
});
}
function getNewInfo(block){
const height = block.height;
if(lastHeight !== 0 && lastHeight + 1 !== height.compact() ){
console.log("roll back from:" +block.height.compact() );
console.log("toll back to:" +lastHeight );
for (let h = block.height.compact() ; h <= lastHeight; h++){
console.log("roll back:" + h);
$(".state" + h).each((i,o)=>{
console.log(o);
$(o).css("color","red");
$(o).removeClass("state" + h);
});
}
}else{
$(".state" + (block.height.compact() - 2).toString()).each((i,o)=>{
$(o).css("color","orange");
});
}
lastHeight = height.compact();
console.log("==============" + block.height);
chainRepo.getChainInfo().subscribe(chain=>{
//次回ファイナライズ予定
if(lastFinalizedTimestamp != 0){
remainMinutes = 10 - ((new Date()).getTime() - lastFinalizedTimestamp) / 1000 / 60;
remainHeight = remainMinutes * 2;
}else{
remainHeight = 38 - (height.compact() - chain.latestFinalizedBlock.height.compact());
remainMinutes = remainHeight / 2
console.log("remainMinutes:" + remainMinutes);
console.log("remainHeight:" + remainHeight);
}
blockRepo.getBlockByHeight(chain.latestFinalizedBlock.height)
.subscribe(finalizedBlock=>{
t3 = dispTimeStamp(finalizedBlock.timestamp.compact(),epochAdjustment)
// $("#finalize_time").text(finalizedBlock.height.toString() );
$("#finalize_range").text(t3 );
t2 = dispTimeStamp(finalizedBlock.timestamp.compact() + (20 * 30000),epochAdjustment)
$("#finalize_next_time").text(remainMinutes.toFixed(1));
$("#finalize_next_range").text(t2.slice(-5));
if(lastFinalizedHeight === 0 ){
console.log(">>>init finalize!!");
lastFinalizedHeight = chain.latestFinalizedBlock.height.compact();
}
})
});
}
$("#jpy_price").blur(function() {showAmountInfo()});
$("#xembook").attr("href", "index.html?address=" + rawAddress);
$("#submit_price").click(function(){
showAmountInfo();
alert("QRコードを出力します" );
tx = nem.TransferTransaction.create(
nem.Deadline.create(epochAdjustment),
address,
[networkCurrency.createRelative(Number($("#xem_price").text()))],
nem.PlainMessage.create($("#cnumber").val()),
networkType
);
qrcode = qr.QRCodeGenerator.createTransactionRequest(tx,networkType,generationHash);
qrcode.toBase64().subscribe(x=>$("#receiverqr").html('<img src=' + x + '>'))
});
</script>
</body>
</html>