コード例 #1
0
ファイル: test_community.py プロジェクト: unoffices/tribler
    def test_proposed_trade_cache_timeout(self):
        """
        Test the timeout method of a proposed trade request in the cache
        """
        ask = Ask(
            OrderId(TraderId(self.market_community.mid), OrderNumber(24)),
            Price(63400, 'DUM1'), Quantity(30, 'DUM2'), Timeout(3600),
            Timestamp.now())
        order = Order(OrderId(TraderId("0"), OrderNumber(23)),
                      Price(20, 'DUM1'), Quantity(30, 'DUM2'), Timeout(3600.0),
                      Timestamp.now(), False)
        self.market_community.order_book.insert_ask(ask)
        self.assertEqual(len(self.market_community.order_book.asks), 1)
        self.market_community.order_manager.order_repository.add(order)
        order.reserve_quantity_for_tick(self.proposed_trade.recipient_order_id,
                                        Quantity(30, 'DUM2'))
        self.market_community.order_manager.order_repository.update(order)

        mocked_match_message = MockObject()
        mocked_match_message.payload = MockObject()
        mocked_match_message.payload.matchmaker_trader_id = 'a'
        self.market_community.incoming_match_messages[
            'a'] = mocked_match_message

        def mocked_send_decline(*_):
            mocked_send_decline.called = True

        mocked_send_decline.called = False
        self.market_community.send_decline_match_message = mocked_send_decline

        cache = ProposedTradeRequestCache(self.market_community,
                                          self.proposed_trade, 'a')
        cache.on_timeout()
        self.assertTrue(mocked_send_decline.called)
コード例 #2
0
    def test_from_network(self):
        # Test for from network
        data = Payment.from_network(
            type(
                'Data', (object, ), {
                    "trader_id":
                    TraderId(b"0"),
                    "transaction_id":
                    TransactionId(TraderId(b'2'), TransactionNumber(2)),
                    "transferred_assets":
                    AssetAmount(3, 'BTC'),
                    "address_from":
                    WalletAddress('a'),
                    "address_to":
                    WalletAddress('b'),
                    "payment_id":
                    PaymentId('aaa'),
                    "timestamp":
                    Timestamp(4.0),
                    "success":
                    True
                }))

        self.assertEquals(TraderId(b"0"), data.trader_id)
        self.assertEquals(TransactionId(TraderId(b'2'), TransactionNumber(2)),
                          data.transaction_id)
        self.assertEquals(AssetAmount(3, 'BTC'), data.transferred_assets)
        self.assertEquals(Timestamp(4.0), data.timestamp)
        self.assertTrue(data.success)
