From fed7e94d8d36bd4aa763b95cb06cc83a51d1297c Mon Sep 17 00:00:00 2001
From: Lex Berezhny <lex@damoti.com>
Date: Mon, 14 Oct 2019 09:49:08 -0400
Subject: [PATCH] added wallet send command

---
 lbry/lbry/extras/daemon/Daemon.py | 98 +++++++++++++++++++------------
 1 file changed, 60 insertions(+), 38 deletions(-)

diff --git a/lbry/lbry/extras/daemon/Daemon.py b/lbry/lbry/extras/daemon/Daemon.py
index 117ad96e3..6740d5525 100644
--- a/lbry/lbry/extras/daemon/Daemon.py
+++ b/lbry/lbry/extras/daemon/Daemon.py
@@ -1166,6 +1166,60 @@ class Daemon(metaclass=JSONRPCServerType):
         )
         return dict_values_to_lbc(balance)
 
+    @requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED])
+    async def jsonrpc_wallet_send(
+            self, amount, addresses, wallet_id=None,
+            change_account_id=None, funding_account_ids=None, preview=False):
+        """
+        Send the same number of credits to multiple addresses using all accounts in wallet to
+        fund the transaction and the default account to receive any change.
+
+        Usage:
+            wallet_send <amount> <addresses>... [--wallet_id=<wallet_id>] [--preview]
+                        [--change_account_id=None] [--funding_account_ids=<funding_account_ids>...]
+
+        Options:
+            --wallet_id=<wallet_id>         : (str) restrict operation to specific wallet
+            --change_account_id=<wallet_id> : (str) account where change will go
+            --funding_account_ids=<funding_account_ids> : (str) accounts to fund the transaction
+            --preview                  : (bool) do not broadcast the transaction
+
+        Returns: {Transaction}
+        """
+        wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
+        account = wallet.get_account_or_default(change_account_id)
+        accounts = wallet.get_accounts_or_all(funding_account_ids)
+
+        amount = self.get_dewies_or_error("amount", amount)
+        if not amount:
+            raise NullFundsError
+        if amount < 0:
+            raise NegativeFundsError()
+
+        if addresses and not isinstance(addresses, list):
+            addresses = [addresses]
+
+        outputs = []
+        for address in addresses:
+            self.valid_address_or_error(address)
+            outputs.append(
+                Output.pay_pubkey_hash(
+                    amount, self.ledger.address_to_hash160(address)
+                )
+            )
+
+        tx = await Transaction.create(
+            [], outputs, accounts, account
+        )
+
+        if not preview:
+            await self.ledger.broadcast(tx)
+            await self.analytics_manager.send_credits_sent()
+        else:
+            await self.ledger.release_tx(tx)
+
+        return tx
+
     ACCOUNT_DOC = """
     Create, modify and inspect wallet accounts.
     """
@@ -1527,9 +1581,9 @@ class Daemon(metaclass=JSONRPCServerType):
         )
 
     @requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED])
-    async def jsonrpc_account_send(self, amount, addresses, account_id=None, wallet_id=None, preview=False):
+    def jsonrpc_account_send(self, amount, addresses, account_id=None, wallet_id=None, preview=False):
         """
-        Send the same number of credits to multiple addresses.
+        Send the same number of credits to multiple addresses from a specific account (or default account).
 
         Usage:
             account_send <amount> <addresses>... [--account_id=<account_id>] [--wallet_id=<wallet_id>] [--preview]
@@ -1541,44 +1595,12 @@ class Daemon(metaclass=JSONRPCServerType):
 
         Returns: {Transaction}
         """
-        wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
-        if account_id:
-            account = wallet.get_account_or_error(account_id)
-            accounts = [account]
-        else:
-            account = wallet.default_account
-            accounts = wallet.accounts
-
-        amount = self.get_dewies_or_error("amount", amount)
-        if not amount:
-            raise NullFundsError
-        if amount < 0:
-            raise NegativeFundsError()
-
-        if addresses and not isinstance(addresses, list):
-            addresses = [addresses]
-
-        outputs = []
-        for address in addresses:
-            self.valid_address_or_error(address)
-            outputs.append(
-                Output.pay_pubkey_hash(
-                    amount, self.ledger.address_to_hash160(address)
-                )
-            )
-
-        tx = await Transaction.create(
-            [], outputs, accounts, account
+        return self.jsonrpc_wallet_send(
+            amount=amount, addresses=addresses, wallet_id=wallet_id,
+            change_account_id=account_id, funding_account_ids=[account_id] if account_id else [],
+            preview=preview
         )
 
-        if not preview:
-            await self.ledger.broadcast(tx)
-            await self.analytics_manager.send_credits_sent()
-        else:
-            await self.ledger.release_tx(tx)
-
-        return tx
-
     SYNC_DOC = """
     Wallet synchronization.
     """