Пример #1
0
def create_address(wallet: UserWallet, request):
    """List all addresses."""

    schema = CreateAddressSchema().bind(request=request)

    # Create a styled button with some extra Bootstrap 3 CSS classes
    b = deform.Button(name='process',
                      title="Create",
                      css_class="btn-primary btn-block btn-lg")
    form = deform.Form(schema, buttons=(b, ))

    # User submitted this form
    if request.method == "POST":
        if 'process' in request.POST:

            try:
                appstruct = form.validate(request.POST.items())

                # Save form data from appstruct
                network = appstruct["network"]
                name = appstruct["name"]
                confirmations = get_required_confirmation_count(
                    request.registry, network,
                    CryptoOperationType.create_address)
                UserCryptoAddress.create_address(wallet.user, network, name,
                                                 confirmations)

                # Thank user and take him/her to the next page
                messages.add(request,
                             kind="info",
                             msg="New account is being created",
                             msg_id="msg-account-created")
                return HTTPFound(request.resource_url(wallet, "transactions"))

            except deform.ValidationFailure as e:
                # Render a form version where errors are visible next to the fields,
                # and the submitted values are posted back
                rendered_form = e.render()
        else:
            # We don't know which control caused form submission
            raise HTTPInternalServerError("Unknown form button pressed")
    else:
        # Render a form with initial values
        rendered_form = form.render()

    title = "Create new account"

    return locals()
Пример #2
0
def get_user_address_resource(request, address: CryptoAddress) -> UserAddress:
    uca = UserCryptoAddress.get_by_address(address)
    if not uca:
        return None
    wallet_root = route_factory(request)
    wallet = wallet_root.get_user_wallet(uca.user)
    return wallet.get_address_resource(uca)
Пример #3
0
def get_user_address_resource(request, address: CryptoAddress) -> UserAddress:
    uca = UserCryptoAddress.get_by_address(address)
    if not uca:
        return None
    wallet_root = route_factory(request)
    wallet = wallet_root.get_user_wallet(uca.user)
    return wallet.get_address_resource(uca)
Пример #4
0
def create_default_user_address(user: User, network: AssetNetwork, confirmations=1) -> CryptoAddressCreation:
    """Initiate operation to create operation in a network."""

    if network.name == "ethereum":
        name = "Default"
    else:
        name = "{} default".format(network.name.title())

    op = UserCryptoAddress.create_address(user, network, name, confirmations)
    return op
Пример #5
0
def create_default_user_address(user: User, network: AssetNetwork, confirmations=1) -> CryptoAddressCreation:
    """Initiate operation to create operation in a network."""

    if network.name == "ethereum":
        name = "Default"
    else:
        name = "{} default".format(network.name.title())

    op = UserCryptoAddress.create_address(user, network, name, confirmations)
    return op
Пример #6
0
def do_faux_withdraw(user_address: UserCryptoAddress, target_address, asset_id,
                     amount) -> UserCryptoOperation:
    """Simulate user withdrawing assets from one of his addresses."""
    dbsession = Session.object_session(user_address)
    asset = dbsession.query(Asset).get(asset_id)
    op = user_address.withdraw(asset,
                               Decimal(amount),
                               eth_address_to_bin(target_address),
                               "Simulated withraw",
                               required_confirmation_count=1)
    return op
Пример #7
0
def get_user_address_asset(request, address: CryptoAddress, asset: Asset) -> UserAddressAsset:
    assert isinstance(asset, Asset)
    uca = UserCryptoAddress.get_by_address(address)
    if not uca:
        return None
    wallet_root = route_factory(request)
    wallet = wallet_root.get_user_wallet(uca.user)
    address = wallet.get_address_resource(uca)
    crypto_address_account = address.address.get_crypto_account(asset)
    asset = address.get_user_address_asset(crypto_address_account)
    return asset
Пример #8
0
def get_user_address_asset(request, address: CryptoAddress,
                           asset: Asset) -> UserAddressAsset:
    assert isinstance(asset, Asset)
    uca = UserCryptoAddress.get_by_address(address)
    if not uca:
        return None
    wallet_root = route_factory(request)
    wallet = wallet_root.get_user_wallet(uca.user)
    address = wallet.get_address_resource(uca)
    crypto_address_account = address.address.get_crypto_account(asset)
    asset = address.get_user_address_asset(crypto_address_account)
    return asset
Пример #9
0
def create_address(wallet: UserWallet, request):
    """List all addresses."""

    schema = CreateAddressSchema().bind(request=request)

    # Create a styled button with some extra Bootstrap 3 CSS classes
    b = deform.Button(name='process', title="Create", css_class="btn-block btn-lg")
    form = deform.Form(schema, buttons=(b,))

    # User submitted this form
    if request.method == "POST":
        if 'process' in request.POST:

            try:
                appstruct = form.validate(request.POST.items())

                # Save form data from appstruct
                network = appstruct["network"]
                name = appstruct["name"]
                confirmations = get_required_confirmation_count(request.registry, network, CryptoOperationType.create_address)
                UserCryptoAddress.create_address(wallet.user, network, name, confirmations)

                # Thank user and take him/her to the next page
                messages.add(request, kind="info", msg="New account is being created", msg_id="msg-account-created")
                return HTTPFound(request.resource_url(wallet, "transactions"))

            except deform.ValidationFailure as e:
                # Render a form version where errors are visible next to the fields,
                # and the submitted values are posted back
                rendered_form = e.render()
        else:
            # We don't know which control caused form submission
            raise HTTPInternalServerError("Unknown form button pressed")
    else:
        # Render a form with initial values
        rendered_form = form.render()

    title = "Create new account"

    return locals()
Пример #10
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")
Пример #11
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")
Пример #12
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")
Пример #13
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")
Пример #14
0
def get_default_balance(request):
    """Get ETH balance in Ethereum network."""

    user = request.user

    if not user:
        return None

    asset = get_ether_asset(request.dbsession)
    network = get_eth_network(request.dbsession)
    default_address = UserCryptoAddress.get_default(user, network)
    if not default_address:
        return format_asset_amount(Decimal(0), asset.asset_class)

    account = default_address.get_crypto_account(asset)

    if account:
        return format_asset_amount(account.account.get_balance(), asset.asset_class)
    else:
        return format_asset_amount(Decimal(0), asset.asset_class)
Пример #15
0
def get_default_balance(request):
    """Get ETH balance in Ethereum network."""

    return "0"

    # TODO: Breaks test_scan_crowdsale_payments_one_participant_paid_with_email

    user = request.user

    if not user:
        return None

    asset = get_ether_asset(request.dbsession)
    network = get_eth_network(request.dbsession)
    default_address = UserCryptoAddress.get_default(user, network)
    if not default_address:
        return format_asset_amount(Decimal(0), asset.asset_class)

    account = default_address.get_crypto_account(asset)

    if account:
        return format_asset_amount(account.account.get_balance(), asset.asset_class)
    else:
        return format_asset_amount(Decimal(0), asset.asset_class)
Пример #16
0
def do_faux_withdraw(user_address: UserCryptoAddress, target_address, asset_id, amount) -> UserCryptoOperation:
    """Simulate user withdrawing assets from one of his addresses."""
    dbsession = Session.object_session(user_address)
    asset = dbsession.query(Asset).get(asset_id)
    op = user_address.withdraw(asset, Decimal(amount), eth_address_to_bin(target_address), "Simulated withraw", required_confirmation_count=1)
    return op