def make_order(self, account, is_primary_lqd, is_sell, amount, price): if is_sell: amount = amount amount_swapped = amount*price self.assertEqual(amount_swapped//price, amount) else: amount_swapped = amount amount = amount*price self.assertEqual(amount//price, amount_swapped) if is_primary_lqd: primary_token = self.lqd_token secondary_token = self.eth_token else: secondary_token = self.lqd_token primary_token = self.eth_token nonce = random.randint(1, 999999) send_swap( test_case=self, eon_number=self.eon_number, account=account, token=primary_token, token_swapped=secondary_token, amount=int(amount), amount_swapped=int(amount_swapped), nonce=nonce, sell_order=is_sell) process_swaps_for_eon(operator_eon_number=self.eon_number) return nonce
def test_checkpoint_creation(self): self.eth_token = Token.objects.first() lqd_token_address = deploy_new_test_token(test_case=self) distribute_token_balance_to_addresses( test_case=self, token_address=lqd_token_address, recipients=testrpc_accounts.accounts) self.lqd_token = register_token(token_address=lqd_token_address, name='Liquidity', short_name='LQD', register_on_chain=True) send_queued_transactions() self.tokens = [self.eth_token, self.lqd_token] TokenPair.objects.create(token_from=self.eth_token, token_to=self.lqd_token) TokenPair.objects.create(token_from=self.lqd_token, token_to=self.eth_token) self.registered_accounts = { self.eth_token: register_testrpc_accounts(self, token=self.eth_token), self.lqd_token: register_testrpc_accounts(self, token=self.lqd_token) } for token in self.tokens: self.assertEqual( self.contract_interface.get_unmanaged_funds(token.address, 1), 0) self.assertEqual( self.contract_interface.get_managed_funds(token.address, 1), 0) for token in self.tokens: create_deposits(self, testrpc_accounts.accounts, token) for token in self.tokens: self.assertEqual( self.contract_interface.get_unmanaged_funds(token.address, 1), self.contract_interface.get_total_balance(token.address)) self.assertEqual( self.contract_interface.get_managed_funds(token.address, 1), 0) for account in testrpc_accounts.accounts[1:5]: eth_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.eth_token) lqd_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.lqd_token) eth_wallet_context = WalletTransferContext(wallet=eth_wallet, transfer=None) lqd_wallet_context = WalletTransferContext(wallet=lqd_wallet, transfer=None) account[ 'reduced_eth_balance'] = eth_wallet_context.available_funds_at_eon( eon_number=1, only_appended=False) - 1 account[ 'reduced_lqd_balance'] = lqd_wallet_context.available_funds_at_eon( eon_number=1, only_appended=False) send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) for account in testrpc_accounts.accounts[5:9]: eth_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.eth_token) lqd_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.lqd_token) eth_wallet_context = WalletTransferContext(wallet=eth_wallet, transfer=None) lqd_wallet_context = WalletTransferContext(wallet=lqd_wallet, transfer=None) account[ 'reduced_eth_balance'] = eth_wallet_context.available_funds_at_eon( eon_number=1, only_appended=False) account[ 'reduced_lqd_balance'] = lqd_wallet_context.available_funds_at_eon( eon_number=1, only_appended=False) - 2 send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=random.randint(1, 999999)) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in testrpc_accounts.accounts[1:5]: finalize_last_swap(test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) for account in testrpc_accounts.accounts[5:9]: finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) buyers_sellers = [ testrpc_accounts.accounts[1:5], testrpc_accounts.accounts[5:9] ] for i in range(0, 2): for account in buyers_sellers[i]: eth_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.eth_token) lqd_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=self.lqd_token) eth_wallet_context = WalletTransferContext(wallet=eth_wallet, transfer=None) lqd_wallet_context = WalletTransferContext(wallet=lqd_wallet, transfer=None) eth_out, eth_in = eth_wallet_context.off_chain_actively_sent_received_amounts( eon_number=1, only_appended=False) lqd_out, lqd_in = lqd_wallet_context.off_chain_actively_sent_received_amounts( eon_number=1, only_appended=False) eth_out -= account['reduced_eth_balance'] lqd_out -= account['reduced_lqd_balance'] if i == 0: # LQD buyers assert eth_out == 1 and eth_in == 0, '{}/{}'.format( eth_out, eth_in) assert lqd_out - 2 == 0 and lqd_in - 2 == 2, '{}/{}'.format( lqd_out, lqd_in) else: # LQD sellers assert lqd_out == 2 and lqd_in == 0, '{}/{}'.format( lqd_out, lqd_in) assert eth_out - 1 == 0 and eth_in - 1 == 1, '{}/{}'.format( eth_out, eth_in) # Verify transfers were complete swaps = Transfer.objects.filter(swap=True) for transfer in swaps: self.assertTrue(transfer.is_fulfilled_swap()) commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2)
def test_unmatched_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) # make a valid swap send_swap(test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) swap = Transfer.objects.filter(swap=True)[0] wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) funds_before = 1 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # proceed to next eon advance_to_next_eon(test_case=self, eon_number=2) commit_eon(test_case=self, eon_number=3) funds_after = wallet_transfer_context.balance_amount_as_of_eon(3) self.assertEqual(funds_before, funds_after) # make and cancel a swap send_swap(test_case=self, eon_number=3, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) freeze_last_swap(test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[1]) cancel_last_swap(test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[1]) funds_before = wallet_transfer_context.balance_amount_as_of_eon(3) # process swaps confirm_swaps_for_eon(operator_eon_number=3) cancel_finalize_swaps_for_eon(operator_eon_number=3) process_swaps_for_eon(operator_eon_number=3) # proceed to next eon advance_to_next_eon(test_case=self, eon_number=3) commit_eon(test_case=self, eon_number=4) funds_after = wallet_transfer_context.balance_amount_as_of_eon(4) self.assertEqual(funds_before, funds_after)
def test_match_multi_eon_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) buy_lqd_nonce = random.randint(1, 999999) sell_lqd_nonce = random.randint(1, 999999) total_remaining_eons = 5 # make persistent swap send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=buy_lqd_nonce, eon_count=total_remaining_eons) self.assertEqual( Transfer.objects.filter(swap=True).count(), total_remaining_eons) swap = Transfer.objects.filter(swap=True)[0] swap_tx_id = swap.tx_id wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) recipient_transfer_context = WalletTransferContext( wallet=swap.recipient, transfer=None) wallet_funds_before = 1 recipient_funds_before = 0 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # skip some eons for i in range(3, 6): # proceed to next eon advance_to_next_eon(test_case=self, eon_number=i - 1) commit_eon(test_case=self, eon_number=i) total_remaining_eons -= 1 # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), recipient_funds_before) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=5, account=self.testrpc_accounts[2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) swap1 = Transfer.objects.filter(eon_number=5).order_by('time')[0] swap2 = Transfer.objects.filter(eon_number=5).order_by('time')[1] self.assertNotEqual(swap1.tx_id, swap2.tx_id) # finalize swaps swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=5) self.assertEqual( Transfer.objects.filter(tx_id=swap_tx_id, eon_number__gt=5, swap=True, voided=False).count(), 0) finalize_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1], eon_count=total_remaining_eons) finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[2], eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) self.assertEqual( wallet_transfer_context.available_funds_at_eon(5, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(5, False), recipient_funds_before + 2)
def test_partial_match_multi_eon_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) buy_lqd_nonce = random.randint(1, 999999) sell_lqd_nonce = random.randint(1, 999999) total_remaining_eons = 8 # make persistent swap send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=4, amount_swapped=8, nonce=buy_lqd_nonce, eon_count=total_remaining_eons) swap = Transfer.objects.filter( swap=True, wallet__token=self.eth_token).order_by('id')[0] swap_tx_id = swap.tx_id wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) recipient_transfer_context = WalletTransferContext( wallet=swap.recipient, transfer=None) wallet_funds_before = 4 recipient_funds_before = 0 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) self.assertEqual( wallet_transfer_context.available_funds_at_eon(2, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(2, False), recipient_funds_before + 2) wallet_funds_before = wallet_funds_before - 1 recipient_funds_before = recipient_funds_before + 2 # skip some eons for i in range(3, 5): # proceed to next eon advance_to_next_eon(test_case=self, eon_number=i - 1) commit_eon(test_case=self, eon_number=i) total_remaining_eons -= 1 swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=i) self.assertEqual(swap.eon_number, i) # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), recipient_funds_before) new_wallet_funds = wallet_funds_before new_recipient_funds = recipient_funds_before # partial match across eons for i in range(5, 8): # proceed to next eon advance_to_next_eon(test_case=self, eon_number=i - 1) commit_eon(test_case=self, eon_number=i) total_remaining_eons -= 1 swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=i) self.assertEqual(swap.eon_number, i) # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), new_wallet_funds) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), new_recipient_funds) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=i, account=self.testrpc_accounts[i - 2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[i - 2], eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), new_wallet_funds - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), new_recipient_funds + 2) new_wallet_funds = new_wallet_funds - 1 new_recipient_funds = new_recipient_funds + 2 swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=7) matched_out, matched_in = swap.matched_amounts(all_eons=True) # self.assertTrue(swap.processed) self.assertTrue(swap.appended) self.assertFalse(swap.voided) self.assertFalse(swap.cancelled) self.assertEqual(matched_out, 4) self.assertEqual(matched_in, 8) self.assertTrue(swap.complete) self.assertEqual( Transfer.objects.filter(tx_id=swap_tx_id, eon_number__gt=7, swap=True, voided=False).count(), 0) finalize_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1], eon_count=total_remaining_eons) # process swaps confirm_swaps_for_eon(operator_eon_number=7) cancel_finalize_swaps_for_eon(operator_eon_number=7) process_swaps_for_eon(operator_eon_number=7) self.assertEqual( wallet_transfer_context.available_funds_at_eon(7, False), wallet_funds_before - 3) self.assertEqual( recipient_transfer_context.available_funds_at_eon(7, False), recipient_funds_before + 6) advance_to_next_eon(test_case=self, eon_number=7) commit_eon(test_case=self, eon_number=8) # process swaps confirm_swaps_for_eon(operator_eon_number=8) cancel_finalize_swaps_for_eon(operator_eon_number=8) process_swaps_for_eon(operator_eon_number=8) self.assertEqual( wallet_transfer_context.available_funds_at_eon(8, False), wallet_funds_before - 3) self.assertEqual( recipient_transfer_context.available_funds_at_eon(8, False), recipient_funds_before + 6)
def test_swap_freezing(self): for i in range(0, 2): for account in self.testrpc_accounts[:4]: send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) # No Matches, just signatures confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in self.testrpc_accounts[:2]: freeze_last_swap(test_case=self, account=account, token=self.eth_token, token_swapped=self.lqd_token) cancel_last_swap(test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) for account in self.testrpc_accounts[4:]: send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=random.randint(1, 999999)) # 2 Matches confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in self.testrpc_accounts[2:4]: finalize_last_swap(test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) for account in self.testrpc_accounts[4:6]: finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in self.testrpc_accounts[6:]: freeze_last_swap(test_case=self, account=account, token=self.lqd_token, token_swapped=self.eth_token) cancel_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account)
def test_cancel_after_partial_match_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) buy_lqd_nonce = random.randint(1, 999999) sell_lqd_nonce = random.randint(1, 999999) total_remaining_eons = 5 # make persistent swap send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=2, amount_swapped=4, nonce=buy_lqd_nonce, eon_count=total_remaining_eons) swap = Transfer.objects.filter( swap=True, wallet__token=self.eth_token).order_by('id')[0] swap_tx_id = swap.tx_id wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) recipient_transfer_context = WalletTransferContext( wallet=swap.recipient, transfer=None) wallet_funds_before = 2 recipient_funds_before = 0 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[2], eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=2) freeze_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1]) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=2) cancel_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1], eon_count=total_remaining_eons) self.assertEqual( Transfer.objects.filter(tx_id=swap_tx_id, eon_number__gt=2, swap=True, voided=False).count(), 0) # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) self.assertEqual( wallet_transfer_context.available_funds_at_eon(2, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(2, False), recipient_funds_before + 2) advance_to_next_eon(test_case=self, eon_number=2) commit_eon(test_case=self, eon_number=3) # process swaps confirm_swaps_for_eon(operator_eon_number=3) cancel_finalize_swaps_for_eon(operator_eon_number=3) process_swaps_for_eon(operator_eon_number=3) self.assertEqual( wallet_transfer_context.available_funds_at_eon(3, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(3, False), recipient_funds_before + 2)
def test_cancel_after_partial_match_multi_eon_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) buy_lqd_nonce = random.randint(1, 999999) sell_lqd_nonce = random.randint(1, 999999) total_remaining_eons = 5 # make persistent swap send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=2, amount_swapped=4, nonce=buy_lqd_nonce, eon_count=total_remaining_eons) swap = Transfer.objects.filter( swap=True, wallet__token=self.eth_token).order_by('id')[0] swap_tx_id = swap.tx_id wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) recipient_transfer_context = WalletTransferContext( wallet=swap.recipient, transfer=None) wallet_funds_before = 2 recipient_funds_before = 0 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # skip some eons for i in range(3, 5): # proceed to next eon advance_to_next_eon(test_case=self, eon_number=i - 1) commit_eon(test_case=self, eon_number=i) total_remaining_eons -= 1 try: swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=i) self.assertTrue(True) except Transfer.DoesNotExist: self.assertTrue(False) # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), recipient_funds_before) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=4, account=self.testrpc_accounts[2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=4) cancel_finalize_swaps_for_eon(operator_eon_number=4) process_swaps_for_eon(operator_eon_number=4) finalize_last_swap(test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[2], eon_count=1) self.assertEqual( wallet_transfer_context.available_funds_at_eon(4, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(4, False), recipient_funds_before + 2) # proceed to next eon advance_to_next_eon(test_case=self, eon_number=4) commit_eon(test_case=self, eon_number=5) total_remaining_eons -= 1 swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=5) self.assertFalse(swap.processed) self.assertTrue(swap.appended) self.assertFalse(swap.voided) self.assertFalse(swap.cancelled) self.assertFalse(swap.complete) self.assertEqual( wallet_transfer_context.available_funds_at_eon(5, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(5, False), recipient_funds_before + 2) # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=5) freeze_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1]) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=5) cancel_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1], eon_count=total_remaining_eons) self.assertEqual( Transfer.objects.filter(tx_id=swap_tx_id, eon_number__gt=5, swap=True, voided=False).count(), 0) # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=5) self.assertTrue(swap.processed) self.assertTrue(swap.appended) self.assertFalse(swap.voided) self.assertTrue(swap.cancelled) self.assertFalse(swap.complete) self.assertEqual( wallet_transfer_context.available_funds_at_eon(5, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(5, False), recipient_funds_before + 2) # proceed to next eon advance_to_next_eon(test_case=self, eon_number=5) commit_eon(test_case=self, eon_number=6) total_remaining_eons -= 1 # process swaps confirm_swaps_for_eon(operator_eon_number=6) cancel_finalize_swaps_for_eon(operator_eon_number=6) process_swaps_for_eon(operator_eon_number=6) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=6) self.assertTrue(swap.processed) self.assertFalse(swap.appended) self.assertTrue(swap.voided) self.assertTrue(swap.cancelled) self.assertFalse(swap.complete) self.assertEqual( wallet_transfer_context.available_funds_at_eon(6, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(6, False), recipient_funds_before + 2) # make opposite swap send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=6, account=self.testrpc_accounts[3], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=6) cancel_finalize_swaps_for_eon(operator_eon_number=6) process_swaps_for_eon(operator_eon_number=6) self.assertEqual( wallet_transfer_context.available_funds_at_eon(6, False), wallet_funds_before - 1) self.assertEqual( recipient_transfer_context.available_funds_at_eon(6, False), recipient_funds_before + 2) commit_eon(test_case=self, eon_number=6)
def test_cancel_multi_eon_swap(self): commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=1) commit_eon(test_case=self, eon_number=2) buy_lqd_nonce = random.randint(1, 999999) sell_lqd_nonce = random.randint(1, 999999) total_remaining_eons = 5 # make persistent swap buy_lqd = send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=2, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=buy_lqd_nonce, eon_count=total_remaining_eons) swap = Transfer.objects.filter( swap=True, wallet__token=self.eth_token).order_by('id')[0] swap_tx_id = swap.tx_id wallet_transfer_context = WalletTransferContext(wallet=swap.wallet, transfer=None) recipient_transfer_context = WalletTransferContext( wallet=swap.recipient, transfer=None) wallet_funds_before = 1 recipient_funds_before = 0 # process swaps confirm_swaps_for_eon(operator_eon_number=2) cancel_finalize_swaps_for_eon(operator_eon_number=2) process_swaps_for_eon(operator_eon_number=2) # skip some eons for i in range(3, 5): # proceed to next eon advance_to_next_eon(test_case=self, eon_number=i - 1) commit_eon(test_case=self, eon_number=i) total_remaining_eons -= 1 # process swaps confirm_swaps_for_eon(operator_eon_number=i) cancel_finalize_swaps_for_eon(operator_eon_number=i) process_swaps_for_eon(operator_eon_number=i) self.assertEqual( wallet_transfer_context.available_funds_at_eon(i, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(i, False), recipient_funds_before) self.assertEqual( wallet_transfer_context.balance_as_of_eon(i).amount(), wallet_funds_before) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=4) freeze_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1]) swap = Transfer.objects.get(swap=True, tx_id=swap_tx_id, eon_number=4) cancel_swap(test_case=self, swap=swap, account=self.testrpc_accounts[1], eon_count=total_remaining_eons) self.assertEqual( Transfer.objects.filter(tx_id=swap_tx_id, eon_number__gt=4, swap=True, voided=False).count(), 0) swap = Transfer.objects.filter(swap=True)[0] self.assertTrue(swap.cancelled) self.assertTrue(swap.processed) self.assertTrue(swap.appended) self.assertTrue(swap.sender_cancellation_active_state. operator_signature is not None) # proceed to next eon advance_to_next_eon(test_case=self, eon_number=4) commit_eon(test_case=self, eon_number=5) total_remaining_eons -= 1 # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) swap = Transfer.objects.filter(swap=True)[0] self.assertTrue(swap.cancelled) self.assertTrue(swap.processed) self.assertTrue(swap.appended) self.assertEqual(swap.eon_number, 4) self.assertEqual( wallet_transfer_context.balance_as_of_eon(5).amount(), wallet_funds_before) self.assertEqual( wallet_transfer_context.available_funds_at_eon(5, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(5, False), recipient_funds_before) # make opposite swap sell_lqd = send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=5, account=self.testrpc_accounts[2], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=sell_lqd_nonce, eon_count=1) # process swaps confirm_swaps_for_eon(operator_eon_number=5) cancel_finalize_swaps_for_eon(operator_eon_number=5) process_swaps_for_eon(operator_eon_number=5) self.assertEqual( wallet_transfer_context.available_funds_at_eon(5, False), wallet_funds_before) self.assertEqual( recipient_transfer_context.available_funds_at_eon(5, False), recipient_funds_before) commit_eon(test_case=self, eon_number=5)
def test_orderbook_sorting(self): for idx, account in enumerate(self.testrpc_accounts[:4], 1): send_swap( # Buy LQD at .5, .25, .16, .125, ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2*idx, nonce=random.randint(1, 999999)) # Match none confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) send_swap( # Sell LQD at 0.5 ETH -> match with BUY at 0.5 test_case=self, eon_number=1, account=self.testrpc_accounts[5], token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=random.randint(1, 999999)) # Match best confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[0]) finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[5]) for account in self.testrpc_accounts[1:4]: # Cancel pending .25, .16... BUY orders freeze_last_swap( test_case=self, account=account, token=self.eth_token, token_swapped=self.lqd_token) cancel_last_swap( test_case=self, account=account, token=self.eth_token, token_swapped=self.lqd_token) # Match none confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) # Test OPPOSITE direction for idx, account in enumerate(self.testrpc_accounts[:5], 1): send_swap( # SELL LQD at .5, .25, .16, .125, 0.1 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=200000*idx, amount_swapped=100000, nonce=random.randint(1, 999999)) # Match none confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) send_swap( # BUY LQD at 0.5 ETH -> match with SELL at 0.1 test_case=self, eon_number=1, account=self.testrpc_accounts[5], token=self.eth_token, token_swapped=self.lqd_token, amount=100000, amount_swapped=200000, nonce=random.randint(1, 999999)) # Match best confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[4]) finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[5]) for account in self.testrpc_accounts[:4]: # Cancel pending .25, .16... BUY orders freeze_last_swap( test_case=self, account=account, token=self.lqd_token, token_swapped=self.eth_token) cancel_last_swap( test_case=self, account=account, token=self.lqd_token, token_swapped=self.eth_token) # Match none confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1)
def test_make_random_valid_swaps(self): for i in range(0, 2): print('--------------') for account in self.testrpc_accounts[:4]: send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) for account in self.testrpc_accounts[4:]: send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=random.randint(1, 999999)) # Match All confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in self.testrpc_accounts[:4]: finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) for account in self.testrpc_accounts[4:]: finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) print('--------------') for account in self.testrpc_accounts[:5]: send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=100, amount_swapped=200, nonce=random.randint(1, 999999)) for account in self.testrpc_accounts[6:]: send_swap( # Sell LQD at 1.0 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=100, amount_swapped=100, nonce=random.randint(1, 999999)) # No Match confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) print('--------------') send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=1, account=self.testrpc_accounts[5], token=self.lqd_token, token_swapped=self.eth_token, amount=1000, amount_swapped=500, nonce=random.randint(1, 999999)) # One Match For All confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=self.testrpc_accounts[5]) for account in self.testrpc_accounts[:5]: finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) print('--------------') send_swap( # Buy LQD at 1.0 ETH test_case=self, eon_number=1, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=200, amount_swapped=200, nonce=random.randint(1, 999999)) # One Match For All confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[1]) for account in self.testrpc_accounts[6:]: finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) print('--------------') send_swap( # Buy LQD at 3/7 ETH (No Match / Empty) test_case=self, eon_number=1, account=self.testrpc_accounts[1], token=self.eth_token, token_swapped=self.lqd_token, amount=3, amount_swapped=7, nonce=random.randint(1, 999999)) send_swap( # Sell LQD at 4/7 ETH (No Match) test_case=self, eon_number=1, account=self.testrpc_accounts[4], token=self.lqd_token, token_swapped=self.eth_token, amount=7, amount_swapped=4, nonce=random.randint(1, 999999)) # No Match confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) print('--------------') send_swap( # Sell LQD at 2/7 ETH (Match with 3/7 at price of 3/7 -> Finalize both and get +1) test_case=self, eon_number=1, account=self.testrpc_accounts[5], token=self.lqd_token, token_swapped=self.eth_token, amount=7, amount_swapped=2, nonce=random.randint(1, 999999)) send_swap( # Sell LQD at 1/7 ETH (No Match / Empty) test_case=self, eon_number=1, account=self.testrpc_accounts[6], token=self.lqd_token, token_swapped=self.eth_token, amount=7, amount_swapped=1, nonce=random.randint(1, 999999)) send_swap( # Buy LQD at 4/7 ETH (Match with 1/7 at its price -> Finalize 1/7 and remain with 3 then match with 4/7 and finalize self) test_case=self, eon_number=1, account=self.testrpc_accounts[7], token=self.eth_token, token_swapped=self.lqd_token, amount=4, amount_swapped=7, nonce=random.randint(1, 999999)) # Three Matches confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) # Cancel pending 4/7 sell order freeze_last_swap( test_case=self, account=self.testrpc_accounts[4], token=self.lqd_token, token_swapped=self.eth_token) cancel_last_swap( test_case=self, account=self.testrpc_accounts[4], token=self.lqd_token, token_swapped=self.eth_token) for account in self.testrpc_accounts[5:7]: finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[1]) finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=self.testrpc_accounts[7]) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for i in range(0, 2): print('--------------') for account in self.testrpc_accounts[:4]: send_swap( # Buy LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.eth_token, token_swapped=self.lqd_token, amount=1, amount_swapped=2, nonce=random.randint(1, 999999)) for account in self.testrpc_accounts[4:]: send_swap( # Sell LQD at 0.5 ETH test_case=self, eon_number=1, account=account, token=self.lqd_token, token_swapped=self.eth_token, amount=2, amount_swapped=1, nonce=random.randint(1, 999999)) # Match all confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1) for account in self.testrpc_accounts[:4]: finalize_last_swap( test_case=self, token=self.eth_token, token_swapped=self.lqd_token, account=account) for account in self.testrpc_accounts[4:]: finalize_last_swap( test_case=self, token=self.lqd_token, token_swapped=self.eth_token, account=account) confirm_swaps_for_eon(operator_eon_number=1) cancel_finalize_swaps_for_eon(operator_eon_number=1) process_swaps_for_eon(operator_eon_number=1)