Пример #1
0
    def include_blockchain_tx_as_event(self, agent, resource):
        faircoin_address = str(resource.digital_currency_address)
        tx_in_blockchain = faircoin_utils.get_address_history(faircoin_address)

        event_list = resource.events.all()
        tx_in_ocp = []
        for event in event_list:
            tx_in_ocp.append(str(event.digital_currency_tx_hash))

        tx_included = []
        for tx in tx_in_blockchain:
            if str(tx[0]) not in tx_in_ocp:
                (amount, time) = faircoin_utils.get_transaction_info(
                    str(tx[0]), faircoin_address)
                qty = Decimal(amount / 1000000)
                date = datetime.date.fromtimestamp(time)
                tt = ExchangeService.faircoin_incoming_transfer_type()
                xt = tt.exchange_type
                exchange = Exchange(
                    exchange_type=xt,
                    use_case=xt.use_case,
                    name="Receive Faircoins",
                    start_date=date,
                )
                exchange.save()
                transfer = Transfer(
                    transfer_type=tt,
                    exchange=exchange,
                    transfer_date=date,
                    name="Receive Faircoins",
                )
                transfer.save()

                et_receive = EventType.objects.get(name="Receive")
                state = "new"
                confirmations, timestamp = faircoin_utils.get_confirmations(
                    str(tx[0]))
                if confirmations > 0:
                    state = "broadcast"
                if confirmations > 2:
                    state = "confirmed"

                event = EconomicEvent(event_type=et_receive,
                                      event_date=date,
                                      to_agent=agent,
                                      resource_type=resource.resource_type,
                                      resource=resource,
                                      digital_currency_tx_hash=str(tx[0]),
                                      digital_currency_tx_state=state,
                                      quantity=qty,
                                      transfer=transfer,
                                      event_reference=faircoin_address)
                event.save()

                tx_included.append(str(tx[0]))
        #import pdb; pdb.set_trace()
        return tx_included
Пример #2
0
    def setUp(self):
        self.transfer = Transfer(
            created_date=date.today(),
            slug='test-xfer-xfer',
            transfer_date=date.today()
        )
        self.transfer.save()

        self.et_give = EventType.objects.get(name='Give') or EventType(
            name='Give',
            label='do not care',
            resource_effect='=',    # don't care
            slug='test-xfer-et-give'
        )
        self.et_give.save()

        self.et_recv = EventType.objects.get(name='Receive') or EventType(
            name='Receive',
            label='do not care',
            resource_effect='=',    # don't care
            slug='test-xfer-et-recv'
        )
        self.et_recv.save()

        self.rt_spam = EconomicResourceType(
            name='Spam',
            slug='test-xfer-rt-spam'
        )
        self.rt_spam.save()

        self.rt_eggs = EconomicResourceType(
            name='Eggs',
            slug='test-xfer-rt-eggs'
        )
        self.rt_eggs.save()

        # both events should have null agents so that there is no extra
        # assignment to give_text or receive_text
        self.ev_give = EconomicEvent(
            event_type=self.et_give,
            event_date=date.today(),
            resource_type=self.rt_spam,
            quantity=Decimal('1.0'),
            transfer=self.transfer,
            slug='test-xfer-ev-give'
        )
        self.ev_give.save()

        self.ev_recv = EconomicEvent(
            event_type=self.et_recv,
            event_date=date.today(),
            resource_type=self.rt_eggs,
            quantity=Decimal('1.0'),
            transfer=self.transfer,
            slug='test-xfer-ev-recv'
        )
        self.ev_recv.save()
