Beispiel #1
0
def get_pending_transactions(app_user, token_type, page_size, cursor):
    # type: (users.User, unicode, long, unicode) -> tuple[list[ThreeFoldPendingTransaction], ndb.Cursor, bool]
    if token_type:
        validate_token_type(token_type)
        return ThreeFoldPendingTransaction.list_by_user_and_token_type(app_user, token_type) \
            .fetch_page(page_size, start_cursor=ndb.Cursor(urlsafe=cursor))
    else:
        return ThreeFoldPendingTransaction.list_by_user(app_user) \
            .fetch_page(page_size, start_cursor=ndb.Cursor(urlsafe=cursor))
Beispiel #2
0
def _sync_transactions():
    def trans_finish():
        bh = ThreeFoldBlockHeight.get_block_height()
        if not bh.updating:
            return
        bh.timestamp = now()
        bh.updating = False
        bh.put()

    qry = ThreeFoldPendingTransaction.query() \
        .filter(ThreeFoldPendingTransaction.synced == False) \
        .order(-ThreeFoldPendingTransaction.timestamp)  # NOQA

    pt_keys = [pt.key for pt in qry.fetch(100)]
    if len(pt_keys) > 0:
        _migrate_pending_transactions(pt_keys)
    else:
        ndb.transaction(trans_finish, xg=True)
Beispiel #3
0
def sync_transactions():
    unprocessed_transactions = ThreeFoldPendingTransaction.count_pending()

    def trans():
        bh = ThreeFoldBlockHeight.get_block_height()
        if bh.updating:
            logging.debug("sync_transactions was already updating...")
            return
        if unprocessed_transactions > 0:
            logging.debug("sync_transactions need to sync %s transactions",
                          unprocessed_transactions)
            bh.updating = True
            bh.put()
            deferred.defer(_sync_transactions, _transactional=True)
            return
        logging.debug(
            "sync_transactions not needed block height was already %s",
            bh.height)
        return

    ndb.transaction(trans)
Beispiel #4
0
def transfer_genesis_coins_to_user(app_user,
                                   token_type,
                                   amount,
                                   memo=None,
                                   epoch=0):
    validate_token_type(token_type)
    if amount <= 0:
        raise HttpBadRequestException('invalid_amount')
    # Validate that this user has a profile
    get_profile(get_iyo_username(app_user))
    if epoch > 0:
        date_signed = datetime.utcfromtimestamp(epoch)
    else:
        date_signed = datetime.now()

    if TokenType.A == token_type:
        token = TOKEN_TFT
        unlock_timestamps = [0]
        unlock_amounts = [amount]

    elif TokenType.B == token_type:
        token = TOKEN_TFT
        d = date_signed + relativedelta(months=6)
        unlock_timestamps = [get_epoch_from_datetime(d)]
        unlock_amounts = [amount]

    elif TokenType.C == token_type:
        token = TOKEN_TFT
        unlock_timestamps = []
        unlock_amounts = []
        a = amount / 48
        for i in xrange(0, 39):
            d = date_signed + relativedelta(months=48 - i)
            unlock_timestamps = [get_epoch_from_datetime(d)
                                 ] + unlock_timestamps
            unlock_amounts = [a] + unlock_amounts

        d = date_signed + relativedelta(months=9)
        unlock_timestamps = [get_epoch_from_datetime(d)] + unlock_timestamps
        unlock_amounts = [amount - sum(unlock_amounts)] + unlock_amounts

    elif TokenType.D == token_type:
        token = TOKEN_TFT_CONTRIBUTOR
        unlock_timestamps = [0]
        unlock_amounts = [amount]

    elif TokenType.I == token_type:
        token = TOKEN_ITFT
        unlock_timestamps = [0]
        unlock_amounts = [amount]

    else:
        raise Exception(u"Unknown token type")

    key = ThreeFoldWallet.create_key(app_user)
    wallet = key.get()
    if not wallet:
        wallet = ThreeFoldWallet(key=key, tokens=[])

    if token not in wallet.tokens:
        wallet.tokens.append(token)

    if unlock_timestamps[0] > 0 and (
            not wallet.next_unlock_timestamp
            or unlock_timestamps[0] < wallet.next_unlock_timestamp):
        wallet.next_unlock_timestamp = unlock_timestamps[0]

    wallet.put()

    transaction_id = unicode(uuid.uuid4())
    pt = ThreeFoldPendingTransaction(
        key=ThreeFoldPendingTransaction.create_key(transaction_id),
        timestamp=now(),
        unlock_timestamps=unlock_timestamps,
        unlock_amounts=unlock_amounts,
        token=token,
        token_type=token_type,
        amount=amount,
        memo=memo,
        app_users=[app_user],
        from_user=None,
        to_user=app_user,
        synced=False,
        synced_status=ThreeFoldPendingTransaction.STATUS_PENDING)
    pt.put()
    return pt
Beispiel #5
0
def get_transaction_of_type_pending(app_user, token):
    return ThreeFoldPendingTransaction.list_unsynced_by_user(app_user, token)
Beispiel #6
0
    def trans():
        pt_key = ThreeFoldPendingTransaction.create_key(data.id)
        pt = pt_key.get()
        if not pt:
            pt = ThreeFoldPendingTransaction(key=pt_key,
                                             timestamp=now())
        elif pt.synced:
            return pt.synced_status

        pt.unlock_timestamps = [0]
        pt.unlock_amounts = [data.amount]
        pt.token = token
        pt.token_type = token_type
        pt.amount = data.amount
        pt.memo = data.memo
        pt.app_users = [from_user, to_user]
        pt.from_user = from_user
        pt.to_user = to_user
        pt.synced = False
        pt.synced_status = ThreeFoldPendingTransaction.STATUS_PENDING
        pt.put()

        return pt.synced_status