def describe_address(request, ua: UserAddress) -> dict: """Fetch address details and link data for rendering.""" detail = {} detail["user_address"] = ua detail["id"] = ua.address.id detail["address"] = ua.address.address detail["network_resource"] = get_network_resource(request, ua.address.address.network) detail["name"] = ua.address.name detail["op"] = ua.address.address.get_creation_op() return detail
def describe_address(request, ua: UserAddress) -> dict: """Fetch address details and link data for rendering.""" detail = {} detail["user_address"] = ua detail["id"] = ua.address.id detail["address"] = ua.address.address detail["network_resource"] = get_network_resource( request, ua.address.address.network) detail["name"] = ua.address.name detail["op"] = ua.address.address.get_creation_op() return detail
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()
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
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
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
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()