Пример #3
0
    def mutate(cls, root, args, context, info):
        action = args.get('action')
        input_of_id = args.get('input_of_id')
        output_of_id = args.get('output_of_id')
        provider_id = args.get('provider_id')
        receiver_id = args.get('receiver_id')
        scope_id = args.get('scope_id')
        affects_id = args.get('affects_id')
        affected_resource_classified_as_id = args.get(
            'affected_resource_classified_as_id')
        affected_numeric_value = args.get('affected_numeric_value')
        affected_unit_id = args.get('affected_unit_id')
        start = args.get('start')
        fulfills_commitment_id = args.get(
            'fulfills_commitment_id'
        )  #TODO see if this needs fixing for multiples when doing exchanges
        url = args.get('url')
        request_distribution = args.get('request_distribution')
        note = args.get('note')
        create_resource = args.get('create_resource')
        resource_tracking_identifier = args.get('resource_tracking_identifier')
        resource_image = args.get('resource_image')
        resource_current_location_id = args.get('resource_current_location_id')
        resource_note = args.get('resource_note')

        if fulfills_commitment_id:
            commitment = Commitment.objects.get(pk=fulfills_commitment_id)
        else:
            commitment = None
        if action:
            event_type = EventType.objects.convert_action_to_event_type(action)
        elif commitment:
            event_type = commitment.event_type
        else:
            raise ValidationError(
                "Must provide an action in either economic event or its commitment"
            )
        if not note:
            note = ""
        if start:
            start = datetime.datetime.strptime(start, '%Y-%m-%d').date()
        else:
            start = datetime.date.today()
        if scope_id:
            scope = EconomicAgent.objects.get(pk=scope_id)
        elif commitment:
            scope = commitment.context_agent
        else:
            raise ValidationError(
                "Must provide a scope in either economic event or its commitment"
            )
        if provider_id:
            provider = EconomicAgent.objects.get(pk=provider_id)
        elif action == "work":
            user_agent = AgentUser.objects.get(user=context.user)
            provider = user_agent.agent
        elif commitment:
            provider = commitment.from_agent
        else:
            provider = scope
        if receiver_id:
            receiver = EconomicAgent.objects.get(pk=receiver_id)
        elif commitment:
            receiver = commitment.to_agent
        else:
            receiver = scope
        if input_of_id:
            process = Process.objects.get(pk=input_of_id)
        elif output_of_id:
            process = Process.objects.get(pk=output_of_id)
        elif commitment:
            process = commitment.process
        else:
            process = None
        if affects_id:
            affects = EconomicResourceProxy.objects.get(pk=affects_id)
        else:
            affects = None
        if affected_resource_classified_as_id:
            affected_resource_classification = EconomicResourceType.objects.get(
                pk=affected_resource_classified_as_id)
        elif affects:
            affected_resource_classification = EconomicResourceType.objects.get(
                pk=affects.resource_type.id)
        elif commitment:
            affected_resource_classification = EconomicResourceType.objects.get(
                pk=commitment.resource_type.id)
        else:
            raise ValidationError(
                "Must provide a resource classification in either economic event or its commitment"
            )
        if affected_unit_id:
            affected_unit = Unit.objects.get(pk=affected_unit_id)
        elif commitment:
            affected_unit = Unit.objects.get(
                pk=commitment.unit_of_quantity__id)
        else:
            raise ValidationError(
                "Must provide a unit in either economic event or its commitment"
            )
        if not url:
            url = ""
        if not request_distribution:
            request_distribution = False
        if resource_current_location_id:
            current_location = Location.objects.get(
                pk=resource_current_location_id)
        if not affects:
            if create_resource:
                if not resource_note:
                    resource_note = ""
                if not resource_image:
                    resource_image = ""
                if not resource_tracking_identifier:
                    resource_tracking_identifier = ""
                affects = EconomicResourceProxy(
                    resource_type=affected_resource_classification,
                    quantity=Decimal(affected_numeric_value),
                    photo_url=resource_image,
                    identifier=resource_tracking_identifier,
                    current_location=current_location,
                    notes=resource_note,
                    created_by=context.user,
                    #location
                )

        economic_event = EconomicEventProxy(
            event_type=event_type,
            process=process,
            from_agent=provider,
            to_agent=receiver,
            resource_type=affected_resource_classification,
            resource=affects,
            quantity=Decimal(affected_numeric_value),
            unit_of_quantity=affected_unit,
            event_date=start,
            description=note,
            context_agent=scope,
            url=url,
            commitment=commitment,
            is_contribution=request_distribution,
            created_by=context.user,
        )

        user_agent = AgentUser.objects.get(user=context.user).agent
        is_authorized = user_agent.is_authorized(
            object_to_mutate=economic_event)
        if is_authorized:
            economic_event.save_api(user=context.user,
                                    create_resource=create_resource)
        else:
            raise PermissionDenied(
                'User not authorized to perform this action.')

        return CreateEconomicEvent(economic_event=economic_event)
