Esempio n. 1
0
    def render_sms(self, template_context):

        op = self.context.op
        amount = format_asset_amount(op.amount, op.asset.asset_class)
        template_context.update({
            "amount": amount,
            "asset": op.asset.name,
            "address": bin_to_eth_address(op.external_address),
        })
        return render("wallet/sms/confirm_withdraw.txt", template_context, request=self.request)
Esempio n. 2
0
    def render_sms(self, template_context):

        op = self.context.op
        amount = format_asset_amount(op.amount, op.asset.asset_class)
        template_context.update({
            "amount": amount,
            "asset": op.asset.name,
            "address": bin_to_eth_address(op.external_address),
        })
        return render("wallet/sms/confirm_withdraw.txt", template_context, request=self.request)
Esempio n. 3
0
def find_eth_balance(view, column, address: CryptoAddress):
    """Get link to a user admin show view from the user owned account."""
    request = view.request
    # TODO: Expensive. Should do in get_query()
    dbsession = request.dbsession
    eth = get_ether_asset(dbsession)
    account = address.get_account(eth)
    if account:
        return format_asset_amount(account.account.get_balance(), account.account.asset.asset_class)
    else:
        return "-"
Esempio n. 4
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)
Esempio n. 5
0
def find_eth_balance(view, column, address: CryptoAddress):
    """Get link to a user admin show view from the user owned account."""
    request = view.request
    # TODO: Expensive. Should do in get_query()
    dbsession = request.dbsession
    eth = get_ether_asset(dbsession)
    account = address.get_account(eth)
    if account:
        return format_asset_amount(account.account.get_balance(),
                                   account.account.asset.asset_class)
    else:
        return "-"
Esempio n. 6
0
def withdraw(user_asset: UserAddressAsset, request):
    """Ask user for the withdraw details."""

    title = "Withdraw"
    wallet = user_asset.wallet
    asset_resource = user_asset
    network_resource = get_network_resource(request, asset_resource.asset.network)
    balance = format_asset_amount(user_asset.balance, user_asset.asset.asset_class)
    address_resource = asset_resource.address
    account = user_asset.account

    schema = WithdrawSchema().bind(request=request, account=account)
    b = deform.Button(name='process', title="Verify with SMS", 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
                amount = appstruct["amount"]
                address = eth_address_to_bin(appstruct["address"])
                note = appstruct["note"]
                confirmations = get_required_confirmation_count(request.registry, user_asset.account.asset.network, CryptoOperationType.withdraw)

                user_crypto_address = asset_resource.address.address

                # Create the withdraw
                user_withdraw = user_crypto_address.withdraw(asset_resource.asset, amount, address, note, confirmations)

                # Mark it as pending for confirmation
                UserWithdrawConfirmation.require_confirmation(user_withdraw)

                # Redirect user to the confirmation page
                user_crypto_operation_resource = get_user_crypto_operation_resource(request, user_withdraw)
                return HTTPFound(request.resource_url(user_crypto_operation_resource, "confirm-withdraw"))

            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()

    return locals()
Esempio n. 7
0
def describe_operation(request, uop: UserOperation) -> dict:
    """Fetch operation details and link data for rendering."""
    assert isinstance(uop, UserOperation)
    detail = {}
    op = uop.uop.crypto_operation

    # Link to the user asset details
    detail["op"] = op
    if op.holding_account and op.holding_account.asset:
        detail["asset_resource"] = get_user_address_asset(
            request, op.address, op.holding_account.asset)

    tx = op.primary_tx

    # From: and To: swap in address rendering
    detail["deposit_like"] = op.operation_type in (
        CryptoOperationType.deposit, )

    confirmations = op.calculate_confirmations()

    if confirmations is not None:
        if confirmations > 30:
            confirmations = "30+"
        detail["confirmations"] = confirmations

    if op.external_address:
        detail["external_address"] = bin_to_eth_address(op.external_address)

    if op.txid:
        detail["txid"] = bin_to_txid(op.txid)

    amount = op.amount

    if amount:
        detail["amount"] = format_asset_amount(amount, op.asset.asset_class)

    detail["uuid"] = str(uop.uop.id)
    detail["resource"] = uop
    detail["tx_name"] = uop.get_title()
    detail["state"] = OP_STATES[op.state]
    detail["address_resource"] = get_user_address_resource(request, op.address)
    detail["network_resource"] = get_network_resource(request, op.network)

    detail[
        "manual_confirmation_needed"] = op.state == CryptoOperationState.confirmation_required

    if tx:
        detail["notes"] = tx.message

    return detail
Esempio n. 8
0
def describe_user_address_asset(request, uaa: UserAddressAsset) -> dict:
    """Fetch user asset holding details."""
    entry = {}
    account = uaa.crypto_account.account
    entry["asset_id"] = account.asset.id
    entry["name"] = uaa.get_title()
    entry["account"] = uaa.crypto_account.account
    entry["asset_resource"] = uaa
    entry["type"] = account.asset.asset_class.value
    entry["address_resource"] = uaa.address
    entry["balance"] = format_asset_amount(account.get_balance(), account.asset.asset_class)
    entry["network_resource"] = get_network_resource(request, account.asset.network)
    entry["can_withdraw"] = account.get_balance() > 0
    return entry
Esempio n. 9
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)
Esempio n. 10
0
def describe_user_address_asset(request, uaa: UserAddressAsset) -> dict:
    """Fetch user asset holding details."""
    entry = {}
    account = uaa.crypto_account.account
    entry["asset_id"] = account.asset.id
    entry["name"] = uaa.get_title()
    entry["account"] = uaa.crypto_account.account
    entry["asset_resource"] = uaa
    entry["type"] = account.asset.asset_class.value
    entry["address_resource"] = uaa.address
    entry["balance"] = format_asset_amount(account.get_balance(),
                                           account.asset.asset_class)
    entry["network_resource"] = get_network_resource(request,
                                                     account.asset.network)
    entry["can_withdraw"] = account.get_balance() > 0
    return entry
