forked from fudgebucket27/moody-brains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.html
113 lines (90 loc) · 2.89 KB
/
display.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>display</title>
<!-- <link href="style.css" rel="stylesheet" /> -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<div id="main">
<h1>Moody Brains NFT</h1>
<div>
<p> nft contract: <span id="nft-contract-address"/></p>
<p> tokenId: <span id="tokenId"/></p>
</div>
<h3>metadata uri: </h3>
<textarea id="metadata-uri" cols="120" rows="2"></textarea>
<br/>
<h3>metadata content: </h3>
<textarea id="metadata-content" cols="90" rows="8"></textarea>
<h3>image: </h3>
<!-- display image hosted on IPFS -->
<img id="nft-img" src="" alt="" />
</div>
<script>
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://goerli.infura.io/v3/b7c22d73c16e4c0ea3f88dadbdffbe03"
)
);
console.log("web3: ", web3);
const ipfsGateway = "https://ipfs.nft.gstop-sandbox.com/ipfs/";
function ipfsUriToHttpUri(ipfsUri) {
return ipfsGateway + ipfsUri.substring("ipfs://".length);
}
// functions:
function readMetadata(url) {
fetch(url)
.then( r => r.text() )
.then( t => {
$("#metadata-content").val(t);
loadImage(t);
});
}
function loadImage( metaData) {
const meta = JSON.parse(metaData);
console.log("meta:", meta);
$("#nft-img").attr("src", ipfsUriToHttpUri(meta.image));
$("#nft-img").attr("alt", meta.description);
}
function fetchNftUri() {
const abi = [{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "uri",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
}];
const address = "0x677b26373e125d8b68F2f33f23A2027e7881c3B9";
// tokenID = 4 << 224 + 1
const tokenID = "107839786668602559178668060348078522694548577690162289924414440996865";
$("#nft-contract-address").html(address);
$("#tokenId").html(tokenID);
const contract = new web3.eth.Contract(abi, address);
contract.methods.uri(tokenID).call().then(function(res) {
console.log("res:", res);
$("#metadata-uri").val(res);
const metadataUri = ipfsUriToHttpUri(res)
readMetadata(metadataUri);
});
}
fetchNftUri();
setInterval(fetchNftUri, 12000);
</script>
</body>
</html>