def other_transfer_receipt(other_account, maker_commitment_msg_signed): amount = maker_commitment_msg_signed.amount # FIXME checkme received_timestamp = timestamp.time() + 1 identifier = maker_commitment_msg_signed.offer_id return TransferReceipt(other_account.address, identifier, amount, received_timestamp)
def make_price_bins(trades_gen_func, nof_buckets, interval): price_group_precision = PRICE_GROUP_PRECISION time_group_interval = int(timestamp.to_milliseconds(interval)) price_bins = {} # timestamp_bin -> price_bin last_close_price = 0. current_timestamp = timestamp.time() from_start_time, to_start_time = tuple( time_bin_gen(current_timestamp, 2, time_group_interval)) from_time_gen = time_bin_gen(from_start_time, nof_buckets + 1, time_group_interval) to_time_gen = time_bin_gen(to_start_time, nof_buckets + 1, time_group_interval) for from_time, to_time in zip(from_time_gen, to_time_gen): price_bin = PriceBin(last_close_price, from_time) price_bins[from_time] = price_bin for trade in trades_gen_func(from_timestamp=from_time, to_timestamp=to_time): quantized_price = Decimal(trade.offer.price).quantize( Decimal(10)**-price_group_precision) price_bin.add(trade.timestamp, trade.offer.base_amount, quantized_price) last_close_price = price_bin.close_price assert len(price_bins) == nof_buckets + 1 unsorted_list = price_bins.values() sorted_list = sorted(unsorted_list) # throw away the first bin again, it was there just to determine the first open price return sorted_list[1:]
def gen_orderbook_messages(market_price=10, max_amount=1000 * ETH, num_messages=200, max_deviation=0.01): assert isinstance(market_price, int) offers = [] asks_price = copy.deepcopy(market_price) bids_price = copy.deepcopy(market_price) for _ in range(num_messages): # odd i stands for bids if i % 2: # asks factor = 1 + random.random() * max_deviation asks_price *= factor bid_amount = random.randrange(1, max_amount) ask_amount = int(bid_amount / asks_price) else: # bids factor = 1 - random.random() * max_deviation bids_price *= factor bid_amount = random.randrange(2, max_amount) ask_amount = int(bid_amount / bids_price) maker = ACCOUNTS[num_messages % 2] offer = messages.SwapOffer(ASSETS[i % 2], ask_amount, ASSETS[1 - i % 2], bid_amount, keccak('offer {}'.format(i)), # TODO better offer_ids int(timestamp.time() * 10000 + 1000 * random.randint(1, 10) + i)) offers.append(offer) return offers
def test_offer(assets): o = SwapOffer(assets[0], 100, assets[1], 110, big_endian_to_int(keccak(text='offer id')), timestamp.time() - 10) assert isinstance(o, SwapOffer) serial = o.serialize(o) assert_serialization(o) assert_envelope_serialization(o) assert SwapOffer.deserialize(serial) == o assert o.timed_out() assert not o.timed_out(at=timestamp.time() - 3600 * 1000) # pretend we come from the past
def test_swap_completed(accounts): commitment_service = accounts[0] swap_completed_msg = SwapCompleted(big_endian_to_int(keccak(text='offer id')), timestamp.time()) swap_completed_msg.sign(commitment_service.privatekey) time_ = timestamp.time_plus(1) assert_serialization(swap_completed_msg) assert_envelope_serialization(swap_completed_msg) assert swap_completed_msg.sender == commitment_service.address assert time_ > swap_completed_msg.timestamp # should be in the past
def test_swap_execution(accounts): maker = accounts[1] swap_execution_msg = SwapExecution(big_endian_to_int(keccak(text='offer id')), timestamp.time()) swap_execution_msg.sign(maker.privatekey) assert_serialization(swap_execution_msg) assert_envelope_serialization(swap_execution_msg) time_ = timestamp.time_plus(1) assert swap_execution_msg.sender == maker.address assert time_ > swap_execution_msg.timestamp # should be in the past
def gen_orderhistory(start_price=10, max_amount=1000 * ETH, num_entries=100, max_deviation=0.01): tstamp = timestamp.time() avg_num_orders_per_second = 0.01 avg_gap_between_orders = 1 / avg_num_orders_per_second avg_gap_deviation = 2 orders = [] for address, price, amount in gen_orders(start_price, max_amount * ETH, num_entries, max_deviation): elapsed = avg_gap_between_orders + (random.random() * 2 - 1) * avg_gap_deviation tstamp += elapsed orders.append(dict( timestamp=int(1000 * tstamp), address=address, price=price, amount=amount, type=random.randint(0, 1) )) return orders
def create_swap_completed(self, offer_id): # type: (int) -> SwapCompleted swap_completed_msg = messages.SwapCompleted(offer_id, timestamp.time()) self._global_sign(swap_completed_msg) return swap_completed_msg
def send_swap_completed(self): swap_completed_message = messages.SwapCompleted( self.offer_id, timestamp.time()) self.queue_send(swap_completed_message, None)
def swap_execution_msg(): swap_execution_msg = messages.SwapExecution(123, timestamp.time()) return swap_execution_msg
def taker_transfer_receipt(taker_account, maker_commitment_msg_signed): amount = maker_commitment_msg_signed.amount received_timestamp = timestamp.time() identifier = maker_commitment_msg_signed.offer_id return TransferReceipt(taker_account.address, identifier, amount, received_timestamp)
def _transform(self, event): if self.initiator is not None: if event.initiator != self.initiator: return None if isinstance(event, EventPaymentReceivedSuccess): # transform event into receipt, with additional timestamp receipt = TransferReceipt(event.initiator, event.amount, event.identifier, timestamp.time()) return receipt else: return None