craft-nft

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

commit 471fdc26a4aecbbff473f8667d3964b17a46c75c
parent fb7bd8c9156f59ed8ff1d62edae38baf027320bf
Author: lash <dev@holbrook.no>
Date:   Sun, 18 Dec 2022 12:38:43 +0000

Add token summary builder in js

Diffstat:
Mjs/manual_test_browser.js | 8++++++++
Mjs/src/engine.js | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/js/manual_test_browser.js b/js/manual_test_browser.js @@ -17,6 +17,9 @@ window.addEventListener('tokenBatch', async (e) => { if (e.detail.tokenId != currentTokenId) { throw 'batch event without matching token ' + tokenId + ' in view'; } + const mintedToken = await window.craftnft.getMintedToken(session, e.detail.tokenId); + console.debug('retrieved minted token data', mintedToken); + const li = document.createElement('li'); const span = document.createElement('span'); li.setAttribute('id', 'token_' + e.detail.tokenId + ' _batch_' + e.detail.batch); @@ -95,6 +98,10 @@ async function uiMintToken(tokenId, batch) { async function uiViewTokenSingle(tokenId) { let li = document.createElement('li'); + + const mintedToken = await window.craftnft.getMintedToken(session, tokenId); + console.debug('retrieved minted single token data', mintedToken); + li.setAttribute('id', 'token_' + tokenId + '_single'); if (!await window.craftnft.isMintAvailable(session, tokenId, 0)) { console.debug('token ' + tokenId + ' is already minted'); @@ -116,6 +123,7 @@ async function uiViewToken(tokenId) { const r = await session.contentGateway.get(tokenId); const tokenData = JSON.parse(r); + const batch_shit = document.getElementById('token_batches'); while (batch_shit.lastChild) { batch_shit.removeChild(batch_shit.lastChild); diff --git a/js/src/engine.js b/js/src/engine.js @@ -83,6 +83,79 @@ async function isMintAvailable(session, tokenId, batch) { return false; } +async function toToken(session, tokenId, tokenContent) { + console.log('process token content', tokenContent); + if (tokenId.substring(0, 2) == '0x') { + tokenId = tokenId.substring(2); + } + + if (tokenContent.substring(0, 2) == '0x') { + tokenContent = tokenContent.substring(2); + } + + const v = parseInt(tokenContent.substring(0, 2), 16); + console.debug('vvv', v); + let data = { + tokenId: tokenId, + minted: false, + mintedTokenId: undefined, + owner: undefined, + issue: undefined, + batches: undefined, + sparse: false, + }; + + let k = tokenId; + let issue = undefined; + + if ((v & 0x80) == 0) { + if ((v & 0x40) == 0) { + issue = {}; + // TODO: the cap may be larger as we need to process for all batches, not matter whether theyre minted or not + //const token = await session.contract.methods.token('0x' + tokenId, 0).call({from: session.account}); + const state = await getBatches(session, tokenId); + data.batches = state.batches; + issue.cap = state.cap; + issue.count = state.count; + data.issue = issue; + return data; + } + } + + data.minted = true; + + issue = {} + if ((v & 0x40) == 0) { + k = tokenId.substring(0, 48) + tokenContent.substring(2, 18); + issue.batch = parseInt(tokenId.substring(48, 50), 16); + issue.index = parseInt(tokenId.substring(50, 64), 16); + + data.cap = parseInt(token.count); + data.count = parseInt(token.cursor); + data.sparse = token.sparse; + } else { + data.batches = 0; + issue.cap = 1; + issue.count = 1; + data.issue = issue; + } + + data.issue = issue; + data.tokenId = k; + data.owner = tokenContent.substring(24); + + return data; +} + +async function getMintedToken(session, tokenId) { + console.log('query for', tokenId); + const v = await session.contract.methods.mintedToken('0x' + tokenId).call({from: session.account}); + + const mintedToken = await toToken(session, tokenId, v); + + return mintedToken; +} + async function getBatches(session, tokenId, callback) { let token = await session.contract.methods.token('0x' + tokenId, 0).call({from: session.account}); if (token.count == 0) { @@ -90,17 +163,31 @@ async function getBatches(session, tokenId, callback) { return; } - callback(0, token.count, token.cursor); + if (callback !== undefined) { + callback(0, token.count, token.cursor); + } + let i = 1; + let count = parseInt(token.cursor); + let cap = parseInt(token.count); while (true) { try { token = await session.contract.methods.token('0x' + tokenId, 1).call({from: session.account}); } catch(e) { break; } - callback(i, token.count, token.cursor); + if (callback !== undefined) { + callback(i, token.count, token.cursor); + } i++; + count += parseInt(token.cursor); + cap += parseInt(token.count); } + return { + batches: i, + count: count, + cap: cap, + }; } async function isOwner(session, address) { @@ -123,4 +210,5 @@ module.exports = { mintToken: mintToken, isMintAvailable: isMintAvailable, isOwner: isOwner, + getMintedToken: getMintedToken, };