def on_update(self): selected_item = self.currentItem() current_token_id = selected_item.data(0, Qt.UserRole) if selected_item else None self.clear() tokens = self.parent.wallet.token_types.copy() for token_id, i in tokens.items(): name = i["name"] decimals = i["decimals"] calculated_balance= self.get_balance_from_token_id(token_id) if decimals != "?": balancestr = format_satoshis_nofloat(calculated_balance, decimal_point=decimals, num_zeros=decimals) balancestr += ' '*(9-decimals) else: balancestr = "double-click to add" typestr = "?" if i['class'] == "SLP1": typestr = "Type 1" elif i['class'] == "SLP65": typestr = "NFT1" elif i['class'] == "SLP129": typestr = "NFT1 Group" try: self.parent.wallet.get_slp_token_baton(token_id) item = QTreeWidgetItem([str(token_id),str(name),str(decimals),balancestr,"★", typestr]) except SlpNoMintingBatonFound: item = QTreeWidgetItem([str(token_id),str(name),str(decimals),balancestr,"", typestr]) squishyfont = QFont(MONOSPACE_FONT) squishyfont.setStretch(85) item.setFont(0, squishyfont) #item.setTextAlignment(2, Qt.AlignRight) item.setTextAlignment(3, Qt.AlignRight) item.setFont(3, QFont(MONOSPACE_FONT)) item.setData(0, Qt.UserRole, token_id) if decimals == "?": item.setForeground(0, QBrush(QColor("#BC1E1E"))) item.setForeground(1, QBrush(QColor("#BC1E1E"))) item.setForeground(2, QBrush(QColor("#BC1E1E"))) item.setForeground(3, QBrush(QColor("#BC1E1E"))) item.setForeground(4, QBrush(QColor("#BC1E1E"))) item.setForeground(5, QBrush(QColor("#BC1E1E"))) if i["class"] == "SLP129": tokens = self.parent.wallet.token_types.copy() for _token_id, _i in tokens.items(): if _i["class"] == "SLP65" and _i.get("group_id", None) == token_id: name = _i["name"] decimals = _i["decimals"] calculated_balance= self.get_balance_from_token_id(_token_id) if decimals != "?": balancestr = format_satoshis_nofloat(calculated_balance, decimal_point=decimals, num_zeros=decimals) balancestr += ' '*(9-decimals) else: balancestr = "double-click to add" _nft_item = QTreeWidgetItem([str(_token_id),str(name),str(decimals),balancestr,"", "NFT1 Child"]) squishyfont = QFont(MONOSPACE_FONT) squishyfont.setStretch(85) _nft_item.setFont(0, squishyfont) #item.setTextAlignment(2, Qt.AlignRight) _nft_item.setTextAlignment(3, Qt.AlignRight) _nft_item.setFont(3, QFont(MONOSPACE_FONT)) _nft_item.setData(0, Qt.UserRole, _token_id) if decimals == "?": _nft_item.setForeground(0, QBrush(QColor("#BC1E1E"))) _nft_item.setForeground(1, QBrush(QColor("#BC1E1E"))) _nft_item.setForeground(2, QBrush(QColor("#BC1E1E"))) _nft_item.setForeground(3, QBrush(QColor("#BC1E1E"))) item.addChild(_nft_item) self.addTopLevelItem(item) elif i["class"] == "SLP65" and i.get("group_id", "?") == "?": self.addTopLevelItem(item) elif i["class"] == "SLP1": self.addTopLevelItem(item) if current_token_id == token_id: self.setCurrentItem(item) self.expandAll()
def handle_genesis_tx(self, tx): self.token_id_e.setReadOnly(True) self.get_info_button.setDisabled(True) self.load_tx_menu_button.setDisabled(True) self.newtoken_genesis_tx = tx self.view_tx_button.setDisabled(False) txid = tx.txid() token_id = self.token_id_e.text().strip() if token_id and txid != token_id: return self.fail_genesis_info(_('TXID does not match token ID!')) self.newtoken_token_id = txid self.token_id_e.setText(self.newtoken_token_id) try: slpMsg = SlpMessage.parseSlpOutputScript(tx.outputs()[0][1]) except SlpUnsupportedSlpTokenType as e: return self.fail_genesis_info(_("Unsupported SLP token version/type - %r.")%(e.args[0],)) except SlpInvalidOutputMessage as e: return self.fail_genesis_info(_("This transaction does not contain a valid SLP message.\nReason: %r.")%(e.args,)) if slpMsg.transaction_type != 'GENESIS': return self.fail_genesis_info(_("This is an SLP transaction, however it is not a genesis transaction.")) f_fieldnames = QTextCharFormat() f_fieldnames.setFont(QFont(MONOSPACE_FONT)) f_normal = QTextCharFormat() self.token_info_e.clear() cursor = self.token_info_e.textCursor() fields = [ ('ticker', _('ticker'), 'utf8', None), ('token_name', _('name'), 'utf8', None), ('token_doc_url', _('doc url'), 'ascii', 'html'), ('token_doc_hash', _('doc hash'), 'hex', None), ] cursor.insertText(_('Issuer-declared strings in genesis:')) cursor.insertBlock() for k,n,e,f in fields: data = slpMsg.op_return_fields[k] if e == 'hex': friendlystring = None else: # Attempt to make a friendly string, or fail to hex try: # Ascii only friendlystring = data.decode(e) # raises UnicodeDecodeError with bytes > 127. # Count ugly characters (that need escaping in python strings' repr()) uglies = 0 for b in data: if b < 0x20 or b == 0x7f: uglies += 1 # Less than half of characters may be ugly. if 2*uglies >= len(data): friendlystring = None except UnicodeDecodeError: friendlystring = None if len(data) == 0: showstr = '(empty)' f=None elif friendlystring is None: showstr = data.hex() f=None else: showstr = repr(friendlystring) cursor.insertText(' '*(10 - len(n)) + n + ': ', f_fieldnames) if f == 'html': enc_url = html.escape(friendlystring) enc_text = html.escape(showstr) cursor.insertHtml('<a href="%s" title="%s">%s</a>'%(enc_url, enc_url, enc_text)) else: cursor.insertText(showstr, f_normal) cursor.insertBlock() # try to auto-fill name input name_ext = '' for key in [ 'ticker', 'token_name' ]: if self.token_name_e.text() == '' and slpMsg.op_return_fields[key] != b'': base_name = slpMsg.op_return_fields[key].decode("utf-8") for k,v in self.wallet.token_types.items(): if v['name'] == base_name: name_ext = "-" + self.token_id_e.text()[:3] self.token_name_e.setText(base_name + name_ext) break self.newtoken_decimals = slpMsg.op_return_fields['decimals'] cursor.insertText(_('Decimals:') + ' ' + str(self.newtoken_decimals)) cursor.insertBlock() numtokens = format_satoshis_nofloat(slpMsg.op_return_fields['initial_token_mint_quantity'], num_zeros=self.newtoken_decimals, decimal_point=self.newtoken_decimals,) mbv = slpMsg.op_return_fields['mint_baton_vout'] if mbv is None or mbv > len(tx.outputs()): issuance_type = _('Initial issuance type: fixed supply') else: issuance_type = _('Initial issuance type: flexible supply') cursor.insertText(_('Initial issuance:') + ' ' + numtokens) cursor.insertBlock() cursor.insertText(issuance_type) #cursor.insertBlock() self.newtoken_genesis_message = slpMsg self.add_button.setDisabled(False) self.activateWindow() self.raise_()
def update_item_state(self, item): tx_hash = item.data(0, Qt.UserRole) status, conf = item.data(0, SortableTreeWidgetItem.DataRole) token_id = item.data(4, Qt.UserRole) delta = item.data(3, Qt.UserRole) try: validity = self.wallet.get_slp_token_info(tx_hash)['validity'] except KeyError: # Can happen if non-token tx (if burning tokens) validity = None try: tinfo = self.wallet.token_types[token_id] except KeyError: unktoken = True tokenname = _("%.4s... (unknown - right click to add)"%(token_id,)) deltastr = '%+d'%(delta,) else: if tinfo['decimals'] == '?': unktoken = True tokenname = _("%.4s... (unknown - right click to add)"%(token_id,)) deltastr = '%+d'%(delta,) else: unktoken = False tokenname=tinfo['name'] deltastr = format_satoshis_nofloat(delta, is_diff=True, decimal_point=tinfo['decimals'],) # right-pad so the decimal points line up # (note that because zeros are stripped, we have to locate decimal point here) dp = localeconv()['decimal_point'] d1,d2 = deltastr.rsplit(dp,1) deltastr += "\u2014"*(9-len(d2)) # \u2014 is long dash if unktoken and validity in (None,0,1,2,3,4): # If a token is not in our list of known token_ids, warn the user. icon=QIcon(":icons/warning.png") icontooltip = _("Unknown token ID") elif validity == 0: # For in-progress validation, always show gears regardless of confirmation status. icon=QIcon(":icons/unconfirmed.svg") icontooltip = _("SLP unvalidated") elif validity in (None,2,3): icon=QIcon(":icons/expired.svg") if validity is None: icontooltip = "non-SLP (tokens burned!)" else: icontooltip = "SLP invalid (tokens burned!)" elif validity == 4: icon=QIcon(":icons/expired.svg") icontooltip = "Bad NFT1 Group" elif validity == 1: # For SLP valid known txes, show the confirmation status (gears, few-confirmations, or green check) icon = QIcon(":icons/" + TX_ICONS[status]) icontooltip = _("SLP valid; ") + str(conf) + " confirmation" + ("s" if conf != 1 else "") else: raise ValueError(validity) if unktoken: item.setForeground(3, QBrush(QColor("#888888"))) item.setForeground(4, QBrush(QColor("#888888"))) elif delta < 0: item.setForeground(3, QBrush(QColor("#BC1E1E"))) item.setIcon(0, icon) item.setToolTip(0, icontooltip) item.setText(4, tokenname) item.setText(3, deltastr)