-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.html
More file actions
139 lines (121 loc) · 5.3 KB
/
index.html
File metadata and controls
139 lines (121 loc) · 5.3 KB
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
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.8.1/web3.min.js" integrity="sha512-vtUOC0YIaNm/UutU7yfnwqnF9LOYnXtpHe2kwi1nvJNloeGQuncNBiEKP/3Ww3D62USAhbXGsnYpAYoiDsa+wA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<center>
<h1>Polygon Digital Collectibles DApp</h1>
<p id="totalNFTs"></p>
<div>
<button class="button" onclick="MintNFT()">Mint NFT</button>
</div>
<hr>
<div>
<button class="button" onclick="GetNFT()">Get NFT</button>
<input id="tokenId" placeholder="Type tokenId">
</div>
<div class="imgdiv">
<img height=100 id="nftpng"/>
<p id="nftname"></p>
<p id="nftowner"></p>
</div>
<hr>
<div>
<button class="button" onclick="TransferNFT()">Transfer NFT</button>
<input id="toAddress" placeholder="Transfer address">
</div>
</center>
<script src="contract.js"></script>
<script>
var accounts;
var myContract;
async function CheckMetamaskConnection(){
if(window.ethereum){
try{
web3 = new Web3(window.ethereum);
accounts = await window.ethereum.request({method:'eth_requestAccounts'})
console.log("Done!")
return true;
}catch(err){
console.log(err)
return false;
}
} else {
console.log("Metamask Not Found!")
return false;
}
}
$(document).ready(async function () {
var IsMetaMask = await CheckMetamaskConnection();
if(IsMetaMask){
console.log("IsMetaMask = true");
myContract = await new web3.eth.Contract(SmartContractABI, SmartContractAddress);
console.log(myContract);
totalNFTsMinted();
} else {
alert("Metamask not detected!")
}
})
async function totalNFTsMinted(){
await myContract.methods.currentTokenId().call(function(err, res){
if(!err){
console.log(res);
document.getElementById("totalNFTs").innerHTML = "Total NFTs Minted : " + res;
}
})
}
async function GetNFT(){
var tokenId = document.getElementById("tokenId").value;
await myContract.methods.tokenURI(tokenId).call(async function(err, res){
if(!err){
console.log(res);
JSONResponse = await httpGet(res);
JSONResponse = JSON.parse(JSONResponse);
console.log(JSONResponse)
document.getElementById("nftpng").src = JSONResponse["image"];
document.getElementById("nftname").innerHTML = "Name : " + JSONResponse["name"];
getNFTOwner(tokenId);
} else{
console.log(err);
}
})
}
async function getNFTOwner(_tokenId){
await myContract.methods.ownerOf(_tokenId).call(function(err, res){
if(!err){
console.log(res);
document.getElementById("nftowner").innerHTML = "Owner : " + res;
}
})
}
async function MintNFT(_tokenId){
await myContract.methods.mintNFT().send({from: accounts[0]}, function(err, res){
if(!err){
console.log(res);
}else{
console.log(err);
}
})
}
async function TransferNFT(_tokenId){
var toAddress = document.getElementById("toAddress").value;
var tokenId = document.getElementById("tokenId").value;
await myContract.methods.transferFrom(accounts[0], toAddress, tokenId).send({from: accounts[0]}, function(err, res){
if(!err){
console.log(res);
}else{
console.log(err);
}
})
}
function httpGet(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
</script>
</body>
</html>