Example #1
0
def transfer_faircoins(request, resource_id):
    if request.method == "POST":
        resource = get_object_or_404(EconomicResource, id=resource_id)
        agent = get_agent(request)
        to_agent = request.POST["to_user"]
        send_coins_form = SendFairCoinsForm(data=request.POST,
                                            agent=resource.owner())
        if send_coins_form.is_valid():
            data = send_coins_form.cleaned_data
            address_end = data["to_address"]
            quantity = data["quantity"]
            notes = data["description"]
            address_origin = resource.faircoin_address.address
            network_fee = faircoin_utils.network_fee()
            if address_origin and address_end and network_fee:
                exchange_service = ExchangeService.get()
                exchange = exchange_service.send_faircoins(
                    from_agent=resource.owner(),
                    recipient=address_end,
                    qty=quantity,
                    resource=resource,
                    notes=notes)
                return HttpResponseRedirect(
                    '/%s/%s/' % ('faircoin/faircoin-history', resource.id))

        return HttpResponseRedirect(
            '/%s/%s/' % ('faircoin/manage-faircoin-account', resource.id))
Example #2
0
def transfer_faircoins(request, resource_id):
    if request.method == "POST":
        resource = get_object_or_404(EconomicResource, id=resource_id)
        agent = get_agent(request)
        to_agent = request.POST["to_user"]
        send_coins_form = SendFairCoinsForm(data=request.POST, agent=resource.owner())
        if send_coins_form.is_valid():
            data = send_coins_form.cleaned_data
            address_end = data["to_address"]
            quantity = data["quantity"]
            notes = data["description"]
            address_origin = resource.faircoin_address.address
            network_fee = faircoin_utils.network_fee()
            if address_origin and address_end and network_fee:
                exchange_service = ExchangeService.get()
                exchange = exchange_service.send_faircoins(
                    from_agent = resource.owner(),
                    recipient = address_end,
                    qty = quantity,
                    resource = resource,
                    notes = notes
                )
                return HttpResponseRedirect('/%s/%s/'
                    % ('faircoin/faircoin-history', resource.id))
        else:
            raise ValidationError(send_coins_form.errors)
    else:
        raise Http404
    return HttpResponseRedirect('/%s/%s/'
        % ('faircoin/manage-faircoin-account', resource.id))
Example #3
0
def manage_faircoin_account(request, resource_id):
    resource = get_object_or_404(EconomicResource, id=resource_id)
    user_agent = get_agent(request)
    if not user_agent or not (resource.owner() == user_agent
                              or resource.owner()
                              in user_agent.managed_projects()):
        raise Http404

    send_coins_form = None
    is_wallet_address = None
    limit = 0
    confirmed_balance = None
    unconfirmed_balance = None
    faircoin_account = False
    payment_due = False
    share_price = False
    number_of_shares = False
    can_pay = False

    wallet = faircoin_utils.is_connected()
    if wallet:
        is_wallet_address = faircoin_utils.is_mine(
            resource.faircoin_address.address)
        if not is_wallet_address:
            if resource.is_address_requested(): is_wallet_address = True
        if is_wallet_address:
            send_coins_form = SendFairCoinsForm(agent=resource.owner())
            try:
                balances = faircoin_utils.get_address_balance(
                    resource.faircoin_address.address)
                confirmed_balance = Decimal(balances[0]) / FAIRCOIN_DIVISOR
                unconfirmed_balance = Decimal(balances[0] +
                                              balances[1]) / FAIRCOIN_DIVISOR
                unconfirmed_balance += resource.balance_in_tx_state_new()
                fee = Decimal(faircoin_utils.network_fee()) / FAIRCOIN_DIVISOR
                limit = min(confirmed_balance, unconfirmed_balance) - fee
            except:
                confirmed_balance = "Not accessible now"
                unconfirmed_balance = "Not accessible now"
                limit = Decimal("0.0")
        else:
            wallet = False

    candidate_membership = resource.owner().candidate_membership()
    if candidate_membership:
        faircoin_account = resource.owner().faircoin_resource()
        if faircoin_account and wallet:
            share = EconomicResourceType.objects.membership_share()
            share_price = share.price_per_unit
            number_of_shares = resource.owner().number_of_shares()
            share_price = share_price * number_of_shares
            payment_due = True
            if resource.owner().owns_resource_of_type(share):
                payment_due = False
            if confirmed_balance and confirmed_balance != "Not accessible now":
                can_pay = confirmed_balance >= share_price

    return render(
        request, "faircoin/faircoin_account.html", {
            "resource": resource,
            "photo_size": (128, 128),
            "agent": resource.owner(),
            "wallet": wallet,
            "send_coins_form": send_coins_form,
            "is_wallet_address": is_wallet_address,
            "confirmed_balance": confirmed_balance,
            "unconfirmed_balance": unconfirmed_balance,
            "limit": limit,
            "faircoin_account": faircoin_account,
            "candidate_membership": candidate_membership,
            "payment_due": payment_due,
            "share_price": share_price,
            "number_of_shares": number_of_shares,
            "can_pay": can_pay,
            "help": get_help("faircoin account"),
        })
