Ejemplo n.º 1
0
    def update_tx_status(self):
        self.tx_status, self.tx_time = get_tx_status(self.txid, self.network, self.created_on)

        # Exit if transaction not mined, otherwise continue
        if self.tx_status != 'success':
            return bool(self.tx_status)

        record_ptoken_activity(self.event, self.ptoken, self.profile, self.metadata)
        self.ptoken.update_token_status()
Ejemplo n.º 2
0
    def update_tx_status(self):
        self.tx_status, self.tx_time = get_tx_status(self.txid, self.network, self.created_on)

        if self.tx_status == 'success':
            metadata = {
                'purchase': self.id,
                'value_in_token': float(self.amount),
                'token_name': self.ptoken.token_symbol,
                'from_user': self.ptoken.token_owner_profile.handle,
                'holder_user': self.ptoken.token_owner_profile.handle
            }

            record_ptoken_activity('buy_ptoken', self.ptoken, self.token_holder_profile, metadata)

        self.ptoken.update_token_status()
        self.ptoken.update_user_balance(self.token_holder_profile, self.token_holder_address)
        return bool(self.tx_status)
Ejemplo n.º 3
0
    def update_tx_status(self):
        self.tx_status, self.tx_time = get_tx_status(self.txid, self.network, self.created_on)

        if self.redemption_state == 'waiting_complete':
            self.ptoken.update_token_status()
            self.ptoken.update_user_balance(self.redemption_requester, self.redemption_requester_address)
            if self.tx_status == 'success':
                metadata = {'redemption': self.id}
                record_ptoken_activity('complete_redemption_ptoken', self.ptoken, self.redemption_requester, metadata, self)
                send_ptoken_redemption_complete_for_requester(self.redemption_requester, self.ptoken, self)
                send_ptoken_redemption_complete_for_owner(self.redemption_requester, self.ptoken, self)
                self.redemption_state = 'completed'

            elif self.tx_status in ['error', 'unknown', 'dropped']:
                self.redemption_state = 'accepted'

        return bool(self.tx_status)
Ejemplo n.º 4
0
    def update_tx_status(self):
        self.tx_status, self.tx_time = get_tx_status(self.txid, self.network, self.created_on)

        # Exit if transaction not mined, otherwise continue
        if (self.tx_status != 'success'):
            return bool(self.tx_status)

        # Get token address from event logs
        if (self.token_address == "0x0"):
            web3 = get_web3(self.network)
            receipt = web3.eth.getTransactionReceipt(self.txid)
            contract = web3.eth.contract(Web3.toChecksumAddress(FACTORY_ADDRESS), abi=ptoken_factory_abi)
            logs = contract.events.NewPToken().processReceipt(receipt)
            self.token_address = logs[0].args.token

            record_ptoken_activity('create_ptoken', self, self.token_owner_profile)
            send_personal_token_created(self.token_owner_profile, self)

        self.update_token_status()
Ejemplo n.º 5
0
def ptoken_redemption(request, redemption_id):
    """Change the state for given redemption"""
    redemption = get_object_or_404(RedemptionToken, id=redemption_id)
    user = request.user if request.user.is_authenticated else None
    send_notifications = None

    if request.method == 'POST':
        kwargs = {}
        metadata = {}
        if not user:
            return JsonResponse(
                {
                    'error':
                    _('You must be authenticated via github to use this feature!'
                      )
                },
                status=401)

        event_name = request.POST.get('event_name')

        if event_name == 'accept_redemption_ptoken':
            if user.profile != redemption.ptoken.token_owner_profile:
                return JsonResponse(
                    {
                        'error':
                        _('You don\'t have permissions on the current redemption!'
                          )
                    },
                    status=401)
            kwargs['redemption_accepted'] = datetime.now()
            kwargs['redemption_state'] = 'accepted'
            metadata['redemption'] = redemption.id

            send_notifications = lambda: send_ptoken_redemption_accepted(
                redemption.redemption_requester, redemption.ptoken, redemption)
        if event_name == 'denies_redemption_ptoken':
            if user.profile != redemption.ptoken.token_owner_profile and user.profile != redemption.redemption_requester:
                return JsonResponse(
                    {
                        'error':
                        _('You don\'t have permissions on the current redemption!'
                          )
                    },
                    status=401)

            if user.profile == redemption.ptoken.token_owner_profile and redemption.redemption_state != 'accepted':
                kwargs['redemption_state'] = 'denied'
                send_notifications = lambda: send_ptoken_redemption_rejected(
                    redemption.redemption_requester, redemption.ptoken,
                    redemption)
            else:
                kwargs['redemption_state'] = 'cancelled'
                profile = redemption.redemption_requester if redemption.redemption_requester != user.profile else redemption.ptoken.token_owner_profile
                send_notifications = lambda: send_ptoken_redemption_cancelled(
                    profile, redemption.ptoken, redemption)

            kwargs['canceller'] = user.profile
            metadata['redemption'] = redemption.id
            metadata[
                'redemption_requester_name'] = redemption.redemption_requester.handle
            metadata['redemption_state'] = kwargs['redemption_state']
        if event_name == 'complete_redemption_ptoken':
            if user.profile != redemption.redemption_requester:
                return JsonResponse(
                    {
                        'error':
                        _('You don\'t have permissions on the current redemption!'
                          )
                    },
                    status=401)
            web3_created = request.POST.get('web3_created')
            if web3_created:
                try:
                    kwargs['web3_created'] = dateutil.parser.isoparse(
                        web3_created)
                except ValueError:
                    return JsonResponse(
                        {'error': _('Bad date format in web3_created')},
                        status=401)
            else:
                kwargs['web3_created'] = datetime.now()

            kwargs['redemption_state'] = 'waiting_complete'
            kwargs['tx_status'] = request.POST.get('tx_status')
            kwargs['txid'] = request.POST.get('txid')
            kwargs['redemption_requester_address'] = request.POST.get(
                'address')

        if kwargs:
            RedemptionToken.objects.filter(pk=redemption.id).update(**kwargs)

            if metadata:
                record_ptoken_activity(event_name, redemption.ptoken,
                                       user.profile, metadata, redemption)

            if send_notifications:
                redemption.refresh_from_db()
                send_notifications()

    return JsonResponse({
        'error': False,
        'data': redemption.to_standard_dict()
    })