Ejemplo n.º 1
0
    def test_from_network(self):
        # Test for from network
        data = StartTransaction.from_network(
            type(
                'Data', (object, ), {
                    "trader_id":
                    TraderId(b'0' * 20),
                    "transaction_id":
                    TransactionId(TraderId(b'0' * 20), TransactionNumber(1)),
                    "order_id":
                    OrderId(TraderId(b'0' * 20), OrderNumber(1)),
                    "recipient_order_id":
                    OrderId(TraderId(b'1' * 20), OrderNumber(2)),
                    "proposal_id":
                    1235,
                    "assets":
                    AssetPair(AssetAmount(30, 'BTC'), AssetAmount(40, 'MC')),
                    "timestamp":
                    Timestamp(0)
                }))

        self.assertEquals(TraderId(b'0' * 20), data.trader_id)
        self.assertEquals(
            TransactionId(TraderId(b'0' * 20), TransactionNumber(1)),
            data.transaction_id)
        self.assertEquals(OrderId(TraderId(b'0' * 20), OrderNumber(1)),
                          data.order_id)
        self.assertEquals(OrderId(TraderId(b'1' * 20), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1235, data.proposal_id)
        self.assertEquals(Timestamp(0), data.timestamp)
Ejemplo n.º 2
0
    def test_from_network(self):
        # Test for from network
        data = Payment.from_network(
            type(
                'Data', (object, ), {
                    "trader_id":
                    TraderId(b'0' * 20),
                    "transaction_id":
                    TransactionId(TraderId(b'2' * 20), TransactionNumber(2)),
                    "transferred_assets":
                    AssetAmount(3, 'BTC'),
                    "address_from":
                    WalletAddress('a'),
                    "address_to":
                    WalletAddress('b'),
                    "payment_id":
                    PaymentId('aaa'),
                    "timestamp":
                    Timestamp(4000),
                    "success":
                    True
                }))

        self.assertEquals(TraderId(b'0' * 20), data.trader_id)
        self.assertEquals(
            TransactionId(TraderId(b'2' * 20), TransactionNumber(2)),
            data.transaction_id)
        self.assertEquals(AssetAmount(3, 'BTC'), data.transferred_assets)
        self.assertEquals(Timestamp(4000), data.timestamp)
        self.assertTrue(data.success)
Ejemplo n.º 3
0
class TransactionIdTestSuite(unittest.TestCase):
    """Transaction ID test cases."""
    def setUp(self):
        # Object creation
        self.transaction_id = TransactionId(TraderId(b'0' * 20),
                                            TransactionNumber(1))
        self.transaction_id2 = TransactionId(TraderId(b'0' * 20),
                                             TransactionNumber(1))
        self.transaction_id3 = TransactionId(TraderId(b'0' * 20),
                                             TransactionNumber(2))

    def test_properties(self):
        # Test for properties
        self.assertEqual(TraderId(b'0' * 20), self.transaction_id.trader_id)
        self.assertEqual(TransactionNumber(1),
                         self.transaction_id.transaction_number)

    def test_conversion(self):
        # Test for conversions
        self.assertEqual('%s.1' % ("30" * 20), str(self.transaction_id))

    def test_equality(self):
        # Test for equality
        self.assertTrue(self.transaction_id == self.transaction_id2)
        self.assertTrue(self.transaction_id != self.transaction_id3)
        self.assertFalse(self.transaction_id == 6)

    def test_hash(self):
        # Test for hashes
        self.assertEqual(self.transaction_id.__hash__(),
                         self.transaction_id2.__hash__())
        self.assertNotEqual(self.transaction_id.__hash__(),
                            self.transaction_id3.__hash__())
 def test_next_identity(self):
     # Test for next identity
     self.assertEquals(
         TransactionId(TraderId(b'0' * 20), TransactionNumber(1)),
         self.memory_transaction_repository.next_identity())
     self.assertEquals(
         TransactionId(TraderId(b'0' * 20), TransactionNumber(2)),
         self.memory_transaction_repository.next_identity())
Ejemplo n.º 5
0
 def from_unpack_list(cls, trader_id, timestamp, transaction_id,
                      incoming_address, outgoing_address):
     return WalletInfoPayload(
         TraderId(trader_id), Timestamp(timestamp),
         TransactionId(transaction_id),
         WalletAddress(incoming_address.decode('utf-8')),
         WalletAddress(outgoing_address.decode('utf-8')))
Ejemplo n.º 6
0
 def setUp(self):
     # Object creation
     self.payment = Payment(
         TraderId(b'0' * 20),
         TransactionId(TraderId(b'2' * 20), TransactionNumber(2)),
         AssetAmount(3, 'BTC'), WalletAddress('a'), WalletAddress('b'),
         PaymentId('aaa'), Timestamp(4000), True)
Ejemplo n.º 7
0
 def next_identity(self):
     """
     :rtype: TransactionId
     """
     self._next_id += 1
     return TransactionId(TraderId(self._mid),
                          TransactionNumber(self._next_id))
Ejemplo n.º 8
0
 def from_unpack_list(cls, trader_id, timestamp, transaction_trader_id, transaction_number,
                      transferred_amount, transferred_type, address_from, address_to, payment_id, success):
     return PaymentPayload(TraderId(trader_id), Timestamp(timestamp),
                           TransactionId(TraderId(transaction_trader_id), TransactionNumber(transaction_number)),
                           AssetAmount(transferred_amount, transferred_type.decode('utf-8')),
                           WalletAddress(address_from.decode('utf-8')),
                           WalletAddress(address_to.decode('utf-8')), PaymentId(payment_id.decode('utf-8')), success)
Ejemplo n.º 9
0
    def setUp(self):
        # Object creation
        self.transaction_id = TransactionId(b'a' * 32)
        self.transaction = Transaction(
            self.transaction_id,
            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')),
            OrderId(TraderId(b'0' * 20), OrderNumber(2)),
            OrderId(TraderId(b'1' * 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(30, 'MC')),
            Timestamp(0))

        self.tick = Tick(
            OrderId(TraderId(b'0' * 20), OrderNumber(1)),
            AssetPair(AssetAmount(5, 'BTC'), AssetAmount(5, 'MC')), Timeout(0),
            Timestamp(00), True)
        self.tick2 = Tick(
            OrderId(TraderId(b'0' * 20), OrderNumber(2)),
            AssetPair(AssetAmount(500, 'BTC'), AssetAmount(5, 'MC')),
            Timeout(0), Timestamp(0), True)

        self.order_timestamp = Timestamp.now()
        self.order = Order(
            OrderId(TraderId(b'0' * 20), OrderNumber(3)),
            AssetPair(AssetAmount(50, 'BTC'), AssetAmount(40, 'MC')),
            Timeout(5000), self.order_timestamp, False)
        self.order.set_verified()
        self.order2 = Order(
            OrderId(TraderId(b'0' * 20), OrderNumber(4)),
            AssetPair(AssetAmount(50, 'BTC'), AssetAmount(10, 'MC')),
            Timeout(5), Timestamp(int(time.time() * 1000) - 1000 * 1000), True)
        self.order2.set_verified()
Ejemplo n.º 10
0
 def next_identity(self):
     """
     :rtype: TransactionId
     """
     return TransactionId(
         TraderId(self._mid),
         TransactionNumber(self.persistence.get_next_transaction_number()))
Ejemplo n.º 11
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(TraderId(b'0' * 20), TransactionNumber(1)),
         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_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
Ejemplo n.º 12
0
    async def setUp(self):
        super(TestDatabase, self).setUp()

        path = os.path.join(self.getStateDir(), 'sqlite')
        if not os.path.exists(path):
            os.makedirs(path)

        self.database = MarketDB(self.getStateDir(), 'market')

        self.order_id1 = OrderId(TraderId(b'3' * 20), OrderNumber(4))
        self.order_id2 = OrderId(TraderId(b'4' * 20), OrderNumber(5))
        self.order1 = Order(
            self.order_id1,
            AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')),
            Timeout(3600), Timestamp.now(), True)
        self.order2 = Order(
            self.order_id2,
            AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')),
            Timeout(3600), Timestamp.now(), False)
        self.order2.reserve_quantity_for_tick(
            OrderId(TraderId(b'3' * 20), OrderNumber(4)), 3)

        self.transaction_id1 = TransactionId(b'a' * 32)
        self.transaction1 = Transaction(
            self.transaction_id1,
            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')),
            OrderId(TraderId(b'0' * 20), OrderNumber(1)),
            OrderId(TraderId(b'1' * 20), OrderNumber(2)), Timestamp(20000))

        self.payment1 = Payment(TraderId(b'0' * 20), self.transaction_id1,
                                AssetAmount(5, 'BTC'), WalletAddress('abc'),
                                WalletAddress('def'), PaymentId("abc"),
                                Timestamp(20000))

        self.transaction1.add_payment(self.payment1)
Ejemplo n.º 13
0
def test_get_specific_transaction(db, transaction):
    """
    Test the retrieval of a specific transaction
    """
    transaction_id = TransactionId(b'a' * 32)
    assert not db.get_transaction(transaction_id)
    db.add_transaction(transaction)
    assert db.get_transaction(transaction_id)
Ejemplo n.º 14
0
 def test_get_specific_transaction(self):
     """
     Test the retrieval of a specific transaction
     """
     transaction_id = TransactionId(b'a' * 32)
     self.assertIsNone(self.database.get_transaction(transaction_id))
     self.database.add_transaction(self.transaction1)
     self.assertIsNotNone(self.database.get_transaction(transaction_id))
Ejemplo n.º 15
0
 def setUp(self):
     # Object creation
     self.start_transaction = StartTransaction(
         TraderId(b'0' * 20),
         TransactionId(TraderId(b'0' * 20), TransactionNumber(1)),
         OrderId(TraderId(b'0' * 20), OrderNumber(1)),
         OrderId(TraderId(b'1' * 20), OrderNumber(1)), 1234,
         AssetPair(AssetAmount(30, 'BTC'), AssetAmount(40, 'MC')),
         Timestamp(0))
Ejemplo n.º 16
0
 def get_all_transactions(self):
     """
     Return all transactions in the database.
     """
     db_result = self.execute(u"SELECT * FROM transactions")
     return [Transaction.from_database(db_item,
                                       self.get_payments(TransactionId(TraderId(bytes(db_item[0])),
                                                                       TransactionNumber(db_item[1]))))
             for db_item in db_result]
Ejemplo n.º 17
0
 def from_unpack_list(cls, trader_id, timestamp, tx_trader_id, transaction_number,
                      order_trader_id, order_number, recipient_trader_id, recipient_order_number, proposal_id,
                      asset1_amount, asset1_type, asset2_amount, asset2_type):
     return StartTransactionPayload(TraderId(trader_id), Timestamp(timestamp),
                                    TransactionId(TraderId(tx_trader_id), TransactionNumber(transaction_number)),
                                    OrderId(TraderId(order_trader_id), OrderNumber(order_number)),
                                    OrderId(TraderId(recipient_trader_id), OrderNumber(recipient_order_number)),
                                    proposal_id, AssetPair(AssetAmount(asset1_amount, asset1_type.decode('utf-8')),
                                                           AssetAmount(asset2_amount, asset2_type.decode('utf-8'))))
Ejemplo n.º 18
0
 def test_get_specific_transaction(self):
     """
     Test the retrieval of a specific transaction
     """
     transaction_id = TransactionId(TraderId(b'0' * 20),
                                    TransactionNumber(4))
     self.assertIsNone(self.database.get_transaction(transaction_id))
     self.database.add_transaction(self.transaction1)
     self.assertIsNotNone(self.database.get_transaction(transaction_id))
Ejemplo n.º 19
0
 def setUp(self):
     # Object creation
     self.transaction_id = TransactionId(TraderId(b'0' * 20),
                                         TransactionNumber(1))
     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(TraderId(b'2' * 20), TransactionNumber(2)),
         AssetAmount(3, 'MB'), WalletAddress('a'), WalletAddress('b'),
         PaymentId('aaa'), Timestamp(4), True)
Ejemplo n.º 20
0
def transaction(payment):
    transaction_id = TransactionId(b'a' * 32)
    transaction = Transaction(
        transaction_id,
        AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')),
        OrderId(TraderId(b'0' * 20), OrderNumber(1)),
        OrderId(TraderId(b'1' * 20), OrderNumber(2)), Timestamp(20000))

    transaction.add_payment(payment)
    return transaction
 def setUp(self):
     # Object creation
     self.memory_transaction_repository = MemoryTransactionRepository(b'0' *
                                                                      20)
     self.transaction_id = TransactionId(b'a' * 32)
     self.transaction = Transaction(
         self.transaction_id,
         AssetPair(AssetAmount(10, 'BTC'), AssetAmount(10, 'MB')),
         OrderId(TraderId(b'0' * 20), OrderNumber(1)),
         OrderId(TraderId(b'2' * 20), OrderNumber(2)), Timestamp(0))
Ejemplo n.º 22
0
def tx_done_block(ask, bid, accepted_trade):
    tx_done_block = MarketBlock()
    tx_done_block.type = b'tx_done'
    transaction = Transaction.from_accepted_trade(accepted_trade,
                                                  TransactionId(b'a' * 32))
    tx_done_block.transaction = {
        'ask': ask.to_block_dict(),
        'bid': bid.to_block_dict(),
        'tx': transaction.to_block_dictionary()
    }
Ejemplo n.º 23
0
    def setUp(self):
        BaseTestCase.setUp(self)

        self.ask = Ask(
            OrderId(TraderId(b'0' * 20), OrderNumber(1)),
            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp(0), True)
        self.bid = Ask(
            OrderId(TraderId(b'1' * 20), OrderNumber(1)),
            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp(0), False)
        self.transaction = Transaction(
            TransactionId(TraderId(b'0' * 20), TransactionNumber(1)),
            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
            OrderId(TraderId(b'0' * 20), OrderNumber(1)),
            OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0))

        ask_tx = self.ask.to_block_dict()
        bid_tx = self.bid.to_block_dict()

        self.tick_block = MarketBlock()
        self.tick_block.type = b'ask'
        self.tick_block.transaction = {'tick': ask_tx}

        self.cancel_block = MarketBlock()
        self.cancel_block.type = b'cancel_order'
        self.cancel_block.transaction = {
            'trader_id': 'a' * 20,
            'order_number': 1
        }

        self.tx_block = MarketBlock()
        self.tx_block.type = b'tx_init'
        self.tx_block.transaction = {
            'ask': ask_tx,
            'bid': bid_tx,
            'tx': self.transaction.to_dictionary()
        }

        payment = {
            'trader_id': 'a' * 40,
            'transaction_number': 3,
            'transferred': {
                'amount': 3,
                'type': 'BTC'
            },
            'payment_id': 'a',
            'address_from': 'a',
            'address_to': 'b',
            'timestamp': 1234,
            'success': True
        }
        self.payment_block = MarketBlock()
        self.payment_block.type = b'tx_payment'
        self.payment_block.transaction = {'payment': payment}
