From 5638bca2d7fdd5e2d6ca418c0f2eff3a561dd0d1 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Tue, 26 Jul 2022 10:44:36 +0300 Subject: [PATCH] refactor: payment: first migration --- .../static/components/payment/payment.html | 140 +++++++++++++++++ .../static/components/payment/payment.js | 115 +++++++++++++- .../extensions/watchonly/static/js/index.js | 112 +------------ .../extensions/watchonly/static/js/tables.js | 3 +- .../watchonly/templates/watchonly/index.html | 147 +----------------- 5 files changed, 259 insertions(+), 258 deletions(-) diff --git a/lnbits/extensions/watchonly/static/components/payment/payment.html b/lnbits/extensions/watchonly/static/components/payment/payment.html index e69de29b..f107824e 100644 --- a/lnbits/extensions/watchonly/static/components/payment/payment.html +++ b/lnbits/extensions/watchonly/static/components/payment/payment.html @@ -0,0 +1,140 @@ +
+ + + + + + + + + + {{sendToList}} + + + + + + +
+
+ +
+
+ + +
+
+ {{feeRate}} + +
+
+
+
+
+ + + +
+
Change Account:
+
+ +
+
+ +
+
+
+
+
+
+ \ No newline at end of file diff --git a/lnbits/extensions/watchonly/static/components/payment/payment.js b/lnbits/extensions/watchonly/static/components/payment/payment.js index 809f57a3..a2be844b 100644 --- a/lnbits/extensions/watchonly/static/components/payment/payment.js +++ b/lnbits/extensions/watchonly/static/components/payment/payment.js @@ -1,18 +1,125 @@ async function payment(path) { - const template = await loadTemplateAsync(path) + const t = await loadTemplateAsync(path) + console.log('### template', path, t) Vue.component('payment', { name: 'payment', - template, + template: t, - props: ['mempool_endpoint', 'sats_denominated'], + props: ['accounts', 'utxos', 'mempool_endpoint', 'sats_denominated'], data: function () { - return {} + return { + paymentTab: 'destination', + sendToList: [], + changeWallet: null, + changeAddress: {}, + changeAmount: 0, + showCustomFee: false, + feeRate: 1 + } + }, + + computed: { + txSize: function () { + const tx = this.createTx() + return Math.round(txSize(tx)) + }, + txSizeNoChange: function () { + const tx = this.createTx(true) + return Math.round(txSize(tx)) + }, + feeValue: function () { + return this.feeRate * this.txSize + } }, methods: { satBtc(val, showUnit = true) { return satOrBtc(val, showUnit, this['sats_denominated']) + }, + createPsbt: async function () { + const wallet = this.g.user.wallets[0] + try { + // this.computeFee(this.feeRate) + const tx = this.createTx() + // txSize(tx) + for (const input of tx.inputs) { + input.tx_hex = await this.fetchTxHex(input.tx_id) + } + + this.payment.tx = tx + const {data} = await LNbits.api.request( + 'POST', + '/watchonly/api/v1/psbt', + wallet.adminkey, + tx + ) + + this.payment.psbtBase64 = data + } catch (err) { + LNbits.utils.notifyApiError(err) + } + }, + createTx: function (excludeChange = false) { + const tx = { + fee_rate: this.feeRate, + // tx_size: this.payment.txSize, ??? + masterpubs: this.accounts.map(w => ({ + public_key: w.masterpub, + fingerprint: w.fingerprint + })) + } + tx.inputs = this.utxos.data + .filter(utxo => utxo.selected) + .map(mapUtxoToPsbtInput) + .sort((a, b) => + a.tx_id < b.tx_id ? -1 : a.tx_id > b.tx_id ? 1 : a.vout - b.vout + ) + + tx.outputs = this.sendToList.map(out => ({ + address: out.address, + amount: out.amount + })) + + if (excludeChange) { + this.changeAmount = 0 + } else { + const change = this.createChangeOutput() + this.changeAmount = change.amount // todo: compute separately + if (change.amount >= this.DUST_LIMIT) { + tx.outputs.push(change) + } + } + // Only sort by amount on UI level (no lib for address decode) + // Should sort by scriptPubKey (as byte array) on the backend + // todo: just shuffle + tx.outputs.sort((a, b) => a.amount - b.amount) + + return tx + }, + createChangeOutput: function () { + const change = this.changeAddress + // const inputAmount = this.getTotalSelectedUtxoAmount() // todo: set amount separately + // const payedAmount = this.getTotalPaymentAmount() + const walletAcount = + this.accounts.find(w => w.id === change.wallet) || {} + + return { + address: change.address, + // amount: inputAmount - payedAmount - this.feeValue, + addressIndex: change.addressIndex, + addressIndex: change.addressIndex, + masterpub_fingerprint: walletAcount.fingerprint + } + }, + selectChangeAddress: function (wallet = {}) { + this.changeAddress = + this.addresses.find( + a => a.wallet === wallet.id && a.isChange && !a.hasActivity + ) || {} + }, + getTotalPaymentAmount: function () { + return this.sendToList.reduce((t, a) => t + (a.amount || 0), 0) } }, diff --git a/lnbits/extensions/watchonly/static/js/index.js b/lnbits/extensions/watchonly/static/js/index.js index 995e3098..d1f18cd4 100644 --- a/lnbits/extensions/watchonly/static/js/index.js +++ b/lnbits/extensions/watchonly/static/js/index.js @@ -10,6 +10,9 @@ const watchOnly = async () => { await sendTo('static/components/send-to/send-to.html') await payment('static/components/payment/payment.html') + //emplate static/components/payment/payment.html + //lnbits/extensions/watchonly/static/components/payment/payment.html + Vue.filter('reverse', function (value) { // slice to make a copy of array, then reverse the copy return value.slice().reverse() @@ -32,7 +35,6 @@ const watchOnly = async () => { currentAddress: null, tab: 'addresses', - paymentTab: 'destination', config: { data: { @@ -84,24 +86,7 @@ const watchOnly = async () => { showAddress: false, addressNote: '', - showPayment: false, - showCustomFee: false, - feeRate: 1, - sendToList: [] - } - }, - - computed: { - txSize: function() { - const tx = this.createTx() - return Math.round(txSize(tx)) - }, - txSizeNoChange: function() { - const tx = this.createTx(true) - return Math.round(txSize(tx)) - }, - feeValue: function(){ - return this.feeRate * this.txSize + showPayment: false } }, @@ -205,58 +190,6 @@ const watchOnly = async () => { }, //################### PAYMENT ################### - createTx: function (excludeChange = false) { - const tx = { - fee_rate: this.feeRate, - tx_size: this.payment.txSize, - masterpubs: this.walletAccounts.map(w => ({ - public_key: w.masterpub, - fingerprint: w.fingerprint - })) - } - tx.inputs = this.utxos.data - .filter(utxo => utxo.selected) - .map(mapUtxoToPsbtInput) - .sort((a, b) => - a.tx_id < b.tx_id ? -1 : a.tx_id > b.tx_id ? 1 : a.vout - b.vout - ) - - tx.outputs = this.sendToList.map(out => ({ - address: out.address, - amount: out.amount - })) - - if (excludeChange) { - this.payment.changeAmount = 0 - } else { - const change = this.createChangeOutput() - this.payment.changeAmount = change.amount - if (change.amount >= this.DUST_LIMIT) { - tx.outputs.push(change) - } - } - // Only sort by amount on UI level (no lib for address decode) - // Should sort by scriptPubKey (as byte array) on the backend - // todo: just shuffle - tx.outputs.sort((a, b) => a.amount - b.amount) - - return tx - }, - createChangeOutput: function () { - const change = this.payment.changeAddress - // const inputAmount = this.getTotalSelectedUtxoAmount() // todo: set amount separately - // const payedAmount = this.getTotalPaymentAmount() - const walletAcount = - this.walletAccounts.find(w => w.id === change.wallet) || {} - - return { - address: change.address, - // amount: inputAmount - payedAmount - this.feeValue, - addressIndex: change.addressIndex, - addressIndex: change.addressIndex, - masterpub_fingerprint: walletAcount.fingerprint - } - }, initPaymentData: async function () { if (!this.payment.show) return @@ -265,19 +198,8 @@ const watchOnly = async () => { this.payment.showAdvanced = false this.payment.changeWallet = this.walletAccounts[0] this.selectChangeAddress(this.payment.changeWallet) - - this.payment.feeRate = this.payment.recommededFees.halfHourFee }, - getTotalPaymentAmount: function () { - return this.payment.data.reduce((t, a) => t + (a.amount || 0), 0) - }, - selectChangeAddress: function (wallet = {}) { - this.payment.changeAddress = - this.addresses.find( - a => a.wallet === wallet.id && a.isChange && !a.hasActivity - ) || {} - }, goToPaymentView: async function () { // this.payment.show = true this.showPayment = true @@ -286,29 +208,7 @@ const watchOnly = async () => { }, //################### PSBT ################### - createPsbt: async function () { - const wallet = this.g.user.wallets[0] - try { - // this.computeFee(this.feeRate) - const tx = this.createTx() - // txSize(tx) - for (const input of tx.inputs) { - input.tx_hex = await this.fetchTxHex(input.tx_id) - } - this.payment.tx = tx - const {data} = await LNbits.api.request( - 'POST', - '/watchonly/api/v1/psbt', - wallet.adminkey, - tx - ) - - this.payment.psbtBase64 = data - } catch (err) { - LNbits.utils.notifyApiError(err) - } - }, extractTxFromPsbt: async function (psbtBase64) { const wallet = this.g.user.wallets[0] try { @@ -789,9 +689,7 @@ const watchOnly = async () => { this.utxos.data = [] this.utxos.total = 0 this.history = [] - console.log('### scanAddressWithAmount1', this.addresses) const addresses = this.addresses.filter(a => a.hasActivity) - console.log('### scanAddressWithAmount2', addresses) await this.updateUtxosForAddresses(addresses) }, scanAddress: async function (addressData) { @@ -954,7 +852,7 @@ const watchOnly = async () => { handleAddressesUpdated: async function (addresses) { this.addresses = addresses await this.scanAddressWithAmount() - }, + } }, created: async function () { if (this.g.user.wallets.length) { diff --git a/lnbits/extensions/watchonly/static/js/tables.js b/lnbits/extensions/watchonly/static/js/tables.js index 5a90066d..d01c0f77 100644 --- a/lnbits/extensions/watchonly/static/js/tables.js +++ b/lnbits/extensions/watchonly/static/js/tables.js @@ -32,8 +32,7 @@ const tableData = { }, payment: { data: [{address: '', amount: undefined}], // todo: remove - changeWallet: null, - changeAddress: {}, + changeAmount: 0, fee: 0, diff --git a/lnbits/extensions/watchonly/templates/watchonly/index.html b/lnbits/extensions/watchonly/templates/watchonly/index.html index 4c3a66ef..94303a48 100644 --- a/lnbits/extensions/watchonly/templates/watchonly/index.html +++ b/lnbits/extensions/watchonly/templates/watchonly/index.html @@ -493,150 +493,7 @@ - - - - - - - - - - {{sendToList}} - - - - - - -
-
- -
-
- - -
-
- {{feeRate}} - -
-
-
-
-
- - - -
-
Change Account:
-
- -
-
- -
-
-
-
-
+
@@ -870,4 +727,4 @@ -{% endblock %} +{% endblock %} \ No newline at end of file