Пример #4
0
    def mutate(cls, root, args, context, info):
        action = args.get('action')
        input_of_id = args.get('input_of_id')
        output_of_id = args.get('output_of_id')
        provider_id = args.get('provider_id')
        receiver_id = args.get('receiver_id')
        scope_id = args.get('scope_id')
        affects_id = args.get('affects_id')
        affected_resource_classified_as_id = args.get('affected_resource_classified_as_id')
        affected_numeric_value = args.get('affected_numeric_value')
        affected_unit_id = args.get('affected_unit_id')
        start = args.get('start')
        fulfills_commitment_id = args.get('fulfills_commitment_id') #TODO see if this needs fixing for multiples when doing exchanges
        url = args.get('url')
        request_distribution = args.get('request_distribution')
        note = args.get('note')
        create_resource = args.get('create_resource')
        resource_tracking_identifier = args.get('resource_tracking_identifier')
        resource_image = args.get('resource_image')
        resource_current_location_id = args.get('resource_current_location_id')
        resource_note = args.get('resource_note')
        resource_url = args.get('resource_url')

        if fulfills_commitment_id:
            commitment = Commitment.objects.get(pk=fulfills_commitment_id)
        else:
            commitment = None
        if action:
            event_type = EventType.objects.convert_action_to_event_type(action)
        elif commitment:
            event_type = commitment.event_type
        else:
            raise ValidationError("Must provide an action in either economic event or its commitment")
        if not note:
            note = ""
        if start:
            start = datetime.datetime.strptime(start, '%Y-%m-%d').date()
        else:
            start = datetime.date.today()
        if scope_id:
            scope = EconomicAgent.objects.get(pk=scope_id)
        elif commitment:
            scope = commitment.context_agent
        else:
            raise ValidationError("Must provide a scope in either economic event or its commitment")
        if provider_id:
            provider = EconomicAgent.objects.get(pk=provider_id)
        elif action == "work":
            user_agent = AgentUser.objects.get(user=context.user)
            provider = user_agent.agent
        elif commitment:
            provider = commitment.from_agent
        else:
            provider = scope
        if receiver_id:
            receiver = EconomicAgent.objects.get(pk=receiver_id)
        elif commitment:
            receiver = commitment.to_agent
        else:
            receiver = scope
        if input_of_id:
            process = Process.objects.get(pk=input_of_id)
        elif output_of_id:
            process = Process.objects.get(pk=output_of_id)
        elif commitment:
            process = commitment.process
        else:
            process = None
        if affects_id:
            affects = EconomicResourceProxy.objects.get(pk=affects_id)
        else:
            affects = None
        if affected_resource_classified_as_id:
            affected_resource_classification = EconomicResourceType.objects.get(pk=affected_resource_classified_as_id)
        elif affects:
            affected_resource_classification = EconomicResourceType.objects.get(pk=affects.resource_type.id)
        elif commitment:
            affected_resource_classification = EconomicResourceType.objects.get(pk=commitment.resource_type.id)
        else:
            raise ValidationError("Must provide a resource classification in either economic event or its commitment")
        if affected_unit_id:
            affected_unit = Unit.objects.get(pk=affected_unit_id)
        elif commitment:
            affected_unit = Unit.objects.get(pk=commitment.unit_of_quantity__id)
        else:
            raise ValidationError("Must provide a unit in either economic event or its commitment")
        if not url:
            url = ""
        if not request_distribution:
            request_distribution = False
        current_location = None
        if resource_current_location_id:
            current_location = Location.objects.get(pk=resource_current_location_id)
        arr = None
        if not affects:
            if create_resource:
                if not resource_note:
                    resource_note = ""
                if not resource_image:
                    resource_image = ""
                if not resource_tracking_identifier:
                    resource_tracking_identifier = ""
                if not resource_url:
                    resource_url = ""
                affects = EconomicResourceProxy(
                    resource_type=affected_resource_classification,
                    quantity=Decimal(affected_numeric_value),
                    photo_url=resource_image,
                    identifier=resource_tracking_identifier,
                    current_location=current_location,
                    notes=resource_note,
                    url=resource_url,
                    created_by=context.user,
                    #location
                )

        economic_event = EconomicEventProxy(
            event_type = event_type,
            process = process,
            from_agent = provider,
            to_agent = receiver,
            resource_type = affected_resource_classification,
            resource = affects,
            quantity = Decimal(affected_numeric_value),
            unit_of_quantity = affected_unit,
            event_date = start,
            description=note,
            context_agent=scope,
            url=url,
            commitment=commitment,
            is_contribution=request_distribution,
            created_by=context.user,
        )

        user_agent = AgentUser.objects.get(user=context.user).agent
        is_authorized = user_agent.is_authorized(object_to_mutate=economic_event)
        if is_authorized:
            economic_event.save_api(user=context.user, create_resource=create_resource)                
            #find the first "owner" type resource-agent role, use it for a relationship so inventory will show up #TODO: make more coherent when VF does so
            if create_resource:
                roles = AgentResourceRoleType.objects.filter(is_owner=True)
                if roles and receiver:
                    owner_role = roles[0]
                    arr = AgentResourceRole(
                        agent=receiver,
                        resource=affects,
                        role=owner_role,
                    )
                    arr.save()
        else:
            raise PermissionDenied('User not authorized to perform this action.')

        return CreateEconomicEvent(economic_event=economic_event)