Ejemplo n.º 24
0
    def from_database(cls, data):
        """
        Create a Payment object based on information in the database.
        """
        (trader_id, transaction_id, payment_id, transferred_amount,
         transferred_id, address_from, address_to, timestamp) = data

        transaction_id = TransactionId(bytes(transaction_id))
        return cls(TraderId(bytes(trader_id)), transaction_id,
                   AssetAmount(transferred_amount, str(transferred_id)),
                   WalletAddress(str(address_from)),
                   WalletAddress(str(address_to)), PaymentId(str(payment_id)),
                   Timestamp(timestamp))
Ejemplo n.º 25
0
    def test_to_network(self):
        # Test for to network
        data = self.payment.to_network()

        self.assertEquals(data[0], TraderId(b'0' * 20))
        self.assertEquals(data[1], Timestamp(4000))
        self.assertEquals(
            data[2], TransactionId(TraderId(b'2' * 20), TransactionNumber(2)))
        self.assertEquals(data[3], AssetAmount(3, 'BTC'))
        self.assertEquals(data[4], WalletAddress('a'))
        self.assertEquals(data[5], WalletAddress('b'))
        self.assertEquals(data[6], PaymentId('aaa'))
        self.assertEquals(data[7], True)
Ejemplo n.º 26
0
 def setUp(self):
     # Object creation
     self.transaction_id = TransactionId(TraderId(b'0' * 20),
                                         TransactionNumber(1))
     self.transaction_id2 = TransactionId(TraderId(b'0' * 20),
                                          TransactionNumber(1))
     self.transaction_id3 = TransactionId(TraderId(b'0' * 20),
                                          TransactionNumber(2))
