craft-nft

A standalone NFT implementation for real-world arts and crafts assets
Log | Files | Refs | README

commit 64223514a5cb9ea1c8c289852a6e0b3951c7ec5f
parent 043cdcaffd7b77e6a97c9ed61f085c4071d8b767
Author: lash <dev@holbrook.no>
Date:   Sun, 26 Feb 2023 06:39:55 +0000

Add localstorage activity log for qr read

Diffstat:
Mjs/manual_test_browser.js | 2+-
Ajs/qrlist.html | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mjs/qrread.html | 3++-
Mjs/qrread.js | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mjs/qrread_ui.js | 8+++++++-
5 files changed, 144 insertions(+), 3 deletions(-)

diff --git a/js/manual_test_browser.js b/js/manual_test_browser.js @@ -100,7 +100,7 @@ async function generateAllocation() { } tokenData = Object.assign(tokenData_ERC721, tokenData_native, tokenData_openSea); - const s = JSON.stringify(tokenData_ERC721); + const s = JSON.stringify(tokenData); const sha_raw = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" }); sha_raw.update(s); diff --git a/js/qrlist.html b/js/qrlist.html @@ -0,0 +1,79 @@ +<html> + <head> + <title>qr list</title> +<style type="text/css"> +#qrlist td { + font-size: 0.5em; +} +th { + text-align: left; +} +</style> + <script> +window.addEventListener('load', () => { + const v = localStorage.getItem("craftNftQr.act.serial"); + const serialMax = parseInt(v); + const list = document.getElementById("qrlist"); + for (let i = 0; i < serialMax; i++) { + const j = localStorage.getItem("craftNftQr.act." + i); + const o = JSON.parse(j); + + let st = 'pending'; + if (o > 0) { + st = 'success'; + } else if (o < 0) { + st = 'failure'; + } + + const tr = document.createElement("tr"); + if ((i % 2) > 0) { + tr.setAttribute("bgColor", "#eee"); + } + let td = document.createElement("td"); + td.innerHTML = i; + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = o.recipient; + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = o.tokenId; + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = o.voucherValue; + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = st; + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = new Date(o.dateCreated); + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = new Date(o.dateUpdated); + tr.appendChild(td); + + list.appendChild(tr); + } +}); + </script> + </head> + <body> + <table id="qrlist"> + <tr> + <th>serial</th> + <th>recipient</th> + <th>tokenId</th> + <th>value</th> + <th>status</th> + <th>created</th> + <th>updated</th> + </tr> + </table> + </body> +</html> + diff --git a/js/qrread.html b/js/qrread.html @@ -1,6 +1,6 @@ <html> <head> - <title>webcam</title> + <title>qr read</title> <script src="node_modules/jsqr/dist/jsQR.js"></script> <script src="node_modules/ethers/dist/ethers.umd.min.js"></script> <script src="src/wala.js"></script> @@ -14,6 +14,7 @@ const BATCH_UNIT_VALUE = 500; window.addEventListener('load', () => { settings.batchUnitValue = BATCH_UNIT_VALUE; + actLoad(); for (let i = 1; i <= MAX_MINT; i<<=1) { const opt = document.createElement('option'); opt.setAttribute('value', i); diff --git a/js/qrread.js b/js/qrread.js @@ -90,7 +90,60 @@ function checkState(stateCheck, exact) { } } +let actSerial = 0; + +function actLoad() { + const v = localStorage.getItem("craftNftQr.act.serial"); + actSerial = parseInt(v); + if (isNaN(actSerial)) { + actSerial = 0; + localStorage.setItem("craftNftQr.act.serial", actSerial); + } + console.log('actserial', actSerial); +} + +function actRegister(address, tokenId, voucherValue) { + const o = { + recipient: address, + tokenId: tokenId, + voucherValue: voucherValue, + serial: actSerial, + dateCreated: Math.floor(Date.now() / 1000), + dateUpdated: Math.floor(Date.now() / 1000), + state: 0, + } + const j = JSON.stringify(o); + localStorage.setItem("craftNftQr.act." + actSerial, j); + const r = actSerial; + actSerial++; + localStorage.setItem("craftNftQr.act.serial", actSerial); + return r; +} + +function actUpdate(serial, success) { + let j = localStorage.getItem("craftNftQr.act." + serial); + let o = JSON.parse(j); + if (o.state != 0) { + console.error("update on final act state " + state + ", serial " + serial); + return; + } + if (success) { + o.state = 1; + } else { + o.state = -1; + } + o.dateUpdated = Math.floor(Date.now() / 1000); + j = JSON.stringify(o); + localStorage.setItem("craftNftQr.act." + serial, j); +} + async function signAndSend() { + let serials = []; + for (let i = 0; i < settings.mintAmount; i++) { + const serial = actRegister(settings.recipient, settings.tokenId, settings.batchUnitValue); + serials.push(serial); + } + let addr = settings.recipient; console.info('found recipient address', addr); let tx = txBase; @@ -98,6 +151,7 @@ async function signAndSend() { if (tx.to.substring(0, 2) != '0x') { tx.to = '0x' + tx.to; } + let nonce = await settings.wallet.getTransactionCount(); addr = addressPrePad + addr; tx.data += addr; @@ -116,6 +170,7 @@ async function signAndSend() { settings: settings, tx: txr, mintAmount: settings.mintAmount, + serial: serials.shift(), }, bubbles: true, cancelable: true, diff --git a/js/qrread_ui.js b/js/qrread_ui.js @@ -138,7 +138,7 @@ window.addEventListener('tx', (e) => { li.appendChild(l); li.appendChild(r); ls.appendChild(li); - watchTx(e.detail.tx); + watchTx(e.detail.tx, e.detail.serial); }); async function watchTx(tx, i) { @@ -147,10 +147,16 @@ async function watchTx(tx, i) { console.debug('rcpt', rcpt); settings.minedAmount++; if (rcpt.status == 1) { + if (i !== undefined) { + actUpdate(i, true); + } txRow.setAttribute('class', 'statusOk'); txRow.innerHTML = 'status: confirmed'; setStatus('transaction ' + i + ' of ' + settings.mintAmount + ' confirmed', STATUS_OK); } else { + if (i !== undefined) { + actUpdate(i, false); + } txRow.setAttribute('class', 'statusError'); txRow.innerHTML = 'status: failed'; setStatus('transaction ' + i + ' of ' + settings.mintAmount + ' failed', STATUS_ERROR);