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)
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)
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(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_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())
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
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)
def next_identity(self): """ :rtype: TransactionId """ return TransactionId( TraderId(self._mid), TransactionNumber(self.persistence.get_next_transaction_number()))
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.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)
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]
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))
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'))))
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))
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)
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}
def setUp(self): # Object creation self.memory_transaction_repository = MemoryTransactionRepository(b'0' * 20) self.transaction_id = TransactionId(TraderId(b'0' * 20), TransactionNumber(1)) 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))
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)
def from_database(cls, data): """ Create a Payment object based on information in the database. """ (trader_id, transaction_trader_id, transaction_number, payment_id, transferred_amount, transferred_id, address_from, address_to, timestamp, success) = data transaction_id = TransactionId(TraderId(bytes(transaction_trader_id)), TransactionNumber(transaction_number)) 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), bool(success))
def render_GET(self, request): """ .. http:get:: /market/transactions/(string:trader_id)/(string:transaction_number)/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(TraderId(unhexlify(self.transaction_trader_id)), TransactionNumber(int(self.transaction_number))) transaction = self.get_market_community().transaction_manager.find_by_id(transaction_id) if not transaction: request.setResponseCode(http.NOT_FOUND) return json.twisted_dumps({"error": "transaction not found"}) return json.twisted_dumps({"payments": [payment.to_dictionary() for payment in transaction.payments]})
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' * 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(TraderId(b'0' * 20), TransactionNumber(4)) 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), False) self.transaction1.add_payment(self.payment1)
def setUp(self): # Object creation self.transaction_number = TransactionNumber(1) self.transaction_number2 = TransactionNumber(1) self.transaction_number3 = TransactionNumber(3)
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_init(self): # Test for init validation with self.assertRaises(ValueError): TransactionNumber(1.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.decode('utf-8')), WalletAddress(outgoing_address.decode('utf-8')))