Пример #5
0
    def mutate(cls, root, args, context, info):
        provider_id = args.get('provider_id')
        receiver_id = args.get('receiver_id')
        affects_id = args.get('affects_id')
        receiver_affects_id = args.get('receiver_affects_id')
        affected_numeric_value = args.get('affected_numeric_value')
        start = args.get('start')
        create_resource = args.get('create_resource', False)
        resource_image = args.get('resource_image')
        resource_note = args.get('resource_note')
        #import pdb; pdb.set_trace()

        if start:
            start = datetime.datetime.strptime(start, '%Y-%m-%d').date()
        else:
            start = datetime.date.today()
        provider = EconomicAgent.objects.get(pk=provider_id)
        receiver = EconomicAgent.objects.get(pk=receiver_id)
        affects = EconomicResourceProxy.objects.get(pk=affects_id)
        receiver_affects = None
        if receiver_affects_id:
            receiver_affects = EconomicResourceProxy.objects.get(
                pk=receiver_affects_id)
        if not receiver_affects:
            if create_resource:
                if not resource_note:
                    resource_note = affects.notes
                if not resource_image:
                    resource_image = affects.image
                receiver_affects = EconomicResourceProxy(
                    resource_type=affects.resource_type,
                    quantity=Decimal(affected_numeric_value),
                    photo_url=resource_image,
                    identifier=affects.identifier,
                    current_location=None,
                    notes=resource_note,
                    url="",
                    created_by=context.user,
                    #location
                )

        transfer = TransferProxy(name="Transfer item",
                                 transfer_date=start,
                                 notes="",
                                 context_agent=provider)
        give_et = EventType.objects.get(name="Give")
        give_event = EconomicEventProxy(
            event_type=give_et,
            process=None,
            from_agent=provider,
            to_agent=receiver,
            resource_type=affects.resource_type,
            resource=affects,
            quantity=Decimal(affected_numeric_value),
            unit_of_quantity=affects.resource_type.unit,
            event_date=start,
            description=resource_note,
            context_agent=provider,
            url="",
            commitment=None,
            is_contribution=False,
        )
        receive_et = EventType.objects.get(name="Receive")
        receive_event = EconomicEventProxy(
            event_type=receive_et,
            process=None,
            from_agent=provider,
            to_agent=receiver,
            resource_type=affects.resource_type,
            quantity=Decimal(affected_numeric_value),
            unit_of_quantity=affects.resource_type.unit,
            event_date=start,
            description=resource_note,
            context_agent=receiver,
            url="",
            commitment=None,
            is_contribution=False,
        )

        #import pdb; pdb.set_trace()
        user_agent = AgentUser.objects.get(user=context.user).agent
        is_authorized = user_agent.is_authorized(object_to_mutate=give_event)
        if is_authorized:
            transfer.save_api()
            give_event.transfer = transfer
            give_event.save_api(user=context.user, create_resource=False)
            if receiver_affects:
                receiver_affects.save()
                receive_event.resource = receiver_affects
            receive_event.transfer = transfer
            receive_event.save_api(user=context.user,
                                   create_resource=create_resource)
            if create_resource:
                roles = AgentResourceRoleType.objects.filter(is_owner=True)
                if roles and receiver:
                    owner_role = roles[0]
                    arr = AgentResourceRole(
                        agent=receiver,
                        resource=receiver_affects,
                        role=owner_role,
                    )
                    arr.save()
        else:
            raise PermissionDenied(
                'User not authorized to perform this action.')

        return CreateTransfer(transfer=transfer)
Пример #6
0
    def include_blockchain_tx_as_event(self, agent, resource):
        if 'faircoin' not in settings.INSTALLED_APPS:
            return []

        redistribution_date = datetime.date(2017, 7, 18)
        redistribution_txs = (
            "009e53988153fbd13f458b0881734ff385f749d020e3ee7360e1ca9ed8ff0490",
            "12bc2f2f699dc75d1cbe0dd17597cd12649bb7698e179c752f22b70a0a3c98d4",
            "218c72570d992dc33456c148c1e26641a4b985e83054a5cae227ba71ee979ea3",
            "231c406153b1e5e75d47aade9dda5a2b806067f577d7d5b8e9574f19bb988d75",
            "39a808302aed7ff4cd68d8d2deb775502ae99c5eb207c65a9712a7125d5329c8",
            "634f11e66604bf38eafec4b0d7aeebf630073cfcb83c74a58fdd5e0772e88305",
            "6abd3941e457d3e68f42f7619eadc19767ad0724009f1f98caffde2403656d8f",
            "7229c6eaca9b2bd55edd10410fd3b223a4b700d53591ddb97690762f52526fdd",
            "7484be3956691dae231d8c4dafb62104dbf805e541b1cc1c572a06b65d9a3f18"
            "7927a0fbf3b6533f282815f49154dae94acde9e347e7a53f84b4e54d276b9792",
            "b31489979bfd28faa2fadcec559a9e5d10413048a5ad80bc7d3c46901a1ff362",
            "b46352e1b87783fbf9ed771d722c08035cfc1c7c4e1509d4654ae195c700af91",
            "d26e3759806a8ae53ae81a643786d2ad34705f8d792330d9bf8bdd306658a982",
        )

        faircoin_address = str(resource.faircoin_address.address)
        tx_in_blockchain = faircoin_utils.get_address_history(faircoin_address)
        if not tx_in_blockchain:  # Something wrong in daemon or network.
            return []

        event_list = EconomicEvent.objects.filter(
            resource=resource,
            event_date__gt=redistribution_date,
            faircoin_transaction__tx_hash__isnull=False)

        tx_in_ocp = []
        for event in event_list:
            tx_in_ocp.append(str(event.faircoin_transaction.tx_hash))

        tx_included = []
        for tx in tx_in_blockchain:
            if str(tx[0]) not in tx_in_ocp and str(
                    tx[0]) not in redistribution_txs:
                amount, time = faircoin_utils.get_transaction_info(
                    str(tx[0]), faircoin_address)
                confirmations, timestamp = faircoin_utils.get_confirmations(
                    str(tx[0]))
                if amount and confirmations:
                    qty = Decimal(amount) / Decimal(100000000)
                    date = datetime.date.fromtimestamp(time)
                    tt = ExchangeService.faircoin_incoming_transfer_type()
                    xt = tt.exchange_type
                    exchange = Exchange(
                        exchange_type=xt,
                        use_case=xt.use_case,
                        name="Receive Faircoins",
                        start_date=date,
                    )
                    exchange.save()
                    transfer = Transfer(
                        transfer_type=tt,
                        exchange=exchange,
                        transfer_date=date,
                        name="Receive Faircoins",
                    )
                    transfer.save()

                    et_receive = EventType.objects.get(name="Receive")
                    state = "external"
                    if confirmations > 0:
                        state = "broadcast"
                    if confirmations > 2:
                        state = "confirmed"

                    event = EconomicEvent(event_type=et_receive,
                                          event_date=date,
                                          to_agent=agent,
                                          resource_type=resource.resource_type,
                                          resource=resource,
                                          quantity=qty,
                                          transfer=transfer,
                                          event_reference=faircoin_address)
                    event.save()
                    fairtx = FaircoinTransaction(
                        event=event,
                        tx_hash=str(tx[0]),
                        tx_state=state,
                        to_address=faircoin_address,
                    )
                    fairtx.save()
                    tx_included.append(str(tx[0]))
        return tx_included
