diff --git a/lnbits/extensions/splitpayments/crud.py b/lnbits/extensions/splitpayments/crud.py
index ef10add4..67ca9dfe 100644
--- a/lnbits/extensions/splitpayments/crud.py
+++ b/lnbits/extensions/splitpayments/crud.py
@@ -2,6 +2,7 @@ from typing import List
from . import db
from .models import Target
+from loguru import logger
async def get_targets(source_wallet: str) -> List[Target]:
@@ -12,6 +13,7 @@ async def get_targets(source_wallet: str) -> List[Target]:
async def set_targets(source_wallet: str, targets: List[Target]):
+ logger.debug(targets)
async with db.connect() as conn:
await conn.execute(
"DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,)
@@ -20,8 +22,8 @@ async def set_targets(source_wallet: str, targets: List[Target]):
await conn.execute(
"""
INSERT INTO splitpayments.targets
- (source, wallet, percent, alias)
- VALUES (?, ?, ?, ?)
+ (source, wallet, percent, tag, alias)
+ VALUES (?, ?, ?, ?, ?)
""",
- (source_wallet, target.wallet, target.percent, target.alias),
+ (source_wallet, target.wallet, target.percent, target.tag, target.alias),
)
diff --git a/lnbits/extensions/splitpayments/migrations.py b/lnbits/extensions/splitpayments/migrations.py
index b3921c42..d9ead03f 100644
--- a/lnbits/extensions/splitpayments/migrations.py
+++ b/lnbits/extensions/splitpayments/migrations.py
@@ -1,3 +1,5 @@
+from lnbits.helpers import urlsafe_short_hash
+
async def m001_initial(db):
"""
Initial split payment table.
@@ -52,3 +54,43 @@ async def m002_float_percent(db):
)
await db.execute("DROP TABLE splitpayments.splitpayments_old")
+
+
+async def m003_float_percent(db):
+ """
+ Add float percent and migrates the existing data.
+ """
+ await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_old")
+ await db.execute(
+ """
+ CREATE TABLE splitpayments.targets (
+ wallet TEXT NOT NULL,
+ source TEXT NOT NULL,
+ percent INT,
+ alias TEXT,
+
+ UNIQUE (source, wallet)
+ );
+ """
+ )
+
+ for row in [
+ list(row)
+ for row in await db.fetchall("SELECT * FROM splitpayments.splitpayments_old")
+ ]:
+ await db.execute(
+ """
+ INSERT INTO splitpayments.targets (
+ id
+ wallet,
+ source,
+ percent,
+ tag,
+ alias
+ )
+ VALUES (?, ?, ?, ?)
+ """,
+ (urlsafe_short_hash(), row[0], row[1], row[2], '', row[3]),
+ )
+
+ await db.execute("DROP TABLE splitpayments.splitpayments_old")
\ No newline at end of file
diff --git a/lnbits/extensions/splitpayments/models.py b/lnbits/extensions/splitpayments/models.py
index 6338d97f..88f98888 100644
--- a/lnbits/extensions/splitpayments/models.py
+++ b/lnbits/extensions/splitpayments/models.py
@@ -8,13 +8,15 @@ class Target(BaseModel):
wallet: str
source: str
percent: float
+ tag: str
alias: Optional[str]
class TargetPutList(BaseModel):
wallet: str = Query(...)
alias: str = Query("")
- percent: float = Query(..., ge=0.01, lt=100)
+ percent: float = Query(..., ge=0, lt=100)
+ tag: str = Query("")
class TargetPut(BaseModel):
diff --git a/lnbits/extensions/splitpayments/static/js/index.js b/lnbits/extensions/splitpayments/static/js/index.js
index 567b8672..9426b41a 100644
--- a/lnbits/extensions/splitpayments/static/js/index.js
+++ b/lnbits/extensions/splitpayments/static/js/index.js
@@ -10,7 +10,7 @@ function hashTargets(targets) {
}
function isTargetComplete(target) {
- return target.wallet && target.wallet.trim() !== '' && target.percent > 0
+ return target.wallet && target.wallet.trim() !== '' && (target.percent > 0 || target.tag != '')
}
new Vue({
@@ -52,33 +52,15 @@ new Vue({
.then(response => {
this.currentHash = hashTargets(response.data)
this.targets = response.data.concat({})
+ for (let i = 0; i < this.targets.length; i++) {
+ if(this.targets[i].tag !=
+ }
})
},
changedWallet(wallet) {
this.selectedWallet = wallet
this.getTargets()
},
- tagChanged(isTag, index) {
- // fix percent min and max range
- if (isTag) {
- this.targets[index].percent = null
- this.targets[index].tag.trim()
- }
-
- // remove empty lines (except last)
- if (this.targets.length >= 2) {
- for (let i = this.targets.length - 2; i >= 0; i--) {
- let target = this.targets[i]
- if (
- (!target.wallet || target.wallet.trim() === '') &&
- (!target.alias || target.alias.trim() === '') &&
- !target.percent
- ) {
- this.targets.splice(i, 1)
- }
- }
- }
- },
clearChanged(index) {
if(this.targets[index].method == 'split'){
this.targets[index].tag = null
@@ -87,12 +69,18 @@ new Vue({
this.targets[index].percent = null
}
},
- percentageChanged(isPercent, index) {
+ targetChanged(index) {
// fix percent min and max range
- if (isPercent) {
+ console.log(this.targets)
+ if (this.targets[index].percent) {
if (this.targets[index].percent > 100) this.targets[index].percent = 100
if (this.targets[index].percent < 0) this.targets[index].percent = 0
}
+
+ // not percentage
+ if (!this.targets[index].percent) {
+ this.targets[index].percent = 0
+ }
// remove empty lines (except last)
if (this.targets.length >= 2) {
@@ -101,6 +89,7 @@ new Vue({
if (
(!target.wallet || target.wallet.trim() === '') &&
(!target.alias || target.alias.trim() === '') &&
+ (!target.tag || target.tag.trim() === '') &&
!target.percent
) {
this.targets.splice(i, 1)
@@ -145,6 +134,15 @@ new Vue({
console.log(this.targets)
},
saveTargets() {
+ console.log(this.targets)
+ for (let i = 0; i < this.targets.length; i++) {
+ if (this.targets[i].tag){
+ this.targets[i].percent = 0
+ }
+ else{
+ this.targets[i].tag = ''
+ }
+ }
LNbits.api
.request(
'PUT',
@@ -153,7 +151,7 @@ new Vue({
{
targets: this.targets
.filter(isTargetComplete)
- .map(({wallet, percent, alias}) => ({wallet, percent, alias}))
+ .map(({wallet, percent, tag, alias}) => ({wallet, percent, tag, alias}))
}
)
.then(response => {
diff --git a/lnbits/extensions/splitpayments/tasks.py b/lnbits/extensions/splitpayments/tasks.py
index 53378b20..c5cac4a6 100644
--- a/lnbits/extensions/splitpayments/tasks.py
+++ b/lnbits/extensions/splitpayments/tasks.py
@@ -37,7 +37,14 @@ async def on_invoice_paid(payment: Payment) -> None:
logger.debug(f"performing split payments to {len(targets)} targets")
for target in targets:
- amount = int(payment.amount * target.percent / 100) # msats
+
+ if target.tag and payment.extra.get("tag") == target.tag:
+ amount = int(payment.amount)
+ elif target.percent:
+ amount = int(payment.amount * target.percent / 100) # msats
+ else:
+ return
+
payment_hash, payment_request = await create_invoice(
wallet_id=target.wallet,
amount=int(amount / 1000), # sats
diff --git a/lnbits/extensions/splitpayments/templates/splitpayments/index.html b/lnbits/extensions/splitpayments/templates/splitpayments/index.html
index 5a3f7895..86a711f0 100644
--- a/lnbits/extensions/splitpayments/templates/splitpayments/index.html
+++ b/lnbits/extensions/splitpayments/templates/splitpayments/index.html
@@ -59,6 +59,7 @@
:true-value="'tag'"
color="primary"
label=""
+ value="True"
style="width: 180px"
v-model="target.method"
:label="`${target.method}` === 'tag' ? 'Send funds by tag' : `${target.method}` === 'split' ? 'Split funds by %' : 'Split/tag?'"
@@ -66,14 +67,13 @@
>