wallet manager refactoring

test uses manager.send_amount_to_address instead of manually creating tx
This commit is contained in:
Lex Berezhny 2018-05-28 23:12:11 -04:00 committed by Jack Robison
parent 80990b959c
commit 1368a6e074
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 42 additions and 55 deletions

View file

@ -2,36 +2,34 @@ import asyncio
from binascii import hexlify
from orchstr8.testcase import IntegrationTestCase
from torba.constants import COIN
from lbrynet.wallet.transaction import Transaction, Input, Output
class StartupTests(IntegrationTestCase):
class BasicTransactionTests(IntegrationTestCase):
VERBOSE = True
async def test_balance(self):
account = self.wallet.default_account
coin = account.coin
ledger = self.manager.ledgers[coin.ledger_class]
address = account.get_least_used_receiving_address()
sendtxid = await self.lbrycrd.sendtoaddress(address.decode(), 2.5)
async def test_sending_and_recieving(self):
self.assertEqual(await self.lbrycrd.get_balance(), 10.0)
self.assertEqual(self.manager.get_balance(), 0.0)
address = self.account.get_least_used_receiving_address()
sendtxid = await self.lbrycrd.send_to_address(address.decode(), 5.5)
await self.lbrycrd.generate(1)
await ledger.on_transaction.where(
lambda tx: tx.id.decode() == sendtxid
)
utxo = account.get_unspent_utxos()[0]
address2 = account.get_least_used_receiving_address()
tx_class = ledger.transaction_class
Input, Output = tx_class.input_class, tx_class.output_class
tx = tx_class() \
.add_inputs([Input.spend(utxo)]) \
.add_outputs([Output.pay_pubkey_hash(int(2.49*COIN), coin.address_to_hash160(address2))]) \
.sign(account)
await self.lbrycrd.decoderawtransaction(hexlify(tx.raw))
sendtxid = await self.lbrycrd.sendrawtransaction(hexlify(tx.raw))
await self.on_transaction(sendtxid)
self.assertAlmostEqual(await self.lbrycrd.get_balance(), 5.5, places=2)
self.assertEqual(self.manager.get_balance(), 5.5)
lbrycrd_address = await self.lbrycrd.get_raw_change_address()
tx = self.manager.send_amount_to_address(5, lbrycrd_address)
await self.broadcast(tx)
await self.on_transaction(tx.id.decode())
await self.lbrycrd.generate(1)
await ledger.on_transaction.where(
lambda tx: tx.id.decode() == sendtxid
)
self.assertAlmostEqual(await self.lbrycrd.get_balance(), 11.5, places=2)
#self.assertEqual(self.manager.get_balance(), 0.5)
class AbandonClaimLookup(IntegrationTestCase):

View file

@ -42,7 +42,10 @@ class LbryWalletManager(BaseWalletManager):
def from_old_config(cls, settings):
coin_id = 'lbc_{}'.format(settings['blockchain_name'][-7:])
wallet_manager = cls.from_config({
'ledgers': {coin_id: {'default_servers': settings['lbryum_servers']}}
'ledgers': {coin_id: {
'default_servers': settings['lbryum_servers'],
'wallet_path': settings['lbryum_wallet_dir']
}}
})
ledger = wallet_manager.ledgers.values()[0]
wallet_manager.create_wallet(
@ -58,7 +61,7 @@ class LbryWalletManager(BaseWalletManager):
return self.stop_ledgers()
def get_balance(self):
return self.default_account.get_balance()
return float(self.default_account.get_balance()) / float(COIN)
def get_best_blockhash(self):
return defer.succeed('')
@ -66,42 +69,16 @@ class LbryWalletManager(BaseWalletManager):
def get_unused_address(self):
return defer.succeed(self.default_account.get_least_used_receiving_address())
def get_new_address(self):
return self.get_unused_address()
def reserve_points(self, address, amount):
# TODO: check if we have enough to cover amount
return ReservedPoints(address, amount)
def send_points_to_address(self, reserved, amount):
account = self.default_account
coin = account.coin
ledger = coin.ledger
tx_class = ledger.transaction_class
in_class, out_class = tx_class.input_class, tx_class.output_class
destination_address = reserved.identifier.encode('latin1')
outputs = [
out_class.pay_pubkey_hash(amount*COIN, coin.address_to_hash160(destination_address))
]
amount += 0.001
amount = amount*COIN
# TODO: use CoinSelector
utxos = account.get_unspent_utxos()
total = account.get_balance()
if amount < total and total-amount > 0.00001*COIN:
change_destination = account.get_least_used_change_address()
outputs.append(
out_class.pay_pubkey_hash(total-amount, coin.address_to_hash160(change_destination))
)
tx = tx_class() \
.add_inputs([in_class.spend(utxo) for utxo in utxos]) \
.add_outputs(outputs)\
.sign(account)
return ledger.broadcast(tx)
return self.send_amount_to_address(amount, destination_address)
def get_wallet_info_query_handler_factory(self):
return LBRYcrdAddressQueryHandlerFactory(self)
@ -109,6 +86,18 @@ class LbryWalletManager(BaseWalletManager):
def get_info_exchanger(self):
return LBRYcrdAddressRequester(self)
def resolve(self, *uris, **kwargs):
return defer.succeed({})
def get_name_claims(self):
return defer.succeed([])
def address_is_mine(self, address):
return defer.succeed(True)
def get_history(self):
return defer.succeed([])
class ReservedPoints:
def __init__(self, identifier, amount):