Пример #7
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
Пример #8
0
class NoThrowTransferText(TestCase):
    """
    Tests fix for the issue wherein fetching a transfer's event text would throw
    """

    fixtures = ['verdun']

    def setUp(self):
        self.transfer = Transfer(
            created_date=date.today(),
            slug='test-xfer-xfer',
            transfer_date=date.today()
        )
        self.transfer.save()

        self.et_give = EventType.objects.get(name='Give') or EventType(
            name='Give',
            label='do not care',
            resource_effect='=',    # don't care
            slug='test-xfer-et-give'
        )
        self.et_give.save()

        self.et_recv = EventType.objects.get(name='Receive') or EventType(
            name='Receive',
            label='do not care',
            resource_effect='=',    # don't care
            slug='test-xfer-et-recv'
        )
        self.et_recv.save()

        self.rt_spam = EconomicResourceType(
            name='Spam',
            slug='test-xfer-rt-spam'
        )
        self.rt_spam.save()

        self.rt_eggs = EconomicResourceType(
            name='Eggs',
            slug='test-xfer-rt-eggs'
        )
        self.rt_eggs.save()

        # both events should have null agents so that there is no extra
        # assignment to give_text or receive_text
        self.ev_give = EconomicEvent(
            event_type=self.et_give,
            event_date=date.today(),
            resource_type=self.rt_spam,
            quantity=Decimal('1.0'),
            transfer=self.transfer,
            slug='test-xfer-ev-give'
        )
        self.ev_give.save()

        self.ev_recv = EconomicEvent(
            event_type=self.et_recv,
            event_date=date.today(),
            resource_type=self.rt_eggs,
            quantity=Decimal('1.0'),
            transfer=self.transfer,
            slug='test-xfer-ev-recv'
        )
        self.ev_recv.save()

    def tearDown(self):
        self.ev_recv.delete()
        self.ev_give.delete()
        self.rt_eggs.delete()
        self.rt_spam.delete()
        self.et_give.delete()
        self.et_recv.delete()
        # why that happens, I don't know, but hey, the test passes anyway.
        #self.transfer.delete()

    def test_xfer_throw(self):
        try:
            dont_care = self.transfer.event_text()
        except Exception as e:
            raise AssertionError((_('Transfer raised an exception: ' + str(e)),))
Пример #9
0
    def send_faircoins(self,
                       from_agent,
                       recipient,
                       qty,
                       resource,
                       notes='',
                       minus_fee=False):
        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")
        if to_resource:
            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,
            amount=qty,
            minus_fee=minus_fee,
        )
        fairtx.save()

        if to_resource:
            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,
                amount=qty,
                minus_fee=minus_fee,
            )
            fairtx.save()

        return exchange