コード例 #3
0
    def setUp(self):
        yield super(PriceTimeStrategyTestSuite, self).setUp()
        # Object creation
        self.ask = Ask(OrderId(TraderId('0'), OrderNumber(1)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.ask2 = Ask(OrderId(TraderId('1'), OrderNumber(2)),
                        AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.ask3 = Ask(OrderId(TraderId('0'), OrderNumber(3)),
                        AssetPair(AssetAmount(40000, 'BTC'), AssetAmount(200, 'MB')), Timeout(100), Timestamp.now())
        self.ask4 = Ask(OrderId(TraderId('1'), OrderNumber(4)),
                        AssetPair(AssetAmount(3000, 'A'), AssetAmount(3000, 'MB')), Timeout(100), Timestamp.now())
        self.ask5 = Ask(OrderId(TraderId('1'), OrderNumber(4)),
                        AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'C')), Timeout(100), Timestamp.now())

        self.bid = Bid(OrderId(TraderId('0'), OrderNumber(5)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.bid2 = Bid(OrderId(TraderId('0'), OrderNumber(6)),
                        AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())

        self.ask_order = Order(OrderId(TraderId('9'), OrderNumber(11)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(100), Timestamp.now(), True)
        self.ask_order2 = Order(OrderId(TraderId('9'), OrderNumber(12)),
                                AssetPair(AssetAmount(600, 'BTC'), AssetAmount(60, 'MB')),
                                Timeout(100), Timestamp.now(), True)

        self.bid_order = Order(OrderId(TraderId('9'), OrderNumber(13)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(100), Timestamp.now(), False)
        self.bid_order2 = Order(OrderId(TraderId('9'), OrderNumber(14)),
                                AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(60, 'MB')),
                                Timeout(100), Timestamp.now(), False)
        self.order_book = OrderBook()
        self.price_time_strategy = PriceTimeStrategy(self.order_book)
コード例 #4
0
ファイル: test_trade.py プロジェクト: yanheven/tribler
    def test_from_network(self):
        # Test for from network
        data = ProposedTrade.from_network(
            type(
                'Data', (object, ), {
                    "trader_id": TraderId('0'),
                    "message_id": MessageId(TraderId('0'), MessageNumber(1)),
                    "order_number": OrderNumber(1),
                    "recipient_order_id": OrderId(TraderId('1'),
                                                  OrderNumber(2)),
                    "proposal_id": 1234,
                    "timestamp": Timestamp(1462224447.117),
                    "price": Price(63400, 'BTC'),
                    "quantity": Quantity(30, 'MC')
                }))

        self.assertEquals(MessageId(TraderId('0'), MessageNumber(1)),
                          data.message_id)
        self.assertEquals(OrderId(TraderId('0'), OrderNumber(1)),
                          data.order_id)
        self.assertEquals(OrderId(TraderId('1'), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1234, data.proposal_id)
        self.assertEquals(Price(63400, 'BTC'), data.price)
        self.assertEquals(Quantity(30, 'MC'), data.quantity)
        self.assertEquals(Timestamp(1462224447.117), data.timestamp)
コード例 #5
0
ファイル: test_payment.py プロジェクト: yanheven/tribler
    def test_from_network(self):
        # Test for from network
        data = Payment.from_network(
            type(
                'Data', (object, ), {
                    "message_id":
                    MessageId(TraderId("0"), MessageNumber(1)),
                    "transaction_id":
                    TransactionId(TraderId('2'), TransactionNumber(2)),
                    "transferee_quantity":
                    Quantity(3, 'MC'),
                    "transferee_price":
                    Price(2, 'BTC'),
                    "address_from":
                    WalletAddress('a'),
                    "address_to":
                    WalletAddress('b'),
                    "payment_id":
                    PaymentId('aaa'),
                    "timestamp":
                    Timestamp(4.0),
                    "success":
                    True
                }))

        self.assertEquals(MessageId(TraderId("0"), MessageNumber(1)),
                          data.message_id)
        self.assertEquals(TransactionId(TraderId('2'), TransactionNumber(2)),
                          data.transaction_id)
        self.assertEquals(Quantity(3, 'MC'), data.transferee_quantity)
        self.assertEquals(Price(2, 'BTC'), data.transferee_price)
        self.assertEquals(Timestamp(4.0), data.timestamp)
        self.assertTrue(data.success)
コード例 #6
0
    def from_database(cls, data, reserved_ticks):
        """
        Create an Order object based on information in the database.
        """
        (trader_id, order_number, asset1_amount, asset1_type, asset2_amount,
         asset2_type, traded_quantity, timeout, order_timestamp,
         completed_timestamp, is_ask, cancelled, verified) = data

        order_id = OrderId(TraderId(bytes(trader_id)),
                           OrderNumber(order_number))
        order = cls(
            order_id,
            AssetPair(AssetAmount(asset1_amount, str(asset1_type)),
                      AssetAmount(asset2_amount, str(asset2_type))),
            Timeout(timeout), Timestamp(order_timestamp), bool(is_ask))
        order._traded_quantity = traded_quantity
        order._cancelled = bool(cancelled)
        order._verified = verified
        if completed_timestamp:
            order._completed_timestamp = Timestamp(completed_timestamp)

        for reserved_order_id, quantity in reserved_ticks:
            order.reserved_ticks[reserved_order_id] = quantity
            order._reserved_quantity += quantity

        return order
コード例 #7
0
    def setUp(self):
        yield super(PriceTimeStrategyTestSuite, self).setUp()
        # Object creation
        self.ask = Ask(OrderId(TraderId(b'0'), OrderNumber(1)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.ask2 = Ask(OrderId(TraderId(b'1'), OrderNumber(2)),
                        AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.ask3 = Ask(OrderId(TraderId(b'0'), OrderNumber(3)),
                        AssetPair(AssetAmount(40000, 'BTC'), AssetAmount(200, 'MB')), Timeout(100), Timestamp.now())
        self.ask4 = Ask(OrderId(TraderId(b'1'), OrderNumber(4)),
                        AssetPair(AssetAmount(3000, 'A'), AssetAmount(3000, 'MB')), Timeout(100), Timestamp.now())
        self.ask5 = Ask(OrderId(TraderId(b'1'), OrderNumber(4)),
                        AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'C')), Timeout(100), Timestamp.now())

        self.bid = Bid(OrderId(TraderId(b'0'), OrderNumber(5)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
        self.bid2 = Bid(OrderId(TraderId(b'0'), OrderNumber(6)),
                        AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())

        self.ask_order = Order(OrderId(TraderId(b'9'), OrderNumber(11)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(100), Timestamp.now(), True)
        self.ask_order2 = Order(OrderId(TraderId(b'9'), OrderNumber(12)),
                                AssetPair(AssetAmount(600, 'BTC'), AssetAmount(60, 'MB')),
                                Timeout(100), Timestamp.now(), True)

        self.bid_order = Order(OrderId(TraderId(b'9'), OrderNumber(13)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(100), Timestamp.now(), False)
        self.bid_order2 = Order(OrderId(TraderId(b'9'), OrderNumber(14)),
                                AssetPair(AssetAmount(6000, 'BTC'), AssetAmount(60, 'MB')),
                                Timeout(100), Timestamp.now(), False)
        self.order_book = OrderBook()
        self.price_time_strategy = PriceTimeStrategy(self.order_book)
コード例 #8
0
    def test_from_network(self):
        # Test for from network
        data = StartTransaction.from_network(
            type(
                'Data', (object, ), {
                    "trader_id": TraderId('0'),
                    "message_number": MessageNumber("1"),
                    "transaction_trader_id": TraderId('0'),
                    "transaction_number": TransactionNumber(1),
                    "order_trader_id": TraderId('0'),
                    "order_number": OrderNumber(1),
                    "recipient_trader_id": TraderId('1'),
                    "recipient_order_number": OrderNumber(2),
                    "proposal_id": 1235,
                    "price": Price(300, 'BTC'),
                    "quantity": Quantity(20, 'MC'),
                    "timestamp": Timestamp(0.0)
                }))

        self.assertEquals(MessageId(TraderId("0"), MessageNumber("1")),
                          data.message_id)
        self.assertEquals(TransactionId(TraderId("0"), TransactionNumber(1)),
                          data.transaction_id)
        self.assertEquals(OrderId(TraderId('0'), OrderNumber(1)),
                          data.order_id)
        self.assertEquals(OrderId(TraderId('1'), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1235, data.proposal_id)
        self.assertEquals(Timestamp(0.0), data.timestamp)
コード例 #9
0
    def test_from_network(self):
        # Test for from network
        data = StartTransaction.from_network(
            type(
                'Data', (object, ), {
                    "trader_id":
                    TraderId('0'),
                    "transaction_id":
                    TransactionId(TraderId('0'), TransactionNumber(1)),
                    "order_id":
                    OrderId(TraderId('0'), OrderNumber(1)),
                    "recipient_order_id":
                    OrderId(TraderId('1'), OrderNumber(2)),
                    "proposal_id":
                    1235,
                    "assets":
                    AssetPair(AssetAmount(30, 'BTC'), AssetAmount(40, 'MC')),
                    "timestamp":
                    Timestamp(0.0)
                }))

        self.assertEquals(TraderId("0"), data.trader_id)
        self.assertEquals(TransactionId(TraderId("0"), TransactionNumber(1)),
                          data.transaction_id)
        self.assertEquals(OrderId(TraderId('0'), OrderNumber(1)),
                          data.order_id)
        self.assertEquals(OrderId(TraderId('1'), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1235, data.proposal_id)
        self.assertEquals(Timestamp(0.0), data.timestamp)
コード例 #10
0
ファイル: test_order.py プロジェクト: zippav/tribler
    def setUp(self):
        # Object creation
        self.transaction_id = TransactionId(TraderId(b"0"), TransactionNumber(1))
        self.transaction = Transaction(self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')),
                                       OrderId(TraderId(b'0'), OrderNumber(2)),
                                       OrderId(TraderId(b'1'), OrderNumber(1)), Timestamp(0.0))
        self.proposed_trade = Trade.propose(TraderId(b'0'),
                                            OrderId(TraderId(b'0'), OrderNumber(2)),
                                            OrderId(TraderId(b'1'), OrderNumber(3)),
                                            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), Timestamp(0.0))

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

        self.order_timestamp = Timestamp.now()
        self.order = Order(OrderId(TraderId(b"0"), OrderNumber(3)),
                           AssetPair(AssetAmount(50, 'BTC'), AssetAmount(5, 'MC')),
                           Timeout(5000), self.order_timestamp, False)
        self.order.set_verified()
        self.order2 = Order(OrderId(TraderId(b"0"), OrderNumber(4)),
                            AssetPair(AssetAmount(50, 'BTC'), AssetAmount(5, 'MC')),
                            Timeout(5), Timestamp(time.time() - 1000), True)
        self.order2.set_verified()
コード例 #11
0
    def test_trade_tick(self):
        """
        Test the trade tick method in an order book
        """
        self.order_book.insert_ask(self.ask)
        self.order_book.insert_bid(self.bid)
        self.order_book.insert_ask(self.ask2)
        self.order_book.insert_bid(self.bid2)

        # Trade self.ask <-> self.bid
        self.order_book.trade_tick(self.ask.order_id, self.bid.order_id,
                                   Quantity(20, 'MC'), Timestamp.now())
        self.assertTrue(self.order_book.tick_exists(self.ask.order_id))
        self.assertTrue(self.order_book.tick_exists(self.bid.order_id))
        self.assertEqual(
            self.order_book.get_tick(self.ask.order_id).tick.quantity,
            Quantity(10, 'MC'))
        self.assertEqual(
            self.order_book.get_tick(self.bid.order_id).tick.quantity,
            Quantity(10, 'MC'))

        # Trade self.bid2 <-> self.ask2
        self.order_book.trade_tick(self.bid2.order_id, self.ask2.order_id,
                                   Quantity(30, 'MC'), Timestamp.now())
        self.assertTrue(self.order_book.tick_exists(self.ask2.order_id))
        self.assertTrue(self.order_book.tick_exists(self.bid2.order_id))
        self.assertEqual(
            self.order_book.get_tick(self.ask2.order_id).tick.quantity,
            Quantity(0, 'MC'))
        self.assertEqual(
            self.order_book.get_tick(self.bid2.order_id).tick.quantity,
            Quantity(0, 'MC'))
コード例 #12
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('0' * 40), OrderNumber(1)), ask_pair,
                 Timeout(3600), Timestamp.now(), True)
     ask._traded_quantity = ask_total_traded
     bid = Order(OrderId(TraderId('1' * 40), OrderNumber(1)), bid_pair,
                 Timeout(3600), Timestamp.now(), False)
     bid._traded_quantity = bid_total_traded
     tx = Transaction(
         TransactionId(TraderId('0' * 40), TransactionNumber(1)),
         AssetPair(AssetAmount(traded_amount, 'BTC'),
                   AssetAmount(traded_amount, 'MB')),
         OrderId(TraderId('0' * 40), OrderNumber(1)),
         OrderId(TraderId('1' * 40), OrderNumber(1)), Timestamp(0.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 = '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
コード例 #13
0
    def setUp(self, annotate=True):
        yield super(TestDatabase, self).setUp(annotate=annotate)

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

        self.database = MarketDB(self.getStateDir())

        self.order_id1 = OrderId(TraderId('3'), OrderNumber(4))
        self.order_id2 = OrderId(TraderId('4'), OrderNumber(5))
        self.order1 = Order(self.order_id1, Price(5,
                                                  'EUR'), Quantity(6, 'BTC'),
                            Timeout(3600), Timestamp.now(), True)
        self.order2 = Order(self.order_id2, Price(5,
                                                  'EUR'), Quantity(6, 'BTC'),
                            Timeout(3600), Timestamp.now(), False)

        self.transaction_id1 = TransactionId(TraderId("0"),
                                             TransactionNumber(4))
        self.transaction1 = Transaction(self.transaction_id1,
                                        Price(100, 'BTC'), Quantity(30, 'MC'),
                                        OrderId(TraderId("0"), OrderNumber(1)),
                                        OrderId(TraderId("1"), OrderNumber(2)),
                                        Timestamp(20.0))

        self.payment1 = Payment(MessageId(TraderId("0"), MessageNumber("4")),
                                self.transaction_id1, Quantity(5, 'MC'),
                                Price(6, 'BTC'), WalletAddress('abc'),
                                WalletAddress('def'), PaymentId("abc"),
                                Timestamp(20.0), False)

        self.transaction1.add_payment(self.payment1)
コード例 #14
0
    def test_from_network(self):
        # Test for from network
        data = ProposedTrade.from_network(
            type(
                'Data', (object, ), {
                    "trader_id":
                    TraderId(b'0'),
                    "order_number":
                    OrderNumber(1),
                    "recipient_order_id":
                    OrderId(TraderId('1'), OrderNumber(2)),
                    "proposal_id":
                    1234,
                    "timestamp":
                    Timestamp(1462224447.117),
                    "assets":
                    AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB'))
                }))

        self.assertEquals(TraderId(b'0'), data.trader_id)
        self.assertEquals(OrderId(TraderId(b'0'), OrderNumber(1)),
                          data.order_id)
        self.assertEquals(OrderId(TraderId(b'1'), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1234, data.proposal_id)
        self.assertEquals(
            AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
            data.assets)
        self.assertEquals(Timestamp(1462224447.117), data.timestamp)
コード例 #15
0
    def setUp(self, annotate=True):
        yield super(MatchingEngineTestSuite, self).setUp(annotate=annotate)
        # Object creation
        self.ask = Ask(
            OrderId(TraderId('2'), OrderNumber(1)),
            AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp.now())
        self.bid = Bid(
            OrderId(TraderId('4'), OrderNumber(2)),
            AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp.now())
        self.ask_order = Order(
            OrderId(TraderId('5'), OrderNumber(3)),
            AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp.now(), True)
        self.bid_order = Order(
            OrderId(TraderId('6'), OrderNumber(4)),
            AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
            Timeout(30), Timestamp.now(), False)
        self.order_book = OrderBook()
        self.matching_engine = MatchingEngine(
            PriceTimeStrategy(self.order_book))

        self.ask_count = 2
        self.bid_count = 2
コード例 #16
0
    def setUp(self):
        yield 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'), OrderNumber(4))
        self.order_id2 = OrderId(TraderId(b'4'), 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'), OrderNumber(4)), 3)

        self.transaction_id1 = TransactionId(TraderId(b"0"), TransactionNumber(4))
        self.transaction1 = Transaction(self.transaction_id1, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')),
                                        OrderId(TraderId(b"0"), OrderNumber(1)),
                                        OrderId(TraderId(b"1"), OrderNumber(2)), Timestamp(20.0))

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

        self.transaction1.add_payment(self.payment1)
コード例 #17
0
 def setUp(self):
     # Object creation
     self.memory_order_repository = MemoryOrderRepository("0")
     self.order_id = OrderId(TraderId(b"0"), OrderNumber(1))
     self.order = Order(self.order_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')),
                        Timeout(0), Timestamp(10.0), False)
     self.order2 = Order(self.order_id, AssetPair(AssetAmount(1000, 'BTC'), AssetAmount(30, 'MC')),
                         Timeout(0), Timestamp(10.0), False)
コード例 #18
0
 def setUp(self):
     # Object creation
     self.proposed_trade = Trade.propose(MessageId(TraderId('0'), MessageNumber('message_number')),
                                         OrderId(TraderId('0'), OrderNumber(1)),
                                         OrderId(TraderId('1'), OrderNumber(2)),
                                         Price(63400, 'BTC'), Quantity(30, 'MC'), Timestamp(1462224447.117))
     self.declined_trade = Trade.decline(MessageId(TraderId('0'), MessageNumber('message_number')),
                                         Timestamp(1462224447.117), self.proposed_trade)
コード例 #19
0
    def setUp(self, annotate=True):
        yield super(TestMarketBlock, self).setUp(annotate=annotate)

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

        ask_tx = self.ask.to_block_dict()
        ask_tx["address"], ask_tx["port"] = "127.0.0.1", 1337
        bid_tx = self.bid.to_block_dict()
        bid_tx["address"], bid_tx["port"] = "127.0.0.1", 1337

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

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

        self.tx_block = MarketBlock()
        self.tx_block.type = '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.3,
            'success': True
        }
        self.payment_block = MarketBlock()
        self.payment_block.type = 'tx_payment'
        self.payment_block.transaction = {'payment': payment}
コード例 #20
0
 def setUp(self):
     # Object creation
     self.proposed_trade = Trade.propose(
         TraderId(b'0'), OrderId(TraderId(b'0'), OrderNumber(1)),
         OrderId(TraderId(b'1'), OrderNumber(2)),
         AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
         Timestamp(1462224447.117))
     self.declined_trade = Trade.decline(
         TraderId(b'0'), Timestamp(1462224447.117), self.proposed_trade,
         DeclinedTradeReason.ORDER_COMPLETED)
コード例 #21
0
ファイル: test_side.py プロジェクト: coldstacks/tribler
    def setUp(self):
        # Object creation

        self.tick = Tick(OrderId(TraderId('0'), OrderNumber(1)),
                         Price(400, 'BTC'), Quantity(30, 'MC'),
                         Timeout(float("inf")), Timestamp(float("inf")), True)
        self.tick2 = Tick(OrderId(TraderId('1'), OrderNumber(2)),
                          Price(800, 'BTC'), Quantity(30, 'MC'),
                          Timeout(float("inf")), Timestamp(float("inf")), True)
        self.side = Side()
コード例 #22
0
    def setUp(self):
        # Object creation

        self.tick = Tick(OrderId(TraderId('0'), OrderNumber(1)),
                         AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
                         Timeout(100), Timestamp.now(), True)
        self.tick2 = Tick(OrderId(TraderId('1'), OrderNumber(2)),
                          AssetPair(AssetAmount(120, 'BTC'), AssetAmount(30, 'MB')),
                          Timeout(100), Timestamp.now(), True)
        self.side = Side()
コード例 #23
0
 def setUp(self):
     # Object creation
     self.memory_order_repository = MemoryOrderRepository("0")
     self.order_id = OrderId(TraderId("0"), OrderNumber(1))
     self.order = Order(self.order_id, Price(100,
                                             'BTC'), Quantity(30, 'MC'),
                        Timeout(0.0), Timestamp(10.0), False)
     self.order2 = Order(self.order_id, Price(1000, 'BTC'),
                         Quantity(30, 'MC'), Timeout(0.0), Timestamp(10.0),
                         False)
コード例 #24
0
 def setUp(self):
     # Object creation
     self.proposed_trade = Trade.propose(
         TraderId(b'0'), OrderId(TraderId(b'0'), OrderNumber(1)),
         OrderId(TraderId(b'1'), OrderNumber(2)),
         AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
         Timestamp(1462224447.117))
     self.counter_trade = Trade.counter(
         TraderId(b'0'),
         AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
         Timestamp(1462224447.117), self.proposed_trade)
コード例 #25
0
 def setUp(self):
     # Object creation
     self.timestamp_now = Timestamp.now()
     self.tick = Tick(OrderId(TraderId(b'0'), OrderNumber(1)),
                      AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0.0), True)
     self.tick2 = Tick(OrderId(TraderId(b'0'), OrderNumber(2)),
                       AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0.0), False)
     self.order_ask = Order(OrderId(TraderId(b'0'), OrderNumber(2)),
                            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
                            Timeout(0), Timestamp(0.0), True)
     self.order_bid = Order(OrderId(TraderId(b'0'), OrderNumber(2)),
                            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
                            Timeout(0), Timestamp(0.0), False)
コード例 #26
0
ファイル: test_pricelevel.py プロジェクト: synctext/tribler
    def setUp(self):
        # Object creation
        tick = Tick(OrderId(TraderId('0'), OrderNumber(1)), AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MC')),
                    Timeout(100), Timestamp.now(), True)
        tick2 = Tick(OrderId(TraderId('0'), OrderNumber(2)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MC')),
                     Timeout(100), Timestamp.now(), True)

        self.price_level = PriceLevel(Price(10, 'MC', 'BTC'))
        self.tick_entry1 = TickEntry(tick, self.price_level)
        self.tick_entry2 = TickEntry(tick, self.price_level)
        self.tick_entry3 = TickEntry(tick, self.price_level)
        self.tick_entry4 = TickEntry(tick, self.price_level)
        self.tick_entry5 = TickEntry(tick2, self.price_level)
コード例 #27
0
ファイル: orderbook.py プロジェクト: yanheven/tribler
    def update_ticks(self, ask_order_dict, bid_order_dict, traded_quantity, unreserve=True):
        """
        Update ticks according to a TradeChain block containing the status of the ask/bid orders.

        :type ask_order_dict: dict
        :type bid_order_dict: dict
        :type traded_quantity: Quantity
        :type unreserve: bool
        """
        assert isinstance(ask_order_dict, dict), type(ask_order_dict)
        assert isinstance(bid_order_dict, dict), type(bid_order_dict)
        assert isinstance(traded_quantity, Quantity), type(traded_quantity)
        assert isinstance(unreserve, bool), type(unreserve)

        ask_order_id = OrderId(TraderId(ask_order_dict["trader_id"]), OrderNumber(ask_order_dict["order_number"]))
        bid_order_id = OrderId(TraderId(bid_order_dict["trader_id"]), OrderNumber(bid_order_dict["order_number"]))

        self._logger.debug("Updating ticks in order book: %s and %s (traded quantity: %s)",
                           str(ask_order_id), str(bid_order_id), str(traded_quantity))

        # Update ask tick
        new_ask_quantity = Quantity(ask_order_dict["quantity"] - ask_order_dict["traded_quantity"],
                                    ask_order_dict["quantity_type"])
        if self.tick_exists(ask_order_id) and new_ask_quantity <= self.get_tick(ask_order_id).quantity:
            tick = self.get_tick(ask_order_id)
            tick.quantity = new_ask_quantity
            if unreserve:
                tick.release_for_matching(traded_quantity)
            if tick.quantity <= Quantity(0, ask_order_dict["quantity_type"]):
                self.remove_tick(tick.order_id)
                self.completed_orders.append(tick.order_id)
        elif not self.tick_exists(ask_order_id) and new_ask_quantity > Quantity(0, ask_order_dict["quantity_type"]):
            ask = Ask(ask_order_id, Price(ask_order_dict["price"], ask_order_dict["price_type"]),
                      new_ask_quantity, Timeout(ask_order_dict["timeout"]), Timestamp(ask_order_dict["timestamp"]))
            self.insert_ask(ask)

        # Update bid tick
        new_bid_quantity = Quantity(bid_order_dict["quantity"] - bid_order_dict["traded_quantity"],
                                    bid_order_dict["quantity_type"])
        if self.tick_exists(bid_order_id) and new_bid_quantity <= self.get_tick(bid_order_id).quantity:
            tick = self.get_tick(bid_order_id)
            tick.quantity = new_bid_quantity
            if unreserve:
                tick.release_for_matching(traded_quantity)
            if tick.quantity <= Quantity(0, bid_order_dict["quantity_type"]):
                self.remove_tick(tick.order_id)
                self.completed_orders.append(tick.order_id)
        elif not self.tick_exists(bid_order_id) and new_bid_quantity > Quantity(0, bid_order_dict["quantity_type"]):
            bid = Bid(bid_order_id, Price(bid_order_dict["price"], bid_order_dict["price_type"]),
                      new_bid_quantity, Timeout(bid_order_dict["timeout"]), Timestamp(bid_order_dict["timestamp"]))
            self.insert_bid(bid)
コード例 #28
0
    def setUp(self, annotate=True):
        yield super(TickEntryTestSuite, self).setUp(annotate=annotate)

        # Object creation
        tick = Tick(OrderId(TraderId('0'), OrderNumber(1)),
                    Price(63400, 'BTC'), Quantity(30, 'MC'), Timeout(0.0),
                    Timestamp(0.0), True)
        tick2 = Tick(OrderId(TraderId('0'), OrderNumber(2)),
                     Price(63400, 'BTC'), Quantity(30, 'MC'), Timeout(100),
                     Timestamp.now(), True)

        self.price_level = PriceLevel('MC')
        self.tick_entry = TickEntry(tick, self.price_level)
        self.tick_entry2 = TickEntry(tick2, self.price_level)
コード例 #29
0
    def setUp(self):
        # Object creation
        tick = Tick(MessageId(TraderId('0'), MessageNumber('message_number')),
                    OrderId(TraderId('0'), OrderNumber(1)),
                    Price(63400, 'BTC'), Quantity(30, 'MC'), Timeout(0.0),
                    Timestamp(0.0), True)
        tick2 = Tick(MessageId(TraderId('0'), MessageNumber('message_number')),
                     OrderId(TraderId('0'), OrderNumber(2)),
                     Price(63400, 'BTC'), Quantity(30, 'MC'), Timeout(100),
                     Timestamp.now(), True)

        self.price_level = PriceLevel('MC')
        self.tick_entry = TickEntry(tick, self.price_level)
        self.tick_entry2 = TickEntry(tick2, self.price_level)
コード例 #30
0
    def setUp(self, annotate=True):
        yield super(CommunityTestSuite, self).setUp(annotate=annotate)

        dummy1_wallet = DummyWallet1()
        dummy2_wallet = DummyWallet2()

        self.market_community = MarketCommunity(self.dispersy,
                                                self.master_member,
                                                self.member)
        self.market_community.initialize(wallets={
            dummy1_wallet.get_identifier():
            dummy1_wallet,
            dummy2_wallet.get_identifier():
            dummy2_wallet
        },
                                         use_database=False)
        self.market_community.use_local_address = True
        self.dispersy._lan_address = ("127.0.0.1", 1234)
        self.dispersy._endpoint.open(self.dispersy)

        self.dispersy.attach_community(self.market_community)

        eccrypto = ECCrypto()
        ec = eccrypto.generate_key(u"curve25519")
        member = Member(self.dispersy, ec, 1)

        trader_id = hashlib.sha1(member.public_key).digest().encode('hex')
        self.ask = Ask(
            MessageId(TraderId('0'), MessageNumber('message_number')),
            OrderId(TraderId(trader_id), OrderNumber(1234)),
            Price(63400, 'DUM1'), Quantity(30, 'DUM2'), Timeout(3600),
            Timestamp.now())
        self.ask.sign(member)

        self.bid = Bid(
            MessageId(TraderId('1'), MessageNumber('message_number')),
            OrderId(TraderId(trader_id), OrderNumber(1235)),
            Price(343, 'DUM1'), Quantity(22, 'DUM2'), Timeout(3600),
            Timestamp.now())
        self.bid.sign(member)
        self.order = Order(
            OrderId(TraderId(self.market_community.mid), OrderNumber(24)),
            Price(20, 'DUM1'), Quantity(30, 'DUM2'), Timeout(3600.0),
            Timestamp.now(), False)
        self.proposed_trade = Trade.propose(
            MessageId(TraderId('0'), MessageNumber('message_number')),
            OrderId(TraderId('0'), OrderNumber(23)),
            OrderId(TraderId(self.market_community.mid), OrderNumber(24)),
            Price(20, 'DUM1'), Quantity(30, 'DUM2'), Timestamp.now())
コード例 #31
0
    def test_from_network(self):
        # Test for from network
        data = DeclinedTrade.from_network(type('Data', (object,), {"trader_id": TraderId('0'),
                                                                   "message_number": MessageNumber('message_number'),
                                                                   "order_number": OrderNumber(1),
                                                                   "recipient_trader_id": TraderId('1'),
                                                                   "recipient_order_number": OrderNumber(2),
                                                                   "proposal_id": 1235,
                                                                   "timestamp": Timestamp(1462224447.117),}))

        self.assertEquals(MessageId(TraderId('0'), MessageNumber('message_number')), data.message_id)
        self.assertEquals(OrderId(TraderId('1'), OrderNumber(2)),
                          data.recipient_order_id)
        self.assertEquals(1235, data.proposal_id)
        self.assertEquals(Timestamp(1462224447.117), data.timestamp)
コード例 #32
0
ファイル: test_pricelevel.py プロジェクト: zippav/tribler
    def setUp(self):
        # Object creation
        tick = Tick(OrderId(TraderId(b'0'), OrderNumber(1)),
                    AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MC')),
                    Timeout(100), Timestamp.now(), True)
        tick2 = Tick(OrderId(TraderId(b'0'), OrderNumber(2)),
                     AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MC')),
                     Timeout(100), Timestamp.now(), True)

        self.price_level = PriceLevel(Price(10, 'MC', 'BTC'))
        self.tick_entry1 = TickEntry(tick, self.price_level)
        self.tick_entry2 = TickEntry(tick, self.price_level)
        self.tick_entry3 = TickEntry(tick, self.price_level)
        self.tick_entry4 = TickEntry(tick, self.price_level)
        self.tick_entry5 = TickEntry(tick2, self.price_level)
コード例 #33
0
ファイル: test_order.py プロジェクト: synctext/tribler
    def setUp(self):
        # Object creation
        self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1))
        self.transaction = Transaction(self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')),
                                       OrderId(TraderId('0'), OrderNumber(2)),
                                       OrderId(TraderId('1'), OrderNumber(1)), Timestamp(0.0))
        self.proposed_trade = Trade.propose(TraderId('0'),
                                            OrderId(TraderId('0'), OrderNumber(2)),
                                            OrderId(TraderId('1'), OrderNumber(3)),
                                            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), Timestamp(0.0))

        self.tick = Tick(OrderId(TraderId('0'), OrderNumber(1)),
                         AssetPair(AssetAmount(5, 'BTC'), AssetAmount(5, 'MC')),
                         Timeout(0), Timestamp(float("inf")), True)
        self.tick2 = Tick(OrderId(TraderId('0'), OrderNumber(2)),
                          AssetPair(AssetAmount(500, 'BTC'), AssetAmount(5, 'MC')),
                          Timeout(0), Timestamp(float("inf")), True)

        self.order_timestamp = Timestamp.now()
        self.order = Order(OrderId(TraderId("0"), OrderNumber(3)),
                           AssetPair(AssetAmount(50, 'BTC'), AssetAmount(5, 'MC')),
                           Timeout(5000), self.order_timestamp, False)
        self.order.set_verified()
        self.order2 = Order(OrderId(TraderId("0"), OrderNumber(4)),
                            AssetPair(AssetAmount(50, 'BTC'), AssetAmount(5, 'MC')),
                            Timeout(5), Timestamp(time.time() - 1000), True)
        self.order2.set_verified()
コード例 #34
0
    def create_payment_message(self, message_id, payment_id, transaction, transferred_assets, success):
        payment_message = Payment(message_id, transaction.transaction_id, transferred_assets,
                                  transaction.outgoing_address, transaction.partner_incoming_address,
                                  payment_id, Timestamp.now(), success)
        transaction.add_payment(payment_message)
        self.transaction_repository.update(transaction)

        return payment_message
コード例 #35
0
    def setUp(self):
        yield super(MatchingEngineTestSuite, self).setUp()
        # Object creation
        self.ask = Ask(OrderId(TraderId('2'), OrderNumber(1)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now())
        self.bid = Bid(OrderId(TraderId('4'), OrderNumber(2)),
                       AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp.now())
        self.ask_order = Order(OrderId(TraderId('5'), OrderNumber(3)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(30), Timestamp.now(), True)
        self.bid_order = Order(OrderId(TraderId('6'), OrderNumber(4)),
                               AssetPair(AssetAmount(3000, 'BTC'), AssetAmount(30, 'MB')),
                               Timeout(30), Timestamp.now(), False)
        self.order_book = OrderBook()
        self.matching_engine = MatchingEngine(PriceTimeStrategy(self.order_book))

        self.ask_count = 2
        self.bid_count = 2
コード例 #36
0
ファイル: order.py プロジェクト: synctext/tribler
    def add_trade(self, other_order_id, quantity):
        self._logger.debug("Adding trade for order %s with quantity %s (other id: %s)",
                           str(self.order_id), quantity, str(other_order_id))
        self._traded_quantity += quantity
        self.release_quantity_for_tick(other_order_id, quantity)
        assert self.available_quantity >= 0, str(self.available_quantity)

        if self.is_complete():
            self._completed_timestamp = Timestamp.now()
コード例 #37
0
ファイル: transaction.py プロジェクト: synctext/tribler
 def from_proposed_trade(cls, proposed_trade, transaction_id):
     """
     :param proposed_trade: The proposed trade to create the transaction for
     :param transaction_id: The transaction id to use for this transaction
     :type proposed_trade: ProposedTrade
     :type transaction_id: TransactionId
     :return: The created transaction
     :rtype: Transaction
     """
     return cls(transaction_id, proposed_trade.assets, proposed_trade.recipient_order_id,
                proposed_trade.order_id, Timestamp.now())
コード例 #38
0
ファイル: test_timestamp.py プロジェクト: Tribler/tribler
class TimestampTestSuite(unittest.TestCase):
    """Timestamp test cases."""

    def setUp(self):
        # Object creation
        self.timestamp = Timestamp(1462224447.117)
        self.timestamp2 = Timestamp(1462224447.117)
        self.timestamp3 = Timestamp(1305743832.438)

    def test_init(self):
        # Test for init validation
        with self.assertRaises(ValueError):
            Timestamp(-1.0)
        with self.assertRaises(ValueError):
            Timestamp("1")

    def test_now(self):
        # Test for Timestamp.now
        self.assertAlmostEqual(time.time(), float(Timestamp.now()), delta=.1)

    def test_conversion(self):
        # Test for conversions
        self.assertEqual(1462224447.117, float(self.timestamp))

        # We cannot check the exact timestamp since this is specific to the configured time zone
        self.assertTrue(str(self.timestamp))

    def test_comparison(self):
        # Test for comparison
        self.assertTrue(self.timestamp3 < self.timestamp)
        self.assertTrue(self.timestamp > self.timestamp3)
        self.assertTrue(self.timestamp3 < 1405743832.438)
        self.assertTrue(self.timestamp <= 1462224447.117)
        self.assertTrue(self.timestamp > 1362224447.117)
        self.assertTrue(self.timestamp3 >= 1305743832.438)
        self.assertEqual(NotImplemented, self.timestamp.__lt__(10))
        self.assertEqual(NotImplemented, self.timestamp.__le__(10))
        self.assertEqual(NotImplemented, self.timestamp.__gt__(10))
        self.assertEqual(NotImplemented, self.timestamp.__ge__(10))

    def test_equality(self):
        # Test for equality
        self.assertTrue(self.timestamp == self.timestamp2)
        self.assertTrue(self.timestamp != self.timestamp3)
        self.assertFalse(self.timestamp == 6)

    def test_hash(self):
        # Test for hashes
        self.assertEqual(self.timestamp.__hash__(), self.timestamp2.__hash__())
        self.assertNotEqual(self.timestamp.__hash__(), self.timestamp3.__hash__())
コード例 #39
0
ファイル: test_tick.py プロジェクト: synctext/tribler
 def setUp(self):
     # Object creation
     self.timestamp_now = Timestamp.now()
     self.tick = Tick(OrderId(TraderId('0'), OrderNumber(1)),
                      AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0.0), True)
     self.tick2 = Tick(OrderId(TraderId('0'), OrderNumber(2)),
                       AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0.0), False)
     self.order_ask = Order(OrderId(TraderId('0'), OrderNumber(2)),
                            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
                            Timeout(0), Timestamp(0.0), True)
     self.order_bid = Order(OrderId(TraderId('0'), OrderNumber(2)),
                            AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')),
                            Timeout(0), Timestamp(0.0), False)
コード例 #40
0
ファイル: test_orderbook.py プロジェクト: synctext/tribler
    def test_update_ticks(self):
        """
        Test updating ticks in an order book
        """
        self.order_book.insert_ask(self.ask)
        self.order_book.insert_bid(self.bid)

        ask_dict = {
            "trader_id": str(self.ask.order_id.trader_id),
            "order_number": int(self.ask.order_id.order_number),
            "assets": self.ask.assets.to_dictionary(),
            "traded": 100,
            "timeout": 3600,
            "timestamp": float(Timestamp.now())
        }
        bid_dict = {
            "trader_id": str(self.bid.order_id.trader_id),
            "order_number": int(self.bid.order_id.order_number),
            "assets": self.bid.assets.to_dictionary(),
            "traded": 100,
            "timeout": 3600,
            "timestamp": float(Timestamp.now())
        }

        self.order_book.get_tick(self.ask.order_id).reserve_for_matching(100)
        self.order_book.get_tick(self.bid.order_id).reserve_for_matching(100)
        self.order_book.update_ticks(ask_dict, bid_dict, 100, unreserve=True)

        self.assertEqual(len(self.order_book.asks), 0)
        self.assertEqual(len(self.order_book.bids), 1)
        self.order_book.remove_bid(self.bid.order_id)

        ask_dict["traded"] = 50
        bid_dict["traded"] = 50
        self.order_book.completed_orders = []
        self.order_book.update_ticks(ask_dict, bid_dict, 100)
        self.assertEqual(len(self.order_book.asks), 1)
        self.assertEqual(len(self.order_book.bids), 1)
コード例 #41
0
ファイル: order_manager.py プロジェクト: Tribler/tribler
    def create_bid_order(self, assets, timeout):
        """
        Create a bid order (buy order)

        :param assets: The assets to be exchanged
        :param timeout: The timeout of the order, when does the order need to be timed out
        :type assets: AssetPair
        :type timeout: Timeout
        :return: The order that is created
        :rtype: Order
        """
        order = Order(self.order_repository.next_identity(), assets, timeout, Timestamp.now(), False)
        self.order_repository.add(order)

        self._logger.info("Bid order created with id: " + str(order.order_id))

        return order
コード例 #42
0
ファイル: test_tickentry.py プロジェクト: Tribler/tribler
    def setUp(self):
        yield super(TickEntryTestSuite, self).setUp()

        # Object creation
        tick = Tick(OrderId(TraderId(b'0'), OrderNumber(1)), AssetPair(AssetAmount(60, 'BTC'), AssetAmount(30, 'MB')),
                    Timeout(0), Timestamp(0.0), True)
        tick2 = Tick(OrderId(TraderId(b'0'), OrderNumber(2)),
                     AssetPair(AssetAmount(63400, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now(), True)

        self.price_level = PriceLevel(Price(100, 'MB', 'BTC'))
        self.tick_entry = TickEntry(tick, self.price_level)
        self.tick_entry2 = TickEntry(tick2, self.price_level)
コード例 #43
0
ファイル: test_orderbook.py プロジェクト: synctext/tribler
 def setUp(self):
     yield super(AbstractTestOrderBook, self).setUp()
     # Object creation
     self.ask = Ask(OrderId(TraderId('0'), OrderNumber(1)),
                    AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
     self.invalid_ask = Ask(OrderId(TraderId('0'), OrderNumber(1)),
                            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0.0))
     self.ask2 = Ask(OrderId(TraderId('1'), OrderNumber(1)),
                     AssetPair(AssetAmount(400, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
     self.bid = Bid(OrderId(TraderId('2'), OrderNumber(1)),
                    AssetPair(AssetAmount(200, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
     self.invalid_bid = Bid(OrderId(TraderId('0'), OrderNumber(1)),
                            AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), Timeout(0), Timestamp(0.0))
     self.bid2 = Bid(OrderId(TraderId('3'), OrderNumber(1)),
                     AssetPair(AssetAmount(300, 'BTC'), AssetAmount(30, 'MB')), Timeout(100), Timestamp.now())
     self.trade = Trade.propose(TraderId('0'),
                                OrderId(TraderId('0'), OrderNumber(1)),
                                OrderId(TraderId('0'), OrderNumber(1)),
                                AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')),
                                Timestamp(1462224447.117))
     self.order_book = OrderBook()
コード例 #44
0
 def create_bid(self, amount1, amount2):
     """
     Create a bid with a specific price and quantity
     """
     new_bid = Bid(OrderId(TraderId('3'), OrderNumber(self.bid_count)),
                   AssetPair(AssetAmount(amount1, 'BTC'), AssetAmount(amount2, 'MB')), Timeout(30), Timestamp.now())
     self.bid_count += 1
     return new_bid
コード例 #45
0
 def create_ask(self, amount1, amount2):
     """
     Create an ask with a specific price and quantity
     """
     new_ask = Ask(OrderId(TraderId('2'), OrderNumber(self.ask_count)),
                   AssetPair(AssetAmount(amount1, 'BTC'), AssetAmount(amount2, 'MB')), Timeout(30), Timestamp.now())
     self.ask_count += 1
     return new_ask
コード例 #46
0
ファイル: test_timestamp.py プロジェクト: Tribler/tribler
 def setUp(self):
     # Object creation
     self.timestamp = Timestamp(1462224447.117)
     self.timestamp2 = Timestamp(1462224447.117)
     self.timestamp3 = Timestamp(1305743832.438)
コード例 #47
0
ファイル: test_timestamp.py プロジェクト: Tribler/tribler
 def test_now(self):
     # Test for Timestamp.now
     self.assertAlmostEqual(time.time(), float(Timestamp.now()), delta=.1)
コード例 #48
0
ファイル: test_community.py プロジェクト: synctext/tribler
 def get_tick_block(return_ask, pair):
     tick_cls = Ask if return_ask else Bid
     ask = tick_cls(OrderId(TraderId('0' * 40), OrderNumber(1)), pair, Timeout(3600), Timestamp.now(), return_ask)
     ask_tx = ask.to_block_dict()
     ask_tx["address"], ask_tx["port"] = "127.0.0.1", 1337
     tick_block = MarketBlock()
     tick_block.type = 'ask' if return_ask else 'bid'
     tick_block.transaction = {'tick': ask_tx, 'version': MarketCommunity.PROTOCOL_VERSION}
     return tick_block
コード例 #49
0
    def create_from_start_transaction(self, start_transaction, match_id):
        """
        :type start_transaction: StartTransaction
        :type match_id: str
        :rtype: Transaction
        """
        transaction = Transaction(start_transaction.transaction_id, start_transaction.assets,
                                  start_transaction.recipient_order_id, start_transaction.order_id, Timestamp.now())
        transaction.match_id = match_id
        self.transaction_repository.add(transaction)

        self._logger.info("Transaction created with id: %s, asset pair %s",
                          transaction.transaction_id, transaction.assets)

        return transaction
コード例 #50
0
ファイル: test_community.py プロジェクト: synctext/tribler
 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('0' * 40), OrderNumber(1)), ask_pair, Timeout(3600), Timestamp.now(), True)
     ask._traded_quantity = ask_total_traded
     bid = Order(OrderId(TraderId('1' * 40), OrderNumber(1)), bid_pair, Timeout(3600), Timestamp.now(), False)
     bid._traded_quantity = bid_total_traded
     tx = Transaction(TransactionId(TraderId('0' * 40), TransactionNumber(1)),
                      AssetPair(AssetAmount(traded_amount, 'BTC'), AssetAmount(traded_amount, 'MB')),
                      OrderId(TraderId('0' * 40), OrderNumber(1)),
                      OrderId(TraderId('1' * 40), OrderNumber(1)), Timestamp(0.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 = '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