def tearDown(self): # make sure checkpont is successful advance_to_next_eon( test_case=self, eon_number=self.eon_number) commit_eon( test_case=self, eon_number=self.eon_number+1) super(BuySellSemanticsSwapTests, self).tearDown()
def test_non_blocking_checkpoint(self): eth_token = Token.objects.first() registered_accounts = register_testrpc_accounts(self, token=eth_token) register_random_accounts(self, 100, eth_token) create_deposits(self, registered_accounts, eth_token) commit_eon(test_case=self, eon_number=1) advance_to_next_eon(test_case=self, eon_number=2) # Make sure we don't miss any on-chain events while self.contract_interface.get_current_subblock( ) <= self.contract_interface.get_blocks_for_confirmation(): self.contract_interface.do_nothing() # Retrieve confirmed events fully_synchronize_contract_state() while self.contract_interface.get_current_subblock( ) <= self.contract_interface.get_blocks_for_creation(): self.contract_interface.do_nothing() # Retrieve confirmed events fully_synchronize_contract_state() checkpoint_thread = multiprocessing.Process(target=create_checkpoint) db.connections.close_all() checkpoint_thread.start() make_random_valid_transactions(test_case=self, eon_number=2, accounts=registered_accounts, token=eth_token, make_deposits=False) checkpoint_thread.join()
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_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)