wallet_send command working (#1382)

* wallet_send command
This commit is contained in:
Lex Berezhny 2018-08-22 23:19:04 -04:00 committed by Jack Robison
parent 518c447fef
commit 2238e1f802
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
4 changed files with 44 additions and 51 deletions

View file

@ -29,7 +29,7 @@ at anytime.
*
### Removed
*
* removed command send_amount_to_address which was previously marked as deprecated
*

View file

@ -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> | --amount=<amount>) (<address> | --address=<address>)
Options:
--amount=<amount> : (float) amount to send
--address=<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)

View file

@ -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

View file

@ -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):