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)
class TransactionNumberTestSuite(unittest.TestCase): """Message number test cases.""" def setUp(self): # Object creation self.transaction_number = TransactionNumber(1) self.transaction_number2 = TransactionNumber(1) self.transaction_number3 = TransactionNumber(3) def test_conversion(self): # Test for conversions self.assertEqual(1, int(self.transaction_number)) self.assertEqual('1', str(self.transaction_number)) def test_init(self): # Test for init validation with self.assertRaises(ValueError): TransactionNumber(1.0) def test_equality(self): # Test for equality self.assertTrue(self.transaction_number == self.transaction_number2) self.assertTrue(self.transaction_number == self.transaction_number) self.assertTrue(self.transaction_number != self.transaction_number3) self.assertFalse(self.transaction_number == 6) def test_hash(self): # Test for hashes self.assertEqual(self.transaction_number.__hash__(), self.transaction_number2.__hash__()) self.assertNotEqual(self.transaction_number.__hash__(), self.transaction_number3.__hash__())
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)
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)
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)
class TransactionNumberTestSuite(unittest.TestCase): """Message number test cases.""" def setUp(self): # Object creation self.transaction_number = TransactionNumber(1) self.transaction_number2 = TransactionNumber(1) self.transaction_number3 = TransactionNumber(3) def test_conversion(self): # Test for conversions self.assertEqual(1, int(self.transaction_number)) self.assertEqual('1', str(self.transaction_number)) def test_init(self): # Test for init validation with self.assertRaises(ValueError): TransactionNumber(1.0) def test_equality(self): # Test for equality self.assertTrue(self.transaction_number == self.transaction_number2) self.assertTrue(self.transaction_number != self.transaction_number3) self.assertFalse(self.transaction_number == 6) def test_hash(self): # Test for hashes self.assertEqual(self.transaction_number.__hash__(), self.transaction_number2.__hash__()) self.assertNotEqual(self.transaction_number.__hash__(), self.transaction_number3.__hash__())
def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId('0'), TransactionNumber(1)) self.transaction_id2 = TransactionId(TraderId('0'), TransactionNumber(1)) self.transaction_id3 = TransactionId(TraderId('0'), TransactionNumber(2))
def setUp(self): # Object creation self.payment = Payment( MessageId(TraderId("0"), MessageNumber(1)), TransactionId(TraderId('2'), TransactionNumber(2)), Quantity(3, 'MC'), Price(2, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True)
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()
def next_identity(self): """ :rtype: TransactionId """ self._next_id += 1 return TransactionId(TraderId(self._mid), TransactionNumber(self._next_id))
def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction(self.transaction_id, Price(100, 'BTC'), Quantity(30, 'MC'), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(1)), Timestamp(0.0)) self.proposed_trade = Trade.propose( MessageId(TraderId('0'), MessageNumber('1')), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(3)), Price(100, 'BTC'), Quantity(30, 'MC'), Timestamp(0.0)) self.tick = Tick( MessageId(TraderId('0'), MessageNumber('message_number')), OrderId(TraderId('0'), OrderNumber(1)), Price(100, 'BTC'), Quantity(5, 'MC'), Timeout(0.0), Timestamp(float("inf")), True) self.tick2 = Tick( MessageId(TraderId('0'), MessageNumber('message_number')), OrderId(TraderId('0'), OrderNumber(2)), Price(100, 'BTC'), Quantity(100, 'MC'), Timeout(0.0), Timestamp(float("inf")), True) self.order = Order(OrderId(TraderId("0"), OrderNumber(3)), Price(100, 'BTC'), Quantity(30, 'MC'), Timeout(5000), Timestamp(time.time()), False) self.order2 = Order(OrderId(TraderId("0"), OrderNumber(4)), Price(100, 'BTC'), Quantity(30, 'MC'), Timeout(5), Timestamp(time.time() - 1000), True)
def setUp(self): # Object creation self.start_transaction_payload = StartTransactionPayload.Implementation( MetaObject(), TraderId('0'), MessageNumber('1'), TraderId('2'), TransactionNumber(2), TraderId('2'), OrderNumber(3), TraderId('0'), OrderNumber(4), 1236, Price(2, 'BTC'), Quantity(3, 'MC'), Timestamp(0.0))
def setUp(self): # Object creation self.payment = Payment(TraderId("0"), TransactionId(TraderId('2'), TransactionNumber(2)), AssetAmount(3, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True)
def next_identity(self): """ :rtype: TransactionId """ return TransactionId( TraderId(self._mid), TransactionNumber(self.persistence.get_next_transaction_number()))
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)
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)
def test_properties(self): """ Test the start transaction payload """ self.assertEquals(MessageNumber('1'), self.start_transaction_payload.message_number) self.assertEquals(TransactionNumber(2), self.start_transaction_payload.transaction_number) self.assertEquals(Timestamp(0.0), self.start_transaction_payload.timestamp) self.assertEquals(TraderId('2'), self.start_transaction_payload.transaction_trader_id) self.assertEquals(TraderId('2'), self.start_transaction_payload.order_trader_id) self.assertEquals(OrderNumber(3), self.start_transaction_payload.order_number) self.assertEquals(TraderId('0'), self.start_transaction_payload.recipient_trader_id) self.assertEquals( OrderNumber(4), self.start_transaction_payload.recipient_order_number) self.assertEquals(1236, self.start_transaction_payload.proposal_id) self.assertEquals(Price(2, 'BTC'), self.start_transaction_payload.price) self.assertEquals(Quantity(3, 'MC'), self.start_transaction_payload.quantity)
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
def setUp(self): # Object creation self.payment_payload = PaymentPayload.Implementation( MetaObject(), TraderId('0'), MessageNumber('1'), TraderId('2'), TransactionNumber(2), Quantity(20, 'MC'), Price(10, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('3'), Timestamp(0.0), True)
def setUp(self): # Object creation self.start_transaction = StartTransaction( MessageId(TraderId('0'), MessageNumber('1')), TransactionId(TraderId("0"), TransactionNumber(1)), OrderId(TraderId('0'), OrderNumber(1)), OrderId(TraderId('1'), OrderNumber(1)), 1234, Price(30, 'BTC'), Quantity(40, 'MC'), Timestamp(0.0))
def from_unpack_list(cls, trader_id, timestamp, transaction_trader_id, transaction_number, incoming_address, outgoing_address): return WalletInfoPayload( TraderId(trader_id), Timestamp(timestamp), TransactionId(TraderId(transaction_trader_id), TransactionNumber(transaction_number)), WalletAddress(incoming_address), WalletAddress(outgoing_address))
def test_get_specific_transaction(self): """ Test the retrieval of a specific transaction """ transaction_id = TransactionId(TraderId(b'0'), TransactionNumber(4)) self.assertIsNone(self.database.get_transaction(transaction_id)) self.database.add_transaction(self.transaction1) self.assertIsNotNone(self.database.get_transaction(transaction_id))
def setUp(self): # Object creation self.start_transaction = StartTransaction( TraderId('0'), TransactionId(TraderId("0"), TransactionNumber(1)), OrderId(TraderId('0'), OrderNumber(1)), OrderId(TraderId('1'), OrderNumber(1)), 1234, AssetPair(AssetAmount(30, 'BTC'), AssetAmount(40, 'MC')), Timestamp(0.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), AssetAmount(asset2_amount, asset2_type)))
def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), OrderId(TraderId('3'), OrderNumber(2)), OrderId(TraderId('2'), 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(100, 'MB')), Timestamp(0.0)) self.payment = Payment( TraderId("0"), TransactionId(TraderId('2'), TransactionNumber(2)), AssetAmount(3, 'MB'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True)
def from_unpack_list(cls, trader_id, message_number, timestamp, transaction_trader_id, transaction_number, order_trader_id, order_number, recipient_trader_id, recipient_order_number, proposal_id, price, price_type, quantity, quantity_type): return StartTransactionPayload(MessageId(TraderId(trader_id), MessageNumber(message_number)), Timestamp(timestamp), TransactionId(TraderId(transaction_trader_id), TransactionNumber(transaction_number)), OrderId(TraderId(order_trader_id), OrderNumber(order_number)), OrderId(TraderId(recipient_trader_id), OrderNumber(recipient_order_number)), proposal_id, Price(price, price_type), Quantity(quantity, quantity_type))
def from_unpack_list(cls, trader_id, message_number, timestamp, transaction_trader_id, transaction_number, transferee_quantity, transferee_quantity_type, transferee_price, transferee_price_type, address_from, address_to, payment_id, success): return PaymentPayload(MessageId(TraderId(trader_id), MessageNumber(message_number)), Timestamp(timestamp), TransactionId(TraderId(transaction_trader_id), TransactionNumber(transaction_number)), Quantity(transferee_quantity, transferee_quantity_type), Price(transferee_price, transferee_price_type), WalletAddress(address_from), WalletAddress(address_to), PaymentId(payment_id), success)
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}
def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction(self.transaction_id, Price(100, 'BTC'), Quantity(30, 'MC'), OrderId(TraderId('3'), OrderNumber(2)), OrderId(TraderId('2'), OrderNumber(1)), Timestamp(0.0)) self.proposed_trade = Trade.propose( MessageId(TraderId('0'), MessageNumber('1')), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(3)), Price(100, 'BTC'), Quantity(30, 'MC'), Timestamp(0.0)) self.payment = Payment( MessageId(TraderId("0"), MessageNumber("1")), TransactionId(TraderId('2'), TransactionNumber(2)), Quantity(3, 'MC'), Price(2, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True)
def setUp(self): # Object creation self.memory_transaction_repository = MemoryTransactionRepository("0") self.transaction_id = TransactionId(TraderId(b"0"), TransactionNumber(1)) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(10, 'BTC'), AssetAmount(10, 'MB')), OrderId(TraderId(b"0"), OrderNumber(1)), OrderId(TraderId(b"2"), OrderNumber(2)), Timestamp(0.0))
def setUp(self): # Object creation self.memory_transaction_repository = MemoryTransactionRepository("0") self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction(self.transaction_id, Price(100, 'BTC'), Quantity(30, 'MC'), OrderId(TraderId("0"), OrderNumber(1)), OrderId(TraderId("2"), OrderNumber(2)), Timestamp(0.0))
def setUp(self): # Object creation self.transaction_number = TransactionNumber(1) self.transaction_number2 = TransactionNumber(1) self.transaction_number3 = TransactionNumber(3)