Esempio n. 11
0
def describe_operation(request, uop: UserOperation) -> dict:
    """Fetch operation details and link data for rendering."""
    assert isinstance(uop, UserOperation)
    detail = {}
    op = uop.uop.crypto_operation

    # Link to the user asset details
    detail["op"] = op
    if op.holding_account and op.holding_account.asset:
        detail["asset_resource"] = get_user_address_asset(request, op.address, op.holding_account.asset)

    tx = op.primary_tx

    # From: and To: swap in address rendering
    detail["deposit_like"] = op.operation_type in (CryptoOperationType.deposit,)

    confirmations = op.calculate_confirmations()

    if confirmations is not None:
        if confirmations > 30:
            confirmations = "30+"
        detail["confirmations"] = confirmations

    if op.external_address:
        detail["external_address"] = bin_to_eth_address(op.external_address)

    if op.txid:
        detail["txid"] = bin_to_txid(op.txid)

    amount = op.amount

    if amount:
        detail["amount"] = format_asset_amount(amount, op.asset.asset_class)

    detail["uuid"] = str(uop.uop.id)
    detail["resource"] = uop
    detail["tx_name"] = uop.get_title()
    detail["state"] = OP_STATES[op.state]
    detail["address_resource"] = get_user_address_resource(request, op.address)
    detail["network_resource"] = get_network_resource(request, op.network)

    detail["manual_confirmation_needed"] = op.state == CryptoOperationState.confirmation_required

    if tx:
        detail["notes"] = tx.message

    return detail
Esempio n. 12
0
 def supply(self):
     if self.asset.supply:
         return format_asset_amount(self.asset.supply, self.asset.asset_class)
     else:
         return ""
Esempio n. 13
0
 def supply(self):
     if self.asset.supply:
         return format_asset_amount(self.asset.supply,
                                    self.asset.asset_class)
     else:
         return ""
Esempio n. 14
0
def withdraw(user_asset: UserAddressAsset, request):
    """Ask user for the withdraw details."""

    title = "Withdraw"
    wallet = user_asset.wallet
    asset_resource = user_asset
    network_resource = get_network_resource(request, asset_resource.asset.network)
    balance = format_asset_amount(user_asset.balance, user_asset.asset.asset_class)
    address_resource = asset_resource.address
    account = user_asset.account

    schema = WithdrawSchema().bind(request=request, account=account)
    withdraw = deform.Button(name='process', title="Verify with SMS", css_class="btn-primary btn-block btn-lg")
    cancel = deform.Button(name='cancel', title="Cancel", css_class="btn-primary btn-block btn-lg")
    form = deform.Form(schema, buttons=(withdraw, cancel), resource_registry=ResourceRegistry(request))

    # 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
                amount = appstruct["amount"]
                address = eth_address_to_bin(appstruct["address"])
                note = appstruct["note"]
                confirmations = get_required_confirmation_count(request.registry, user_asset.account.asset.network, CryptoOperationType.withdraw)

                user_crypto_address = asset_resource.address.address

                # Create the withdraw
                user_withdraw = user_crypto_address.withdraw(asset_resource.asset, amount, address, note, confirmations)

                # Ethereum special parameters
                user_withdraw.crypto_operation.other_data["gas"] = appstruct["advanced"]["gas"]
                data = appstruct["advanced"]["data"]
                if data:
                    user_withdraw.crypto_operation.other_data["data"] = data

                # Mark it as pending for confirmation
                UserWithdrawConfirmation.require_confirmation(user_withdraw)

                # Redirect user to the confirmation page
                user_crypto_operation_resource = get_user_crypto_operation_resource(request, user_withdraw)
                return HTTPFound(request.resource_url(user_crypto_operation_resource, "confirm-withdraw"))

            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()
        elif "cancel" in request.POST:
            return HTTPFound(request.resource_url(wallet))
        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()

    # This loads widgets specific CSS/JavaScript in HTML code,
    # if form widgets specify any static assets.
    form.resource_registry.pull_in_resources(request, form)

    return locals()