def test_starter_token(dbsession, registry, eth_network_id, web3: Web3, eth_service: EthereumService, house_address, toybox): """See that a fresh user account is supplied with some play assets.""" with transaction.manager: user = create_user(dbsession, registry) setup_user_account(user) # Let all the events completed success, fail = eth_service.run_event_cycle() assert success == 1 assert fail == 0 # We need another event cycle to process the initial asset transfers with transaction.manager: user = dbsession.query(User).first() opid = user.user_data["starter_asset_txs"][0]["toybox"] assert opid wait_for_op_confirmations(eth_service, opid) # Let the transfer come through eth_service.run_event_cycle() # Make sure we confirm the deposit with transaction.manager: user = dbsession.query(User).first() user_depo = user.owned_crypto_operations.join(CryptoOperation).filter_by(operation_type=CryptoOperationType.deposit).first() opid = user_depo.crypto_operation.id wait_for_op_confirmations(eth_service, opid) with transaction.manager: # Sanity check our token contract posts us logs user = dbsession.query(User).first() client = get_rpc_client(web3) asset = dbsession.query(Asset).get(toybox) address = bin_to_eth_address(asset.external_id) network = dbsession.query(AssetNetwork).get(eth_network_id) user_address = UserCryptoAddress.get_default(user, network).address house_address = dbsession.query(CryptoAddress).get(house_address) house = bin_to_eth_address(house_address.address) token = Token.get(web3, address) # Check we correctly resolved low level logs token_logs = client.get_logs(from_block=0, address=[address]) wallet_logs = client.get_logs(from_block=0, address=[house]) assert len(token_logs) > 0 assert len(wallet_logs) > 0 # Check contract state matches assert token.contract.call().balanceOf(house) == 9990 assert token.contract.call().balanceOf(bin_to_eth_address(user_address.address)) == 10 # Check our internal book keeping matches assert house_address.get_account(asset).account.get_balance() == 9990 assert user_address.get_account(asset).account.get_balance() == 10
def test_starter_eth(dbsession, registry, eth_network_id, web3: Web3, eth_service: EthereumService, house_address, starter_eth): """Test the user gets some starter ETH when signing up.""" with transaction.manager: user = create_user(dbsession, registry) setup_user_account(user, do_mainnet=True) # Let all the events completed success, fail = eth_service.run_event_cycle() assert success >= 1 assert fail == 0 # When op is confirmed, the user account is correctly credited with transaction.manager: user = dbsession.query(User).first() txid = user.user_data["starter_asset_txs"][0]["eth"] confirm_transaction(web3, txid) # Let the transfer come through eth_service.run_event_cycle() with transaction.manager: user = dbsession.query(User).first() client = get_rpc_client(web3) asset = get_ether_asset(dbsession) ua = user.owned_crypto_addresses.first() address = bin_to_eth_address(ua.address.address) # Sanity check we get events from starter deposit logs = client.get_logs(from_block=0, address=[address]) ops = list(user.owned_crypto_operations) # The event was processed on log level assert len(logs) == 1 # The last user operation is deposit depo = ops[-1] assert isinstance(depo.crypto_operation, CryptoAddressDeposit) opid = depo.crypto_operation.id # Wait deposit to confirm wait_for_op_confirmations(eth_service, opid) with transaction.manager: # User ETH balance is expected asset = get_ether_asset(dbsession) user = dbsession.query(User).first() ua = user.owned_crypto_addresses.first() caccout = ua.address.get_account(asset) assert caccout.account.get_balance() == Decimal("0.1")
def test_starter_eth(dbsession, registry, eth_network_id, web3: Web3, eth_service: EthereumService, house_address, starter_eth): """Test the user gets some starter ETH when signing up.""" with transaction.manager: user = create_user(dbsession, registry) setup_user_account(user) # Let all the events completed success, fail = eth_service.run_event_cycle() assert success >= 1 assert fail == 0 # When op is confirmed, the user account is correctly credited with transaction.manager: user = dbsession.query(User).first() txid = user.user_data["starter_asset_txs"][0]["eth"] confirm_transaction(web3, txid) # Let the transfer come through eth_service.run_event_cycle() with transaction.manager: user = dbsession.query(User).first() client = get_rpc_client(web3) asset = get_ether_asset(dbsession) ua = user.owned_crypto_addresses.first() address = bin_to_eth_address(ua.address.address) # Sanity check we get events from starter deposit logs = client.get_logs(from_block=0, address=[address]) ops = list(user.owned_crypto_operations) # The event was processed on log level assert len(logs) == 1 # The last user operation is deposit depo = ops[-1] assert isinstance(depo.crypto_operation, CryptoAddressDeposit) opid = depo.crypto_operation.id # Wait deposit to confirm wait_for_op_confirmations(eth_service, opid) with transaction.manager: # User ETH balance is expected asset = get_ether_asset(dbsession) user = dbsession.query(User).first() ua = user.owned_crypto_addresses.first() caccout = ua.address.get_account(asset) assert caccout.account.get_balance() == Decimal("0.1")
def mock_eth_service(eth_network_id, dbsession, registry): """Non-functional Ethereum Service without a connection to geth.""" from web3 import RPCProvider, Web3 web3 = Web3(RPCProvider("127.0.0.1", 666)) s = EthereumService(web3, eth_network_id, dbsession, registry) return s
def token_asset(dbsession, eth_network_id, deposit_address, eth_service: EthereumService) -> UUID: """Database asset referring to the token contract. deposit_address will hold 10000 tokens :return: Asset id """ with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) asset = network.create_asset(name="MyToken", symbol="MY", supply=Decimal(10000), asset_class=AssetClass.token) address = CryptoAddress.get_network_address( network, eth_address_to_bin(deposit_address)) op = address.create_token(asset) opid = op.id aid = asset.id # This gives op a txid success, fails = eth_service.run_waiting_operations() assert success == 1 wait_for_op_confirmations(eth_service, opid) with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) asset = dbsession.query(Asset).get(aid) address = CryptoAddress.get_network_address( network, eth_address_to_bin(deposit_address)) account = address.get_account(asset) assert account.account.get_balance() > 0 return aid
def token_asset(dbsession, eth_network_id, deposit_address, eth_service: EthereumService) -> UUID: """Database asset referring to the token contract. deposit_address will hold 10000 tokens :return: Asset id """ with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) asset = network.create_asset(name="MyToken", symbol="MY", supply=Decimal(10000), asset_class=AssetClass.token) address = CryptoAddress.get_network_address(network, eth_address_to_bin(deposit_address)) op = address.create_token(asset) opid = op.id aid = asset.id # This gives op a txid success, fails = eth_service.run_waiting_operations() assert success == 1 wait_for_op_confirmations(eth_service, opid) with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) asset = dbsession.query(Asset).get(aid) address = CryptoAddress.get_network_address(network, eth_address_to_bin(deposit_address)) account = address.get_account(asset) assert account.account.get_balance() > 0 return aid
def test_deposit_token(dbsession, eth_network_id, web3: Web3, eth_service: EthereumService, coinbase: str, deposit_address: str, token: Token): """"See that we can deposit tokens to accounts.""" # Import a contract where coinbase has all balance with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) import_token(network, eth_address_to_bin(token.address)) # Run import token operation success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 # Coinbase transfers token balance to deposit address txid = token.transfer(deposit_address, Decimal(4000)) confirm_transaction(web3, txid) # We should pick up incoming deposit success_count, failure_count = eth_service.run_listener_operations() assert success_count == 1 assert failure_count == 0 # Check that data is setup correctly on incoming transaction with transaction.manager: op = dbsession.query(CryptoOperation).all()[-1] opid = op.id assert not op.completed_at address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = op.holding_account.asset assert op.holding_account.get_balance() == 4000 assert op.completed_at is None assert address.get_account(asset).account.get_balance() == 0 # Not credited until confirmations reached assert address.crypto_address_accounts.count() == 1 # Wait the token transaction to get enough confirmations wait_for_op_confirmations(eth_service, opid) # Check that the transaction is not final with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at assert op.completed_at address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = op.holding_account.asset assert op.holding_account.get_balance() == 0 assert address.get_account(asset).account.get_balance() == 4000 assert op.state == CryptoOperationState.success
def test_buy_crowdfund_not_enough_gas(dbsession: Session, eth_network_id: UUID, web3: Web3, eth_service: EthereumService, token, toycrowd, withdraw_address): """Perform a crowdfundn buy operation without giving enough gas for the transaction.""" with transaction.manager: # Create withdraw operation caccount = dbsession.query(CryptoAddressAccount).one() # Use 4 as the heurestics for block account that doesn't happen right away, but still sensible to wait for it soonish asset = toycrowd() caccount.withdraw(Decimal(0.005), asset.external_id, "Buying Toycrowd", required_confirmation_count=1) print("Withdrawing") success_op_count, failed_op_count = eth_service.run_waiting_operations() assert success_op_count == 1 assert failed_op_count == 0 with transaction.manager: ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 3 # Create + deposit + withdraw op = ops[-1] txid = bin_to_txid(op.txid) # This should make the tx to included in a block confirm_transaction(web3, txid) # This should trigger incoming notification eth_service.run_listener_operations() # Now we should get block number for the withdraw updates, failures = eth_service.run_confirmation_updates() assert failures == 1 with transaction.manager: op = dbsession.query(CryptoOperation).all()[-1] assert op.is_failed() assert "gas" in op.get_failure_reason()
def test_import_no_address_token(dbsession: Session, eth_network_id, web3: Web3, eth_service: EthereumService): """Import should fail for address that doesn't exist.""" with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) # A random address op = import_token(network, eth_address_to_bin("0x2f70d3d26829e412a602e83fe8eebf80255aeea5")) opid = op.id # Success count here means the operation passed, but might be marked as failure success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.failed_at assert op.other_data["error"].startswith("Address did not provide EIP-20 token API:")
def withdraw_address(web3: Web3, dbsession, eth_service: EthereumService, coinbase, deposit_address) -> str: """Create a managed hosted wallet that has withdraw balance for testing.""" txid = send_balance_to_address(web3, deposit_address, TEST_VALUE) confirm_transaction(web3, txid) assert web3.eth.getBalance(deposit_address) > 0 success_op_count, failed_op_count = eth_service.run_listener_operations() assert success_op_count == 1 assert failed_op_count == 0 with transaction.manager: # We bypass normal transction confirmation count mechanism to credit the account right away to speed up the test deposit = dbsession.query(CryptoAddressDeposit).one() deposit.resolve() return deposit_address
def withdraw_address(web3: Web3, dbsession, eth_service: EthereumService, coinbase, deposit_address) -> str: """Create a managed hosted wallet that has withdraw balance for testing.""" # Do a transaction over ETH network txid = send_balance_to_address(web3, deposit_address, TEST_VALUE) confirm_transaction(web3, txid) assert web3.eth.getBalance(deposit_address) > 0 success_op_count, failed_op_count = eth_service.run_listener_operations() assert success_op_count == 1 assert failed_op_count == 0 with transaction.manager: # We bypass normal transction confirmation count mechanism to credit the account right away to speed up the test deposit = dbsession.query(CryptoAddressDeposit).one() deposit.resolve() return deposit_address
def test_withdraw_token(dbsession, eth_network_id, web3: Web3, eth_service: EthereumService, deposit_address: str, token_asset: str, target_account: str): """"See that we can withdraw token outside to an account.""" with transaction.manager: address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = dbsession.query(Asset).get(token_asset) caccount = address.get_account(asset) op = caccount.withdraw(Decimal(2000), eth_address_to_bin(target_account), "Sending to friend") opid = op.id assert caccount.account.get_balance() == 8000 # Run import token operation success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 # TX was broadcasted, marked as complete with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert isinstance(op, CryptoAddressWithdraw) asset = dbsession.query(Asset).get(token_asset) assert op.broadcasted_at assert not op.completed_at wait_for_op_confirmations(eth_service, opid) # After tx has been mined the new balances should match with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.broadcasted_at assert op.completed_at asset = dbsession.query(Asset).get(token_asset) # Tokens have been removed on from account token = Token.get(web3, bin_to_eth_address(asset.external_id)) assert token.contract.call().balanceOf(deposit_address) == 8000 # Tokens have been credited on to account token = Token.get(web3, bin_to_eth_address(asset.external_id)) assert token.contract.call().balanceOf(target_account) == 2000
def wait_for_op_confirmations(eth_service: EthereumService, opid: UUID, timeout=60): """Wait that an op reaches required level of confirmations.""" with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) if op.completed_at: assert op.required_confirmation_count return # Wait until the transaction confirms (1 confirmations) deadline = time.time() + timeout while time.time() < deadline: success_op_count, failed_op_count = eth_service.run_event_cycle() if success_op_count > 0: # Check our op went through with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) if op.completed_at: return if failed_op_count > 0: with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) raise OpConfirmationFailed( "Failures within confirmation wait should not happen, op is {}" .format(op)) time.sleep(1) with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) logger.info("Waiting op to complete %s", op) if time.time() > deadline: with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) raise OpConfirmationFailed( "Did not receive confirmation updates: {}".format(op))
def test_withdraw_token(dbsession, eth_network_id, web3: Web3, eth_service: EthereumService, deposit_address: str, token_asset: str, target_account: str): """"See that we can withdraw token outside to an account.""" with transaction.manager: address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = dbsession.query(Asset).get(token_asset) caccount = address.get_account(asset) op = caccount.withdraw(Decimal(2000), eth_address_to_bin(target_account), "Sending to friend") opid = op.id assert caccount.account.get_balance() == 8000 # Run import token operation success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 # TX was broadcasted, marked as complete with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert isinstance(op, CryptoAddressWithdraw) asset = dbsession.query(Asset).get(token_asset) assert op.broadcasted_at assert not op.completed_at wait_for_op_confirmations(eth_service, opid) with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.broadcasted_at assert op.completed_at asset = dbsession.query(Asset).get(token_asset) # Tokens have been removed on from account token = Token.get(web3, bin_to_eth_address(asset.external_id)) assert token.contract.call().balanceOf(deposit_address) == 8000 # Tokens have been credited on to account token = Token.get(web3, bin_to_eth_address(asset.external_id)) assert token.contract.call().balanceOf(target_account) == 2000
def test_import_token(dbsession, eth_network_id, web3: Web3, eth_service: EthereumService, coinbase: str, deposit_address: str, token: Token): """Import an existing smart contract token to system.""" # Make sure we have an address that holds some of the tokens so it is cleared up during import txid = token.transfer(deposit_address, Decimal(4000)) confirm_transaction(web3, txid) with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) op = import_token(network, eth_address_to_bin(token.address)) opid = op.id # Let's create another address that doesn't hold tokens # and see that import doesn't fail for it addr = CryptoAddress(network=network, address=eth_address_to_bin("0x2f70d3d26829e412a602e83fe8eebf80255aeea5")) dbsession.add(addr) success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 # Check that we created a new asset and its imports where fulfilled with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at # We got account with tokens on it network = dbsession.query(AssetNetwork).get(eth_network_id) caddress = CryptoAddress.get_network_address(network, eth_address_to_bin(deposit_address)) asset = network.assets.filter_by(external_id=eth_address_to_bin(token.address)).one() assert asset.name == "Mootoken" assert asset.symbol == "MOO" assert asset.supply == 10000 caccount = caddress.get_account_by_address(eth_address_to_bin(token.address)) assert caccount.account.get_balance() == 4000
def test_withdraw_eth_data(dbsession: Session, eth_network_id: UUID, web3: Web3, eth_service: EthereumService, withdraw_address: str, target_account: str, decode_data_contract): """Perform a withdraw operation with data and gas set. """ # First check what's our balance before sending coins back current_balance = wei_to_eth(web3.eth.getBalance(target_account)) assert current_balance == 0 with transaction.manager: # Create withdraw operation caccount = dbsession.query(CryptoAddressAccount).one() #: We are going to withdraw the full amount on the account assert caccount.account.get_balance() == TEST_VALUE # Use 4 as the heurestics for block account that doesn't happen right away, but still sensible to wait for it soonish op = caccount.withdraw(TEST_VALUE, eth_address_to_bin(decode_data_contract.address), "Getting all the moneys", required_confirmation_count=4) op.other_data["gas"] = 1000000 op.other_data["data"] = "0x001234" success_op_count, failed_op_count = eth_service.run_waiting_operations() assert success_op_count == 1 assert failed_op_count == 0 with transaction.manager: # create + deposit + withdraw op = dbsession.query(CryptoOperation).all()[2] txid = bin_to_txid(op.txid) # This should make the tx to included in a block confirm_transaction(web3, txid) value = decode_data_contract.call().value() data = decode_data_contract.call().data() assert value == 10000000000000000 assert data == '0x001234'
def wait_for_op_confirmations(eth_service: EthereumService, opid: UUID, timeout=60): """Wait that an op reaches required level of confirmations.""" with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) if op.completed_at: assert op.required_confirmation_count return # Wait until the transaction confirms (1 confirmations) deadline = time.time() + timeout while time.time() < deadline: success_op_count, failed_op_count = eth_service.run_event_cycle() if success_op_count > 0: # Check our op went through with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) if op.completed_at: return if failed_op_count > 0: with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) raise OpConfirmationFailed("Failures within confirmation wait should not happen, op is {}".format(op)) time.sleep(1) with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) logger.info("Waiting op to complete %s", op) if time.time() > deadline: with transaction.manager: op = eth_service.dbsession.query(CryptoOperation).get(opid) raise OpConfirmationFailed("Did not receive confirmation updates: {}".format(op))
def test_withdraw_eth(dbsession: Session, eth_network_id: UUID, web3: Web3, eth_service: EthereumService, withdraw_address: str, target_account: str): """Perform a withdraw operation. Create a database address with balance. """ # First check what's our balance before sending coins back current_balance = wei_to_eth(web3.eth.getBalance(target_account)) assert current_balance == 0 with transaction.manager: # Create withdraw operation caccount = dbsession.query(CryptoAddressAccount).one() #: We are going to withdraw the full amount on the account assert caccount.account.get_balance() == TEST_VALUE # Use 4 as the heurestics for block account that doesn't happen right away, but still sensible to wait for it soonish op = caccount.withdraw(TEST_VALUE, eth_address_to_bin(target_account), "Getting all the moneys", required_confirmation_count=4) success_op_count, failed_op_count = eth_service.run_waiting_operations() assert success_op_count == 1 assert failed_op_count == 0 with transaction.manager: # We should have now three ops # One for creating the address # One for depositing value for the test # One for withdraw # We have one complete operation ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 3 # Create + deposit + withdraw op = ops[-1] assert isinstance(op, CryptoAddressWithdraw) assert op.broadcasted_at is not None # This completes instantly, cannot be cancelled assert op.completed_at is None # We need at least one confirmation assert op.block is None assert op.txid is not None txid = bin_to_txid(op.txid) # This should make the tx to included in a block confirm_transaction(web3, txid) # Now we should get block number for the withdraw eth_service.run_confirmation_updates() # Geth reflects the deposit instantly internally, doesn't wait for blocks fee = get_withdrawal_fee(web3) new_balance = wei_to_eth(web3.eth.getBalance(target_account)) assert new_balance == current_balance + TEST_VALUE - fee current_block = web3.eth.blockNumber with transaction.manager: # Check we get block and txid # We have one complete operation ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 3 # Create + deposit + withdraw op = ops[-1] assert op.broadcasted_at is not None # This completes instantly, cannot be cancelled assert op.completed_at is None, "Got confirmation for block {}, current {}, requires {}".format(op.block, current_block, op.required_confirmation_count) assert op.block is not None assert op.txid is not None block_num = op.block required_conf = op.required_confirmation_count # Wait block to make the confirmation happen wait_for_block_number(web3, block_num + required_conf + 1, timeout=60) # Now we should have enough blocks to mark the transaction as confirmed eth_service.run_confirmation_updates() with transaction.manager: # Check we get block and txid # We have one complete operation ops = list(dbsession.query(CryptoOperation).all()) op = ops[-1] assert op.state == CryptoOperationState.success assert op.completed_at is not None
def test_starter_token(dbsession, registry, eth_network_id, web3: Web3, eth_service: EthereumService, house_address, toybox): """See that a fresh user account is supplied with some play assets.""" with transaction.manager: user = create_user(dbsession, registry) setup_user_account(user, do_mainnet=True) # Let all the events completed success, fail = eth_service.run_event_cycle() assert success == 1 assert fail == 0 # We need another event cycle to process the initial asset transfers with transaction.manager: user = dbsession.query(User).first() opid = user.user_data["starter_asset_txs"][0]["toybox"] assert opid wait_for_op_confirmations(eth_service, opid) # Let the transfer come through eth_service.run_event_cycle() # Make sure we confirm the deposit with transaction.manager: user = dbsession.query(User).first() user_depo = user.owned_crypto_operations.join( CryptoOperation).filter_by( operation_type=CryptoOperationType.deposit).first() opid = user_depo.crypto_operation.id wait_for_op_confirmations(eth_service, opid) with transaction.manager: # Sanity check our token contract posts us logs user = dbsession.query(User).first() client = get_rpc_client(web3) asset = dbsession.query(Asset).get(toybox) address = bin_to_eth_address(asset.external_id) network = dbsession.query(AssetNetwork).get(eth_network_id) user_address = UserCryptoAddress.get_default(user, network).address house_address = dbsession.query(CryptoAddress).get(house_address) house = bin_to_eth_address(house_address.address) token = Token.get(web3, address) # Check we correctly resolved low level logs token_logs = client.get_logs(from_block=0, address=[address]) wallet_logs = client.get_logs(from_block=0, address=[house]) assert len(token_logs) > 0 assert len(wallet_logs) > 0 # Check contract state matches assert token.contract.call().balanceOf(house) == 9990 assert token.contract.call().balanceOf( bin_to_eth_address(user_address.address)) == 10 # Check our internal book keeping matches assert house_address.get_account(asset).account.get_balance() == 9990 assert user_address.get_account(asset).account.get_balance() == 10
def test_transfer_tokens_between_accounts(dbsession, eth_network_id, web3: Web3, eth_service: EthereumService, deposit_address: str, token_asset: str, coinbase: str): """Transfer tokens between two internal accounts. Do transfers in two batches to make sure subsequent events top up correctly. """ # Initiate a target address creatin with transaction.manager: network = dbsession.query(AssetNetwork).get(eth_network_id) opid = CryptoAddress.create_address(network).id # Run address creation success_count, failure_count = eth_service.run_waiting_operations() assert success_count == 1 assert failure_count == 0 # We resolved address creation operation. # Get the fresh address for the future withdraw targets. with transaction.manager: op = dbsession.query(CryptoAddressCreation).get(opid) addr = op.address.address assert addr # Move tokens between accounts with transaction.manager: address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = dbsession.query(Asset).get(token_asset) caccount = address.get_account(asset) op = caccount.withdraw(Decimal(2000), addr, "Sending to friend") opid = op.id # Resolve the 1st transaction eth_service.run_event_cycle() wait_for_op_confirmations(eth_service, opid) # See that our internal balances match with transaction.manager: # Withdraw operation is complete op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at assert op.completed_at, "Op not confirmed {}".format(op) # We should have received a Transfer operation targetting target account op = dbsession.query(CryptoOperation).join(CryptoAddressAccount).join(CryptoAddress).filter_by(address=addr).one() opid = op.id # Confirm incoming Transfer wait_for_op_confirmations(eth_service, opid, timeout=180) # Check Transfer looks valid with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at assert op.completed_at, "Op not confirmed {}".format(op) asset = dbsession.query(Asset).get(token_asset) source = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() target = dbsession.query(CryptoAddress).filter_by(address=addr).one() assert source.get_account(asset).account.get_balance() == 8000 assert target.get_account(asset).account.get_balance() == 2000 # Add some ETH on deposit_address txid = send_balance_to_address(web3, deposit_address, TEST_VALUE) confirm_transaction(web3, txid) eth_service.run_event_cycle() # Wait for confirmations to have ETH deposit credired with transaction.manager: op = dbsession.query(CryptoOperation).filter_by(txid=txid_to_bin(txid)).one() opid = op.id confirmed = op.completed_at if not confirmed: wait_for_op_confirmations(eth_service, opid) # Send some more tokens + ether, so we see account can't get mixed up with transaction.manager: address = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() asset = dbsession.query(Asset).get(token_asset) caccount = address.get_account(asset) op = caccount.withdraw(Decimal(4000), addr, "Sending tokens to friend äää") eth_asset = get_ether_asset(dbsession) caccount = address.get_account(eth_asset) op = caccount.withdraw(TEST_VALUE, addr, "Sending ETH to friend äää") opid = op.id # Resolve second transaction eth_service.run_event_cycle() wait_for_op_confirmations(eth_service, opid) # See that our internal balances match with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at assert op.completed_at asset = dbsession.query(Asset).get(token_asset) source = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() target = dbsession.query(CryptoAddress).filter_by(address=addr).one() eth_asset = get_ether_asset(dbsession) assert source.get_account(asset).account.get_balance() == 4000 assert target.get_account(asset).account.get_balance() == 6000 assert target.get_account(eth_asset).account.get_balance() == TEST_VALUE # Transfer 3: # Send everything back plus some ether with transaction.manager: address = dbsession.query(CryptoAddress).filter_by(address=addr).one() asset = dbsession.query(Asset).get(token_asset) caccount = address.get_account(asset) op = caccount.withdraw(Decimal(6000), eth_address_to_bin(deposit_address), "Sending everything back to friend äää") opid = op.id # Resolve third transaction wait_for_op_confirmations(eth_service, opid) eth_service.run_event_cycle() # See that our internal balances match with transaction.manager: op = dbsession.query(CryptoOperation).get(opid) assert op.completed_at assert op.completed_at asset = dbsession.query(Asset).get(token_asset) source = dbsession.query(CryptoAddress).filter_by(address=eth_address_to_bin(deposit_address)).one() target = dbsession.query(CryptoAddress).filter_by(address=addr).one() assert source.get_account(asset).account.get_balance() == 10000 assert target.get_account(asset).account.get_balance() == 0 eth_asset = get_ether_asset(dbsession) assert source.get_account(eth_asset).account.get_balance() == 0 assert target.get_account(eth_asset).account.get_balance() == TEST_VALUE
def test_buy_crowdfund_with_gas(dbsession: Session, eth_network_id: UUID, web3: Web3, eth_service: EthereumService, token, toycrowd, rich_withdraw_address): """Perform a crowdfunnd buy operation without giving enough gas for the transaction.""" with transaction.manager: # Create withdraw operation caccount = dbsession.query(CryptoAddressAccount).one() # Use 4 as the heurestics for block account that doesn't happen right away, but still sensible to wait for it soonish asset = toycrowd() op = caccount.withdraw(Decimal(3), asset.external_id, "Buying Toycrowd", required_confirmation_count=1) op.other_data["gas"] = 2500333 # Limit should be ~100k success_op_count, failed_op_count = eth_service.run_waiting_operations() assert failed_op_count == 0 assert success_op_count == 1 with transaction.manager: ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 3 # Create + deposit + withdraw op = ops[-1] txid = bin_to_txid(op.txid) # This should make the tx to included in a block confirm_transaction(web3, txid) # Set op.block eth_service.run_confirmation_updates() # Grab block number where out tx is with transaction.manager: ops = list(dbsession.query(CryptoOperation).all()) op = ops[-1] block_num = op.block wallet = get_wallet(web3, rich_withdraw_address) token_events = token.get_all_events() wallet_events = wallet.get_all_events() # Confirm we got it all right receipt = web3.eth.getTransactionReceipt(txid) logs = receipt["logs"] assert logs[0]["topics"][0] == token_events["Buy"] assert logs[1]["topics"][0] == token_events["Transfer"] assert logs[2]["topics"][0] == wallet_events["Withdraw"] data = get_crowdsale_data(token) assert data["wei_raised"] == to_wei("3", "ether") # Give tx time to confirm, so all confirmations will be there for db update run required_conf = 3 wait_for_block_number(web3, block_num + required_conf + 1, timeout=60) # This should trigger incoming notification eth_service.run_listener_operations() updates, failures = eth_service.run_confirmation_updates() assert failures == 0 assert updates == 2 # 1 eth withdraw, 1 token deposit # Check our db is updated with transaction.manager: # There is now new operation to deposit tokens ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 4 op = ops[-1] # type: CryptoOperation assert op.operation_type == CryptoOperationType.deposit assert op.state == CryptoOperationState.success assert op.amount == 6 asset = toycrowd() crypto_address = dbsession.query(CryptoAddress).one() # type: CryptoAddress caccount = crypto_address.get_account(asset) assert caccount.account.get_balance() == 6
def eth_service(web3, eth_network_id, dbsession, registry): """Create Ethereum Service to run ops or mock ups.""" s = EthereumService(web3, eth_network_id, dbsession, registry) return s
def testnet_service(web3, testnet_network_id, dbsession, registry): """Create Ethereum Service for testnet to run ops or mock ups.""" s = EthereumService(web3, testnet_network_id, dbsession, registry) return s
def eth_service(web3, eth_network_id, dbsession, registry): s = EthereumService(web3, eth_network_id, dbsession, registry) return s