forked from wighawag/blockies-nft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
311 lines (283 loc) · 8.5 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
<html>
<head>
<title>Inspect NFT</title>
<style>
* {
background-color: #111111;
color: wheat;
margin: 0;
padding: 0;
}
p,
h1 {
margin: 1em 1em 1em 1em;
}
:root {
--width: 80vw;
}
#wrapper {
text-align: center;
height: 100%;
margin: 0;
}
#nft-image {
width: 30%;
height: auto;
}
@media only screen and (max-width: 1000px) {
#nft-image {
width: 60%;
height: auto;
}
}
@media only screen and (max-width: 600px) {
#nft-image {
width: 90%;
height: auto;
}
}
@media only screen and (max-width: 400px) {
#nft-image {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<div id="wrapper">
<h1 id="nft-title">Loading...</h1>
<p id="nft-description"></p>
<p>
<img id="nft-image" />
</p>
<p>
<div id="nft-iframe-wrapper">
<iframe id="nft-iframe"></iframe>
</div>
</p>
<p><audio id="nft-sound" controls autoplay></audio></p>
</div>
<script>
function hex_to_ascii(str1) {
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
}
const title = document.getElementById('nft-title');
const description = document.getElementById('nft-description');
const img = document.getElementById('nft-image');
const sound = document.getElementById('nft-sound');
const iframeWrapper = document.getElementById('nft-iframe-wrapper');
const iframe = document.getElementById('nft-iframe');
async function fetchLatestBlock() {
const result = await fetch('http://localhost:8545', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
})
}).then(v => v.json());
return parseInt(result.result.slice(2), 16);
}
async function fetchTokenFromLastTransferEvent(batchSize, back, latestBlock) {
if (!latestBlock) {
latestBlock = await fetchLatestBlock();
}
if (!batchSize) {
batchSize = 100;
}
batchSize = Math.min(batchSize, latestBlock);
if (!back) {
back = 0;
}
const fromBlockNumber = (latestBlock - batchSize - back);
const toBlockNumber = (latestBlock - back);
// console.log({fromBlockNumber, toBlockNumber});
const fromBlock = '0x' + fromBlockNumber.toString(16);
const toBlock = '0x' + toBlockNumber.toString(16);
try {
const result = await fetch('http://localhost:8545', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_getLogs',
params: [
{
fromBlock,
toBlock,
topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']
}
]
})
}).then(v => v.json());
if (result.result.length) {
return {contractAddress: result.result[result.result.length - 1].address, tokenIDAsHex: result.result[result.result.length - 1].topics[3].slice(26)};
}
if (fromBlockNumber == 0) {
latestBlock = undefined;
return undefined;
} else {
console.log(`fetching more logs...`)
return fetchTokenFromLastTransferEvent(100, batchSize, latestBlock);
}
} catch(err) {
console.error(err)
}
}
function fetchToken(contractAddress, tokenIDAsHex) {
const data = '0xc87b56dd' + tokenIDAsHex.padStart(64, '0');
return fetch('http://localhost:8545', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_call',
params: [
{
data,
to: contractAddress
},
'latest'
]
})
});
}
let firsTime = true;
async function fetchIt(contractAddress, tokenIDAsHex) {
if (contractAddress && tokenIDAsHex) {
return fetchToken(contractAddress, tokenIDAsHex);
} else {
if (firsTime) {
firsTime = false;
console.log(`no contract or token specified, fetching latest transfer event...`);
}
const data = await fetchTokenFromLastTransferEvent();
if (!data) {
title.innerHTML = "No Token Found";
return undefined;
}
return fetchToken(data.contractAddress, data.tokenIDAsHex).
then((response) => response.json())
.then((json) => {
if (json.error) {
console.error(json.error);
throw json.error;
}
return json;
})
.then((json) => json.result)
}
}
let last_tokenURI;
let last_tokenID;
let last_contractAddress;
async function refresh() {
const hashStr = location.hash && location.hash.slice(1);
let tokenIDAsHex;
let contractAddress;
if (hashStr) {
const splitted = hashStr.split('_');
contractAddress = splitted[0];
const tokenID = splitted[1];
if (last_contractAddress !== contractAddress || last_tokenID != tokenID) {
last_contractAddress = contractAddress;
last_tokenIDx = tokenID;
console.log({tokenID, contractAddress});
}
tokenIDAsHex = tokenID.startsWith('0x') ? tokenID.slice(2) : parseInt(tokenID).toString(16);
}
// console.log({tokenIDAsHex, contractAddress});
const tokenURIData = await fetchIt(contractAddress, tokenIDAsHex);
if (!tokenURIData) {
console.log(`no token found, please mint one.`);
return;
}
const hex = tokenURIData.slice(2);
const hex2 = hex.slice(64);
const len = hex2.slice(0, 64);
const size = parseInt(len, 16);
const hexData = hex2.slice(64, 64 + size * 2);
const tokenURI = hex_to_ascii(hexData);
let showJSON = false;
if (last_tokenURI != tokenURI) {
last_tokenURI = tokenURI;
showJSON = true;
console.log(tokenURI);
}
let tokenURIResponse;
return fetch(tokenURI)
.then(async (response) => {
tokenURIResponse = response.clone();
// console.log({
// metadata: await response.clone().text()
// });
return response.json();
})
.then((json) => {
if (showJSON) {
console.log(json);
}
if (json.name) {
title.innerHTML = json.name;
}
if (json.description) {
description.innerHTML = json.description;
}
if (json.image) {
if (json.image !== img.src) {
img.src = json.image;
}
}
if (json.animation_url) {
if (json.animation_url.startsWith('data:audio/')) {
if (json.animation_url !== sound.src) {
sound.style.display = 'inline-block';
sound.src = json.animation_url;
}
} else if (json.animation_url.startsWith('data:text/html')) {
if (json.animation_url !== iframe.src) {
iframeWrapper.style.display = 'inline-block';
iframe.src = json.animation_url;
}
} else {
// TODO ?
}
}
})
.catch(async (e) => {
console.error('ERROR', e);
try {
console.log({
tokenURIRAW: await tokenURIResponse.text()
});
} catch(err2) {
}
});
}
sound.style.display = 'none';
iframeWrapper.style.display = 'none';
let timeout;
function refreshAgainAndAgain() {
refresh();
timeout = setTimeout(refreshAgainAndAgain, 1000);
}
refreshAgainAndAgain();
</script>
</body>
</html>