-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
379 lines (301 loc) · 10.5 KB
/
test.js
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
// set current account
currentAccount = null;
function handleAccountsChangedv2(accounts) {
if (accounts.length === 0) {
// console.log('Please connect to MetaMask.');
} else if (accounts[0] !== currentAccount) {
currentAccount = accounts[0];
w3 = new Web3(window.ethereum);
jQuery('#_auction_fr_connect').css({'display' : 'none'});
if(currentAccount != null) {
// Set the button label
//jQuery('#enableMetamask').html(currentAccount)
}
}
}
function connectHandlerv2() {
connectv2();
setInterval(connectv2, 1000);
}
async function handleUI(){
let resp2 = await GetAuctionDetails2(nftData.auction);
GetData();
timerUpdate();
//console.log(resp2);
jQuery("#_auction_owned_by").css("display","block");
if (resp2.approved){
jQuery("#_auction_fr_approve").css('display','none');
jQuery("#_auction_not_approved").css('display','none');
let done_resp = await isDone();
if ( !done_resp){
jQuery("#_auction_amount").css("display","block");
}
loadHistory();
CheckBid();
}else{
if (currentAccount && currentAccount.toLowerCase() == resp2.curator.toLowerCase()){
jQuery("#_auction_fr_approve").css('display','block');
}else{
if (resp2.tokenOwner == "0x0000000000000000000000000000000000000000"){
jQuery("#_auction_not_approved").html("Auction Ended");
jQuery('#_auction_fr_end_auction').css('display','none')
jQuery("#_auction_owned_by").css("display","none");
jQuery("#_auction_amount").css("display","none");
loadHistory();
}else{
jQuery("#_auction_fr_connect").html('connect To Approve')
}
jQuery("#_auction_not_approved").css('display','block');
}
}
}
function connectv2() {
ethereum
.request({ method: "eth_requestAccounts" })
.then(
(accounts) => {
handleAccountsChangedv2(accounts);
//handleUI();
}
)
.catch((err) => {
if (err.code === 4001) {
// console.log("Please connect to MetaMask.");
} else {
// console.error(err);
}
});
}
// connect wallet
async function GetData(){
// run code
let _nft_data_url = await GetNftUri( nftData.token , nftData.tokenContract );
jQuery.getJSON(_nft_data_url, function( _data ){
jQuery('#main img.wp-post-image').attr('src', _data.image);
jQuery('#main img.wp-post-image').attr('alt', _data.name);
let _old_product_name = jQuery('#main .entry-summary .product_title').html();
jQuery('#main .entry-summary .product_title').html( _data.name );
let _breadCrumb = jQuery('.woocommerce-breadcrumb').html();
jQuery('.woocommerce-breadcrumb').html( _breadCrumb.replace(_old_product_name, _data.name ) );
});
}
async function GetNFTData($token, $contract) {
// body...
let __url = await GetNftUri( $token , $contract );
return __url;
}
function ShopProducts(){
console.log("called");
if( jQuery('ul.products').length > 0 ){
jQuery('ul.products > li').each(function(){
let __this = jQuery(this);
console.log("reached");
GetNFTData( jQuery(__this).data('token'), jQuery(__this).data('tokencontract') ).then( function( _url ) {
console.log( _url );
console.log("setting");
jQuery.getJSON( _url, function( _data ){
if( jQuery(__this).find('img').length ){
let __img = jQuery(__this).find('img');
jQuery(__img).attr('src', _data.image);
jQuery(__img).attr('srcset', '');
}
if( jQuery(__this).find('.woocommerce-loop-product__title').length ){
let __title = jQuery(__this).find('.woocommerce-loop-product__title');
// console.log(_data.name);
jQuery(__title).html(_data.name);
}
});
} );
});
}
}
async function isDone(){
let resp2 = await GetAuctionDetails2(nftData.auction);
let d1 = new Date((Number(resp2.duration) + Number(resp2.firstBidTime)) * 1000);
time = timeBetweenDates(d1,resp2);
let cond = (JSON.stringify(time) == JSON.stringify([0,0,0,0]) && resp2.tokenOwner != "0x0000000000000000000000000000000000000000") && resp2.firstBidTime != "0" || resp2.firstBidTime == "0" && resp2.tokenOwner == "0x0000000000000000000000000000000000000000"
return cond
}
async function CheckBid(){
let aunctionDetails = await GetAuctionDetails2( nftData.auction );
//jQuery('#_auction_timing').css({ 'display': 'block' });
if( aunctionDetails.firstBidTime === '0' && currentAccount != null ){
// show auction price
jQuery('#_auction_reserve_price').css({ 'display': 'block' });
console.log("block here")
jQuery('#_auction_fr_place_bid').css({'display' : 'block'});
}else if (currentAccount == null){
let done_resp = await isDone();
if (done_resp){
jQuery("#_auction_fr_connect").html("Connect To End Auction")
}else{
jQuery("#_auction_fr_connect").html("Connect To place Bid")
}
}
}
function loadContract2(abi,add){
let c = new w3.eth.Contract(abi,add);
return c;
}
async function GetAuctionDetails2(auctionId){
let c;
if (currentAccount){
c = loadContract2(HouseAbi,TestNetContract);
}else{
c = loadContract3(HouseAbi,TestNetContract);
}
let resp = await c.methods.auctions(auctionId).call();
return resp;
}
function createBid(auctionId,amount){
// console.log( auctionId + '::' + amount );
let c = loadContract2(HouseAbi,TestNetContract);
c.methods.createBid(auctionId,Web3.utils.toWei(String(amount),'ether')).send(
{
from:currentAccount,
value: Web3.utils.toWei(String(amount),'ether')
}
).then(
(res) => {
alert('created bid');
loadHistory();
CheckBid();
}
).catch(
error => {
console.log(error);
alert('failed bid');
}
)
}
async function loadHistory(){
let c;
if (currentAccount){
c = loadContract2(HouseAbi,TestNetContract);
}else{
c = loadContract3(HouseAbi,TestNetContract);
}
c.getPastEvents('AuctionBid', {fromBlock:nftData.block,filter : {
auctionId : nftData.auction
}}).then(res => {
let _liHtml = '';
// console.log( res );
let tempres = res.reverse();
if( tempres.length ){
tempres.forEach( function( _elem, _index ) {
_liHtml += '<li>' + _elem.returnValues.sender + ' : ' +
Web3.utils.fromWei(_elem.returnValues.value,'ether') + '</li>';
} )
}else{
_liHtml = 'No Bids Available';
}
console.log(_liHtml)
jQuery('#_action_history ul').html( _liHtml );
});
}
// date diff formater
function timeBetweenDates(toDate,res) {
var dateEntered = toDate;
var now = new Date();
var difference = dateEntered.getTime() - now.getTime();
if (difference <= 0) {
//console.log('ended ??')
// console.log('done');
if (res.tokenOwner != "0x0000000000000000000000000000000000000000" && currentAccount && res.firstBidTime != "0"){
jQuery('#_auction_fr_end_auction').css('display','block');
jQuery('#_auction_amount').css('display','none');
jQuery('#_auction_fr_place_bid').css('display','none');
}
//jQuery('#_bidder_amount').css('display','none');
return [0,0,0,0]
} else {
jQuery("#_auction_amount").css("display","block");
var seconds = Math.floor(difference / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
// console.log(days);
// console.log(hours);
// console.log(minutes);
// console.log(seconds);
//jQuery('#_auction_fr_place_bid').css('display','block');
return [days,hours,minutes,seconds]
}
}
// update timer
async function timerUpdate(){
let res = await GetAuctionDetails2(nftData.auction);
// console.log(res);
if (res.tokenOwner != "0x0000000000000000000000000000000000000000"){
jQuery('#__token__owner').html( res.tokenOwner );
jQuery('#__token__owner').attr('href', 'https://etherscan.io/address/' + res.tokenOwner)
if (res.bidder != "0x0000000000000000000000000000000000000000"){
jQuery('#_bidder').text(res.bidder);
jQuery('#_bidder_amount').text(Web3.utils.fromWei(res.amount,'ether')+ ' ETH');
jQuery('#_bidder').css("display","block");
jQuery('#_bidder_amount').css("display","block");
}else{
jQuery('#_bidder').css("display","none");
jQuery('#_bidder_amount').css("display","none");
}
}else{
jQuery('#__token__owner').css('display','none');
jQuery('#_bidder').css('display','none');
jQuery('#_bidder_amount').css("display","none");
}
let time;
if (res.firstBidTime != "0"){
let d1 = new Date((Number(res.duration) + Number(res.firstBidTime)) * 1000);
time = timeBetweenDates(d1,res);
}else if(res.firstBidTime == "0" && res.tokenOwner != "0x0000000000000000000000000000000000000000"){
let now = new Date();
//console.log(res);
let d1 = new Date(now.getTime() + (Number(res.duration) * 1000));
//console.log(d1);
time = timeBetweenDates(d1,res);
time[3] = 0
}else{
time = [0,0,0,0]
}
//console.log(time)
jQuery('#_auction_timing').css({ 'display': 'block' });
jQuery('#_day').text(time[0]);
jQuery('#_hours').text(time[1]);
jQuery('#_mins').text(time[2]);
jQuery('#_secs').text(time[3]);
}
async function Approve(){
let c = loadContract2(HouseAbi,TestNetContract);
let res = await c.methods.setAuctionApproval(nftData.auction,true).send({from:currentAccount});
if (res){
console.log(res);
}
}
async function EndAuction(){
let c = loadContract2(HouseAbi,TestNetContract);
let res = await c.methods.endAuction(nftData.auction).send({from:currentAccount});
console.log(res);
}
window.addEventListener("load", function () {
ShopProducts();
if( document.getElementById("_auction_fr_place_bid") ){
// create bid button
const btnCreateBid = document.getElementById("_auction_fr_place_bid");
const btnConnect = document.getElementById("_auction_fr_connect");
const btnEnd = document.getElementById("_auction_fr_end_auction");
const approveBtn = document.getElementById("_auction_fr_approve");
btnCreateBid.addEventListener("click", function() { createBid(nftData.auction, document.getElementById("_auction_amount").value) } );
btnConnect.addEventListener("click",connectHandlerv2);
btnEnd.addEventListener("click",EndAuction);
approveBtn.addEventListener("click",Approve);
jQuery("#_auction_owned_by").css("display","none");
jQuery("#_auction_amount").css("display","none");
setInterval(handleUI,1000);
}else{
// console.log( "nothing found" );
}
});
//0x0000000000000000000000000000000000000000