diff --git a/lnbits/extensions/splitpayments/README.md b/lnbits/extensions/splitpayments/README.md new file mode 100644 index 000000000..c34055ba4 --- /dev/null +++ b/lnbits/extensions/splitpayments/README.md @@ -0,0 +1,7 @@ +# Split Payments + +Set this and forget. It will keep splitting your payments across wallets forever. + +## Sponsored by + +[![](https://cdn.shopify.com/s/files/1/0826/9235/files/cryptograffiti_logo_clear_background.png?v=1504730421)](https://cryptograffiti.com/) diff --git a/lnbits/extensions/splitpayments/__init__.py b/lnbits/extensions/splitpayments/__init__.py new file mode 100644 index 000000000..2cd2d7c64 --- /dev/null +++ b/lnbits/extensions/splitpayments/__init__.py @@ -0,0 +1,18 @@ +from quart import Blueprint + +from lnbits.db import Database + +db = Database("ext_splitpayments") + +splitpayments_ext: Blueprint = Blueprint( + "splitpayments", __name__, static_folder="static", template_folder="templates" +) + + +from .views_api import * # noqa +from .views import * # noqa +from .tasks import register_listeners + +from lnbits.tasks import record_async + +splitpayments_ext.record(record_async(register_listeners)) diff --git a/lnbits/extensions/splitpayments/config.json b/lnbits/extensions/splitpayments/config.json new file mode 100644 index 000000000..cd2f05aaf --- /dev/null +++ b/lnbits/extensions/splitpayments/config.json @@ -0,0 +1,9 @@ +{ + "name": "SplitPayments", + "short_description": "Split incoming payments to other wallets.", + "icon": "call_split", + "contributors": [ + "fiatjaf", + "cryptograffiti" + ] +} diff --git a/lnbits/extensions/splitpayments/crud.py b/lnbits/extensions/splitpayments/crud.py new file mode 100644 index 000000000..82a59c34d --- /dev/null +++ b/lnbits/extensions/splitpayments/crud.py @@ -0,0 +1,23 @@ +from typing import List + +from . import db +from .models import Target + + +async def get_targets(source_wallet: str) -> List[Target]: + rows = await db.fetchall("SELECT * FROM targets WHERE source = ?", (source_wallet,)) + return [Target(**dict(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 targets WHERE source = ?", (source_wallet,)) + for target in targets: + await conn.execute( + """ + INSERT INTO targets + (source, wallet, percent, alias) + VALUES (?, ?, ?, ?) + """, + (source_wallet, target.wallet, target.percent, target.alias), + ) diff --git a/lnbits/extensions/splitpayments/migrations.py b/lnbits/extensions/splitpayments/migrations.py new file mode 100644 index 000000000..cb7cf34dc --- /dev/null +++ b/lnbits/extensions/splitpayments/migrations.py @@ -0,0 +1,16 @@ +async def m001_initial(db): + """ + Initial split payment table. + """ + await db.execute( + """ + CREATE TABLE targets ( + wallet TEXT NOT NULL, + source TEXT NOT NULL, + percent INTEGER NOT NULL CHECK (percent >= 0 AND percent <= 100), + alias TEXT, + + UNIQUE (source, wallet) + ); + """ + ) diff --git a/lnbits/extensions/splitpayments/models.py b/lnbits/extensions/splitpayments/models.py new file mode 100644 index 000000000..17578f871 --- /dev/null +++ b/lnbits/extensions/splitpayments/models.py @@ -0,0 +1,8 @@ +from typing import NamedTuple + + +class Target(NamedTuple): + wallet: str + source: str + percent: int + alias: str diff --git a/lnbits/extensions/splitpayments/static/js/index.js b/lnbits/extensions/splitpayments/static/js/index.js new file mode 100644 index 000000000..d9750bef1 --- /dev/null +++ b/lnbits/extensions/splitpayments/static/js/index.js @@ -0,0 +1,143 @@ +/* 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 +} + +new Vue({ + el: '#vue', + mixins: [windowMixin], + data() { + return { + selectedWallet: null, + currentHash: '', // a string that must match if the edit data is unchanged + targets: [] + } + }, + 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 + }) + }, + 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({}) + }) + }, + changedWallet(wallet) { + this.selectedWallet = wallet + this.getTargets() + }, + targetChanged(isPercent, index) { + // fix percent min and max range + if (isPercent) { + if (this.targets[index].percent > 100) this.targets[index].percent = 100 + if (this.targets[index].percent < 0) 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.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() !== '' && last.percent > 0) { + 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 -= Math.round(diff * target.percent) + }) + } + + // overwrite so changes appear + this.targets = this.targets + }, + saveTargets() { + LNbits.api + .request( + 'PUT', + '/splitpayments/api/v1/targets', + this.selectedWallet.adminkey, + { + targets: this.targets + .filter(isTargetComplete) + .map(({wallet, percent, alias}) => ({wallet, percent, 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/lnbits/extensions/splitpayments/tasks.py b/lnbits/extensions/splitpayments/tasks.py new file mode 100644 index 000000000..7e57a9e9f --- /dev/null +++ b/lnbits/extensions/splitpayments/tasks.py @@ -0,0 +1,77 @@ +import json +import trio # type: ignore + +from lnbits.core.models import Payment +from lnbits.core.crud import create_payment +from lnbits.core import db as core_db +from lnbits.tasks import register_invoice_listener, internal_invoice_paid +from lnbits.helpers import urlsafe_short_hash + +from .crud import get_targets + + +async def register_listeners(): + invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2) + register_invoice_listener(invoice_paid_chan_send) + await wait_for_paid_invoices(invoice_paid_chan_recv) + + +async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel): + async for payment in invoice_paid_chan: + await on_invoice_paid(payment) + + +async def on_invoice_paid(payment: Payment) -> None: + if "splitpayments" == payment.extra.get("tag") or payment.extra.get("splitted"): + # already splitted, ignore + return + + # now we make some special internal transfers (from no one to the receiver) + targets = await get_targets(payment.wallet_id) + transfers = [ + (target.wallet, int(target.percent * payment.amount / 100)) + for target in targets + ] + transfers = [(wallet, amount) for wallet, amount in transfers if amount > 0] + amount_left = payment.amount - sum([amount for _, amount in transfers]) + + if amount_left < 0: + print("splitpayments failure: amount_left is negative.", payment.payment_hash) + return + + if not targets: + return + + # mark the original payment with one extra key, "splitted" + # (this prevents us from doing this process again and it's informative) + # and reduce it by the amount we're going to send to the producer + await core_db.execute( + """ + UPDATE apipayments + SET extra = ?, amount = ? + WHERE hash = ? + AND checking_id NOT LIKE 'internal_%' + """, + ( + json.dumps(dict(**payment.extra, splitted=True)), + amount_left, + payment.payment_hash, + ), + ) + + # perform the internal transfer using the same payment_hash + for wallet, amount in transfers: + internal_checking_id = f"internal_{urlsafe_short_hash()}" + await create_payment( + wallet_id=wallet, + checking_id=internal_checking_id, + payment_request="", + payment_hash=payment.payment_hash, + amount=amount, + memo=payment.memo, + pending=False, + extra={"tag": "splitpayments"}, + ) + + # manually send this for now + await internal_invoice_paid.send(internal_checking_id) diff --git a/lnbits/extensions/splitpayments/templates/splitpayments/_api_docs.html b/lnbits/extensions/splitpayments/templates/splitpayments/_api_docs.html new file mode 100644 index 000000000..e92fac96f --- /dev/null +++ b/lnbits/extensions/splitpayments/templates/splitpayments/_api_docs.html @@ -0,0 +1,90 @@ + + + +