Ejemplo n.º 27
0
    def from_block(cls, block):
        """
        Restore a payment from a TrustChain block

        :param block: TrustChainBlock
        :return: Restored payment
        :rtype: Payment
        """
        tx_dict = block.transaction["payment"]
        return cls(
            TraderId(unhexlify(tx_dict["trader_id"])),
            TransactionId(unhexlify(tx_dict["transaction_id"])),
            AssetAmount(tx_dict["transferred"]["amount"],
                        tx_dict["transferred"]["type"]),
            WalletAddress(tx_dict["address_from"]),
            WalletAddress(tx_dict["address_to"]),
            PaymentId(tx_dict["payment_id"]), Timestamp(tx_dict["timestamp"]))
Ejemplo n.º 28
0
    async def get_payments(self, request):
        """
        .. http:get:: /market/transactions/(string:transaction_id)/payments

        A GET request to this endpoint will return all payments tied to a specific transaction.

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/market/transactions/
                12c406358ba05e5883a75da3f009477e4ca699a9/3/payments

            **Example response**:

            .. sourcecode:: javascript

                {
                    "payments": [{
                        "trader_id": "12c406358ba05e5883a75da3f009477e4ca699a9",
                        "transaction_number": 3,
                        "price": 10,
                        "price_type": "MC",
                        "quantity": 10,
                        "quantity_type": "BTC",
                        "transferred_quantity": 4,
                        "payment_id": "abcd",
                        "address_from": "my_mc_address",
                        "address_to": "my_btc_address",
                        "timestamp": 1493906434.627721,
                    ]
                }
        """
        transaction_id = TransactionId(
            unhexlify(request.match_info['transaction_id']))
        transaction = self.get_market_community(
        ).transaction_manager.find_by_id(transaction_id)

        if not transaction:
            return Response({"error": "transaction not found"},
                            status=HTTP_NOT_FOUND)

        return Response({
            "payments":
            [payment.to_dictionary() for payment in transaction.payments]
        })
Ejemplo n.º 29
0
    def add_transaction_and_payment(self):
        """
        Add a transaction and a payment to the market
        """
        self.accepted_trade = AcceptedTrade(
            TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(1)),
            OrderId(TraderId(b'1' * 20), OrderNumber(1)), 1234,
            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
            Timestamp(0))
        transaction = Transaction.from_accepted_trade(self.accepted_trade,
                                                      TransactionId(b'a' * 32))

        payment = Payment(TraderId(b'0' * 20), transaction.transaction_id,
                          AssetAmount(20, 'BTC'), WalletAddress('a'),
                          WalletAddress('b'), PaymentId('aaa'),
                          Timestamp(4000))
        transaction.add_payment(payment)
        self.nodes[
            0].overlay.transaction_manager.transaction_repository.update(
                transaction)

        return transaction
Ejemplo n.º 30
0
def payment():
    transaction_id = TransactionId(b'a' * 32)
    payment = Payment(TraderId(b'0' * 20), transaction_id,
                      AssetAmount(5, 'BTC'), WalletAddress('abc'),
                      WalletAddress('def'), PaymentId("abc"), Timestamp(20000))
    return payment