From ed083e4268631136fb244c46beb0d7053cc4f9c8 Mon Sep 17 00:00:00 2001 From: Arc <33088785+arcbtc@users.noreply.github.com> Date: Tue, 14 Feb 2023 14:46:19 +0000 Subject: [PATCH] Add files via upload --- README.md | 34 +++++ __init__.py | 35 +++++ config.json | 6 + crud.py | 36 +++++ migrations.py | 99 +++++++++++++ models.py | 28 ++++ static/image/split-payments.png | Bin 0 -> 9839 bytes static/js/index.js | 195 +++++++++++++++++++++++++ tasks.py | 76 ++++++++++ templates/splitpayments/_api_docs.html | 97 ++++++++++++ templates/splitpayments/index.html | 147 +++++++++++++++++++ views.py | 17 +++ views_api.py | 63 ++++++++ 13 files changed, 833 insertions(+) create mode 100644 README.md create mode 100644 __init__.py create mode 100644 config.json create mode 100644 crud.py create mode 100644 migrations.py create mode 100644 models.py create mode 100644 static/image/split-payments.png create mode 100644 static/js/index.js create mode 100644 tasks.py create mode 100644 templates/splitpayments/_api_docs.html create mode 100644 templates/splitpayments/index.html create mode 100644 views.py create mode 100644 views_api.py 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 + +![choose wallet](https://i.imgur.com/nPQudqL.png) + +2. Add the wallet or wallets info to split payments to + +![split wallets](https://i.imgur.com/5hCNWpg.png) - 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\ + ![wallet info](https://i.imgur.com/betqflC.png) - 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\ + ![get 20 sats payment](https://i.imgur.com/BKp0xvy.png) + - source wallet gets 18 sats\ + ![source wallet](https://i.imgur.com/GCxDZ5s.png) + - Ben's wallet (the wallet from the example) instantly, and feeless, gets the corresponding 10%, or 2 sats\ + ![ben wallet](https://i.imgur.com/MfsccNa.png) + +## 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/__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 0000000000000000000000000000000000000000..10b8e7f2370d7b325ff5b27964e18e79bbeeacb2 GIT binary patch literal 9839 zcmeAS@N?(olHy`uVBq!ia0y~yU}ykg4mJh`hQoG=rx_Roc&kDpN`ey06$*;-(=u~X z6-p`#QWa7wGSe6sDsHWvm02dUv(N2+vFKhFzXuy+_#Rm9ZH}vpHwe~R8n^0{p8d&l z9>)(TFxD_Dz4`M0-}fE=AF6+sik)`LOaJKS$<^-)KP>%u{QQpj2cQ40theL)_i%m2oz>fq%`Ja$MM6TS;AH;)UHA7skZE?} z*jIb3tMX{Z!8EC9mcO5v7zv#-UQxvS=i~3c$3M+Fu^?g!uhQHD=Z*^mABy*xkhe%! zo$EGxjMLroj2wd7#ngm_Bo(q1Jv9zqm&oQ|*)(y%!7It93MVxx2$@grv0Unu7qun2 z*pv7B6^)5^`~FUloH-#RNY#8nPs&rBEB84SZnH1DS-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H!We*Yn%H+;jKoWSEyRiQ$}3=Yit4M{nmG_h2}# zD)5T2g=GSR6cfn04^!qbytrslar!5N#qz=rlQ3_>{ z9Wm_-oA>IgPyFY@+y<&nGB@~ZSdKKTICgE)gN4iQybaes@V~nGtpn4GDI9L|ZqH}0 eI?NLP%zo2?)-^{8vnGLtA3a_DT-G@yGywqnn1xmV literal 0 HcmV?d00001 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>}, ...]}' + +
+
+
+
diff --git a/templates/splitpayments/index.html b/templates/splitpayments/index.html new file mode 100644 index 0000000..b105bf2 --- /dev/null +++ b/templates/splitpayments/index.html @@ -0,0 +1,147 @@ +{% extends "base.html" %} {% from "macros.jinja" import window_vars with context +%} {% block page %} +
+
+ + + + + + + + + + + +
+
Target Wallets
+
+ + +
+ + + + + + + + + + + + Add more + + +
+
+
+ + Clear + +
+ +
+ + Save Targets + +
+
+
+
+
+
+ +
+ + +
+ {{SITE_TITLE}} SplitPayments extension +
+
+ + + {% include "splitpayments/_api_docs.html" %} + +
+
+
+{% endblock %} {% block scripts %} {{ window_vars(user) }} + +{% endblock %} diff --git a/views.py b/views.py new file mode 100644 index 0000000..c33fd42 --- /dev/null +++ b/views.py @@ -0,0 +1,17 @@ +from fastapi import Depends, Request +from fastapi.templating import Jinja2Templates +from starlette.responses import HTMLResponse + +from lnbits.core.models import User +from lnbits.decorators import check_user_exists + +from . import splitpayments_ext, splitpayments_renderer + +templates = Jinja2Templates(directory="templates") + + +@splitpayments_ext.get("/", response_class=HTMLResponse) +async def index(request: Request, user: User = Depends(check_user_exists)): + return splitpayments_renderer().TemplateResponse( + "splitpayments/index.html", {"request": request, "user": user.dict()} + ) diff --git a/views_api.py b/views_api.py new file mode 100644 index 0000000..f83f2b4 --- /dev/null +++ b/views_api.py @@ -0,0 +1,63 @@ +from http import HTTPStatus + +from fastapi import Depends, Request +from starlette.exceptions import HTTPException + +from lnbits.core.crud import get_wallet, get_wallet_for_key +from lnbits.decorators import WalletTypeInfo, require_admin_key + +from . import splitpayments_ext +from .crud import get_targets, set_targets +from .models import Target, TargetPut + + +@splitpayments_ext.get("/api/v1/targets") +async def api_targets_get(wallet: WalletTypeInfo = Depends(require_admin_key)): + targets = await get_targets(wallet.wallet.id) + return [target.dict() for target in targets] or [] + + +@splitpayments_ext.put("/api/v1/targets") +async def api_targets_set( + req: Request, wal: WalletTypeInfo = Depends(require_admin_key) +): + body = await req.json() + targets = [] + data = TargetPut.parse_obj(body["targets"]) + for entry in data.__root__: + wallet = await get_wallet(entry.wallet) + if not wallet: + wallet = await get_wallet_for_key(entry.wallet, "invoice") + if not wallet: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail=f"Invalid wallet '{entry.wallet}'.", + ) + + if wallet.id == wal.wallet.id: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail="Can't split to itself." + ) + + if entry.percent < 0: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail=f"Invalid percent '{entry.percent}'.", + ) + + targets.append( + Target( + wallet=wallet.id, + source=wal.wallet.id, + tag=entry.tag, + percent=entry.percent, + alias=entry.alias, + ) + ) + percent_sum = sum([target.percent for target in targets]) + if percent_sum > 100: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail="Splitting over 100%." + ) + await set_targets(wal.wallet.id, targets) + return ""