Esempio n. 1
0
 def get_tx_done_block(ask_amount, bid_amount, traded_amount,
                       ask_total_traded, bid_total_traded):
     ask_pair = AssetPair(AssetAmount(ask_amount, 'BTC'),
                          AssetAmount(ask_amount, 'MB'))
     bid_pair = AssetPair(AssetAmount(bid_amount, 'BTC'),
                          AssetAmount(bid_amount, 'MB'))
     ask = Order(OrderId(TraderId(b'0' * 20), OrderNumber(1)), ask_pair,
                 Timeout(3600), Timestamp.now(), True)
     ask._traded_quantity = ask_total_traded
     bid = Order(OrderId(TraderId(b'1' * 20), OrderNumber(1)), bid_pair,
                 Timeout(3600), Timestamp.now(), False)
     bid._traded_quantity = bid_total_traded
     tx = Transaction(
         TransactionId(b'a' * 32),
         AssetPair(AssetAmount(traded_amount, 'BTC'),
                   AssetAmount(traded_amount, 'MB')),
         OrderId(TraderId(b'0' * 20), OrderNumber(1)),
         OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0))
     tx.transferred_assets.first += AssetAmount(traded_amount, 'BTC')
     tx.transferred_assets.second += AssetAmount(traded_amount, 'MB')
     tx_done_block = MarketBlock()
     tx_done_block.type = b'tx_done'
     tx_done_block.transaction = {
         'ask': ask.to_status_dictionary(),
         'bid': bid.to_status_dictionary(),
         'tx': tx.to_block_dictionary(),
         'version': MarketCommunity.PROTOCOL_VERSION
     }
     tx_done_block.transaction['ask']['address'], tx_done_block.transaction[
         'ask']['port'] = "1.1.1.1", 1234
     tx_done_block.transaction['bid']['address'], tx_done_block.transaction[
         'bid']['port'] = "1.1.1.1", 1234
     return tx_done_block
Esempio n. 2
0
class TransactionTestSuite(unittest.TestCase):
    """Transaction test cases."""
    def setUp(self):
        # Object creation
        self.maxDiff = None
        self.transaction_id = TransactionId(b'a' * 32)
        self.transaction = Transaction(
            self.transaction_id,
            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')),
            OrderId(TraderId(b'3' * 20), OrderNumber(2)),
            OrderId(TraderId(b'2' * 20), OrderNumber(1)), Timestamp(0))
        self.proposed_trade = Trade.propose(
            TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)),
            OrderId(TraderId(b'1' * 20), OrderNumber(3)),
            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')),
            Timestamp(0))
        self.payment = Payment(TraderId(b'0' * 20), TransactionId(b'a' * 32),
                               AssetAmount(100, 'MB'), WalletAddress('a'),
                               WalletAddress('b'), PaymentId('aaa'),
                               Timestamp(4))
        self.payment2 = Payment(TraderId(b'0' * 20), TransactionId(b'a' * 32),
                                AssetAmount(100, 'BTC'), WalletAddress('a'),
                                WalletAddress('b'), PaymentId('aaa'),
                                Timestamp(4))

    def test_add_payment(self):
        """
        Test the addition of a payment to a transaction
        """
        self.transaction.add_payment(self.payment)
        self.assertEqual(self.transaction.transferred_assets.first.amount, 0)
        self.assertEqual(self.transaction.transferred_assets.second.amount,
                         100)
        self.assertTrue(self.transaction.payments)

    def test_next_payment(self):
        """
        Test the process of determining the next payment details during a transaction
        """
        self.assertEqual(self.transaction.next_payment(True),
                         AssetAmount(100, 'BTC'))
        self.assertEqual(self.transaction.next_payment(False),
                         AssetAmount(100, 'MB'))

    def test_is_payment_complete(self):
        """
        Test whether a payment is correctly marked as complete
        """
        self.assertFalse(self.transaction.is_payment_complete())
        self.transaction.add_payment(self.payment)
        self.assertFalse(self.transaction.is_payment_complete())
        self.transaction._transferred_assets = AssetPair(
            AssetAmount(100, 'BTC'), AssetAmount(100, 'MB'))
        self.assertTrue(self.transaction.is_payment_complete())

    def test_to_dictionary(self):
        """
        Test the to dictionary method of a transaction
        """
        self.assertDictEqual(
            self.transaction.to_block_dictionary(), {
                'trader_id': "33" * 20,
                'transaction_id': "61" * 32,
                'order_number': 2,
                'partner_trader_id': "32" * 20,
                'partner_order_number': 1,
                'assets': {
                    'first': {
                        'amount': 100,
                        'type': 'BTC',
                    },
                    'second': {
                        'amount': 100,
                        'type': 'MB'
                    }
                },
                'transferred': {
                    'first': {
                        'amount': 0,
                        'type': 'BTC',
                    },
                    'second': {
                        'amount': 0,
                        'type': 'MB'
                    }
                },
                'timestamp': 0,
            })

    def test_status(self):
        """
        Test the status of a transaction
        """
        self.assertEqual(self.transaction.status, 'pending')

        self.transaction.add_payment(self.payment)
        self.transaction.add_payment(self.payment2)
        self.assertEqual(self.transaction.status, 'completed')