-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
379 lines (268 loc) · 11.1 KB
/
script.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
// const balance = document.getElementById('balance');
// const money_plus = document.getElementById('money-plus');
// const money_minus = document.getElementById('money-minus');
var visitingPage = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
const list = document.getElementById('list');
const form = document.getElementById('form');
const text = document.getElementById('text');
const amount = document.getElementById('amount');
const nftImg = document.getElementById('nftImg');
const Desc = document.getElementById('Desc');
const ipfs = window.IpfsApi({host:'ipfs.infura.io',port: 5001, protocol: "https"});
let listtransactions = [];
var transaction ;
// Add transaction for NFT
function addTransaction(e) {
e.preventDefault();
if (text.value.trim() === '' || amount.value.trim() === '' || nftImg.files.length <= 0 ) {
alert('Please enter name and price and select image .');
} else {
let desc = Desc.value != "" ? Desc.value : "" ;
transaction = {
name: text.value,
price: +amount.value,
desc: desc
};
let nftHash = getNFTHash();
if(nftHash == 0)
{
alert("Something went wrong while saving image. Please try again.");
return;
}
// recordTransaction(transaction.name, transaction.price, transaction.hash, transaction.desc);
// text.value = '';
// amount.value = '';
// Desc.value = '';
//eleCheck[selectedIndex].checked = false;
}
}
function transferTransaction(e) {
e.preventDefault();
let nftID = document.getElementById('nftID');
let transferToAddress = document.getElementById('transferToAddress');
if (nftID.value.trim() === '' || transferToAddress.value.trim() === '' ) {
alert('Please enter id and adress to transfer .');
} else {
$("#txStatus").text("Creating new transaction on the blockchain. This may take a while...");
return expIncomeContract.methods.transferFrom(userAccount, transferToAddress.value.trim(), nftID.value.trim())
.send({ from: userAccount, gas : 1000000 })
.on("receipt", function (receipt) {
$("#txStatus").text("Successfully tranferred " + nftID.value.trim() + " !");
})
.on("error", function (error) {
$("#txStatus").text(error);
});
}
}
// Add transactions to DOM list
function addTransactionDOM(_transaction,_id) {
// Get sign
listtransactions.push(_transaction);
//const sign = _transaction.tranType == "exp" ? '-' : '+';
const item = document.createElement('li');
// Add class based on value
item.classList.add('img1');
//item.Id = _id;
item.innerHTML = `<figure>
<img src="https://ipfs.io/ipfs/${_transaction.ipfsHash}" alt="${_transaction.name}" width="300" height="250">
<figcaption> ${_transaction.name}</figcaption>
<figcaption>$<span>${_transaction.price}</span></figcaption>
<figcaption>ID: <span>${_id}</span></figcaption>
</figure>
`;
list.appendChild(item);
}
function getNFTHash() {
let inputElement = nftImg;
var file = inputElement.files[0];
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = function() {
// console.log('Encoded Base 64 File String:', reader.result);
/******************* for Binary ***********************/
// var data=(reader.result).split(',')[1];
// var binaryBlob = atob(data);
//console.log('buffer', new Uint8Array(reader.result));
//console.log('buffer',new Uint8Array(reader.result));
const buf = buffer.Buffer(reader.result)
ipfs.files.add(buf, (error, result) => {
if(error) {
console.log(error)
return 0;
}
//console.log('ifpsHash', result[0].hash);
// this.simpleStorageInstance.set(result[0].hash, { from: this.state.account }).then((r) => {
// return this.setState({ ipfsHash: result[0].hash })
// console.log('ifpsHash', this.state.ipfsHash)
// })
//return result[0].hash;
recordTransaction(transaction.name, transaction.price, result[0].hash, transaction.desc);
// let url = `https://ipfs.io/ipfs/${result[0].hash}`
// console.log(`Url --> ${url}`)
// document.getElementById("url").innerHTML= url
// document.getElementById("url").href= url
// document.getElementById("output").src = url
})
}
}
var expIncomeContract;
var userAccount;
const init = async() => {
/* const id = await web3js.eth.net.getId();
const deployedNetwork = MyContract.networks[id];
expIncomeContract = new web3js.eth.Contract(
MyContract.abi,
deployedNetwork.address
);*/
const address_eth_cli = "0xf3c7230F2B6989b9afDd21431CD26C7fe6bD88FF";
//const addresses = await web3js.eth.getAccounts();
userAccount = address_eth_cli;//addresses[2];
// var contAddress = "0xDCeA0EB6c5D1Bfe22fdF20DCE791a7fF154cA753";
// expIncomeContract = new web3js.eth.contract(MyContract.abi, contAddress);
var contAddress = "0x326e371BFFceBccd96F104824056993670F15eC4";
expIncomeContract = new web3js.eth.Contract(MyContract.abi, contAddress);
// var accountInterval = setInterval(function() {
// // Check if account has changed
// if (web3.eth.accounts[0] !== userAccount) {
// userAccount = web3.eth.accounts[0];
// // Call a function to update the UI with the new account
// getZombiesByOwner(userAccount)
// .then(displayZombies);
// }
// }, 100);
// var expTrackrAddress = "CONTRACT_ADDRESS";
// expIncomeContract = new web3js.eth.Contract(expTrackrABI, expTrackrAddress);
// var accountInterval = setTimeout(function () {
// if (web3js.eth.accounts[0] !== userAccount) {
// userAccount = web3js.eth.accounts[0];
// getTransactionByOwner(userAccount)
// .then(displayTransactions);
//}
// }, 100);
// expIncomeContract.events.Newtransaction({})
// .on("data", function (event) {
// let data = event.returnValues;
// getTransactionByOwner(userAccount).then(displayTransactions);
// }).on("error", console.error);
if(visitingPage == "index.html")
getTransactionByOwner(userAccount).then(displayTransactions);
}
function displayTransactions11(ids) {
list.innerHTML = '';
for (id of ids) {
getTransactionDetails(id)
.then(function (tran,id) {
addTransactionDOM(tran,id);
// updateValues();
});
}
}
async function displayTransactions(ids) {
list.innerHTML = '';
for (id of ids) {
let tran = await getTransactionDetails(id);
addTransactionDOM(tran,id);
}
}
function recordTransaction(_name, _price, _hash, _desc) {
listtransactions = [];
$("#txStatus").text("Creating new transaction on the blockchain. This may take a while...");
return expIncomeContract.methods.recordTransaction(_name, _price, _hash, _desc)
.send({ from: userAccount, gas : 1000000 })
.on("receipt", function (receipt) {
$("#txStatus").text("Successfully created " + _name + "!");
})
.on("error", function (error) {
$("#txStatus").text(error);
});
}
function getTransactionDetails(id) {
return expIncomeContract.methods.listnftsImgs(id).call()
}
function getTransactionByOwner(owner) {
return expIncomeContract.methods.getTransactionByOwner(owner).call()
}
window.addEventListener('load', async()=> {
// if (typeof Web3 !== 'undefined') {
// //web3js = new Web3(web3.currentProvider);
// web3js = new Web3("http://localhost:7545");
// } else {
// }
// Initialize Web3
// if (typeof web3js !== 'undefined') {
// web3js = new Web3(web3js.currentProvider);
// } else {
// web3js = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
// }
// // Set Account
// web3js.eth.defaultAccount = web3js.eth.accounts[0];
// init();
let accounts;
try {
let web3;
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
// Request account access if needed
//await window.ethereum.enable();
await window.ethereum.request({ method: "eth_requestAccounts" });
//await window.ethereum.send('eth_requestAccounts');
window.ethereum.on('accountsChanged', function (_accounts) {
if (_accounts[0] !== userAccount) {
userAccount = _accounts[0];
if(visitingPage == "index.html")
getTransactionByOwner(userAccount).then(displayTransactions);
}
});
} catch (error) {
console.log(error);
}
}
// Legacy dapp browsers...
else if (window.web3) {
// Use Mist/MetaMask's provider.
web3 = new Web3(window.web3.currentProvider);
console.log("Injected web3 detected.");
}
// Fallback to localhost; use dev console port by default...
else {
web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
}
accounts = await web3.eth.getAccounts();
userAccount = accounts[0];
//console.log(userAccount);
// Get the contract instance.
const networkId = await web3.eth.net.getId();
const deployedNetwork = MyContract.networks[networkId];
// const instance = new web3.eth.Contract(
// SimpleStorageContract.abi,
// deployedNetwork && deployedNetwork.address,
// );
expIncomeContract = new web3.eth.Contract(MyContract.abi,
deployedNetwork && deployedNetwork.address,
);
} catch (error) {
// Catch any errors for any of the above operations.
alert(
`Failed to load web3, accounts, or contract. Check console for details.`,
);
console.error(error);
}
if(visitingPage == "index.html")
getTransactionByOwner(userAccount).then(displayTransactions);
// var accountInterval = setInterval(function () {
// if (accounts[0] !== userAccount) {
// userAccount = accounts[0];
// if(visitingPage == "index.html")
// getTransactionByOwner(userAccount).then(displayTransactions);
// }
// else
// getTransactionByOwner(userAccount).then(displayTransactions);
// }, 1000);
})
if(visitingPage.toLowerCase().includes("createnft"))
form.addEventListener('submit', addTransaction);
if(visitingPage.toLowerCase().includes("transfernft"))
{
document.getElementById('formTransfer').addEventListener('submit', transferTransaction);
}