def test_get_user_account_assets(dbsession, registry, mock_eth_service, eth_network_id, eth_asset_id):
    """get_user_assets() gives sane results."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        setup_user_account(user)

    mock_create_addresses(mock_eth_service, dbsession)

    # Play around with user accounts
    with transaction.manager:

        # No accounts yet
        assert UserCryptoAddress.get_user_asset_accounts(user) == []

        # User gets a deposit, an account gets created
        user = dbsession.query(User).first()
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")

        current_accounts = UserCryptoAddress.get_user_asset_accounts(user)
        assert len(current_accounts) == 1
        current_address, current_account = current_accounts[0]
        assert current_account == account
        assert current_address == address
def test_setup_user_account(dbsession, registry, eth_service, testnet_service, eth_network_id):
    """See that we create primary and testnet address."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        setup_user_account(user)
        assert user.owned_crypto_addresses.count() == 2  # 2 addresses
        assert user.owned_crypto_operations.count() == 2  # 2 account creations

    def _create_address(web3, dbsession, opid):
        with transaction.manager:
            op = dbsession.query(CryptoOperation).get(opid)
            assert isinstance(op.address, CryptoAddress)
            op.address.address = eth_address_to_bin(TEST_ADDRESS)
            op.mark_performed()
            op.mark_complete()

    with mock.patch("websauna.wallet.ethereum.ops.create_address", new=_create_address):

        # Create address on main net
        success_op_count, failed_op_count = eth_service.run_waiting_operations()
        assert success_op_count == 1
        assert failed_op_count == 0

        # Create address on test net
        success_op_count, failed_op_count = testnet_service.run_waiting_operations()
        assert success_op_count == 1
        assert failed_op_count == 0

    # Make sure more addresses is not get created when we are called again
    with transaction.manager:
        user = dbsession.query(User).first()
        setup_user_account(user)
        assert user.owned_crypto_addresses.count() == 2  # 2 addresses
        assert user.owned_crypto_operations.count() == 2  # 2 account creations
Esempio n. 3
0
def test_get_user_account_assets(dbsession, registry, mock_eth_service,
                                 eth_network_id, eth_asset_id):
    """get_user_assets() gives sane results."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        setup_user_account(user, do_mainnet=True)

    mock_create_addresses(mock_eth_service, dbsession)

    # Play around with user accounts
    with transaction.manager:

        # No accounts yet
        assert UserCryptoAddress.get_user_asset_accounts(user) == []

        # User gets a deposit, an account gets created
        user = dbsession.query(User).first()
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")

        current_accounts = UserCryptoAddress.get_user_asset_accounts(user)
        assert len(current_accounts) == 1
        current_address, current_account = current_accounts[0]
        assert current_account == account
        assert current_address == address
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
Esempio n. 5
0
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")
Esempio n. 7
0
def top_up_user(dbsession, registry, wallet_user, eth_network_id, eth_asset_id):
    """Directly inject some assets to user wallet."""

    with transaction.manager:
        user = dbsession.query(User).get(wallet_user["user_id"])
        setup_user_account(user)

    with transaction.manager:
        user = dbsession.query(User).get(wallet_user["user_id"])
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")
Esempio n. 8
0
def top_up_user(dbsession, registry, wallet_user, eth_network_id, eth_asset_id):
    """Directly inject some assets to user wallet."""

    with transaction.manager:
        user = dbsession.query(User).get(wallet_user["user_id"])
        setup_user_account(user)

    with transaction.manager:
        user = dbsession.query(User).get(wallet_user["user_id"])
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")
Esempio n. 9
0
def topped_up_user(dbsession, registry, mock_eth_service, user_id, eth_network_id, eth_asset_id):
    """User has some ETH on their account."""
    with transaction.manager:
        user = dbsession.query(User).get(user_id)
        setup_user_account(user)

    mock_create_addresses(mock_eth_service, dbsession)

    with transaction.manager:
        user = dbsession.query(User).first()
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")
Esempio n. 10
0
def topped_up_user(dbsession, registry, mock_eth_service, user_id,
                   eth_network_id, eth_asset_id):
    """User has some ETH on their account."""
    with transaction.manager:
        user = dbsession.query(User).get(user_id)
        setup_user_account(user, do_mainnet=True)

    mock_create_addresses(mock_eth_service, dbsession)

    with transaction.manager:
        user = dbsession.query(User).first()
        network = dbsession.query(AssetNetwork).get(eth_network_id)
        asset = dbsession.query(Asset).get(eth_asset_id)
        address = UserCryptoAddress.get_default(user, network)
        account = address.address.get_or_create_account(asset)
        account.account.do_withdraw_or_deposit(Decimal("+10"), "Top up")
Esempio n. 11
0
def wallet_overview(wallet: UserWallet, request: Request):
    """Wallet overview page."""

    request.registry.notify(WalletOverviewViewed(request, wallet))

    # Whose wallet we are dealing with
    user = wallet.user

    # Set up initial addresses if user doesn't have any yet
    setup_user_account(user, request=request)

    asset_details = [describe_user_address_asset(request, asset) for asset in wallet.list_all_assets()]
    address_details = [describe_address(request, address) for address in wallet.list_addresses()]
    breadcrumbs = get_breadcrumbs(wallet, request)

    return locals()
Esempio n. 12
0
def wallet_user(dbsession, registry, mock_eth_service) -> dict:
    """Create a user having a wallet in place with some faux tokens."""

    details = {
        "email": "*****@*****.**",
        "password": "******"
    }

    with transaction.manager:
        user = create_user(dbsession, registry, email=details["email"], password=details["password"])
        setup_user_account(user)
        details["user_id"] = user.id

    success_op_count, failed_op_count = mock_create_addresses(mock_eth_service, dbsession)

    assert success_op_count == 1
    assert failed_op_count == 0

    return details
Esempio n. 13
0
def test_get_user_account_operations(dbsession, registry, eth_network_id):
    """get_active_operations() gives sane results."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        setup_user_account(user, do_mainnet=True)
        operations = UserCryptoOperation.get_active_operations(user)
        assert operations.count() == 2

        # Ticking operation off shortens the lsit
        op = operations.first()
        op.mark_complete()

        operations = UserCryptoOperation.get_active_operations(user)
        assert operations.count() == 1

        op = operations.first()
        op.mark_complete()
        assert operations.count() == 0