Пример #10
0
    def include_blockchain_tx_as_event(self, agent, resource):
        if 'faircoin' not in settings.INSTALLED_APPS:
            return []

        redistribution_date = datetime.date(2017, 7, 18)
        redistribution_txs = (
            "009e53988153fbd13f458b0881734ff385f749d020e3ee7360e1ca9ed8ff0490",
            "12bc2f2f699dc75d1cbe0dd17597cd12649bb7698e179c752f22b70a0a3c98d4",
            "218c72570d992dc33456c148c1e26641a4b985e83054a5cae227ba71ee979ea3",
            "231c406153b1e5e75d47aade9dda5a2b806067f577d7d5b8e9574f19bb988d75",
            "39a808302aed7ff4cd68d8d2deb775502ae99c5eb207c65a9712a7125d5329c8",
            "634f11e66604bf38eafec4b0d7aeebf630073cfcb83c74a58fdd5e0772e88305",
            "6abd3941e457d3e68f42f7619eadc19767ad0724009f1f98caffde2403656d8f",
            "7229c6eaca9b2bd55edd10410fd3b223a4b700d53591ddb97690762f52526fdd",
            "7484be3956691dae231d8c4dafb62104dbf805e541b1cc1c572a06b65d9a3f18"
            "7927a0fbf3b6533f282815f49154dae94acde9e347e7a53f84b4e54d276b9792",
            "b31489979bfd28faa2fadcec559a9e5d10413048a5ad80bc7d3c46901a1ff362",
            "b46352e1b87783fbf9ed771d722c08035cfc1c7c4e1509d4654ae195c700af91",
            "d26e3759806a8ae53ae81a643786d2ad34705f8d792330d9bf8bdd306658a982",
        )

        faircoin_address = str(resource.faircoin_address.address)
        tx_in_blockchain = faircoin_utils.get_address_history(faircoin_address)
        if not tx_in_blockchain:  # Something wrong in daemon or network.
            return []

        event_list = EconomicEvent.objects.filter(
            Q(resource=resource)
            | Q(faircoin_transaction__to_address=resource.faircoin_address.
                address)).annotate(numev=Count("transfer__events")).exclude(
                    numev__gt=1, event_type__name="Receive")
        event_list = event_list.filter(
            event_date__gt=redistribution_date,
            faircoin_transaction__tx_hash__isnull=False)
        event_list2 = EconomicEvent.objects.filter(  #
            resource=resource,
            event_date__gt=redistribution_date,
            faircoin_transaction__tx_hash__isnull=False)
        if event_list2 and not len(event_list) == len(event_list2):
            print "Different event_list counts! ;; evt_list:" + str(
                len(event_list)) + " ev_list2:" + str(len(event_list2))
            loger.info("Different event_list counts! ;; evt_list:" +
                       str(len(event_list)) + " ev_list2:" +
                       str(len(event_list2)))

        tx_in_ocp = []
        for event in event_list:
            tx_in_ocp.append(str(event.faircoin_transaction.tx_hash))

        tx_included = []
        for tx in tx_in_blockchain:
            if str(tx[0]) not in tx_in_ocp and str(
                    tx[0]) not in redistribution_txs:
                amount, time = faircoin_utils.get_transaction_info(
                    str(tx[0]), faircoin_address)
                confirmations, timestamp = faircoin_utils.get_confirmations(
                    str(tx[0]))
                if amount and confirmations:
                    qty = Decimal(amount) / Decimal(100000000)
                    date = datetime.date.fromtimestamp(time)
                    tt = ExchangeService.faircoin_incoming_transfer_type()
                    xt = tt.exchange_type
                    jn_req = event = fairtx = None
                    if hasattr(resource.owner(),
                               'project') and resource.owner().project:
                        for req in resource.owner().project.join_requests.all(
                        ):
                            #print  ";; req:"+str(req)
                            if req.exchange and req.agent.faircoin_resource():
                                txpay = req.exchange.txpay()
                                if txpay:
                                    for ev in txpay.events.all():
                                        if ev.to_agent == resource.owner(
                                        ) and ev.faircoin_transaction and not ev.faircoin_transaction.tx_hash:
                                            jn_req = req
                                            exchange = req.exchange
                                            transfer = txpay
                                            event = ev
                                            fairtx = ev.faircoin_transaction
                                            print ";;; found jn_req:" + str(
                                                jn_req.id) + " ev:" + str(
                                                    ev.id) + " fairtx:" + str(
                                                        fairtx.id)
                                            loger.info(";;; found jn_req:" +
                                                       str(jn_req.id) +
                                                       " ev:" + str(ev.id) +
                                                       " fairtx:" +
                                                       str(fairtx.id))
                                            break
                                        #else:
                                        #    print ";; skip ev "
                                else:
                                    print ";;; not found txpay in req:" + str(
                                        req.id)
                                    loger.info(";;; not found txpay in req:" +
                                               str(req.id))
                            else:
                                print ";;; not req.exchange ?"
                            if jn_req:
                                #print ";; found"
                                break
                    else:
                        print ";;; no project"
                    for req in resource.owner().project_join_requests.all():
                        if req.exchange:  #project.shares_account_type() == resource.resource_type:
                            for tf in req.exchange.transfers.all():
                                for ev in tf.events.all():
                                    if hasattr(ev, 'faircoin_transaction'
                                               ) and ev.faircoin_transaction:
                                        if ev.faircoin_transaction.to_address == resource.faircoin_address.address:
                                            jn_req = req
                                            exchange = req.exchange
                                            transfer = tf  #req.exchange.txpay()
                                            event = ev  #transfer.events.get(faircoin_transaction__tx_hash__isnull=True)
                                            fairtx = ev.faircoin_transaction
                                            print ";;; found jr:" + str(
                                                req.id) + " pro:" + str(
                                                    req.project.agent
                                                ) + " ev:" + str(
                                                    ev.id) + " tf:" + str(
                                                        tf.id) + " ex:" + str(
                                                            exchange.id)
                                            loger.info(";;; found jr:" +
                                                       str(req.id) + " pro:" +
                                                       str(req.project.agent) +
                                                       " ev:" + str(ev.id) +
                                                       " tf:" + str(tf.id) +
                                                       " ex:" +
                                                       str(exchange.id))
                                            break
                    if not jn_req:
                        exchange = Exchange(
                            exchange_type=xt,
                            use_case=xt.use_case,
                            name="Receive Faircoins",
                            start_date=date,
                        )
                        exchange.save()
                        transfer = Transfer(
                            transfer_type=tt,
                            exchange=exchange,
                            transfer_date=date,
                            name="Receive Faircoins",
                        )
                        transfer.save()

                    et_receive = EventType.objects.get(name="Receive")
                    state = "external"
                    if confirmations > 0:
                        state = "broadcast"
                    if confirmations > 2:
                        state = "confirmed"

                    if not event:
                        event = EconomicEvent(
                            event_type=et_receive,
                            event_date=date,
                            to_agent=agent,
                            resource_type=resource.resource_type,
                            resource=resource,
                            quantity=qty,
                            transfer=transfer,
                            event_reference=faircoin_address)
                    else:
                        event.quantity = qty
                        event.event_reference = faircoin_address
                        #event.event_date = date
                    event.save()
                    if not fairtx:
                        fairtx = FaircoinTransaction(
                            event=event,
                            tx_hash=str(tx[0]),
                            tx_state=state,
                            to_address=faircoin_address,
                            amount=qty,
                            minus_fee=False,
                        )
                    else:
                        fairtx.tx_state = state
                        fairtx.tx_hash = str(tx[0])
                        fairtx.amount = qty
                    fairtx.save()
                    tx_included.append(str(tx[0]))
        return tx_included
