From 5b72b3021e9fb279e08f1e9bc61122e594833d07 Mon Sep 17 00:00:00 2001 From: sairaj mote Date: Wed, 25 Oct 2023 21:14:51 +0530 Subject: [PATCH] Code refactoring --- index.html | 54 ++++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index b61f2f8..ae14f92 100644 --- a/index.html +++ b/index.html @@ -580,7 +580,6 @@ if (this.routingStart) { this.routingStart(this.state) } - console if (this.routes[page]) { await this.routes[page](this.state) this.lastPage = page @@ -848,14 +847,17 @@ }).catch(err => reject(err)) }) } - function getConvertedAmount(amount) { + function getConvertedAmount(amount, shouldFormat = false) { // check if amount is a string and convert it to a number if (typeof amount === 'string') { amount = parseFloat(amount) } + let convertedAmount = amount; if (globalExchangeRate[selectedCurrency]) - return parseFloat((amount * globalExchangeRate[selectedCurrency]).toFixed(8)) - else return amount + convertedAmount = parseFloat((amount * globalExchangeRate[selectedCurrency]).toFixed(8)) + if (shouldFormat) + convertedAmount = formatAmount(convertedAmount) + return convertedAmount } function roundUp(amount, precision = 2) { return parseFloat((Math.ceil(amount * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision)) @@ -891,7 +893,7 @@ el.isValid // trigger validation } else { if (el.dataset.btcAmount === undefined) return - el.textContent = formatAmount(getConvertedAmount(el.dataset.btcAmount)) + el.textContent = getConvertedAmount(el.dataset.btcAmount, true) } }) previouslySelectedCurrency = selectedCurrency @@ -934,7 +936,7 @@
-
${formatAmount(getConvertedAmount(amount))}
+
${getConvertedAmount(amount, true)}
${transactionReceiver} @@ -970,7 +972,7 @@ getRef('transactions_list').innerHTML = ''; getRef('address_balance').innerHTML = ''; btcOperator.getAddressData(address).then(result => { - getRef('address_balance').value = formatAmount(getConvertedAmount(result.balance)); + getRef('address_balance').value = getConvertedAmount(result.balance, true); getRef('address_balance').dataset.btcAmount = result.balance; getRef('address_balance').parentElement.classList.remove('hidden') getRef('filter_selector').classList.remove('hidden') @@ -1045,7 +1047,7 @@
Fee
-
${formatAmount(getConvertedAmount(fee))}
+
${getConvertedAmount(fee, true)}
@@ -1059,14 +1061,14 @@
Total Inputs
-
${formatAmount(getConvertedAmount(total_input_value))}
+
${getConvertedAmount(total_input_value, true)}
Total Outputs
-
${formatAmount(getConvertedAmount(total_output_value))}
+
${getConvertedAmount(total_output_value, true)}
@@ -1087,7 +1089,7 @@ ${inputs.map(input => html`
  • ${input.address} -
    ${formatAmount(getConvertedAmount(input.value))}
    +
    ${getConvertedAmount(input.value, true)}
  • `)} @@ -1101,7 +1103,7 @@ ${outputs.map(output => html`
  • ${output.address} -
    ${formatAmount(getConvertedAmount(output.value))}
    +
    ${getConvertedAmount(output.value, true)}
  • `)} @@ -1357,7 +1359,7 @@

    ${address}

    - Balance: ${formatAmount(getConvertedAmount(balance))} + Balance: ${getConvertedAmount(balance, true)}

    `) @@ -1450,14 +1452,14 @@ ${Object.entries(receivers).map(([address, amount]) => html.node/*html*/`
    ${address} - ${formatAmount(getConvertedAmount(amount))} + ${getConvertedAmount(amount, true)}
    `)}
    Fee - ${formatAmount(getConvertedAmount(fee))} + ${getConvertedAmount(fee, true)}
    `, @@ -1614,16 +1616,16 @@
    Amount
    - ${formatAmount(getConvertedAmount(value))} + ${getConvertedAmount(value, true)}
    `)}

    - Previous fee: ${formatAmount(getConvertedAmount(previousFee))} ${recommendedFee ? html`| Recommended fee: ${formatAmount(getConvertedAmount(recommendedFee))}` : ''} + Previous fee: ${getConvertedAmount(previousFee, true)} ${recommendedFee ? html`| Recommended fee: ${getConvertedAmount(recommendedFee, true)}` : ''}

    - +
    @@ -1697,16 +1699,12 @@ SEGWIT_OUTPUT_SIZE = 23, BECH32M_OUTPUT_SIZE = 35; // Check this later - const util = {}; - - util.Sat_to_BTC = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8)); - util.BTC_to_Sat = value => parseInt(value * SATOSHI_IN_BTC); function get_fee_rate() { return new Promise((resolve, reject) => { fetch('https://api.blockchain.info/mempool/fees').then(response => { if (response.ok) response.json() - .then(result => resolve(util.Sat_to_BTC(result.regular))) + .then(result => resolve(btcOperator.util.Sat_to_BTC(result.regular))) .catch(error => reject(error)); else reject(response); @@ -1782,16 +1780,16 @@ const opts = {}; const tx = new taproot.Transaction(opts); const totalAmount = amounts.reduce((total, amount) => total + amount, 0) - const amountInSat = util.BTC_to_Sat(totalAmount) + const amountInSat = btcOperator.util.BTC_to_Sat(totalAmount) // check if sender has enough balance const senderBalance = await btcOperator.getBalance(address) const feeRate = await get_fee_rate(); let calculatedFee = 0 - const { input_size } = await addUTXOs(tx, tr, [address], util.BTC_to_Sat(totalAmount), feeRate) + const { input_size } = await addUTXOs(tx, tr, [address], btcOperator.util.BTC_to_Sat(totalAmount), feeRate) calculatedFee += input_size // add receivers receivers.forEach((receiver, i) => { - tx.addOutputAddress(receiver, BigInt(util.BTC_to_Sat(amounts[i]))) + tx.addOutputAddress(receiver, BigInt(btcOperator.util.BTC_to_Sat(amounts[i]))) calculatedFee += _sizePerOutput(receiver) }) calculatedFee += _sizePerOutput(address) @@ -1800,8 +1798,8 @@ // add change address const changeAmount = senderBalance - (totalAmount + fee) if (changeAmount < 0) - return reject(`Insufficient balance. Required: ${formatAmount(getConvertedAmount(totalAmount + fee))}, Available: ${formatAmount(getConvertedAmount(util.Sat_to_BTC(senderBalance)))}`) - tx.addOutputAddress(address, BigInt(util.BTC_to_Sat(changeAmount))); + return reject(`Insufficient balance. Required: ${getConvertedAmount(totalAmount + fee, true)}, Available: ${getConvertedAmount(btcOperator.util.Sat_to_BTC(senderBalance), true)}`) + tx.addOutputAddress(address, BigInt(btcOperator.util.BTC_to_Sat(changeAmount))); const privKey = coinjs.wif2privkey(senderPrivateKey).privkey; const privKey_arrayForm = hex.decode(privKey); tx.sign(privKey_arrayForm, undefined, new Uint8Array(32));