commit ed083e4268631136fb244c46beb0d7053cc4f9c8
Author: Arc <33088785+arcbtc@users.noreply.github.com>
Date: Tue Feb 14 14:46:19 2023 +0000
Add files via upload
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8b0554c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,34 @@
+# Split Payments
+
+## Have payments split between multiple wallets
+
+LNbits Split Payments extension allows for distributing payments across multiple wallets. Set it and forget it. It will keep splitting your payments across wallets forever.
+
+## Usage
+
+1. After enabling the extension, choose the source wallet that will receive and distribute the Payments
+
+
+
+2. Add the wallet or wallets info to split payments to
+
+ - get the wallet id, or an invoice key from a different wallet. It can be a completely different user as long as it's under the same LNbits instance/domain. You can get the wallet information on the API Info section on every wallet page\
+  - set a wallet _Alias_ for your own identification\
+
+- set how much, in percentage, this wallet will receive from every payment sent to the source wallets
+
+3. When done, click "SAVE TARGETS" to make the splits effective
+
+4. You can have several wallets to split to, as long as the sum of the percentages is under or equal to 100%
+
+5. When the source wallet receives a payment, the extension will automatically split the corresponding values to every wallet\
+ - on receiving a 20 sats payment\
+ 
+ - source wallet gets 18 sats\
+ 
+ - Ben's wallet (the wallet from the example) instantly, and feeless, gets the corresponding 10%, or 2 sats\
+ 
+
+## Sponsored by
+
+[](https://cryptograffiti.com/)
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..5efb633
--- /dev/null
+++ b/__init__.py
@@ -0,0 +1,35 @@
+import asyncio
+
+from fastapi import APIRouter
+from fastapi.staticfiles import StaticFiles
+
+from lnbits.db import Database
+from lnbits.helpers import template_renderer
+from lnbits.tasks import catch_everything_and_restart
+
+db = Database("ext_splitpayments")
+
+splitpayments_static_files = [
+ {
+ "path": "/splitpayments/static",
+ "app": StaticFiles(packages=[("lnbits", "extensions/splitpayments/static")]),
+ "name": "splitpayments_static",
+ }
+]
+splitpayments_ext: APIRouter = APIRouter(
+ prefix="/splitpayments", tags=["splitpayments"]
+)
+
+
+def splitpayments_renderer():
+ return template_renderer(["lnbits/extensions/splitpayments/templates"])
+
+
+from .tasks import wait_for_paid_invoices
+from .views import * # noqa: F401,F403
+from .views_api import * # noqa: F401,F403
+
+
+def splitpayments_start():
+ loop = asyncio.get_event_loop()
+ loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
diff --git a/config.json b/config.json
new file mode 100644
index 0000000..1e0c967
--- /dev/null
+++ b/config.json
@@ -0,0 +1,6 @@
+{
+ "name": "Split Payments",
+ "short_description": "Split incoming payments across wallets",
+ "tile": "/splitpayments/static/image/split-payments.png",
+ "contributors": ["fiatjaf", "cryptograffiti"]
+}
diff --git a/crud.py b/crud.py
new file mode 100644
index 0000000..737e7bb
--- /dev/null
+++ b/crud.py
@@ -0,0 +1,36 @@
+from typing import List
+
+from lnbits.helpers import urlsafe_short_hash
+
+from . import db
+from .models import Target
+
+
+async def get_targets(source_wallet: str) -> List[Target]:
+ rows = await db.fetchall(
+ "SELECT * FROM splitpayments.targets WHERE source = ?", (source_wallet,)
+ )
+ return [Target(**row) for row in rows]
+
+
+async def set_targets(source_wallet: str, targets: List[Target]):
+ async with db.connect() as conn:
+ await conn.execute(
+ "DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,)
+ )
+ for target in targets:
+ await conn.execute(
+ """
+ INSERT INTO splitpayments.targets
+ (id, source, wallet, percent, tag, alias)
+ VALUES (?, ?, ?, ?, ?, ?)
+ """,
+ (
+ urlsafe_short_hash(),
+ source_wallet,
+ target.wallet,
+ target.percent,
+ target.tag,
+ target.alias,
+ ),
+ )
diff --git a/migrations.py b/migrations.py
new file mode 100644
index 0000000..eb72387
--- /dev/null
+++ b/migrations.py
@@ -0,0 +1,99 @@
+from lnbits.helpers import urlsafe_short_hash
+
+
+async def m001_initial(db):
+ """
+ Initial split payment table.
+ """
+ await db.execute(
+ """
+ CREATE TABLE splitpayments.targets (
+ wallet TEXT NOT NULL,
+ source TEXT NOT NULL,
+ percent INTEGER NOT NULL CHECK (percent >= 0 AND percent <= 100),
+ alias TEXT,
+
+ UNIQUE (source, wallet)
+ );
+ """
+ )
+
+
+async def m002_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 REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
+ 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 (
+ wallet,
+ source,
+ percent,
+ alias
+ )
+ VALUES (?, ?, ?, ?)
+ """,
+ (row[0], row[1], row[2], row[3]),
+ )
+
+ await db.execute("DROP TABLE splitpayments.splitpayments_old")
+
+
+async def m003_add_id_and_tag(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 (
+ id TEXT PRIMARY KEY,
+ wallet TEXT NOT NULL,
+ source TEXT NOT NULL,
+ percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
+ tag TEXT NOT NULL,
+ 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")
diff --git a/models.py b/models.py
new file mode 100644
index 0000000..4f2bb01
--- /dev/null
+++ b/models.py
@@ -0,0 +1,28 @@
+from sqlite3 import Row
+from typing import List, Optional
+
+from fastapi import Query
+from pydantic import BaseModel
+
+
+class Target(BaseModel):
+ wallet: str
+ source: str
+ percent: float
+ tag: str
+ alias: Optional[str]
+
+ @classmethod
+ def from_row(cls, row: Row):
+ return cls(**dict(row))
+
+
+class TargetPutList(BaseModel):
+ wallet: str = Query(...)
+ alias: str = Query("")
+ percent: float = Query(..., ge=0, lt=100)
+ tag: str
+
+
+class TargetPut(BaseModel):
+ __root__: List[TargetPutList]
diff --git a/static/image/split-payments.png b/static/image/split-payments.png
new file mode 100644
index 0000000..10b8e7f
Binary files /dev/null and b/static/image/split-payments.png differ
diff --git a/static/js/index.js b/static/js/index.js
new file mode 100644
index 0000000..f5f1627
--- /dev/null
+++ b/static/js/index.js
@@ -0,0 +1,195 @@
+/* globals Quasar, Vue, _, VueQrcode, windowMixin, LNbits, LOCALE */
+
+Vue.component(VueQrcode.name, VueQrcode)
+
+function hashTargets(targets) {
+ return targets
+ .filter(isTargetComplete)
+ .map(({wallet, percent, alias}) => `${wallet}${percent}${alias}`)
+ .join('')
+}
+
+function isTargetComplete(target) {
+ return (
+ target.wallet &&
+ target.wallet.trim() !== '' &&
+ (target.percent > 0 || target.tag != '')
+ )
+}
+
+new Vue({
+ el: '#vue',
+ mixins: [windowMixin],
+ data() {
+ return {
+ selectedWallet: null,
+ currentHash: '', // a string that must match if the edit data is unchanged
+ targets: [
+ {
+ method: 'split'
+ }
+ ]
+ }
+ },
+ computed: {
+ isDirty() {
+ return hashTargets(this.targets) !== this.currentHash
+ }
+ },
+ methods: {
+ clearTargets() {
+ this.targets = [{}]
+ this.$q.notify({
+ message:
+ 'Cleared the form, but not saved. You must click to save manually.',
+ timeout: 500
+ })
+ },
+ clearTarget(index) {
+ this.targets.splice(index, 1)
+ console.log(this.targets)
+ this.$q.notify({
+ message: 'Removed item. You must click to save manually.',
+ timeout: 500
+ })
+ },
+ getTargets() {
+ LNbits.api
+ .request(
+ 'GET',
+ '/splitpayments/api/v1/targets',
+ this.selectedWallet.adminkey
+ )
+ .catch(err => {
+ LNbits.utils.notifyApiError(err)
+ })
+ .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.length > 0) {
+ this.targets[i].method = 'tag'
+ } else if (this.targets[i].percent.length > 0) {
+ this.targets[i].method = 'split'
+ } else {
+ this.targets[i].method = ''
+ }
+ }
+ })
+ },
+ changedWallet(wallet) {
+ this.selectedWallet = wallet
+ this.getTargets()
+ },
+ clearChanged(index) {
+ if (this.targets[index].method == 'split') {
+ this.targets[index].tag = null
+ this.targets[index].method = 'split'
+ } else {
+ this.targets[index].percent = null
+ this.targets[index].method = 'tag'
+ }
+ },
+ targetChanged(index) {
+ // fix percent min and max range
+ 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
+ this.targets[index].tag = ''
+ }
+
+ // not percentage
+ if (!this.targets[index].percent) {
+ this.targets[index].percent = 0
+ }
+
+ // 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.tag || target.tag.trim() === '') &&
+ !target.percent
+ ) {
+ this.targets.splice(i, 1)
+ }
+ }
+ }
+
+ // add a line at the end if the last one is filled
+ let last = this.targets[this.targets.length - 1]
+ if (last.wallet && last.wallet.trim() !== '') {
+ this.targets.push({})
+ }
+
+ // sum of all percents
+ let currentTotal = this.targets.reduce(
+ (acc, target) => acc + (target.percent || 0),
+ 0
+ )
+
+ // remove last (unfilled) line if the percent is already 100
+ if (currentTotal >= 100) {
+ let last = this.targets[this.targets.length - 1]
+ if (
+ (!last.wallet || last.wallet.trim() === '') &&
+ (!last.alias || last.alias.trim() === '') &&
+ !last.percent
+ ) {
+ this.targets = this.targets.slice(0, -1)
+ }
+ }
+
+ // adjust percents of other lines (not this one)
+ if (currentTotal > 100 && isPercent) {
+ let diff = (currentTotal - 100) / (100 - this.targets[index].percent)
+ this.targets.forEach((target, t) => {
+ if (t !== index) target.percent -= +(diff * target.percent).toFixed(2)
+ })
+ }
+ // overwrite so changes appear
+ this.targets = this.targets
+ },
+ saveTargets() {
+ 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',
+ '/splitpayments/api/v1/targets',
+ this.selectedWallet.adminkey,
+ {
+ targets: this.targets
+ .filter(isTargetComplete)
+ .map(({wallet, percent, tag, alias}) => ({
+ wallet,
+ percent,
+ tag,
+ alias
+ }))
+ }
+ )
+ .then(response => {
+ this.$q.notify({
+ message: 'Split payments targets set.',
+ timeout: 700
+ })
+ this.getTargets()
+ })
+ .catch(err => {
+ LNbits.utils.notifyApiError(err)
+ })
+ }
+ },
+ created() {
+ this.selectedWallet = this.g.user.wallets[0]
+ this.getTargets()
+ }
+})
diff --git a/tasks.py b/tasks.py
new file mode 100644
index 0000000..59aa8e0
--- /dev/null
+++ b/tasks.py
@@ -0,0 +1,76 @@
+import asyncio
+
+from loguru import logger
+
+from lnbits.core.models import Payment
+from lnbits.core.services import create_invoice, pay_invoice
+from lnbits.helpers import get_current_extension_name
+from lnbits.tasks import register_invoice_listener
+
+from .crud import get_targets
+
+
+async def wait_for_paid_invoices():
+ invoice_queue = asyncio.Queue()
+ register_invoice_listener(invoice_queue, get_current_extension_name())
+
+ while True:
+ payment = await invoice_queue.get()
+ await on_invoice_paid(payment)
+
+
+async def on_invoice_paid(payment: Payment) -> None:
+
+ if payment.extra.get("tag") == "splitpayments" or payment.extra.get("splitted"):
+ # already a splitted payment, ignore
+ return
+
+ targets = await get_targets(payment.wallet_id)
+
+ if not targets:
+ return
+
+ # validate target percentages
+ total_percent = sum([target.percent for target in targets])
+
+ if total_percent > 100:
+ logger.error("splitpayment: total percent adds up to more than 100%")
+ return
+
+ logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")
+
+ if payment.extra.get("amount"):
+ amount_to_split = (payment.extra.get("amount") or 0) * 1000
+ else:
+ amount_to_split = payment.amount
+
+ if not amount_to_split:
+ logger.error("splitpayments: no amount to split")
+ return
+
+ for target in targets:
+ tagged = target.tag in payment.extra
+
+ if tagged or target.percent > 0:
+
+ if tagged:
+ memo = f"Pushed tagged payment to {target.alias}"
+ amount_msat = int(amount_to_split)
+ else:
+ amount_msat = int(amount_to_split * target.percent / 100)
+ memo = f"Split payment: {target.percent}% for {target.alias or target.wallet}"
+
+ payment_hash, payment_request = await create_invoice(
+ wallet_id=target.wallet,
+ amount=int(amount_msat / 1000),
+ internal=True,
+ memo=memo,
+ )
+
+ extra = {**payment.extra, "tag": "splitpayments", "splitted": True}
+
+ await pay_invoice(
+ payment_request=payment_request,
+ wallet_id=payment.wallet_id,
+ extra=extra,
+ )
diff --git a/templates/splitpayments/_api_docs.html b/templates/splitpayments/_api_docs.html
new file mode 100644
index 0000000..4b5ed97
--- /dev/null
+++ b/templates/splitpayments/_api_docs.html
@@ -0,0 +1,97 @@
+
+ Add some wallets to the list of "Target Wallets", each with an
+ associated percent. After saving, every time any payment
+ arrives at the "Source Wallet" that payment will be split with the
+ target wallets according to their percent.
+ This is valid for every payment, doesn't matter how it was created. Target wallets can be any wallet from this same LNbits instance.
+ To remove a wallet from the targets list, just erase its fields and
+ save. To remove all, click "Clear" then save.
+ GET
+ /splitpayments/api/v1/targets
+ Headers
+ {"X-Api-Key": <admin_key>}
+ Body (application/json)
+
+ Returns 200 OK (application/json)
+
+ [{"wallet": <wallet id>, "alias": <chosen name for this
+ wallet>, "percent": <number between 1 and 100>}, ...]
+ Curl example
+ curl -X GET {{ request.base_url }}splitpayments/api/v1/targets -H
+ "X-Api-Key: {{ user.wallets[0].inkey }}"
+
+ PUT
+ /splitpayments/api/v1/targets
+ Headers
+ {"X-Api-Key": <admin_key>}
+ Body (application/json)
+
+ Returns 200 OK (application/json)
+
+ Curl example
+ curl -X PUT {{ request.base_url }}splitpayments/api/v1/targets -H
+ "X-Api-Key: {{ user.wallets[0].adminkey }}" -H 'Content-Type:
+ application/json' -d '{"targets": [{"wallet": <wallet id or invoice
+ key>, "alias": <name to identify this>, "percent": <number
+ between 1 and 100>}, ...]}'
+
+