-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
671 lines (557 loc) · 28.6 KB
/
index.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
<!DOCTYPE html>
<html lang="en">
<head>
<title>Divvy</title>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<!-- development version, includes helpful console warnings -->
<!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
<!-- production version, optimized for size and speed -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="ethers.min.js"
charset="utf-8"
type="text/javascript"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
td a {
display: block;
text-decoration: none;
}
form label {
display: block;
}
img.preview-image {
max-height: 100%;
max-width: 100%;
margin-top: 10px;
margin-bottom: 10px;
}
.help-text a {
font-size: 1.2rem;
color: red;
}
</style>
</head>
<body>
<div id="app" class="container">
<div class="mb-2">
<a :href="$buildUrl({}, false)" style="text-decoration: none;"><h1 class="d-inline mr-2">Divvy</h1></a><p class="d-inline help-text"><a :href="$buildUrl({page: 'help'}, false)">Help</a></p>
</div>
<div v-if="$readOnly">
<p class="text-danger">MetaMask was not detected, so you are in read-only mode.</p>
</div>
<div v-if="page === Page.HOME">
<new-post-form class="d-inline" v-if="!$readOnly" :default-sub="sub"></new-post-form>
<form class="d-inline float-right">
<label>Sub:
<input type="text" :value="sub" ref="sub-box"></input>
<button @click="subGo($event)" type="submit">Go</button>
</label>
</form>
<table>
<tr>
<th>Date/Time</th>
<th>Title</th>
<th>Rating</th>
<th>Replies</th>
</tr>
<tr v-for="post in sortedPosts" :key="post.postId">
<td><a :href="$buildUrl({postId: post.postId}, false)">{{ $formatTimestamp(post.createdAt) }}</a></td>
<td><a :href="$buildUrl({postId: post.postId}, false)">{{ $truncateString(post.title) }}</a></td>
<td><a :href="$buildUrl({postId: post.postId}, false)">{{ post.upvotes - post.downvotes }}</a></td>
<td><a :href="$buildUrl({postId: post.postId}, false)">{{ post.replyIds.length }}</a></td>
</tr>
</table>
<div class="d-block clearfix mt-2">
<button @click="nextClick" class="float-right">Next</button>
<button @click="previousClick" v-if="offset > 0" class="float-right">Previous</button>
</div>
<div class="border border-secondary mt-3 p-2">
<p>Recently active subs:</p>
<ul>
<li v-for="subName in recentlyActiveSubs"><a :href="$buildUrl({s: subName})">{{ subName }}</a></li>
</ul>
</div>
</div>
<div v-else-if="page === Page.VIEWPOST">
<view-post-page :post-id="postId"></view-post-page>
</div>
<div v-else-if="page === Page.HELP" class="border border-secondary mt-3 p-3">
<h2>Useful information:</h2>
<p>You (probably) need MetaMask to do posts, replies, votes, etc. You should still be able to view everything even without it.</p>
<p>In posts you can:</p>
<ol>
<li>Prefix a URL with <code>img:</code> to show a preview. <code>E.g. img:https://ipfs.io/ipfs/QmUwHMFY9GSiKgjqyZpgAv2LhBrh7GV8rtLuagbry9wmMU</code></li>
<li>Previews are shown automatically for URLs ending in .jpg, .jpeg, .png, and .gif.</li>
<li>Prefix an Ethereum transaction hash with <code>eth-tx-img:</code> to embed the image stored inside the transaction input. <code style="white-space: pre;">E.g. eth-tx-img:0x1013929251687ff7f6a7e2e0c7e3caae6fd93a2342dd5ad9793ff33c5802269f</code></li>
</ol>
<p>If you save a local copy of this page then you should still be able to view everything, however MetaMask cannot interact with the file:// protocol.</p>
<p>All the front-end code is in this single file, so you can edit it directly.</p>
</div>
</div>
<script>
function convertTimestamp(timestamp){
let date = new Date(timestamp*1000);
return date.toLocaleString();
}
function reverseEnum(e, value) {
const keys = Object.keys(e);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const ev = e[key];
if (ev === value) {
return key;
}
}
}
function hexlifyString(str) {
return ethers.utils.hexlify(ethers.utils.toUtf8Bytes(str));
}
function hexToBase64(hexstring) {
return btoa(hexstring.match(/\w{2}/g).map(function(a) {
return String.fromCharCode(parseInt(a, 16));
}).join(""));
}
const CONTRACT_ADDRESS = '0x8D3AbD6664bdf0abfA5cf5344cf0F81f2f34d79D';
const CONTRACT_ABI = [{"constant":false,"inputs":[{"internalType":"string","name":"title","type":"string"},{"internalType":"bytes","name":"content","type":"bytes"},{"internalType":"bytes32","name":"subName","type":"bytes32"}],"name":"createPost","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"parentPostId","type":"uint256"},{"internalType":"bytes","name":"content","type":"bytes"}],"name":"createReply","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"postId","type":"uint256"}],"name":"downvotePost","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"replyId","type":"uint256"}],"name":"downvoteReply","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"}],"name":"getPost","outputs":[{"internalType":"uint256","name":"createdAt","type":"uint256"},{"internalType":"uint256","name":"postId","type":"uint256"},{"internalType":"string","name":"title","type":"string"},{"internalType":"address","name":"author","type":"address"},{"internalType":"bytes","name":"content","type":"bytes"},{"internalType":"uint256","name":"upvotes","type":"uint256"},{"internalType":"uint256","name":"downvotes","type":"uint256"},{"internalType":"bytes32","name":"subName","type":"bytes32"},{"internalType":"uint256[]","name":"replyIds","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"subName","type":"bytes32"},{"internalType":"uint256","name":"offset","type":"uint256"},{"internalType":"uint256","name":"limit","type":"uint256"}],"name":"getRecentPostIdsInSub","outputs":[{"internalType":"uint256[]","name":"postIds","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getRecentlyActiveSubs","outputs":[{"internalType":"bytes32[]","name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"replies","outputs":[{"internalType":"uint256","name":"createdAt","type":"uint256"},{"internalType":"uint256","name":"replyId","type":"uint256"},{"internalType":"uint256","name":"parentPostId","type":"uint256"},{"internalType":"address","name":"author","type":"address"},{"internalType":"bytes","name":"content","type":"bytes"},{"internalType":"uint256","name":"upvotes","type":"uint256"},{"internalType":"uint256","name":"downvotes","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"subCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalPosts","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalReplies","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"postId","type":"uint256"}],"name":"upvotePost","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"replyId","type":"uint256"}],"name":"upvoteReply","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}];
var provider;
var signer = null;
// if (typeof window.web3 !== 'undefined' && typeof window.ethereum !== 'undefined') {
// // MetaMask injected
// provider = new ethers.providers.Web3Provider(web3.currentProvider);
// signer = provider.getSigner();
//
// window.ethereum.enable();
// } else {
// provider = ethers.getDefaultProvider();
// }
//
// const readOnly = signer === null;
//
//
// var contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider);
// if (!readOnly) {
// contract = contract.connect(signer);
// }
var readOnly;
var contract;
const Page = {"HOME": 0, "VIEWPOST": 1, "HELP": 2};
Object.freeze(Page);
const IMAGE_REGEX = /(?=(?:img:)?|.*(?:\.(?:jpg|jpeg|png|gif)))((?:(?:f|ht)tp(?:s)?:\/\/)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)/gi;
Vue.component('new-post-form', {
props: {
defaultSub: {required: false, default: ''}
},
data() {
return {
showing: false,
sub: this.defaultSub,
title: '',
body: '',
link: '',
ipfs: ''
}
},
methods: {
async submitted(e) {
e.preventDefault();
const content = hexlifyString(this.body);
const tx = await contract.createPost(this.title, content, ethers.utils.formatBytes32String(this.sub));
alert("Post sent successfully. PLEASE BE PATIENT AS IT CAN TAKE A FEW MINUTES FOR POSTS TO CONFIRM AND SHOW UP.");
this.showing = false;
await tx.wait();
document.location = this.$buildUrl({s: this.sub}, false);
},
canceled(e) {
e.preventDefault();
this.showing = false;
}
},
template: `
<div>
<button @click="showing=!showing">New Post</button>
<div v-show="showing" class="m-3">
<form>
<label>Sub: <input type="text" size="30" v-model="sub"></input></label>
<label>Title: <input type="text" size="50" v-model="title"></input></label>
<label>Body:</label>
<textarea cols="100" rows="8" v-model="body" class="d-block"></textarea>
<button @click="submitted($event)">Save</button>
<button @click="canceled($event)">Cancel</button>
</form>
</div>
</div>
`
});
Vue.component('new-reply-form', {
props: ['parentPostId'],
data() {
return {
showing: false,
body: ''
}
},
methods: {
async submitted(e) {
e.preventDefault();
const content = hexlifyString(this.body);
const tx = await contract.createReply(this.parentPostId, content);
alert("Reply created successfully. PLEASE BE PATIENT AS IT CAN TAKE A FEW MINUTES FOR REPLIES TO CONFIRM AND SHOW UP.");
this.showing = false;
await tx.wait();
this.body = '';
},
canceled(e) {
e.preventDefault();
this.showing = false;
}
},
template: `
<div>
<button @click="showing=!showing">Reply</button>
<div v-show="showing" class="m-3">
<form>
<label>Message:</label>
<textarea cols="100" rows="8" v-model="body" class="d-block"></textarea>
<button @click="submitted($event)">Save</button>
<button @click="canceled($event)">Cancel</button>
</form>
</div>
</div>
`
});
Vue.component('post', {
props: ['post', 'reply', 'index'],
data() {
return {
previews: [],
showPreviews: false
};
},
computed: {
contentText() {
return ethers.utils.toUtf8String(this.post ? this.post.content : this.reply.content);
}
},
methods: {
replaceLinks() {
const original = this.$refs['post-text'].innerHTML;
const newTxt = original.replace(/(((f|ht)tp(s)?:\/\/)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)/gi, '<a href="$1" target="_blank">$1</a>');
this.$refs['post-text'].innerHTML = newTxt;
},
loadPreviews() {
const imageMatches = Array.from(new Set([...this.contentText.matchAll(IMAGE_REGEX)].map((match) => match[0])));
imageMatches.forEach((imageUrl) => {
this.previews.push(`<a href="${imageUrl}" target="_blank"><img src="${imageUrl}" class="preview-image" alt="Preview image" /></a>`)
});
const imageTxs = Array.from(new Set([...this.contentText.matchAll(/eth-tx-img:(0x[A-Fa-f0-9]{64})/gi)].map((match) => match[1])));
imageTxs.forEach((txHash) => {
provider.getTransaction(txHash).then((tx) => {
if (tx !== null) {
const hex = tx.data.slice(2);
const b64 = hexToBase64(hex);
this.previews.push(`<a href="https://etherscan.io/tx/${txHash}" target="_blank"><img src="data:image/jpeg;base64, ${b64}" class="preview-image" alt="Preview image" /></a>`)
}
});
});
},
async upvote() {
let tx;
if (this.post !== undefined) {
tx = await contract.upvotePost(this.post.postId);
} else {
tx = await contract.upvoteReply(this.reply.replyId);
}
alert("Transaction sent successfully.");
},
async downvote() {
let tx;
if (this.post !== undefined) {
tx = await contract.downvotePost(this.post.postId);
} else {
tx = await contract.downvoteReply(this.reply.replyId);
}
alert("Transaction sent successfully.");
}
},
mounted() {
this.replaceLinks();
this.loadPreviews();
},
template: `
<div class="border border-secondary mt-3 mb-3 p-1">
<div v-if="post !== undefined">
<!-- original post -->
<p class="d-inline font-weight-bold">{{ post.title }}</p>
<p class="d-inline" v-if="post.subName !== ''">in <a :href="$buildUrl({s: post.subName}, false)">{{ post.subName }}</a></p>
<p class="d-inline"> {{ post.upvotes - post.downvotes }} points <span v-if="!$readOnly">(<a href="javascript:void(0)" @click="upvote">Upvote</a> <a href="javascript:void(0)" @click="downvote">Downvote</a>)</span></p>
<a :href="'https://etherscan.io/address/' + post.author" target="_blank"><p class="d-inline">{{ post.author }}</p></a>
<p class="d-inline">{{ $formatTimestamp(post.createdAt) }}</p>
<p class="d-inline">{{ post.replyIds.length }} replies</p>
<p class="d-inline float-right font-weight-bold">#{{ index }}</p>
</div>
<div v-else>
<!-- reply -->
<p class="d-inline"> {{ reply.upvotes - reply.downvotes }} points <span v-if="!$readOnly">(<a href="javascript:void(0)" @click="upvote">Upvote</a> <a href="javascript:void(0)" @click="downvote">Downvote</a>)</span></p>
<a :href="'https://etherscan.io/address/' + reply.author" target="_blank"><p class="d-inline">{{ reply.author }}</p></a>
<p class="d-inline">{{ $formatTimestamp(reply.createdAt) }}</p>
<p class="d-inline float-right font-weight-bold">#{{ index }}</p>
</div>
<div class="p-2">
<p><span style="white-space: pre;" ref="post-text">{{ contentText }}</span></p>
<div v-if="showPreviews">
<!-- Previews -->
<div v-for="preview in previews" v-html="preview"></div>
</div>
<div v-else-if="previews.length > 0">
<p><a href="javascript:void(0)" @click="showPreviews=true" style="color: green;">Click here to load image previews!</a><span class="text-danger" style="font-size: 0.8rem;"> Loading previews can leak your IP to the image host.</span></p>
</div>
</div>
<div v-if="!$readOnly">
<div v-if="post !== undefined">
<new-reply-form :parentPostId="post.postId"></new-reply-form>
</div>
<div v-else>
<new-reply-form :parentPostId="reply.parentPostId"></new-reply-form>
</div>
</div>
</div>
`
});
Vue.component('view-post-page', {
template: `
<div v-if="post !== null && replies.length === post.replyIds.length">
<post :post="post" index="1"></post>
<post v-for="(reply, index) in sortedReplies" :key="reply.replyId" :reply="reply" :index="index+2"></post>
</div>
`,
props: {
postId: { required: true }
},
data() {
return {
post: null,
replies: []
};
},
computed: {
sortedReplies() {
return this.replies.sort((a, b) => {
return b.createdAt - a.createdAt;
});
}
},
async mounted() {
const post = await contract.getPost(this.postId);
this.post = {
createdAt: post.createdAt.toNumber(),
postId: post.postId.toNumber(),
title: post.title,
author: post.author,
content: post.content,
upvotes: post.upvotes,
downvotes: post.downvotes,
subName: ethers.utils.parseBytes32String(post.subName),
replyIds: post.replyIds
};
// Load the replies
this.post.replyIds.forEach(async (replyId) => {
const reply = await contract.replies(replyId);
this.replies.push({
createdAt: post.createdAt.toNumber(),
replyId: reply.replyId.toNumber(),
author: reply.author,
parentPostId: reply.parentPostId.toNumber(),
content: reply.content,
upvotes: reply.upvotes,
downvotes: reply.downvotes
});
});
},
});
Vue.prototype.$readOnly = readOnly;
Vue.prototype.$buildUrl = function(paramsObject, preserve=true) {
const keys = Object.keys(paramsObject);
const origin = window.location.origin + window.location.pathname;
let usp;
if (preserve) {
usp = new URLSearchParams(window.location.search);
} else {
usp = new URLSearchParams();
}
keys.forEach((key) => {
usp.set(key, paramsObject[key]);
});
// Set a random value to bust the cache
usp.set('cachebust', Math.floor(Math.random() * 1000000000000));
return origin + '?' + usp.toString();
}
Vue.prototype.$truncateString = function(str, length, ending) {
if (length == null) {
length = 100;
}
if (ending == null) {
ending = '...';
}
if (str.length > length) {
return str.substring(0, length - ending.length) + ending;
} else {
return str;
}
};
Vue.prototype.$formatTimestamp = convertTimestamp;
var app;
function createApp() {
app = new Vue({
el: '#app',
data () {
return {
posts: [],
recentlyActiveSubs: [],
Page
}
},
computed: {
pathname() {
return window.location.pathname;
},
origin() {
return window.location.origin;
},
urlParams () {
return new URLSearchParams(window.location.search);
},
page() {
if (this.urlParams.has('postId')) {
return Page.VIEWPOST;
} else if (this.urlParams.has('page') && this.urlParams.get('page') === 'help') {
return Page.HELP;
}
return Page.HOME;
},
postId() {
return parseInt(this.urlParams.get('postId'));
},
sub() {
return this.urlParams.has('s') ? this.urlParams.get('s') : '';
},
offset() {
return this.urlParams.has('offset') ? parseInt(this.urlParams.get('offset')) : 0;
},
limit() {
return this.urlParams.has('limit') ? parseInt(this.urlParams.get('limit')) : 20;
},
sortedPosts() {
return this.posts.sort((a, b) => {
return b.createdAt - a.createdAt;
})
}
},
methods: {
async loadPosts() {
contract.getRecentPostIdsInSub(ethers.utils.formatBytes32String(this.sub), this.offset, this.limit).then((result) => {
result.forEach((postId) => {
contract.getPost(postId).then((post) => {
this.posts.push({
createdAt: post.createdAt.toNumber(),
postId: post.postId.toNumber(),
title: post.title,
author: post.author,
content: post.content,
upvotes: post.upvotes,
downvotes: post.downvotes,
subName: post.subName,
replyIds: post.replyIds
});
});
});
});
},
subGo(e) {
e.preventDefault();
const newSub = this.$refs['sub-box'].value;
document.location = this.$buildUrl({s: newSub}, false);
},
nextClick() {
document.location = this.$buildUrl({offset: this.offset + this.limit});
},
previousClick() {
document.location = this.$buildUrl({offset: this.offset - this.limit});
},
fetchRecentlyActiveSubs() {
this.recentlyActiveSubs = [];
contract.getRecentlyActiveSubs().then((result) => {
const subNames = result.map((bytes32) => ethers.utils.parseBytes32String(bytes32)).filter((name) => name !== '');
subNames.forEach((name) => {
if (!this.recentlyActiveSubs.includes(name)) {
this.recentlyActiveSubs.push(name);
}
});
});
}
},
mounted () {
this.loadPosts();
this.fetchRecentlyActiveSubs();
}
});
}
window.addEventListener('load', async () => {
let useDefaultProvider = true;
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.enable();
// Acccounts now exposed
useDefaultProvider = false;
} catch (error) {
// User denied account access...
useDefaultProvider = true;
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
// Acccounts always exposed
useDefaultProvider = false;
}
// Non-dapp browsers...
else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
useDefaultProvider = true;
}
if (useDefaultProvider) {
readOnly = true;
Vue.prototype.$readOnly = readOnly;
provider = ethers.getDefaultProvider();
} else {
provider = new ethers.providers.Web3Provider(web3.currentProvider);
signer = provider.getSigner();
readOnly = false;
Vue.prototype.$readOnly = readOnly;
}
contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider);
if (!useDefaultProvider) {
contract = contract.connect(signer);
}
createApp();
});
</script>
</body>
</html>