Пример #11
0
class NoThrowTransferText(TestCase):
    def setUp(self):
        self.transfer = Transfer(created_date=date.today(),
                                 slug='test-xfer-xfer',
                                 transfer_date=date.today())
        self.transfer.save()

        self.et_give = EventType(
            name='Give',
            label='do not care',
            resource_effect='=',  # don't care
            slug='test-xfer-et-give')
        self.et_give.save()

        self.et_recv = EventType(
            name='Receive',
            label='do not care',
            resource_effect='=',  # don't care
            slug='test-xfer-et-recv')
        self.et_recv.save()

        self.rt_spam = EconomicResourceType(name='Spam',
                                            slug='test-xfer-rt-spam')
        self.rt_spam.save()

        self.rt_eggs = EconomicResourceType(name='Eggs',
                                            slug='test-xfer-rt-eggs')
        self.rt_eggs.save()

        # both events should have null agents so that there is no extra
        # assignment to give_text or receive_text
        self.ev_give = EconomicEvent(event_type=self.et_give,
                                     event_date=date.today(),
                                     resource_type=self.rt_spam,
                                     quantity=Decimal('1.0'),
                                     transfer=self.transfer,
                                     slug='test-xfer-ev-give')
        self.ev_give.save()

        self.ev_recv = EconomicEvent(event_type=self.et_recv,
                                     event_date=date.today(),
                                     resource_type=self.rt_eggs,
                                     quantity=Decimal('1.0'),
                                     transfer=self.transfer,
                                     slug='test-xfer-ev-recv')
        self.ev_recv.save()

    def tearDown(self):
        self.ev_recv.delete()
        self.ev_give.delete()
        self.rt_eggs.delete()
        self.rt_spam.delete()
        self.et_give.delete()
        self.et_recv.delete()
        self.transfer.delete()

    def test_xfer_throw(self):
        try:
            dont_care = self.transfer.event_text()
        except Exception as e:
            raise AssertionError(
                (_('Transfer raised an exception: ' + str(e)), ))
