From 2238e1f8029a83ea371c15bca5cae9aff2ad60a1 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 22 Aug 2018 23:19:04 -0400 Subject: [PATCH] wallet_send command working (#1382) * wallet_send command --- CHANGELOG.md | 2 +- lbrynet/daemon/Daemon.py | 64 ++++++++++++----------------------- lbrynet/wallet/manager.py | 23 ++++++++----- lbrynet/wallet/transaction.py | 6 ++++ 4 files changed, 44 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e6597e7e..47da4f09e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ at anytime. * ### Removed - * + * removed command send_amount_to_address which was previously marked as deprecated * diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 83659bb91..873cad902 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -2366,36 +2366,6 @@ class Daemon(AuthJSONRPCServer): d.addCallback(lambda address: self._render_response(address)) return d - @requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED]) - @AuthJSONRPCServer.deprecated("wallet_send") - @defer.inlineCallbacks - def jsonrpc_send_amount_to_address(self, amount, address): - """ - Queue a payment of credits to an address - - Usage: - send_amount_to_address ( | --amount=) (
| --address=
) - - Options: - --amount= : (float) amount to send - --address=
: (str) address to send credits to - - Returns: - (bool) true if payment successfully scheduled - """ - - if amount < 0: - raise NegativeFundsError() - elif not amount: - raise NullFundsError() - - reserved_points = self.wallet.reserve_points(address, amount) - if reserved_points is None: - raise InsufficientFundsError() - yield self.wallet.send_points_to_address(reserved_points, amount) - self.analytics_manager.send_credits_sent() - defer.returnValue(True) - @requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED]) @defer.inlineCallbacks def jsonrpc_wallet_send(self, amount, address=None, claim_id=None): @@ -2415,7 +2385,16 @@ class Daemon(AuthJSONRPCServer): Returns: If sending to an address: - (bool) true if payment successfully scheduled + (dict) true if payment successfully scheduled + { + "hex": (str) raw transaction, + "inputs": (list) inputs(dict) used for the transaction, + "outputs": (list) outputs(dict) for the transaction, + "total_fee": (int) fee in dewies, + "total_input": (int) total of inputs in dewies, + "total_output": (int) total of outputs in dewies(input - fees), + "txid": (str) txid of the transaction, + } If sending a claim tip: (dict) Dictionary containing the result of the support @@ -2426,25 +2405,26 @@ class Daemon(AuthJSONRPCServer): } """ + amount = self.get_dewies_or_error("amount", amount) + if not amount: + raise NullFundsError + elif amount < 0: + raise NegativeFundsError() + if address and claim_id: raise Exception("Given both an address and a claim id") elif not address and not claim_id: raise Exception("Not given an address or a claim id") - try: - amount = Decimal(str(amount)) - except InvalidOperation: - raise TypeError("Amount does not represent a valid decimal.") - - if amount < 0: - raise NegativeFundsError() - elif not amount: - raise NullFundsError() - if address: # raises an error if the address is invalid decode_address(address) - result = yield self.jsonrpc_send_amount_to_address(amount, address) + + reserved_points = self.wallet.reserve_points(address, amount) + if reserved_points is None: + raise InsufficientFundsError() + result = yield self.wallet.send_points_to_address(reserved_points, amount) + self.analytics_manager.send_credits_sent() else: validate_claim_id(claim_id) result = yield self.wallet.tip_claim(claim_id, amount) diff --git a/lbrynet/wallet/manager.py b/lbrynet/wallet/manager.py index 742155796..e9719c345 100644 --- a/lbrynet/wallet/manager.py +++ b/lbrynet/wallet/manager.py @@ -15,6 +15,12 @@ from .database import WalletDatabase log = logging.getLogger(__name__) +class ReservedPoints: + def __init__(self, identifier, amount): + self.identifier = identifier + self.amount = amount + + class BackwardsCompatibleNetwork: def __init__(self, manager): self.manager = manager @@ -157,8 +163,15 @@ class LbryWalletManager(BaseWalletManager): # TODO: check if we have enough to cover amount return ReservedPoints(address, amount) - def send_points_to_address(self, reserved, amount): - destination_address = reserved.identifier.encode('latin1') + @defer.inlineCallbacks + def send_amount_to_address(self, amount: int, destination_address: bytes): + account = self.default_account + tx = yield Transaction.pay(amount, destination_address, [account], account) + yield account.ledger.broadcast(tx) + return tx + + def send_points_to_address(self, reserved: ReservedPoints, amount: int): + destination_address: bytes = reserved.identifier.encode('latin1') return self.send_amount_to_address(amount, destination_address) def get_wallet_info_query_handler_factory(self): @@ -275,12 +288,6 @@ class LbryWalletManager(BaseWalletManager): wallet.save() -class ReservedPoints: - def __init__(self, identifier, amount): - self.identifier = identifier - self.amount = amount - - class ClientRequest: def __init__(self, request_dict, response_identifier=None): self.request_dict = request_dict diff --git a/lbrynet/wallet/transaction.py b/lbrynet/wallet/transaction.py index 23644400f..d71d3ce74 100644 --- a/lbrynet/wallet/transaction.py +++ b/lbrynet/wallet/transaction.py @@ -72,6 +72,12 @@ class Transaction(BaseTransaction): input_class = Input output_class = Output + @classmethod + def pay(cls, amount: int, address: bytes, funding_accounts: List[Account], change_account: Account): + ledger = cls.ensure_all_have_same_ledger(funding_accounts, change_account) + output = Output.pay_pubkey_hash(amount, ledger.address_to_hash160(address)) + return cls.create([], [output], funding_accounts, change_account) + @classmethod def claim(cls, name: str, meta: ClaimDict, amount: int, holding_address: bytes, funding_accounts: List[Account], change_account: Account):