-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
229 lines (187 loc) · 5.61 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
<!doctype html>
<!-- Dev Notes in Javascript at the bottom -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Marketplace</title>
<style type = "text/css">
:root{
--bgcolor: #F0F0A0;
--titlecolor: #000090;
--textcolor: #000030;
--boxcolor: #A0A0FF;
}
html, body{
height: 100%;
width: 100%;
background-color: var(--bgcolor);
color: var(--textcolor);
text-align: center;
}
h1{
font-size: 2em;
font-weight: bold;
color: var(--titlecolor);
}
h3{
font-size: 1em;
font-weight: bold;
color: var(--titlecolor);
}
.container{
width: 100%;
text-align: center;
}
.nftbox{
display: inline-block;
background-color: var(--boxcolor);
width: 300px;
border-width: 2px;
border-color: var(--titlecolor);
border-radius: 10px;
margin: 10px;
}
.nftimg{
border-radius: 5px;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="header">
<h1>MARKETPLACE</h1>
</div>
<div class="header">
<button onclick="connect();" id="walletbtn">CONNECT WALLET</button>
</div>
<br>
<div class="header">
<h1>YOUR NFTS</h1>
<br>
<div id="inventory"></div>
</div>
<br>
<div class="header">
<h1>NFTS FOR SALE</h1>
<br>
<div id="sales"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.5.2/web3.min.js" integrity="sha512-mYc+D+NmmyR0Gcrzyae7q5HguBCS4cBHAsIk7gGhu0/ZyGg4z2YZDjyR2YQA/IMCMTNs4mnlw3vVdERzewpekQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="./abi.js"></script>
<script language="JavaScript" type="text/javascript">
// Dev Notes:
// Change contract to actual deployed contract address.
// Consider adding checks to ensure that the correct blockchain is selected
// in the wallet.
// At the end of connect() function, it would be prudent to do something with
// the "CONNECT" button in the page code, add address as txt, etc.
const CONTRACT = '0xb336ED960D66C738e04b2924DBA410cB5BB7475C';
const ABI = top.CLASS_ABI;
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
var wallet = null;
var web3 = null;
var inventory = [];
var sales = [];
async function connect(){
try{
if (window.ethereum) {
await window.ethereum.enable();
web3 = new Web3(window.ethereum);
let walletget = await web3.eth.getAccounts();
wallet = walletget[0];
refresh();
}
} catch(err) {
alert("Something went Wrong, Connection not Established");
}
}
async function refresh(){
let txn = new web3.eth.Contract(ABI, CONTRACT);
try{
inventory = await txn.methods.viewHolderItems( wallet ).call();
} catch(err) {}
try{
sales = await txn.methods.getAllListings().call();
} catch(err) {}
console.log(inventory);
console.log(sales);
// pass specific things to track
let userItemsListed = [];
document.getElementById("sales").innerHTML = "";
for (i=0; i<sales.length; i++){
let tokenid = sales[i][0];
let pricestr = sales[i][1];
let price = Number(pricestr.substring(0, (pricestr.length - 12))) / 1000000;
if ( (sales[i][2] == ZERO_ADDRESS) || (sales[i][2].toLowerCase() == wallet.toLowerCase()) ){
document.getElementById("sales").innerHTML += `
<div class="nftbox">
<h3>`+tokenid+`</h3>
<img class="nftimg" src="`+tokenid+`.png">
<br>
<div id="purchasePrice`+tokenid+`">` + price + `</div> Eth <br>
<button onclick="buy(`+tokenid+`);">BUY NOW</button>
</div>
`
}
// added provision for the next step
if (inventory.includes(tokenid)){
userItemsListed.push(tokenid);
}
}
document.getElementById("inventory").innerHTML = "";
for (i=0; i<inventory.length; i++){
let tokenid = inventory[i];
var listButton = '<button onclick="list(' + tokenid + ');">LIST</button>';
if ( userItemsListed.includes(tokenid) ){
listButton += '<button onclick="delist(' + tokenid + ');">DELIST</button>';
}
document.getElementById("inventory").innerHTML += `
<div class="nftbox">
<h3>`+tokenid+`</h3>
<img class="nftimg" src="`+tokenid+`.png">
<br>
<input type="numeric" id="inv` + tokenid + `" width="100%"></input>
<br>
` + listButton + `
</div>
`
}
}
async function buy( tokenid ){
priceNumeric = Number(document.getElementById('purchasePrice' + tokenid).innerHTML);
price = (priceNumeric * 1000000) + "000000000000"
//try{
let txn = new web3.eth.Contract(ABI, CONTRACT);
txn.methods.buyItem(tokenid).send({from: wallet, value: price}).then( onResolved => {
refresh();
});
//}
}
async function list( tokenid ){
let priceNumeric = document.getElementById('inv' + tokenid).value;
let price = (priceNumeric * 1000000) + "000000000000";
//try{
let txn = new web3.eth.Contract(ABI, CONTRACT);
txn.methods.listItem(tokenid, price, ZERO_ADDRESS).send({from: wallet, value: 0}).then( onResolved => {
refresh();
});
//}
}
async function delist( tokenid ){
let priceNumeric = document.getElementById('inv' + tokenid).value;
//try{
let txn = new web3.eth.Contract(ABI, CONTRACT);
txn.methods.delistItem(tokenid).send({from: wallet, value: 0}).then( onResolved => {
refresh();
});
//}
}
window.addEventListener('DOMContentLoaded', (event) => {
console.log('DOM fully loaded and parsed');
// with heavy page loading the mint button can be nerfed
// and then updated here.
});
</script>
</body>