async def test_get_payments(self): """ Get all payments (incoming and outgoing) corresponding to the wallet. """ w = self.new_wallet() succeed_backend() await w.create_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None p1 = Payment(amount=30.3, payment_id=TEST_PID) p1.transaction = Transaction(hash=TEST_HASH, height=120909) p2 = Payment(amount=12.7, payment_id=TEST_PID) p2.transaction = Transaction(has=TEST_HASH, height=118909) mock_wallet.incoming = lambda **_: [p1] mock_wallet.outgoing = lambda **_: [p2] w.wallet = mock_wallet payments = await w._get_payments() self.assertEqual(2, len(payments)) w.cancel_all_pending_tasks()
async def test_get_transactions(self): """ Test for get transactions """ self.wallet.create_wallet() tx = Transaction( hash="0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060", date_time=datetime(2015, 8, 7, 3, 30, 33), from_=self.wallet.get_address().result(), to="0x5df9b87991262f6ba471f09758cde1c0fc1de734", gas=5, gas_price=5, nonce=0, is_pending=False, value=31337, block_number=46147 ) mock_provider = MockObject() mock_provider.get_latest_blocknr = lambda *_: 46147 mock_provider.get_transactions = lambda *_, **x: [tx] self.wallet.provider = mock_provider transactions = await self.wallet.get_transactions() tx_dict = { 'id': tx.hash, 'outgoing': True, 'from': self.wallet.get_address().result(), 'to': "0x5df9b87991262f6ba471f09758cde1c0fc1de734", 'amount': 31337, 'fee_amount': 25, 'currency': self.identifier, 'timestamp': time.mktime(datetime(2015, 8, 7, 3, 30, 33).timetuple()), 'description': f'Confirmations: {1}' } self.assertEqual([tx_dict], transactions)
def setUp(self): super().setUp() mock = MockObject() mock.get_account_sequence = lambda *_: 100 self.wallet = StellarTestnetWallet(self.session_base_dir, mock) self.identifier = 'TXLM' self.name = 'testnet stellar' self.tx = Transaction( hash= '96ad71731b1b46fceb0f1c32adbcc16a93cefad1e6eb167efe8a8c8e4e0cbb98', ledger_nr=26529414, date_time=datetime.fromisoformat('2020-06-05T08:45:33'), source_account= 'GDQWI6FKB72DPOJE4CGYCFQZKRPQQIOYXRMZ5KEVGXMG6UUTGJMBCASH', operation_count=1, transaction_envelope= "AAAAAOFkeKoP9De5JOCNgRYZVF8IIdi8WZ6olTXYb1KTMlgRAAAAZAGOO+wAAAA9AAAAAQAAAAAAAAAA" "AAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAADhZHiq" "D/Q3uSTgjYEWGVRfCCHYvFmeqJU12G9SkzJYEQAAAAAAAAAAXQbflbQZVvhfaHtF6ESvgGrNnl2gi440" "84MWUaGbmNkAAAAAAcnDgAAAAAAAAAABHvBc2AAAAEBUL1wo8IGHEpgpQ7llGaFE+rC9v5kk2KPJe53/" "gIdWF+792HYg5yTTmhJII97YgM+Be8yponPH0YjMjeYphewI", fee=100, is_pending=False, succeeded=True, sequence_number=112092925529161789, min_time_bound=datetime.fromisoformat('1970-01-01T00:00:00'))
def test_get_account_sequence(self): mock = MockObject() fun = lambda *_: mock # the library we use uses the builder pattern mock.account_id = fun mock.call = lambda *_: self.sample_account_response self.provider.server.accounts = fun self.assertEqual(113942965513805922, self.provider.get_account_sequence('XXX'))
def test_get_balance(self): mock = MockObject() fun = lambda *_: mock # the library we use uses the builder pattern mock.account_id = fun mock.call = lambda *_: self.sample_account_response self.provider.server.accounts = fun self.assertEqual('25.0779942', self.provider.get_balance('XXX'))
def create_fake_block(self): """ Create a dummy block and return it """ block = MockObject() block.hash = b'a' return block
def fail_backend(): mock_backend = MockObject() def fail_request(): raise ConnectionError mock_backend.accounts = lambda *_: fail_request() monero.backends.jsonrpc.JSONRPCWallet.__new__ = lambda *_, **__: mock_backend
def test_get_ledger_height(self): mock = MockObject() fun = lambda *_: mock # the library we use uses the builder pattern self.provider.server.ledgers = fun mock.limit = fun mock.order = fun mock.call = lambda: self.sample_legder_response self.assertEqual(1, self.provider.get_ledger_height())
async def test_correct_transfer(self): """ Test that the transfer method of a wallet works """ await self.wallet.create_wallet() self.wallet.get_balance = lambda: succeed({'available': 100000, 'pending': 0, 'currency': self.network_currency, 'precision': self.precision}) mock_tx = MockObject() mock_tx.hash = 'a' * 20 self.wallet.wallet.send_to = lambda *_: mock_tx await self.wallet.transfer(3000, '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF')
def test_get_address_wallet(self): """ Get Monero wallet address with wallet initialized. """ w = self.new_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda: None mock_wallet.address = lambda: TEST_ADDRESS w.wallet = mock_wallet addr = w.get_address().result() self.assertEqual(TEST_ADDRESS, addr) w.cancel_all_pending_tasks()
def on_wallet_created(_): wallet.get_balance = lambda: succeed({ 'available': 100000, 'pending': 0, 'currency': 'BTC', 'precision': 8 }) mock_tx = MockObject() mock_tx.hash = 'a' * 20 wallet.wallet.send_to = lambda *_: mock_tx wallet.transfer(3000, '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF').addCallback( lambda _: test_deferred.callback(None))
async def test_get_transactions_wallet(self): """ Test retrieval of transactions from Monero wallet in case wallet does exist. """ w = self.new_wallet() timestamp = datetime.now() p1 = IncomingPayment(amount=30.3, payment_id=TEST_PID, local_address=TEST_ADDRESS) p1.transaction = Transaction(hash=TEST_HASH, height=120909, fee=1, timestamp=timestamp, confirmations=21) p2 = IncomingPayment(amount=12.7, payment_id=TEST_PID, local_address=TEST_ADDRESS) p2.transaction = Transaction(hash=TEST_HASH, height=118909, fee=1, timestamp=timestamp, confirmations=17) ts = mktime(timestamp.timetuple()) mock_wallet = MockObject() mock_wallet.refresh = lambda: None mock_wallet.incoming = lambda **_: [p1] mock_wallet.outgoing = lambda **_: [p2] w.wallet = mock_wallet transactions = await w.get_transactions() self.assertDictEqual({ 'id': TEST_HASH, 'outgoing': False, 'from': '', 'to': TEST_ADDRESS, 'amount': 30.3, 'fee_amount': 1, 'currency': self.identifier, 'timestamp': ts, 'description': 'Confirmations: 21' }, transactions.result()[0]) self.assertDictEqual({ 'id': TEST_HASH, 'outgoing': False, 'from': '', 'to': TEST_ADDRESS, 'amount': 12.7, 'fee_amount': 1, 'currency': self.identifier, 'timestamp': ts, 'description': 'Confirmations: 17' }, transactions.result()[1]) w.cancel_all_pending_tasks()
async def test_transfer_wallet_enough(self): """ Attempt XMR transfer in case wallet exists and enough XMR available. """ w = self.new_wallet() succeed_backend() await w.create_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None mock_wallet.balance = lambda unlocked: 20.2 mock_transaction = MockObject() mock_transaction.hash = TEST_HASH mock_wallet.transfer = lambda *_, **__: succeed(mock_transaction) w.wallet = mock_wallet result = await w.transfer(20.1, 'test_address', payment_id='test_id', priority=1, unlock_time=0) self.assertEqual(TEST_HASH, result.result()) w.cancel_all_pending_tasks()
async def test_btc_wallet_transfer(wallet): """ Test that the transfer method of a BTC wallet works """ await wallet.create_wallet() wallet.get_balance = lambda: succeed({ 'available': 100000, 'pending': 0, 'currency': 'BTC', 'precision': 8 }) mock_tx = MockObject() mock_tx.hash = 'a' * 20 wallet.wallet.send_to = lambda *_: mock_tx await wallet.transfer(3000, '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF')
async def test_get_outgoing_payments_wallet(self): """ Get outgoing payments for Monero wallet in case wallet exists. """ w = self.new_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None mock_wallet.outgoing = lambda **_: [Payment()] w.wallet = mock_wallet result = await w.get_outgoing_payments(False) self.assertIsNotNone(result) self.assertTrue(isinstance(result[0], Payment)) w.cancel_all_pending_tasks()
async def test_get_transactions_created(self): """ Test for get_transactions when wallet has been created """ self.wallet.database.insert_transaction(self.tx) self.wallet.get_address = lambda: succeed( 'GBOQNX4VWQMVN6C7NB5UL2CEV6AGVTM6LWQIXDRU6OBRMUNBTOMNSOAW') self.wallet.provider = MockObject() self.wallet.provider.get_ledger_height = lambda: 26529414 self.wallet.provider.get_transactions = lambda *_: [] self.wallet.created = True self.wallet.created_on_network = True payment_dict = { 'id': '96ad71731b1b46fceb0f1c32adbcc16a93cefad1e6eb167efe8a8c8e4e0cbb98', # use tx hash for now 'outgoing': False, 'from': 'GDQWI6FKB72DPOJE4CGYCFQZKRPQQIOYXRMZ5KEVGXMG6UUTGJMBCASH', 'to': 'GBOQNX4VWQMVN6C7NB5UL2CEV6AGVTM6LWQIXDRU6OBRMUNBTOMNSOAW', 'amount': 30000000, 'fee_amount': 100, 'currency': self.identifier, 'timestamp': None, # timestamp is timezone specific 'description': f'confirmations: 1' } payments_from_wallet = await self.wallet.get_transactions() payments_from_wallet[0][ 'timestamp'] = None # timestamp is timezone specific self.assertEqual([payment_dict], payments_from_wallet)
async def test_get_balance_wallet(self): """ Check balance in case wallet connection exists. """ w = self.new_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None mock_wallet.balance = lambda unlocked: 20.2 w.wallet = mock_wallet self.assertDictEqual({ 'available': 20.2, 'pending': 0, 'currency': self.identifier, 'precision': 12 }, await w.get_balance()) w.cancel_all_pending_tasks()
async def test_transfer_multiple_wallet_not_enough(self): """ Make multiple transfers at once, but not enough balance. """ w = self.new_wallet() transfers = [ (TEST_ADDRESS, Decimal('20.2')), (TEST_ADDRESS, Decimal('7.8')) ] mock_wallet = MockObject() mock_wallet.refresh = lambda: None mock_wallet.balance = lambda **_: 21.3 w.wallet = mock_wallet self.assertAsyncRaises(InsufficientFunds, await w.transfer_multiple(transfers, priority=3)) w.cancel_all_pending_tasks()
async def test_get_confirmations_wallet(self): """ Verify number of confirmations returned in case wallet does exist. """ w = self.new_wallet() self.assertIsNone(w.wallet) succeed_backend() await w.create_wallet() self.assertIsNotNone(w.wallet) mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None mock_wallet.confirmations = lambda *_: 4 w.wallet = mock_wallet p = Payment() self.assertEqual(4, await w.get_confirmations(p)) w.cancel_all_pending_tasks()
async def test_get_balance(self): """ Test for getting the balance of a created wallet. """ self.wallet.create_wallet() mock_obj = MockObject() mock_obj.get_balance = lambda *_: 992 self.wallet.provider = mock_obj self.wallet.get_transactions = lambda *_: [] self.wallet._update_database = lambda *_: None self.wallet.get_outgoing_amount = lambda *_: 0 self.wallet.get_incoming_amount = lambda *_: 0 balance = { 'available': 992, 'pending': 0, 'currency': self.identifier, 'precision': 18 } self.assertEqual(balance, await self.wallet.get_balance())
async def test_transfer_wallet_not_enough(self): """ Attempt XMR transfer in case not enough XMR available. """ w = self.new_wallet() succeed_backend() await w.create_wallet() mock_wallet = MockObject() mock_wallet.refresh = lambda *_: None mock_wallet.balance = lambda unlocked: 20.2 w.wallet = mock_wallet self.assertAsyncRaises(InsufficientFunds, await w.transfer(47.8, 'test_address', payment_id='test_id', priority=1, unlock_time=0)) w.cancel_all_pending_tasks()
def test_get_transactions(self): mock = MockObject() fun = lambda *_: mock # the library we use uses the builder pattern mock.for_account = fun mock.include_failed = fun mock.limit = fun mock.call = lambda *_: self.sample_get_txs_response self.provider.server.transactions = lambda: mock tx = Transaction(hash='96ad71731b1b46fceb0f1c32adbcc16a93cefad1e6eb167efe8a8c8e4e0cbb98', ledger_nr=26529414, date_time=datetime.fromisoformat('2019-10-29T00:50:35'), source_account='DQWI6FKB72DPOJE4CGYCFQZKRPQQIOYXRMZ5KEVGXMG6UUTGJMBCASH', operation_count=1, transaction_envelope="AAAAAOFkeKoP9De5JOCNgRYZVF8IIdi8WZ6olTXYb1KTMlgRAAAAZAGOO" "+wAAAA9AAAAAQAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAABAAAAAQAAAADhZHiqD/Q3uSTgjYEWGVRfCCHYvFmeqJU12G9SkzJYEQA" "AAAAAAAAAXQbflbQZVvhfaHtF6ESvgGrNnl2gi44084MWUaGbmNkAAAAAAcnDgAAAAAAAAA" "ABHvBc2AAAAEBUL1wo8IGHEpgpQ7llGaFE+rC9v5kk2KPJe53/gIdWF+792HYg5yTTmhJII" "97YgM+Be8yponPH0YjMjeYphewI", fee=100, is_pending=False, succeeded=True, sequence_number=112092925529161789, min_time_bound=datetime.fromisoformat('1970-01-01T00:00:00')) self.assertEqual([tx], self.provider.get_transactions('XXX'))
def test_normalize_transaction_outgoing_payment(self): """ Verify monero.transaction.Transaction instances are correctly formatted. """ w = self.new_wallet() payment = OutgoingPayment() payment.local_address = TEST_ADDRESS payment.amount = 11.8 timestamp = datetime.now() mock_transaction = MockObject() mock_transaction.hash = TEST_HASH mock_transaction.fee = 0.3 ts = mktime(timestamp.timetuple()) mock_transaction.timestamp = timestamp mock_transaction.confirmations = 23 payment.transaction = mock_transaction self.assertDictEqual({ 'id': TEST_HASH, 'outgoing': True, 'from': TEST_ADDRESS, 'to': '', 'amount': 11.8, 'fee_amount': 0.3, 'currency': self.identifier, 'timestamp': ts, 'description': 'Confirmations: 23' }, w._normalize_transaction(payment)) w.cancel_all_pending_tasks()
async def test_transfer_multiple_wallet(self): """ Make multiple transfers at once, enough balance. """ w = self.new_wallet() transfers = [ (TEST_ADDRESS, Decimal('20.2')), (TEST_ADDRESS, Decimal('7.8')) ] mock_wallet = MockObject() mock_wallet.refresh = lambda: None mock_wallet.balance = lambda **_: 57.3 mock_wallet.transfer_multiple = \ lambda *_, **__: succeed([(Transaction(hash=TEST_HASH), Decimal('20.2')), (Transaction(hash=TEST_HASH), Decimal('7.8'))]) w.wallet = mock_wallet hashes = await w.transfer_multiple(transfers, priority=3) self.assertEqual(2, len(hashes.result())) w.cancel_all_pending_tasks()
async def test_monitor_transactions_not_found(self): """ Test for monitor_transactions when the transaction is not found """ self.wallet.created = True self.wallet.get_address = lambda: succeed( 'GBOQNX4VWQMVN6C7NB5UL2CEV6AGVTM6LWQIXDRU6OBRMUNBTOMNSOAW') self.wallet.provider = MockObject() self.wallet.provider.get_ledger_height = lambda: 26529414 self.wallet.provider.get_transactions = lambda *_: [] future = self.wallet.monitor_transaction( '96ad71731b1b46fceb0f1c32adbcc16a93cefad1e6eb167efe8a8c8e4e0cbb98', interval=1) # the monitor transaction runs every 5 secs so this should be enough time.sleep(2) self.assertFalse(future.done())
async def test_monitor_transactions_found(self): """ Test for monitor_transactions when the transaction is found :return: """ self.wallet.created = True self.wallet.created_on_network = True self.wallet.database.insert_transaction(self.tx) self.wallet.get_address = lambda: succeed( 'GDQWI6FKB72DPOJE4CGYCFQZKRPQQIOYXRMZ5KEVGXMG6UUTGJMBCASH') self.wallet.provider = MockObject() self.wallet.provider.get_ledger_height = lambda: 26529414 self.wallet.provider.get_transactions = lambda *_: [] self.assertIsNone(await self.wallet.monitor_transaction( '96ad71731b1b46fceb0f1c32adbcc16a93cefad1e6eb167efe8a8c8e4e0cbb98', interval=1))
def test_address_resolv_fail(self): """ Test whether an order is unreserved when address resolution fails """ yield self.introduce_nodes() # Clean the mid register of node 1 and make sure DHT peer connection fails self.nodes[1].overlay.mid_register = {} self.nodes[1].overlay.dht = MockObject() self.nodes[1].overlay.dht.connect_peer = lambda *_: fail( Failure(RuntimeError())) ask_order = yield self.nodes[0].overlay.create_ask( AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600) bid_order = yield self.nodes[1].overlay.create_bid( AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600) yield self.sleep(0.5) self.assertEqual(ask_order.reserved_quantity, 0) self.assertEqual(bid_order.reserved_quantity, 0)
async def test_address_resolv_fail(self): """ Test whether an order is unreserved when address resolution fails """ await self.introduce_nodes() def mock_connect_peer(_): raise DHTError() # Clean the mid register of node 1 and make sure DHT peer connection fails self.nodes[1].overlay.mid_register = {} self.nodes[1].overlay.dht = MockObject() self.nodes[1].overlay.dht.connect_peer = mock_connect_peer ask_order = await self.nodes[0].overlay.create_ask( AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600) bid_order = await self.nodes[1].overlay.create_bid( AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600) await sleep(0.5) self.assertEqual(ask_order.reserved_quantity, 0) self.assertEqual(bid_order.reserved_quantity, 0)
def new_wallet(self): mock = MockObject() mock.get_account_sequence = lambda *_: 100 mock.check_account_created = lambda *_: None return StellarWallet(self.session_base_dir, mock)
async def test_get_transactions(wallet): """ Test whether transactions in bitcoinlib are correctly returned """ raw_tx = '02000000014bca66ebc0e3ab0c5c3aec6d0b3895b968497397752977dfd4a2f0bc67db6810000000006b483045022100fc' \ '93a034db310fbfead113283da95e980ac7d867c7aa4e6ef0aba80ef321639e02202bc7bd7b821413d814d9f7d6fc76ff46' \ 'b9cd3493173ef8d5fac40bce77a7016d01210309702ce2d5258eacc958e5a925b14de912a23c6478b8e2fb82af43d20212' \ '14f3feffffff029c4e7020000000001976a914d0115029aa5b2d2db7afb54a6c773ad536d0916c88ac90f4f70000000000' \ '1976a914f0eabff37e597b930647a3ec5e9df2e0fed0ae9888ac108b1500' mock_wallet = MockObject() mock_wallet.transactions_update = lambda **_: None mock_wallet._session = MockObject() mock_all = MockObject() mock_all.all = lambda *_: [(raw_tx, 3, datetime.datetime( 2012, 9, 16, 0, 0), 12345)] mock_filter = MockObject() mock_filter.filter = lambda *_: mock_all mock_wallet._session.query = lambda *_: mock_filter wallet.wallet = mock_wallet wallet.wallet.wallet_id = 3 mock_key = MockObject() mock_key.address = 'n3Uogo82Tyy76ZNuxmFfhJiFqAUbJ5BPho' wallet.wallet.keys = lambda **_: [mock_key] wallet.created = True transactions = await wallet.get_transactions() assert transactions assert transactions[0]["fee_amount"] == 12345 assert transactions[0]["amount"] == 16250000