Example #4
0
    def send_faircoins(self, from_agent, recipient, qty, resource, notes=''):
        if 'faircoin' not in settings.INSTALLED_APPS:
            return None
        to_resources = EconomicResource.objects.filter(
            faircoin_address__address=recipient)
        to_resource = None
        to_agent = None
        if to_resources:
            to_resource = to_resources[0]  # shd be only one
            to_agent = to_resource.owner()
        et_give = EventType.objects.get(name="Give")
        network_fee = faircoin_utils.network_fee()
        if to_resource and network_fee:
            tt = ExchangeService.faircoin_internal_transfer_type()
            xt = tt.exchange_type
            date = datetime.date.today()
            exchange = Exchange(
                exchange_type=xt,
                use_case=xt.use_case,
                name="Transfer Faircoins",
                start_date=date,
            )
            exchange.save()
            transfer = Transfer(
                transfer_type=tt,
                exchange=exchange,
                transfer_date=date,
                name="Transfer Faircoins",
                notes=notes,
            )
            transfer.save()
        else:
            tt = ExchangeService.faircoin_outgoing_transfer_type()
            xt = tt.exchange_type
            date = datetime.date.today()
            exchange = Exchange(
                exchange_type=xt,
                use_case=xt.use_case,
                name="Send Faircoins",
                start_date=date,
            )
            exchange.save()
            transfer = Transfer(
                transfer_type=tt,
                exchange=exchange,
                transfer_date=date,
                name="Send Faircoins",
                notes=notes,
            )
            transfer.save()

        state = "new"
        event = EconomicEvent(
            event_type=et_give,
            event_date=date,
            from_agent=from_agent,
            to_agent=to_agent,
            resource_type=resource.resource_type,
            resource=resource,
            quantity=qty,
            transfer=transfer,
            event_reference=recipient,
            description=notes,
        )
        event.save()
        fairtx = FaircoinTransaction(
            event=event,
            tx_state=state,
            to_address=recipient,
        )
        fairtx.save()

        if to_resource:
            # The events are saved without fee.
            # When the wallet constructs the transactions and knows how large is,
            # it calculates the fee and it will add the fee to the et_give event.
            # quantity = qty - Decimal(float(network_fee) / 1.e8)
            et_receive = EventType.objects.get(name="Receive")
            event = EconomicEvent(
                event_type=et_receive,
                event_date=date,
                from_agent=from_agent,
                to_agent=to_agent,
                resource_type=to_resource.resource_type,
                resource=to_resource,
                quantity=qty,
                transfer=transfer,
                event_reference=recipient,
                description=notes,
            )
            event.save()
            fairtx = FaircoinTransaction(
                event=event,
                tx_state=state,
                to_address=recipient,
            )
            fairtx.save()

        return exchange
