From 719c1c48685eec60bb8d5d78d74e86cd12634c5e Mon Sep 17 00:00:00 2001 From: Alessandro Rezzi Date: Thu, 21 Sep 2023 11:38:09 +0200 Subject: [PATCH] prettier --- scripts/charting.js | 11 +++--- scripts/global.js | 59 +++++++++++++++++++++--------- scripts/mempool.js | 80 ++++++++++++++++++++++++++--------------- scripts/transactions.js | 51 +++++++++++++++++++------- scripts/wallet.js | 6 ++-- 5 files changed, 142 insertions(+), 65 deletions(-) diff --git a/scripts/charting.js b/scripts/charting.js index f739f8031..26a271ef8 100644 --- a/scripts/charting.js +++ b/scripts/charting.js @@ -71,14 +71,17 @@ async function getWalletDataset() { const masternode = await (await Database.getInstance()).getMasternode(); // Masternode (Locked) - if(masternode){ - const mnOp = new COutpoint({txid: masternode.collateralTxId, n: masternode.outidx}) - if(await mempool.getUTXO(mnOp, UTXO_WALLET_STATE.SPENDABLE, true)){ + if (masternode) { + const mnOp = new COutpoint({ + txid: masternode.collateralTxId, + n: masternode.outidx, + }); + if (await mempool.getUTXO(mnOp, UTXO_WALLET_STATE.SPENDABLE, true)) { arrBreakdown.push({ type: 'Masternode', balance: cChainParams.current.collateralInSats / COIN, colour: 'rgba(19, 13, 30, 1)', - }) + }); } } return arrBreakdown; diff --git a/scripts/global.js b/scripts/global.js index cc3fa1f51..7cfdf6f21 100644 --- a/scripts/global.js +++ b/scripts/global.js @@ -660,7 +660,8 @@ export async function updatePriceDisplay(domValue, fCold = false) { if (nPrice) { // Calculate the value const nCurrencyValue = - ((fCold ? await getStakingBalance() : await getBalance()) / COIN) * nPrice; + ((fCold ? await getStakingBalance() : await getBalance()) / COIN) * + nPrice; const { nValue, cLocale } = optimiseCurrencyLocale(nCurrencyValue); @@ -696,7 +697,9 @@ export async function getBalance(updateGUI = false) { } export async function getStakingBalance(updateGUI = false) { - const nBalance = await mempool.getBalanceNew(UTXO_WALLET_STATE.SPENDABLE_COLD); + const nBalance = await mempool.getBalanceNew( + UTXO_WALLET_STATE.SPENDABLE_COLD + ); const nCoins = nBalance / COIN; if (updateGUI) { @@ -736,8 +739,6 @@ export function selectMaxBalance(domCoin, domValue, fCold = false) { * Prompt a QR scan for a Payment (Address or BIP21) */ export async function openSendQRScanner() { - await mempool.getBalanceNew(UTXO_WALLET_STATE.SPENDABLE); - await mempool.getUTXOs(UTXO_WALLET_STATE.SPENDABLE); const cScan = await scanQRCode(); if (!cScan || !cScan.data) return; @@ -1436,16 +1437,22 @@ export async function importMasternode() { if (!wallet.isHD()) { // Find the first UTXO matching the expected collateral size - const cCollaUTXO = (await mempool.getAllUTXOsWithValue(cChainParams.current.collateralInSats, UTXO_WALLET_STATE.SPENDABLE, true)).values().next().value; + const cCollaUTXO = ( + await mempool.getAllUTXOsWithValue( + cChainParams.current.collateralInSats, + UTXO_WALLET_STATE.SPENDABLE, + true + ) + ) + .values() + .next().value; const balance = await getBalance(false); // If there's no valid UTXO, exit with a contextual message if (!cCollaUTXO) { if (balance < cChainParams.current.collateralInSats) { // Not enough balance to create an MN UTXO const amount = - (cChainParams.current.collateralInSats - - balance) / - COIN; + (cChainParams.current.collateralInSats - balance) / COIN; const ticker = cChainParams.current.TICKER; createAlert( 'warning', @@ -1477,7 +1484,9 @@ export async function importMasternode() { collateralPrivKeyPath = 'legacy'; } else { const path = doms.domMnTxId.value; - const masterUtxo = (await mempool.getUTXOs(UTXO_WALLET_STATE.SPENDABLE, true)).findLast((u) => u.path === path); // first UTXO for each address in HD + const masterUtxo = ( + await mempool.getUTXOs(UTXO_WALLET_STATE.SPENDABLE, true) + ).findLast((u) => u.path === path); // first UTXO for each address in HD // sanity check: if (masterUtxo.sats !== cChainParams.current.collateralInSats) { return createAlert('warning', ALERTS.MN_COLLAT_NOT_SUITABLE, 10000); @@ -2589,7 +2598,10 @@ export async function updateMasternodeTab() { return; } - if (!(await mempool.getUTXOs(UTXO_WALLET_STATE.SPENDABLE_TOTAL, true)).length) { + if ( + !(await mempool.getUTXOs(UTXO_WALLET_STATE.SPENDABLE_TOTAL, true)) + .length + ) { doms.domMnTextErrors.innerHTML = 'Your wallet is empty or still loading, re-open the tab in a few seconds!'; return; @@ -2600,10 +2612,11 @@ export async function updateMasternodeTab() { let cMasternode = await database.getMasternode(); // If the collateral is missing (spent, or switched wallet) then remove the current MN if (cMasternode) { - const op = new COutpoint({txid: cMasternode.collateralTxId, n: cMasternode.outidx}) - if ( - !(await mempool.getUTXO(op, UTXO_WALLET_STATE.SPENDABLE, true)) - ) { + const op = new COutpoint({ + txid: cMasternode.collateralTxId, + n: cMasternode.outidx, + }); + if (!(await mempool.getUTXO(op, UTXO_WALLET_STATE.SPENDABLE, true))) { database.removeMasternode(); cMasternode = null; } @@ -2617,7 +2630,15 @@ export async function updateMasternodeTab() { doms.masternodeLegacyAccessText; doms.domMnTxId.style.display = 'none'; // Find the first UTXO matching the expected collateral size - const cCollaUTXO = (await mempool.getAllUTXOsWithValue(cChainParams.current.collateralInSats, UTXO_WALLET_STATE.SPENDABLE, true)).values().next().value; + const cCollaUTXO = ( + await mempool.getAllUTXOsWithValue( + cChainParams.current.collateralInSats, + UTXO_WALLET_STATE.SPENDABLE, + true + ) + ) + .values() + .next().value; const balance = await getBalance(false); if (cCollaUTXO) { if (cMasternode) { @@ -2647,7 +2668,11 @@ export async function updateMasternodeTab() { doms.domMnAccessMasternodeText.innerHTML = doms.masternodeHDAccessText; // First UTXO for each address in HD - const mapCollateralAddresses = await mempool.getAllUTXOsWithValue(cChainParams.current.collateralInSats, UTXO_WALLET_STATE.SPENDABLE, true); + const mapCollateralAddresses = await mempool.getAllUTXOsWithValue( + cChainParams.current.collateralInSats, + UTXO_WALLET_STATE.SPENDABLE, + true + ); const fHasCollateral = mapCollateralAddresses.size > 0; @@ -2775,7 +2800,7 @@ export async function createProposal() { return; } // Must have enough funds - const balance = await getBalance() + const balance = await getBalance(); if (balance * COIN < cChainParams.current.proposalFee) { return createAlert('warning', ALERTS.PROPOSAL_NOT_ENOUGH_FUNDS, 4500); } diff --git a/scripts/mempool.js b/scripts/mempool.js index 894bb2232..b966fcc02 100644 --- a/scripts/mempool.js +++ b/scripts/mempool.js @@ -64,7 +64,9 @@ export class Transaction { * @type {Array}*/ this.vout = vout; } - isConfirmed(){return this.blockHeight != -1} + isConfirmed() { + return this.blockHeight != -1; + } } /** An Unspent Transaction Output, used as Inputs of future transactions */ export class COutpoint { @@ -484,9 +486,14 @@ export class Mempool { return; } for (const tx of txs) { - if (!this.txmap.has(tx.txid) || !this.txmap.get(tx.txid).isConfirmed()){ - const fullTx = this.parseTransaction(await getNetwork().getTxFullInfo(tx.txid)); - this.updateMempool(fullTx); + if ( + !this.txmap.has(tx.txid) || + !this.txmap.get(tx.txid).isConfirmed() + ) { + const fullTx = this.parseTransaction( + await getNetwork().getTxFullInfo(tx.txid) + ); + this.updateMempool(fullTx); } } console.log('txmap', this.txmap); @@ -520,25 +527,25 @@ export class Mempool { totBalance += vout.value; } } - console.log(totBalance/COIN); - return totBalance/COIN; + console.log(totBalance / COIN); + return totBalance / COIN; } /** * Outpoint that we want to fetch * @param {COutpoint} op */ - async getUTXO(op,filter, onlyConfirmed){ + async getUTXO(op, filter, onlyConfirmed) { // If the outpoint is spent return false - if (this.isSpent(op)){ + if (this.isSpent(op)) { return false; } // If we don't have the outpoint return false - if(!this.txmap.has(op.txid)){ + if (!this.txmap.has(op.txid)) { return false; } const tx = this.txmap.get(op.txid); // Check if the tx is confirmed - if(onlyConfirmed && !tx.isConfirmed()){ + if (onlyConfirmed && !tx.isConfirmed()) { return false; } const vout = tx.vout[op.n]; @@ -549,14 +556,14 @@ export class Mempool { } return true; } - async getAllUTXOsWithValue(val,filter, onlyConfirmed){ + async getAllUTXOsWithValue(val, filter, onlyConfirmed) { let utxos = new Map(); for (let [txid, tx] of this.txmap) { - if(onlyConfirmed && !tx.isConfirmed()){ + if (onlyConfirmed && !tx.isConfirmed()) { continue; } for (let vout of tx.vout) { - if(vout.value != val){ + if (vout.value != val) { continue; } const op = new COutpoint({ txid: txid, n: vout.n }); @@ -567,13 +574,16 @@ export class Mempool { if ((UTXO_STATE & filter) == 0) { continue; } - utxos.set(path, new UTXO({ - id: txid, - sats: vout.value * COIN, - script: vout.script, - path: path, - vout: vout.n, - })); + utxos.set( + path, + new UTXO({ + id: txid, + sats: vout.value * COIN, + script: vout.script, + path: path, + vout: vout.n, + }) + ); } } return utxos; @@ -582,7 +592,7 @@ export class Mempool { async getUTXOs(filter, onlyConfirmed = false) { let utxos = []; for (let [txid, tx] of this.txmap) { - if(onlyConfirmed && !tx.isConfirmed()){ + if (onlyConfirmed && !tx.isConfirmed()) { continue; } for (let vout of tx.vout) { @@ -609,23 +619,37 @@ export class Mempool { console.log(utxos); return utxos; } - parseTransaction(tx){ + parseTransaction(tx) { let vout = []; let vin = []; - for(const out of tx.vout){ - vout.push(new CTxOut({n: out.n,script: out.scriptPubKey.hex,value: out.value * COIN})); + for (const out of tx.vout) { + vout.push( + new CTxOut({ + n: out.n, + script: out.scriptPubKey.hex, + value: out.value * COIN, + }) + ); } - for(const inp of tx.vin){ + for (const inp of tx.vin) { const op = new COutpoint({ txid: inp.txid, n: inp.vout }); - vin.push(new CTxIn({outpoint : op,scriptSig: inp.scriptSig.hex})); + vin.push(new CTxIn({ outpoint: op, scriptSig: inp.scriptSig.hex })); } - return new Transaction({txid: tx.txid, blockHeight: getNetwork().cachedBlockCount - (tx.confirmations - 1) - tx.confirmations, vin: vin, vout:vout}) + return new Transaction({ + txid: tx.txid, + blockHeight: + getNetwork().cachedBlockCount - + (tx.confirmations - 1) - + tx.confirmations, + vin: vin, + vout: vout, + }); } /** * Update the mempool status * @param {Transaction} tx */ - updateMempool(tx){ + updateMempool(tx) { this.txmap.set(tx.txid, tx); for (const vin of tx.vin) { const op = vin.outpoint; diff --git a/scripts/transactions.js b/scripts/transactions.js index e413c1610..40b0c2336 100644 --- a/scripts/transactions.js +++ b/scripts/transactions.js @@ -13,7 +13,14 @@ import { import { cHardwareWallet, strHardwareName } from './ledger.js'; import { UTXO_WALLET_STATE, wallet } from './wallet.js'; import { HdMasterKey } from './masterkey.js'; -import { COutpoint, CTxIn, CTxOut, Mempool, Transaction, UTXO } from './mempool.js'; +import { + COutpoint, + CTxIn, + CTxOut, + Mempool, + Transaction, + UTXO, +} from './mempool.js'; import { getNetwork } from './network.js'; import { cChainParams, COIN, COIN_DECIMALS } from './chain_params.js'; import { @@ -411,20 +418,36 @@ export async function createAndSendTransaction({ } // Build Transaction object - let vin = [] - let vout = [] - for(const inp of cTx.inputs){ - const op = new COutpoint({txid: inp.outpoint.hash, n: inp.outpoint.index}) - vin.push(new CTxIn({outpoint: op,scriptSig: bytesToHex(inp.script)})) + let vin = []; + let vout = []; + for (const inp of cTx.inputs) { + const op = new COutpoint({ + txid: inp.outpoint.hash, + n: inp.outpoint.index, + }); + vin.push( + new CTxIn({ outpoint: op, scriptSig: bytesToHex(inp.script) }) + ); } let i = 0; - for(const out of cTx.outputs){ - vout.push(new CTxOut({n:i, script: bytesToHex(out.script), value: Number(out.value)})) - i+=1; + for (const out of cTx.outputs) { + vout.push( + new CTxOut({ + n: i, + script: bytesToHex(out.script), + value: Number(out.value), + }) + ); + i += 1; } - const parsedTx = new Transaction({txid: futureTxid, blockHeight: -1,vin:vin,vout:vout }) - mempool.updateMempool(parsedTx) - console.log(parsedTx) + const parsedTx = new Transaction({ + txid: futureTxid, + blockHeight: -1, + vin: vin, + vout: vout, + }); + mempool.updateMempool(parsedTx); + console.log(parsedTx); if (!isDelegation && !isProposal) { const path = await wallet.isOwnAddress(address); @@ -546,7 +569,9 @@ async function chooseUTXOs( // Select the UTXO type bucket //const arrUTXOs - const filter = fColdOnly ? UTXO_WALLET_STATE.SPENDABLE_COLD : UTXO_WALLET_STATE.SPENDABLE; + const filter = fColdOnly + ? UTXO_WALLET_STATE.SPENDABLE_COLD + : UTXO_WALLET_STATE.SPENDABLE; const arrUTXOs = await mempool.getUTXOs(filter); // Select and return UTXO pointers (filters applied) diff --git a/scripts/wallet.js b/scripts/wallet.js index e6991156f..c5811fc97 100644 --- a/scripts/wallet.js +++ b/scripts/wallet.js @@ -173,7 +173,7 @@ class Wallet { addresses.push(getAddressFromPKH(dataBytes.slice(3, 23))); const path = await this.isOwnAddress(addresses[0]); if (path) { - return [UTXO_WALLET_STATE.SPENDABLE,path]; + return [UTXO_WALLET_STATE.SPENDABLE, path]; } } else if (isP2CS(dataBytes)) { addresses.push(getAddressFromPKH(dataBytes.slice(6, 26))); @@ -182,9 +182,9 @@ class Wallet { const path2 = await this.isOwnAddress(addresses[1]); if (path1) { // TODO: update isOwnADdress to take in consideration of cold stake addresses - return [UTXO_WALLET_STATE.COLD_RECEIVED,path1]; + return [UTXO_WALLET_STATE.COLD_RECEIVED, path1]; } else if (path2) { - return [UTXO_WALLET_STATE.SPENDABLE_COLD,path2]; + return [UTXO_WALLET_STATE.SPENDABLE_COLD, path2]; } } return [UTXO_WALLET_STATE.NOT_MINE, null];