From 1c1f358d82cc98e42b9330eb3fafe607a2519dd0 Mon Sep 17 00:00:00 2001 From: padreug Date: Thu, 19 Jun 2025 16:35:11 +0200 Subject: [PATCH] Add source wallet ID support for DCA distributions: update Lamassu configuration to include source_wallet_id, modify related models and CRUD operations, and enhance transaction processing to utilize the configured source wallet for payments. Update UI components to allow selection of the source wallet. --- crud.py | 5 +++-- migrations.py | 12 +++++++++++ models.py | 6 ++++++ static/js/index.js | 5 ++++- templates/myextension/index.html | 14 +++++++++++++ transaction_processor.py | 34 ++++++++++++++++++-------------- 6 files changed, 58 insertions(+), 18 deletions(-) diff --git a/crud.py b/crud.py index 9143e05..bf85351 100644 --- a/crud.py +++ b/crud.py @@ -309,9 +309,9 @@ async def create_lamassu_config(data: CreateLamassuConfigData) -> LamassuConfig: await db.execute( """ INSERT INTO myextension.lamassu_config - (id, host, port, database_name, username, password, is_active, created_at, updated_at, + (id, host, port, database_name, username, password, source_wallet_id, is_active, created_at, updated_at, use_ssh_tunnel, ssh_host, ssh_port, ssh_username, ssh_password, ssh_private_key) - VALUES (:id, :host, :port, :database_name, :username, :password, :is_active, :created_at, :updated_at, + VALUES (:id, :host, :port, :database_name, :username, :password, :source_wallet_id, :is_active, :created_at, :updated_at, :use_ssh_tunnel, :ssh_host, :ssh_port, :ssh_username, :ssh_password, :ssh_private_key) """, { @@ -321,6 +321,7 @@ async def create_lamassu_config(data: CreateLamassuConfigData) -> LamassuConfig: "database_name": data.database_name, "username": data.username, "password": data.password, + "source_wallet_id": data.source_wallet_id, "is_active": True, "created_at": datetime.now(), "updated_at": datetime.now(), diff --git a/migrations.py b/migrations.py index 7cf19e3..3d3435d 100644 --- a/migrations.py +++ b/migrations.py @@ -188,3 +188,15 @@ async def m009_add_username_to_dca_clients(db): ADD COLUMN username TEXT; """ ) + + +async def m010_add_source_wallet_to_lamassu_config(db): + """ + Add source wallet ID to Lamassu configuration table for DCA distributions. + """ + await db.execute( + """ + ALTER TABLE myextension.lamassu_config + ADD COLUMN source_wallet_id TEXT; + """ + ) diff --git a/models.py b/models.py index 3952ea7..928e9a6 100644 --- a/models.py +++ b/models.py @@ -109,6 +109,8 @@ class CreateLamassuConfigData(BaseModel): database_name: str username: str password: str + # Source wallet for DCA distributions + source_wallet_id: Optional[str] = None # SSH Tunnel settings use_ssh_tunnel: bool = False ssh_host: Optional[str] = None @@ -130,6 +132,8 @@ class LamassuConfig(BaseModel): test_connection_success: Optional[bool] created_at: datetime updated_at: datetime + # Source wallet for DCA distributions + source_wallet_id: Optional[str] = None # SSH Tunnel settings use_ssh_tunnel: bool = False ssh_host: Optional[str] = None @@ -149,6 +153,8 @@ class UpdateLamassuConfigData(BaseModel): username: Optional[str] = None password: Optional[str] = None is_active: Optional[bool] = None + # Source wallet for DCA distributions + source_wallet_id: Optional[str] = None # SSH Tunnel settings use_ssh_tunnel: Optional[bool] = None ssh_host: Optional[str] = None diff --git a/static/js/index.js b/static/js/index.js index 43ae843..f7065a2 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -71,6 +71,7 @@ window.app = Vue.createApp({ database_name: '', username: '', password: '', + selectedWallet: null, // SSH Tunnel settings use_ssh_tunnel: false, ssh_host: '', @@ -169,6 +170,7 @@ window.app = Vue.createApp({ database_name: this.configDialog.data.database_name, username: this.configDialog.data.username, password: this.configDialog.data.password, + source_wallet_id: this.configDialog.data.selectedWallet?.id, // SSH Tunnel settings use_ssh_tunnel: this.configDialog.data.use_ssh_tunnel, ssh_host: this.configDialog.data.ssh_host, @@ -206,6 +208,7 @@ window.app = Vue.createApp({ database_name: '', username: '', password: '', + selectedWallet: null, // SSH Tunnel settings use_ssh_tunnel: false, ssh_host: '', @@ -705,7 +708,7 @@ window.app = Vue.createApp({ const data = this.configDialog.data // Basic database fields are required - const basicValid = data.host && data.database_name && data.username + const basicValid = data.host && data.database_name && data.username && data.selectedWallet // If SSH tunnel is enabled, validate SSH fields if (data.use_ssh_tunnel) { diff --git a/templates/myextension/index.html b/templates/myextension/index.html index 86a7d8b..1e22471 100644 --- a/templates/myextension/index.html +++ b/templates/myextension/index.html @@ -568,6 +568,20 @@ +
DCA Source Wallet
+ + + + +
SSH Tunnel (Recommended)
diff --git a/transaction_processor.py b/transaction_processor.py index e47b237..6dbc14e 100644 --- a/transaction_processor.py +++ b/transaction_processor.py @@ -650,27 +650,31 @@ class LamassuTransactionProcessor: return False # Pay the invoice from the DCA admin wallet (this extension's wallet) - # We need to get the admin wallet that manages DCA funds + # Get the admin wallet that manages DCA funds admin_config = await get_active_lamassu_config() if not admin_config: logger.error("No active Lamassu config found - cannot determine source wallet") return False - # TODO: We need to determine which wallet holds the DCA funds - # For now, we'll need to add a source_wallet_id to the LamassuConfig - # This is the wallet that receives the Bitcoin from Lamassu and distributes to clients - logger.warning("DCA source wallet not configured - payment creation successful but not sent") - logger.info(f"Created invoice for {amount_sats} sats to client {client.username or client.user_id}") - logger.info(f"Invoice: {new_payment.bolt11}") + if not admin_config.source_wallet_id: + logger.warning("DCA source wallet not configured - payment creation successful but not sent") + logger.info(f"Created invoice for {amount_sats} sats to client {client.username or client.user_id}") + logger.info(f"Invoice: {new_payment.bolt11}") + return True - # TODO: Implement the actual payment once source wallet is configured - # await pay_invoice( - # payment_request=new_payment.bolt11, - # wallet_id=source_wallet_id, - # description=memo, - # extra=extra ) - - return True + # Pay the invoice from the configured source wallet + try: + await pay_invoice( + payment_request=new_payment.bolt11, + wallet_id=admin_config.source_wallet_id, + description=memo, + extra=extra + ) + logger.info(f"DCA payment completed: {amount_sats} sats sent to {client.username or client.user_id}") + return True + except Exception as e: + logger.error(f"Failed to pay invoice for client {client.username or client.user_id}: {e}") + return False except Exception as e: logger.error(f"Error sending DCA payment to client {client.username or client.user_id}: {e}")