-
Notifications
You must be signed in to change notification settings - Fork 8
/
list.html
600 lines (556 loc) · 25.9 KB
/
list.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Crypto red packet with password to open, protected by zk-proof.">
<title>Red Packet 🧧</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<script src="/static/js/blockchains.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ethers.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/snarkjs.min.js"></script>
<style>
body {
height: 100vh;
background-size: cover;
background-image: linear-gradient(30deg, #9943c7 0%, #C850C0 20%, #ffa770 100%);
}
.fs-7 {
font-size: 0.875rem;
}
a {
text-decoration: none;
}
</style>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-G71EH5JJVS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-G71EH5JJVS');
</script>
<script>
window.ERC20_ABI = '[{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]';
class Token {
#address;
#symbol;
#decimals;
constructor(address, symbol, decimals) {
this.#address = address;
this.#symbol = symbol;
this.#decimals = decimals;
}
get address() {
return this.#address;
}
get symbol() {
return this.#symbol;
}
get decimals() {
return this.#decimals;
}
}
class RedPacket {
chain;
id;
creator; // creator address
token; // token address
amount;
total;
bonusType; // 0=identical, 1=random
symbol = '';
decimals = NaN;
constructor(chain, id, creator, token, amount, total, bonusType) {
this.chain = chain;
this.id = id;
this.creator = creator;
this.token = token;
this.amount = amount;
this.total = total;
this.bonusType = bonusType;
}
setErcInfo(symbol, decimals) {
this.symbol = symbol;
this.decimals = decimals;
}
#BN_1K = ethers.utils.parseUnits('1000', 0);
#BN_1M = ethers.utils.parseUnits('1000000', 0);
#BN_1B = ethers.utils.parseUnits('1000000000', 0);
#intBN(bn, decimals) {
for (let i = 0; i < decimals; i++) {
bn = bn.div(10);
}
return bn;
}
abbrBN(bn, decimals, precision = 5) {
if (this.#intBN(bn, decimals).lt(this.#BN_1K)) {
let s = ethers.utils.formatUnits(bn, decimals);
return s.substr(0, 6);
}
bn = bn.div(1000);
if (this.#intBN(bn, decimals).lt(this.#BN_1K)) {
let s = ethers.utils.formatUnits(bn, decimals);
return s.substr(0, 6) + 'K';
}
bn = bn.div(1000);
if (this.#intBN(bn, decimals).lt(this.#BN_1K)) {
let s = ethers.utils.formatUnits(bn, decimals);
return s.substr(0, 6) + 'M';
}
bn = bn.div(1000);
if (this.#intBN(bn, decimals).lt(this.#BN_1K)) {
let s = ethers.utils.formatUnits(bn, decimals);
return s.substr(0, 6) + 'B';
}
bn = bn.div(1000);
let s = ethers.utils.formatUnits(bn, decimals);
let n = s.indexOf('.');
if (n < 0) {
return s + 'T';
}
return s.substring(0, n) + 'T';
}
abbrAddress(address, chars = 4) {
let addr = ethers.utils.getAddress(address);
return addr.substring(0, chars + 2) + '...' + addr.substring(addr.length - chars);
}
}
function toBN(s, decimals) {
try {
return ethers.utils.parseUnits(s.trim(), decimals);
} catch (e) {
return null;
}
}
function isValidBN(s, decimals) {
let bn = toBN(s, decimals);
return bn !== null && !bn.isZero() && !bn.isNegative();
}
function isValidAddress(s) {
try {
ethers.utils.getAddress(s);
return true;
} catch (e) {
return false;
}
}
function getRpcProvider(rpcUrl) {
return new ethers.providers.JsonRpcProvider({
url: rpcUrl,
timeout: 20000
});
}
function getWeb3Provider() {
if (!window.web3Provider) {
if (!window.ethereum) {
console.error("there is no web3 provider.");
return null;
}
window.web3Provider = new ethers.providers.Web3Provider(window.ethereum, "any");
}
return window.web3Provider;
}
function showAlert(title, message) {
let m = $('#alertModal');
m.find('.x-title').text(title);
m.find('.x-message').text(message);
let myModal = new bootstrap.Modal(m.get(0), { backdrop: 'static', keyboard: false });
myModal.show();
}
function translateError(err) {
window.err = err;
if (typeof (err) === 'string') {
return err;
}
if (err.error && err.error.code && err.error.message) {
return `Error (${err.error.code}): ${err.error.message}`;
}
if (err.code && err.message) {
return `Error (${err.code}): ${err.message}`;
}
return err.message || err.toString();
}
function init() {
console.log('init vue...');
window.vm = new Vue({
el: '#vm',
data: {
account: null,
chainId: 0,
selectedChainId: 137,
loaded: false,
loading: false,
redPackets: [],
version: 0
},
computed: {
ready: function () {
return this.account && window.BLOCKCHAINS[this.chainId];
},
networkName: function () {
if (this.account) {
let c = window.BLOCKCHAINS[this.chainId];
if (c) {
return c.name;
}
return 'Unsupported Network (0x' + this.chainId.toString(16) + ')';
}
return 'Not Connected';
}
},
methods: {
postJson: async function (url, data) {
let opt = {
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: url,
data: JSON.stringify(data)
};
return await $.ajax(opt);
},
loadRedPackets: async function () {
if (this.loaded) {
return;
}
this.version++;
this.redPackets = [];
console.log('start load redpackets...');
this.loading = true;
let currentChain = this.selectedChainId;
let currentVersion = this.version;
try {
let query = {
query:
`{
creates(first: 20, orderBy: redPacketId, orderDirection: desc) {
id
redPacketId
creator
token
amount
total
bonusType
}
}`
};
let list = await this.postJson('https://api.thegraph.com/subgraphs/name/michaelliao/redpacket', query);
console.log(list);
let rps = [];
for (let c of list.data.creates) {
rps.push(new RedPacket(
currentChain,
c.redPacketId,
c.creator,
c.token,
c.amount,
c.total,
c.bonusType
));
}
this.redPackets = rps;
this.version++;
this.loaded = true;
} catch (err) {
console.error(err);
}
this.loading = false;
},
tokenIconUrl: function (addr) {
let c = window.BLOCKCHAINS[this.selectedChainId];
if (c) {
t = c.tokens.find(t => t.address === addr.toLowerCase());
if (t) {
return t.icon;
}
}
return '/static/icons/default.svg';
},
tokenUrl: function (addr) {
let c = window.BLOCKCHAINS[this.selectedChainId];
if (c) {
if (addr.toLowerCase() === window.ETH_ADDRESS) {
return c.scan;
}
return c.scan + '/token/' + addr;
} else {
return '#1';
}
},
tokenSymbol: function (addr) {
let c = window.BLOCKCHAINS[this.selectedChainId];
if (c) {
let token = c.tokens.find(t => t.address === addr.toLowerCase());
if (token) {
return token.symbol;
}
}
return this.abbrAddress(addr);
},
scanUrl: function (addr) {
let c = window.BLOCKCHAINS[this.selectedChainId];
if (c) {
return c.scan + '/address/' + addr;
} else {
return '#1';
}
},
gotoScanUrl: function (addr) {
let c = window.BLOCKCHAINS[this.chainId];
if (c) {
window.open(c.scan + '/address/' + addr);
} else {
console.error('Invalid chain id: ', this.chainId);
}
},
abbrAddress: function (addr) {
if (!addr) {
return '';
}
let s = addr.toString();
if (s.indexOf('0x') === 0 && s.length === 42) {
let addr = ethers.utils.getAddress(s.substring(0));
return addr.substring(0, 6) + '...' + addr.substring(38);
}
return s;
},
accountChanged: function (accounts) {
console.log('wallet account changed:', accounts.length === 0 ? null : accounts[0]);
if (accounts.length === 0) {
this.disconnected();
} else {
this.account = accounts[0];
document.cookie = '__account__=' + this.account + ';max-age=1296000';
}
},
disconnected: async function () {
console.warn('wallet disconnected.');
this.account = null;
},
chainChanged: async function (chainId) {
console.log('wallet chainId changed: ' + chainId + ' = ' + parseInt(chainId, 16));
this.chainId = parseInt(chainId, 16);
},
connectWallet: async function () {
console.log('try connect wallet...');
if (window.getWeb3Provider() === null) {
console.error('there is no web3 provider.');
return false;
}
try {
this.accountChanged(await window.ethereum.request({
method: 'eth_requestAccounts',
}));
this.chainChanged(await window.ethereum.request({
method: 'eth_chainId'
}));
window.ethereum.on('disconnect', this.disconnected);
window.ethereum.on('accountsChanged', this.accountChanged);
window.ethereum.on('chainChanged', this.chainChanged);
} catch (e) {
console.error('could not get a wallet connection.', e);
return false;
}
console.log('wallet connected.');
return true;
}
},
mounted: async function () {
$('#vm').show();
await this.connectWallet();
await this.loadRedPackets();
}
});
}
$(function () {
init();
});
</script>
</head>
<body>
<!-- Input Modal -->
<div id="inputModal" class="modal fade" role="dialog" aria-hidden="true" aria-labelledby="inputLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header align-items-center d-flex">
<h4 class="modal-title" id="inputLabel">Password</h4>
</div>
<div class="modal-body">
<form onsubmit="return false">
<div class="mb-3">
<label for="inputPassword" class="form-label">Password for this red packet:</label>
<input id="inputPassword" type="text" maxlength="100" class="form-control">
<div class="form-text">Input the password of the red packet set by creator.</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">OK</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"
aria-label="Close">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Loading Modal -->
<div id="loadingModal" class="modal fade" role="dialog" aria-hidden="true" aria-labelledby="loadingLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header align-items-center d-flex">
<h4 class="modal-title x-title" id="loadingLabel"> </h4>
</div>
<div class="modal-body">
<div class="float-start">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<div class="ms-5 ps-4 float-none">
<p class="x-message"> </p>
</div>
</div>
<!-- <div class="modal-footer">
<button class="btn btn-outline-primary" data-bs-dismiss="modal" aria-label="Close">Cancel</button>
</div> -->
</div>
</div>
</div>
<!-- Alert Modal -->
<div id="alertModal" class="modal fade" role="dialog" aria-hidden="true" aria-labelledby="alertLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header align-items-center d-flex">
<h4 class="modal-title x-title" id="alertLabel"> </h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><i class="fe fe-x-circle"></i></span>
</button>
</div>
<div class="modal-body">
<div class="float-start">
<i class="fs-2 text-danger fe fe-alert-triangle"></i>
</div>
<div class="ms-5 ps-4 float-none">
<p class="x-message"> </p>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-outline-primary" data-bs-dismiss="modal" aria-label="Close">OK</button>
</div>
</div>
</div>
</div>
<!-- Info Modal -->
<div id="infoModal" class="modal fade" role="dialog" aria-hidden="true" aria-labelledby="infoLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header align-items-center d-flex">
<h4 class="modal-title x-title" id="infoLabel"> </h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><i class="fe fe-x-circle"></i></span>
</button>
</div>
<div class="modal-body">
<div class="float-start">
<i class="fs-2 fe fe-info"></i>
</div>
<div class="ms-5 ps-4 float-none">
<div class="x-message"> </div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-outline-primary" data-bs-dismiss="modal" aria-label="Close">OK</button>
</div>
</div>
</div>
</div>
<div id="vm" class="container" style="display:none">
<nav class="navbar navbar-expand-lg navbar-light"
style="position:fixed; top:0; left:0; right:0; z-index: 99; background-image: url(/static/img/nav.png); background-size: cover;">
<div class="container">
<a class="navbar-brand text-white" href="/" target="_blank">Create Red Packet</a>
<ul class="mr-2 navbar-nav">
<li class="nav-item">
<span class="nav-link active text-white">
<i class="bi bi-list-ul"></i>
Explore
</span>
</li>
</ul>
<ul id="wallet" class="mr-2 navbar-nav navbar-right-wrap" style="flex-direction: row !important;">
<li class="nav-item">
<span class="nav-link text-white"><i class="bi bi-globe"></i>
<span v-text="networkName"></span> </span>
</li>
<li v-if="account===null" class="nav-item">
<button v-on:click="connectWallet" type="button" class="btn btn-light">Connect Wallet</button>
</li>
<li v-if="account!==null" class="nav-item">
<a href="#0" class="nav-link text-white" v-on:click="gotoScanUrl"><i class="bi bi-wallet"></i>
<span v-text="abbrAddress(account)"></span></a>
</li>
</ul>
</div>
</nav>
<div class="pb-5" style="padding-top: 80px;">
<div class="row g-0 mt-4">
<div class="col-12 mb-4">
<select class="form-select" style="width:30%">
<option selected>Polygon</option>
</select>
</div>
<div v-show="loading" class="col-12 mb-4">
<div class="spinner-border text-white"></div>
</div>
<div v-show="loaded" class=" col-12 mb-4">
<table class="table text-white">
<thead>
<tr>
<th scope="col"># ID</th>
<th scope="col">Type</th>
<th scope="col">Token</th>
<th scope="col">Participates</th>
<th scope="col">Creator</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="rp in redPackets">
<td v-text="rp.id"></td>
<td v-text="rp.bonusType==0?'Identical':'Random'"></td>
<td>
<a v-bind:href="tokenUrl(rp.token)" target="_blank" class="text-white"><span
v-text="tokenSymbol(rp.token)"></span>
<i class="bi bi-box-arrow-up-right"></i></a>
</td>
<td v-text="rp.total"></td>
<td>
<a v-bind:href="scanUrl(rp.creator)" target="_blank" class="text-white"><span
v-text="abbrAddress(rp.creator)"></span>
<i class="bi bi-box-arrow-up-right"></i></a>
</td>
<td>
<a v-bind:href="'rp.html?chain='+rp.chain+'&id='+rp.id" target="_blank"
class="text-white">
🧧 Try Open! <i class="bi bi-box-arrow-up-right"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-12">
<div class="alert alert-warning text-center mb-4" role="alert">
Please note the query service is only available on Polygon now. Query services on other
chains are still under construction.
</div>
</div>
</div>
</div>
</div>
</body>
</html>