2018-11-03 18:50:34 -04:00
|
|
|
import logging
|
2018-07-01 17:20:17 -04:00
|
|
|
import asyncio
|
2018-11-04 01:55:50 -04:00
|
|
|
from torba.testcase import IntegrationTestCase
|
|
|
|
from torba.client.constants import COIN
|
2018-06-07 23:47:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BasicTransactionTests(IntegrationTestCase):
|
|
|
|
|
2018-11-04 01:55:50 -04:00
|
|
|
VERBOSITY = logging.WARN
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-06-13 21:37:02 -04:00
|
|
|
async def test_sending_and_receiving(self):
|
2018-06-07 23:47:46 -04:00
|
|
|
account1, account2 = self.account, self.wallet.generate_account(self.ledger)
|
2018-11-18 22:54:00 -05:00
|
|
|
await self.ledger.subscribe_account(account2)
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-11-18 22:54:00 -05:00
|
|
|
await self.assertBalance(account1, '0.0')
|
|
|
|
await self.assertBalance(account2, '0.0')
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-07-01 17:20:17 -04:00
|
|
|
sendtxids = []
|
2018-07-14 21:34:07 -04:00
|
|
|
for i in range(5):
|
2018-10-14 22:16:51 -04:00
|
|
|
address1 = await account1.receiving.get_or_create_usable_address()
|
2018-07-14 21:34:07 -04:00
|
|
|
sendtxid = await self.blockchain.send_to_address(address1, 1.1)
|
2018-07-01 17:20:17 -04:00
|
|
|
sendtxids.append(sendtxid)
|
|
|
|
await self.on_transaction_id(sendtxid) # mempool
|
2018-06-12 10:02:04 -04:00
|
|
|
await self.blockchain.generate(1)
|
2018-07-01 17:20:17 -04:00
|
|
|
await asyncio.wait([ # confirmed
|
|
|
|
self.on_transaction_id(txid) for txid in sendtxids
|
|
|
|
])
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-11-18 22:54:00 -05:00
|
|
|
await self.assertBalance(account1, '5.5')
|
|
|
|
await self.assertBalance(account2, '0.0')
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-10-14 22:16:51 -04:00
|
|
|
address2 = await account2.receiving.get_or_create_usable_address()
|
2018-06-26 20:22:35 -04:00
|
|
|
hash2 = self.ledger.address_to_hash160(address2)
|
2018-10-14 22:16:51 -04:00
|
|
|
tx = await self.ledger.transaction_class.create(
|
2018-08-03 10:41:40 -04:00
|
|
|
[],
|
2018-06-26 20:22:35 -04:00
|
|
|
[self.ledger.transaction_class.output_class.pay_pubkey_hash(2*COIN, hash2)],
|
2018-06-12 10:02:04 -04:00
|
|
|
[account1], account1
|
2018-10-14 22:16:51 -04:00
|
|
|
)
|
2018-06-07 23:47:46 -04:00
|
|
|
await self.broadcast(tx)
|
2018-11-07 14:42:17 -05:00
|
|
|
await self.ledger.wait(tx) # mempool
|
2018-06-13 20:57:57 -04:00
|
|
|
await self.blockchain.generate(1)
|
2018-11-07 14:42:17 -05:00
|
|
|
await self.ledger.wait(tx) # confirmed
|
2018-06-07 23:47:46 -04:00
|
|
|
|
2018-11-18 22:54:00 -05:00
|
|
|
await self.assertBalance(account1, '3.499802')
|
|
|
|
await self.assertBalance(account2, '2.0')
|
2018-07-04 21:30:38 -04:00
|
|
|
|
2018-10-14 22:16:51 -04:00
|
|
|
utxos = await self.account.get_utxos()
|
|
|
|
tx = await self.ledger.transaction_class.create(
|
2018-08-03 10:41:40 -04:00
|
|
|
[self.ledger.transaction_class.input_class.spend(utxos[0])],
|
|
|
|
[],
|
|
|
|
[account1], account1
|
2018-10-14 22:16:51 -04:00
|
|
|
)
|
2018-07-09 22:02:18 -04:00
|
|
|
await self.broadcast(tx)
|
2018-11-07 14:42:17 -05:00
|
|
|
await self.ledger.wait(tx) # mempool
|
2018-07-09 22:02:18 -04:00
|
|
|
await self.blockchain.generate(1)
|
2018-11-07 14:42:17 -05:00
|
|
|
await self.ledger.wait(tx) # confirmed
|
2018-09-21 22:18:30 -04:00
|
|
|
|
2018-10-14 22:16:51 -04:00
|
|
|
txs = await account1.get_transactions()
|
2018-09-21 22:18:30 -04:00
|
|
|
tx = txs[1]
|
|
|
|
self.assertEqual(round(tx.inputs[0].txo_ref.txo.amount/COIN, 1), 1.1)
|
|
|
|
self.assertEqual(round(tx.inputs[1].txo_ref.txo.amount/COIN, 1), 1.1)
|
|
|
|
self.assertEqual(round(tx.outputs[0].amount/COIN, 1), 2.0)
|
|
|
|
self.assertEqual(tx.outputs[0].get_address(self.ledger), address2)
|
|
|
|
self.assertEqual(tx.outputs[0].is_change, False)
|
|
|
|
self.assertEqual(tx.outputs[1].is_change, True)
|