Пример #12
0
    def send_faircoins(self, from_agent, recipient, qty, resource, notes=None):
        to_resources = EconomicResource.objects.filter(
            digital_currency_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")
        if to_resource:
            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",
            )
            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",
            )
            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,
            digital_currency_tx_state=state,
            quantity=qty,
            transfer=transfer,
            event_reference=recipient,
        )
        event.save()
        if to_resource:
            # network_fee is subtracted from quantity
            # so quantity is correct for the giving event
            # but receiving event will get quantity - network_fee
            from valuenetwork.valueaccounting.faircoin_utils import network_fee
            quantity = qty - Decimal(float(network_fee()) / 1.e6)
            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,
                digital_currency_tx_state=state,
                quantity=quantity,
                transfer=transfer,
                event_reference=recipient,
            )
            event.save()
        return exchange
Пример #13
0
    def include_blockchain_tx_as_event(self, agent, resource):
        if 'faircoin' not in settings.INSTALLED_APPS:
            return []

        redistribution_date = datetime.date(2017, 7, 18)
        redistribution_txs = (
            "009e53988153fbd13f458b0881734ff385f749d020e3ee7360e1ca9ed8ff0490",
            "12bc2f2f699dc75d1cbe0dd17597cd12649bb7698e179c752f22b70a0a3c98d4",
            "218c72570d992dc33456c148c1e26641a4b985e83054a5cae227ba71ee979ea3",
            "231c406153b1e5e75d47aade9dda5a2b806067f577d7d5b8e9574f19bb988d75",
            "39a808302aed7ff4cd68d8d2deb775502ae99c5eb207c65a9712a7125d5329c8",
            "634f11e66604bf38eafec4b0d7aeebf630073cfcb83c74a58fdd5e0772e88305",
            "6abd3941e457d3e68f42f7619eadc19767ad0724009f1f98caffde2403656d8f",
            "7229c6eaca9b2bd55edd10410fd3b223a4b700d53591ddb97690762f52526fdd",
            "7484be3956691dae231d8c4dafb62104dbf805e541b1cc1c572a06b65d9a3f18"
            "7927a0fbf3b6533f282815f49154dae94acde9e347e7a53f84b4e54d276b9792",
            "b31489979bfd28faa2fadcec559a9e5d10413048a5ad80bc7d3c46901a1ff362",
            "b46352e1b87783fbf9ed771d722c08035cfc1c7c4e1509d4654ae195c700af91",
            "d26e3759806a8ae53ae81a643786d2ad34705f8d792330d9bf8bdd306658a982",
        )

        faircoin_address = str(resource.faircoin_address.address)
        tx_in_blockchain = faircoin_utils.get_address_history(faircoin_address)
        if not tx_in_blockchain: # Something wrong in daemon or network.
            return []

        event_list = EconomicEvent.objects.filter(
            resource=resource,
            event_date__gt=redistribution_date,
            faircoin_transaction__tx_hash__isnull=False
        )

        tx_in_ocp = []
        for event in event_list:
            tx_in_ocp.append(str(event.faircoin_transaction.tx_hash))

        tx_included = []
        for tx in tx_in_blockchain:
            if str(tx[0]) not in tx_in_ocp and str(tx[0]) not in redistribution_txs:
                amount, time = faircoin_utils.get_transaction_info(str(tx[0]), faircoin_address)
                confirmations, timestamp = faircoin_utils.get_confirmations(str(tx[0]))
                if amount and confirmations:
                    qty=Decimal(amount)/Decimal(100000000)
                    date = datetime.date.fromtimestamp(time)
                    tt = ExchangeService.faircoin_incoming_transfer_type()
                    xt = tt.exchange_type
                    exchange = Exchange(
                        exchange_type=xt,
                        use_case=xt.use_case,
                        name="Receive Faircoins",
                        start_date=date,
                    )
                    exchange.save()
                    transfer = Transfer(
                        transfer_type=tt,
                        exchange=exchange,
                        transfer_date=date,
                        name="Receive Faircoins",
                    )
                    transfer.save()

                    et_receive = EventType.objects.get(name="Receive")
                    state = "external"
                    if confirmations > 0:
                        state = "broadcast"
                    if confirmations > 2:
                        state = "confirmed"

                    event = EconomicEvent(
                        event_type=et_receive,
                        event_date=date,
                        to_agent=agent,
                        resource_type=resource.resource_type,
                        resource=resource,
                        quantity=qty,
                        transfer=transfer,
                        event_reference=faircoin_address
                    )
                    event.save()
                    fairtx = FaircoinTransaction(
                        event=event,
                        tx_hash=str(tx[0]),
                        tx_state=state,
                        to_address=faircoin_address,
                    )
                    fairtx.save()
                    tx_included.append(str(tx[0]))
        return tx_included
Пример #14
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
Пример #15
0
    def send_faircoins(self, from_agent, recipient, qty, resource, notes='', minus_fee=False):
        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")
        if to_resource:
            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,
            amount=qty,
            minus_fee=minus_fee,
        )
        fairtx.save()

        if to_resource:
            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,
                amount=qty,
                minus_fee=minus_fee,
            )
            fairtx.save()

        return exchange