Example #5
0
    def send_faircoins(self, from_agent, recipient, qty, resource, notes=''):
        if 'faircoin' not in settings.INSTALLED_APPS:
            return None
        to_resources = EconomicResource.objects.filter(faircoin_address__address=recipient)
        to_resource = None
        to_agent = None
        if to_resources:
            to_resource = to_resources[0]  # shd be only one
            to_agent = to_resource.owner()
        et_give = EventType.objects.get(name="Give")
        network_fee = faircoin_utils.network_fee()
        if to_resource and network_fee:
            tt = ExchangeService.faircoin_internal_transfer_type()
            xt = tt.exchange_type
            date = datetime.date.today()
            exchange = Exchange(
                exchange_type=xt,
                use_case=xt.use_case,
                name="Transfer Faircoins",
                start_date=date,
            )
            exchange.save()
            transfer = Transfer(
                transfer_type=tt,
                exchange=exchange,
                transfer_date=date,
                name="Transfer Faircoins",
                notes=notes,
            )
            transfer.save()
        else:
            tt = ExchangeService.faircoin_outgoing_transfer_type()
            xt = tt.exchange_type
            date = datetime.date.today()
            exchange = Exchange(
                exchange_type=xt,
                use_case=xt.use_case,
                name="Send Faircoins",
                start_date=date,
            )
            exchange.save()
            transfer = Transfer(
                transfer_type=tt,
                exchange=exchange,
                transfer_date=date,
                name="Send Faircoins",
                notes=notes,
            )
            transfer.save()

        state = "new"
        event = EconomicEvent(
            event_type=et_give,
            event_date=date,
            from_agent=from_agent,
            to_agent=to_agent,
            resource_type=resource.resource_type,
            resource=resource,
            quantity=qty,
            transfer=transfer,
            event_reference=recipient,
            description=notes,
        )
        event.save()
        fairtx = FaircoinTransaction(
            event=event,
            tx_state=state,
            to_address=recipient,
        )
        fairtx.save()

        if to_resource:
            # The events are saved without fee.
            # When the wallet constructs the transactions and knows how large is,
            # it calculates the fee and it will add the fee to the et_give event.
            # quantity = qty - Decimal(float(network_fee) / 1.e8)
            et_receive = EventType.objects.get(name="Receive")
            event = EconomicEvent(
                event_type=et_receive,
                event_date=date,
                from_agent=from_agent,
                to_agent=to_agent,
                resource_type=to_resource.resource_type,
                resource=to_resource,
                quantity=qty,
                transfer=transfer,
                event_reference=recipient,
                description=notes,
            )
            event.save()
            fairtx = FaircoinTransaction(
                event=event,
                tx_state=state,
                to_address=recipient,
            )
            fairtx.save()

        return exchange
Example #6
0
def manage_faircoin_account(request, resource_id):
    resource = get_object_or_404(EconomicResource, id=resource_id)
    user_agent = get_agent(request)
    if not user_agent or not (resource.owner() == user_agent or resource.owner() in user_agent.managed_projects()):
        raise Http404

    send_coins_form = None
    is_wallet_address = None
    limit = 0
    confirmed_balance = None
    unconfirmed_balance = None
    faircoin_account = False
    payment_due = False
    share_price = False
    number_of_shares = False
    can_pay = False

    wallet = faircoin_utils.is_connected()
    if wallet:
        is_wallet_address = faircoin_utils.is_mine(resource.faircoin_address.address)
        if not is_wallet_address:
            if resource.is_address_requested(): is_wallet_address = True
        if is_wallet_address:
            send_coins_form = SendFairCoinsForm(agent=resource.owner())
            try:
                balances = faircoin_utils.get_address_balance(resource.faircoin_address.address)
                confirmed_balance = Decimal(balances[0]) / FAIRCOIN_DIVISOR
                unconfirmed_balance =  Decimal(balances[0] + balances[1]) / FAIRCOIN_DIVISOR
                unconfirmed_balance += resource.balance_in_tx_state_new()
                fee = Decimal(faircoin_utils.network_fee()) / FAIRCOIN_DIVISOR
                limit = min(confirmed_balance, unconfirmed_balance) - fee
            except:
                confirmed_balance = "Not accessible now"
                unconfirmed_balance = "Not accessible now"
                limit = Decimal("0.0")
        else:
            wallet = False

    candidate_membership = resource.owner().candidate_membership()
    if candidate_membership:
        faircoin_account = resource.owner().faircoin_resource()
        if faircoin_account and wallet:
            share = EconomicResourceType.objects.membership_share()
            share_price = share.price_per_unit
            number_of_shares = resource.owner().number_of_shares()
            share_price = share_price * number_of_shares
            payment_due = True
            if resource.owner().owns_resource_of_type(share):
                payment_due = False
            if confirmed_balance and confirmed_balance != "Not accessible now":
                can_pay = confirmed_balance >= share_price

    return render(request, "faircoin/faircoin_account.html", {
        "resource": resource,
        "photo_size": (128, 128),
        "agent": resource.owner(),
        "wallet": wallet,
        "send_coins_form": send_coins_form,
        "is_wallet_address": is_wallet_address,
        "confirmed_balance": confirmed_balance,
        "unconfirmed_balance": unconfirmed_balance,
        "limit": limit,
        "faircoin_account": faircoin_account,
        "candidate_membership": candidate_membership,
        "payment_due": payment_due,
        "share_price": share_price,
        "number_of_shares": number_of_shares,
        "can_pay": can_pay,
        "help": get_help("faircoin account"),
    })