Esempio n. 14
0
def test_get_user_account_operations(dbsession, registry, eth_network_id):
    """get_active_operations() gives sane results."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        setup_user_account(user)
        operations = UserCryptoOperation.get_active_operations(user)
        assert operations.count() == 2

        # Ticking operation off shortens the lsit
        op = operations.first()
        op.mark_complete()

        operations = UserCryptoOperation.get_active_operations(user)
        assert operations.count() == 1

        op = operations.first()
        op.mark_complete()
        assert operations.count() == 0
Esempio n. 15
0
def wallet_user(dbsession, registry, mock_eth_service) -> dict:
    """Create a user having a wallet in place with some faux tokens."""

    details = {
        "email": "*****@*****.**",
        "password": "******"
    }

    with transaction.manager:
        user = create_user(dbsession, registry, email=details["email"], password=details["password"])
        setup_user_account(user, do_mainnet=True)
        details["user_id"] = user.id

    success_op_count, failed_op_count = mock_create_addresses(mock_eth_service, dbsession)

    assert success_op_count == 1
    assert failed_op_count == 0

    return details
Esempio n. 16
0
def test_setup_user_account(dbsession, registry, eth_service, testnet_service,
                            eth_network_id):
    """See that we create primary and testnet address."""

    with transaction.manager:
        user = create_user(dbsession, registry)
        assert user.owned_crypto_addresses.count() == 0  # 2 addresses
        setup_user_account(user, do_mainnet=True)
        assert user.owned_crypto_addresses.count() == 2  # 2 addresses
        assert user.owned_crypto_operations.count() == 2  # 2 account creations

    def _create_address(web3, dbsession, opid):
        with transaction.manager:
            op = dbsession.query(CryptoOperation).get(opid)
            assert isinstance(op.address, CryptoAddress)
            op.address.address = eth_address_to_bin(TEST_ADDRESS)
            op.mark_performed()
            op.mark_complete()

    with mock.patch("websauna.wallet.ethereum.ops.create_address",
                    new=_create_address):

        # Create address on main net
        success_op_count, failed_op_count = eth_service.run_waiting_operations(
        )
        assert success_op_count == 1
        assert failed_op_count == 0

        # Create address on test net
        success_op_count, failed_op_count = testnet_service.run_waiting_operations(
        )
        assert success_op_count == 1
        assert failed_op_count == 0

    # Make sure more addresses is not get created when we are called again
    with transaction.manager:
        user = dbsession.query(User).first()
        setup_user_account(user, do_mainnet=True)
        assert user.owned_crypto_addresses.count() == 2  # 2 addresses
        assert user.owned_crypto_operations.count() == 2  # 2 account creations
Esempio n. 17
0
def wallet_overview(wallet: UserWallet, request: Request):
    """Wallet overview page."""

    request.registry.notify(WalletOverviewViewed(request, wallet))

    # Whose wallet we are dealing with
    user = wallet.user

    # Set up initial addresses if user doesn't have any yet
    setup_user_account(user, request=request)

    asset_details = [
        describe_user_address_asset(request, asset)
        for asset in wallet.list_all_assets()
    ]
    address_details = [
        describe_address(request, address)
        for address in wallet.list_addresses()
    ]
    breadcrumbs = get_breadcrumbs(wallet, request)

    return locals()
Esempio n. 18
0
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