+ 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.url_root }}api/v1/livestream -H "X-Api-Key: {{ + g.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.url_root }}api/v1/splitpayments/targets -H + "X-Api-Key: {{ g.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>}, ...]}' + +
+
+
+
diff --git a/lnbits/extensions/splitpayments/templates/splitpayments/index.html b/lnbits/extensions/splitpayments/templates/splitpayments/index.html new file mode 100644 index 000000000..d592bcd1c --- /dev/null +++ b/lnbits/extensions/splitpayments/templates/splitpayments/index.html @@ -0,0 +1,98 @@ +{% extends "base.html" %} {% from "macros.jinja" import window_vars with context +%} {% block page %} +
+
+ + + + + + + + + + + +
+
Target Wallets
+
+ + +
+ + + +
+ + + + + Clear + + + + + + Save Targets + + + +
+
+
+
+ +
+ + +
LNbits SplitPayments extension
+
+ + + {% include "splitpayments/_api_docs.html" %} + +
+
+
+{% endblock %} {% block scripts %} {{ window_vars(user) }} + +{% endblock %} diff --git a/lnbits/extensions/splitpayments/views.py b/lnbits/extensions/splitpayments/views.py new file mode 100644 index 000000000..acded737e --- /dev/null +++ b/lnbits/extensions/splitpayments/views.py @@ -0,0 +1,12 @@ +from quart import g, render_template + +from lnbits.decorators import check_user_exists, validate_uuids + +from . import splitpayments_ext + + +@splitpayments_ext.route("/") +@validate_uuids(["usr"], required=True) +@check_user_exists() +async def index(): + return await render_template("splitpayments/index.html", user=g.user) diff --git a/lnbits/extensions/splitpayments/views_api.py b/lnbits/extensions/splitpayments/views_api.py new file mode 100644 index 000000000..e0fe475ed --- /dev/null +++ b/lnbits/extensions/splitpayments/views_api.py @@ -0,0 +1,70 @@ +from quart import g, jsonify +from http import HTTPStatus + +from lnbits.decorators import api_check_wallet_key, api_validate_post_request +from lnbits.core.crud import get_wallet, get_wallet_for_key + +from . import splitpayments_ext +from .crud import get_targets, set_targets +from .models import Target + + +@splitpayments_ext.route("/api/v1/targets", methods=["GET"]) +@api_check_wallet_key("admin") +async def api_targets_get(): + targets = await get_targets(g.wallet.id) + return jsonify([target._asdict() for target in targets] or []) + + +@splitpayments_ext.route("/api/v1/targets", methods=["PUT"]) +@api_check_wallet_key("admin") +@api_validate_post_request( + schema={ + "targets": { + "type": "list", + "schema": { + "type": "dict", + "schema": { + "wallet": {"type": "string"}, + "alias": {"type": "string"}, + "percent": {"type": "integer"}, + }, + }, + } + } +) +async def api_targets_set(): + targets = [] + + for entry in g.data["targets"]: + wallet = await get_wallet(entry["wallet"]) + if not wallet: + wallet = await get_wallet_for_key(entry["wallet"], "invoice") + if not wallet: + return ( + jsonify({"message": f"Invalid wallet '{entry['wallet']}'."}), + HTTPStatus.BAD_REQUEST, + ) + + if wallet.id == g.wallet.id: + return ( + jsonify({"message": "Can't split to itself."}), + HTTPStatus.BAD_REQUEST, + ) + + if entry["percent"] < 0: + return ( + jsonify({"message": f"Invalid percent '{entry['percent']}'."}), + HTTPStatus.BAD_REQUEST, + ) + + targets.append( + Target(wallet.id, g.wallet.id, entry["percent"], entry["alias"] or "") + ) + + percent_sum = sum([target.percent for target in targets]) + if percent_sum > 100: + return jsonify({"message": "Splitting over 100%."}), HTTPStatus.BAD_REQUEST + + await set_targets(g.wallet.id, targets) + return "", HTTPStatus.OK