craft-nft

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

commit f219d8c565f5debeb63090eb9dc53ec001f746fd
parent 471fdc26a4aecbbff473f8667d3964b17a46c75c
Author: lash <dev@holbrook.no>
Date:   Sun, 18 Dec 2022 14:04:13 +0000

Add metadata retriever for minted token, fix broken dump script

Diffstat:
Ajs/ISSUES | 1+
Mjs/index.html | 2++
Mjs/manual_test_browser.js | 24++++++++++++++++--------
Mjs/src/engine.js | 35++++++++++++++++++++++++++++++++++-
Mpython/eth_craft_nft/nft.py | 6+++---
Mpython/eth_craft_nft/runnable/dump.py | 8+++++---
6 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/js/ISSUES b/js/ISSUES @@ -0,0 +1 @@ +- Sometimes when clicking mint the metamask signer does not trigger. If mint is clicked a second time, it always appears. Detected with metamask 10.22.2 , chromium 108.0.5359.98 diff --git a/js/index.html b/js/index.html @@ -61,6 +61,8 @@ a:hover { <dd id="token_name"></dd> <dt>description</dt> <dd id="token_description"></dd> + <dt>supply</dt> + <dd id="token_cap"></dd> <dt class="token_batch_list">batches</dt> <dd class="token_batch_list"> <ol id="token_batches"></ol> diff --git a/js/manual_test_browser.js b/js/manual_test_browser.js @@ -17,16 +17,18 @@ 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); span.innerHTML = 'used ' + e.detail.cursor + ' of ' + e.detail.count + ' '; li.appendChild(span); - const isMintable = await window.craftnft.isMintAvailable(session, e.detail.tokenId, e.detail.batch); - if (isMintable) { + + const mintedTokenData = await window.craftnft.getMintedToken(session, e.detail.tokenId, e.detail.batch); + console.debug('retrieved minted token data', mintedTokenData); + + //const isMintable = await window.craftnft.isMintAvailable(session, e.detail.tokenId, e.detail.batch); + if (mintedTokenData.mintable) { const a = document.createElement('a'); a.setAttribute('onClick', 'uiMintToken("' + e.detail.tokenId + '", ' + e.detail.batch + ')'); a.innerHTML = 'mint'; @@ -98,12 +100,12 @@ async function uiMintToken(tokenId, batch) { async function uiViewTokenSingle(tokenId) { let li = document.createElement('li'); + li.setAttribute('id', 'token_' + tokenId + '_single'); - const mintedToken = await window.craftnft.getMintedToken(session, tokenId); - console.debug('retrieved minted single token data', mintedToken); + const mintedTokenData = await window.craftnft.getMintedToken(session, tokenId, 0); + console.debug('retrieved minted single token data', mintedTokenData); - li.setAttribute('id', 'token_' + tokenId + '_single'); - if (!await window.craftnft.isMintAvailable(session, tokenId, 0)) { + if (!mintedTokenData.mintable) { console.debug('token ' + tokenId + ' is already minted'); li.innerHTML = '(already minted)'; } else { @@ -129,9 +131,15 @@ async function uiViewToken(tokenId) { batch_shit.removeChild(batch_shit.lastChild); } + const tokenChainData = await window.craftnft.getTokenChainData(session, tokenId); + console.debug('retrieved token chain data', tokenChainData); + document.getElementById('token_id').innerHTML = tokenId; document.getElementById('token_name').innerHTML = tokenData.name; document.getElementById('token_description').innerHTML = tokenData.description; + document.getElementById('token_cap').innerHTML = tokenChainData.issue.cap; + + window.craftnft.getBatches(session, tokenId, (batch, count, cursor) => { if (batch == -1) { uiViewTokenSingle(tokenId); diff --git a/js/src/engine.js b/js/src/engine.js @@ -67,6 +67,38 @@ async function mintToken(session, tokenId, batch, recipient) { }); } +async function getMintedToken(session, tokenId, batch) { + let o = { + mintable: false, + single: false, + cap: 0, + count: 0, + sparse: false, + } + let token = await session.contract.methods.token('0x' + tokenId, batch).call({from: session.account}); + if (token === undefined) { + return o; + } + if (batch == 0) { + if (token.count == 0) { + o.cap = 1; + o.count = parseInt(token.cursor); + if (token.cursor == 0) { + o.mintable = true; + } + o.single = true; + return o; + } + } + o.sparse = token.sparse; + o.cap = token.count; + o.count = token.cursor; + if (token.cursor < token.count) { + o.mintable = true; + } + return o; +} + async function isMintAvailable(session, tokenId, batch) { let token = await session.contract.methods.token('0x' + tokenId, batch).call({from: session.account}); if (token === undefined) { @@ -147,7 +179,7 @@ async function toToken(session, tokenId, tokenContent) { return data; } -async function getMintedToken(session, tokenId) { +async function getTokenChainData(session, tokenId) { console.log('query for', tokenId); const v = await session.contract.methods.mintedToken('0x' + tokenId).call({from: session.account}); @@ -210,5 +242,6 @@ module.exports = { mintToken: mintToken, isMintAvailable: isMintAvailable, isOwner: isOwner, + getTokenChainData: getTokenChainData, getMintedToken: getMintedToken, }; diff --git a/python/eth_craft_nft/nft.py b/python/eth_craft_nft/nft.py @@ -270,7 +270,7 @@ class CraftNFT(ERC721): return MintedToken(addr, token_id=token_id, batched=True, minted=True) o = MintedToken(addr, minted=True) - o.index = int(token_id[59:], 16) - o.batch = int(token_id[54:59], 16) - o.token_id = token_id[:54] + v[2:12] + o.index = int(token_id[48:52], 16) + o.batch = int(token_id[52:64], 16) + o.token_id = token_id[:48] + v[2:18] return o diff --git a/python/eth_craft_nft/runnable/dump.py b/python/eth_craft_nft/runnable/dump.py @@ -29,6 +29,7 @@ from chainlib.eth.cli.log import process_log from chainlib.eth.cli.config import Config from chainlib.eth.cli.config import process_config from hexathon import strip_0x +from hexathon import add_0x # local imports from eth_craft_nft import CraftNFT @@ -87,9 +88,9 @@ def render_token_batches(c, conn, token_address, token_id, w=sys.stdout): spec = c.parse_token_spec(r) for j in range(spec.cursor): - cursor_hex = j.to_bytes(3, byteorder='big').hex() - batch_hex = i.to_bytes(3, byteorder='big').hex() - token_id_indexed = token_id[:54] + batch_hex[1:] + cursor_hex[1:] + cursor_hex = j.to_bytes(6, byteorder='big').hex() + batch_hex = i.to_bytes(2, byteorder='big').hex() + token_id_indexed = token_id[:48] + batch_hex + cursor_hex render_token_mint(c, conn, token_address, token_id_indexed, w=w) i += 1 @@ -99,6 +100,7 @@ def render_token_mint(c, conn, token_address, token_id, w=sys.stdout): o = c.get_token(token_address, token_id) r = conn.do(o) token = c.parse_token(r, token_id) + logg.info('specccc {} {}'.format(token_id, token)) if token.minted: w.write('token {}\n'.format(token))