def test_offer_comparison(): timeouts = [int(time.time() + i) for i in range(0, 4)] offer_ids = list(range(0, 4)) offer1 = OfferDeprecated(OfferType.BUY, 50, 5, timeout=timeouts[0], offer_id=offer_ids[0]) offer2 = OfferDeprecated(OfferType.BUY, 100, 1, timeout=timeouts[1], offer_id=offer_ids[1]) offer3 = OfferDeprecated(OfferType.BUY, 100, 2, timeout=timeouts[2], offer_id=offer_ids[2]) offer4 = OfferDeprecated(OfferType.BUY, 100, 1, timeout=timeouts[3], offer_id=offer_ids[3]) offers = OfferView() for offer in [offer1, offer2, offer3, offer4]: offers.add_offer(offer) assert list(offers.values()) == [offer2, offer4, offer3, offer1]
def test_offer_book_task(message_broker, commitment_service, market): offer_book = OfferBook() OfferBookTask(offer_book, market, message_broker).start() gevent.sleep(0.001) offer = OfferDeprecated(OfferType.SELL, 100, 1000, offer_id=123, timeout=timestamp.time_plus(20)) proof = commitment_service.maker_commit_async(offer).get() message_broker.broadcast(proof) gevent.sleep(0.001) assert len(offer_book.sells) == 1
def test_swap_completed_task(message_broker, commitment_service): trades = TradesView() SwapCompletedTask(trades, message_broker).start() gevent.sleep(0.001) offer = OfferDeprecated(OfferType.SELL, 100, 1000, offer_id=123, timeout=timestamp.time_plus(2)) # set it to pending, as it was taken trades.add_pending(offer) assert len(trades.pending_offer_by_id) == 1 swap_completed = commitment_service.create_swap_completed(offer.offer_id) # send swap_completed message_broker.broadcast(swap_completed) gevent.sleep(0.001) assert len(trades) == 1
def test_taken_task(message_broker, commitment_service): offer_book = OfferBook() trades = TradesView() OfferTakenTask(offer_book, trades, message_broker).start() gevent.sleep(0.001) offer = OfferDeprecated(OfferType.SELL, 100, 1000, offer_id=123, timeout=timestamp.time_plus(2)) # insert manually for the first time offer_book.insert_offer(offer) assert len(offer_book.sells) == 1 offer_taken = commitment_service.create_taken(offer.offer_id) # send offer_taken message_broker.broadcast(offer_taken) gevent.sleep(0.001) assert len(offer_book.sells) == 0 assert len(trades.pending_offer_by_id) == 1
def gen_offer(magic_number, market_price=10.0, max_amount=1000 * ETH, max_deviation=0.01): price = market_price operator = [-1, 1] switch = random.choice((0, 1)) type_ = OfferType(switch) for _ in range(magic_number): drift = random.random() * max_deviation * operator[switch] factor = 1 + drift price *= factor base_amount = random.randint(1, max_amount) quote_amount = int(base_amount * float(price)) assert type_ in (OfferType.BUY, OfferType.SELL) offer = OfferDeprecated(type_, base_amount, quote_amount, # reuse random privkey generation for random offer-ids: big_endian_to_int(make_privkey_address()[0]), # timeout int 10-100 seconds timestamp.time_plus(random.randint(10, 100)) ) return offer
def test_node_to_commitment_service_integration(raidex_nodes, commitment_service): commitment_service.start() [node.start() for node in raidex_nodes] maker_node = raidex_nodes[0] taker_node = raidex_nodes[1] commitment_amount = 5 # this are the initial commitment balances initial_maker_balance = maker_node.trader_client.commitment_balance initial_taker_balance = taker_node.trader_client.commitment_balance initial_commitment_service_balance = commitment_service.trader_client.commitment_balance offer_id = generate_random_offer_id() offer = OfferDeprecated(OfferType.SELL, 100, 1000, offer_id=offer_id, commitment_amount=commitment_amount, timeout=timestamp.time_plus(seconds=0, milliseconds=500)) maker_commit_result = maker_node.commitment_service.maker_commit_async(offer) gevent.sleep(0.01) assert commitment_service.trader_client.commitment_balance == initial_commitment_service_balance + commitment_amount assert maker_node.trader_client.commitment_balance == initial_maker_balance - commitment_amount commitment_service_balance = commitment_service.trader_client.commitment_balance maker_proven_offer = maker_commit_result.get() assert isinstance(maker_proven_offer, messages.ProvenOffer) # CommitmentProof has to be signed by the CS assert maker_proven_offer.commitment_proof.sender == commitment_service.address # ProvenOffer has to be signed by the maker assert maker_proven_offer.sender == maker_node.address # broadcast the ProvenOffer maker_node.message_broker.broadcast(maker_proven_offer) gevent.sleep(0.01) # the taker needs to have the additional commitment-amount information from the ProvenOffer # he should have got it from the broadcasted ProvenOffer taker_internal_offer = taker_node.offer_book.get_offer_by_id(offer.offer_id) taker_commit_result = taker_node.commitment_service.taker_commit_async(taker_internal_offer) gevent.sleep(0.01) assert commitment_service.trader_client.commitment_balance == commitment_service_balance + commitment_amount assert taker_node.trader_client.commitment_balance == initial_taker_balance - commitment_amount taker_proven_commitment = taker_commit_result.get() assert isinstance(taker_proven_commitment, messages.ProvenCommitment) assert taker_proven_commitment.commitment_proof.sender == commitment_service.address assert taker_proven_commitment.sender == taker_node.address maker_node.commitment_service.received_inbound_from_swap(offer.offer_id) taker_node.commitment_service.received_inbound_from_swap(offer.offer_id) gevent.sleep(0.01) # should be processed by the commitment_service, offer_id usable again assert offer_id not in commitment_service.swaps # Check the earnings and refunds assert float_isclose(maker_node.trader_client.commitment_balance, initial_maker_balance - (commitment_amount * commitment_service.fee_rate)) assert float_isclose(taker_node.trader_client.commitment_balance, initial_taker_balance - (commitment_amount * commitment_service.fee_rate)) assert float_isclose(commitment_service.trader_client.commitment_balance, 2 * commitment_amount + 2 * (commitment_amount * commitment_service.fee_rate)) # overall balance shouldn't have changed assert maker_node.trader_client.commitment_balance + taker_node.trader_client.commitment_balance \ + commitment_service.trader_client.commitment_balance == initial_commitment_service_balance \ + initial_taker_balance + initial_maker_balance