Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Sep 21, 2023
1 parent 2eecf56 commit 719c1c4
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 65 deletions.
11 changes: 7 additions & 4 deletions scripts/charting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 42 additions & 17 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
80 changes: 52 additions & 28 deletions scripts/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class Transaction {
* @type {Array<CTxOut>}*/
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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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 });
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
51 changes: 38 additions & 13 deletions scripts/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 719c1c4

Please sign in to comment.