How to use the futures.models.Wallet function in futures

To help you get started, we’ve selected a few futures examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github domino14 / Webolith / djAerolith / futures / tests.py View on Github external
def test_simultaneous_sell_short(self):
        """
            Can't short-sell and regular sell the same shares simultaneously.
        """
        user = User.objects.get(pk=1)
        wallet = Wallet.objects.get(user=user)
        wallet.shares_owned = """{"1": 3}"""
        future = Future.objects.get(pk=1)
        response = process_order(4, 200, wallet, 'sell', future)
        # We only own 3 but are trying to sell 4.
        self.assertContains(response, 'two separate orders', status_code=400)
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
def wallet_response(user):
    if not user.is_authenticated():
        return None
    try:
        wallet = Wallet.objects.get(user=user)
    except Wallet.DoesNotExist:
        wallet = None
    if not wallet:
        resp = None
    else:
        resp = {'points': wallet.points,
                'frozen': wallet.frozen,
                'id': wallet.pk,
                'shares_owned': json.loads(wallet.shares_owned)}
    return resp
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
def execute_transfer(transfer):
    """
        Executes a single transfer. This needs to be an atomic operation.
    """
    # Lock the entire wallet. This is expensive but I can't think of a
    # better way.
    lonelock(Wallet, 0)
    buyer_wallet = Wallet.objects.get(user__pk=transfer['buyer'])
    seller_wallet = Wallet.objects.get(user__pk=transfer['seller'])
    points = transfer['points']
    shares = transfer['share_quantity']
    future = Future.objects.get(pk=transfer['share_type'])
    buyer_owned_shares = json.loads(buyer_wallet.shares_owned)
    seller_owned_shares = json.loads(seller_wallet.shares_owned)

    future_key = '%s' % future.pk
    old_buyer_owned = buyer_owned_shares.get(future_key, 0)
    buyer_owned_shares[future_key] = (old_buyer_owned + shares)
    seller_owned_shares[future_key] = (
        seller_owned_shares.get(future_key, 0) - shares)

    # Transfer some points.
    buyer_wallet.points -= points
    seller_wallet.points += points
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
def wallet(request):
    if request.method == 'POST':
        # Set up a new wallet.
        if not request.user.is_authenticated():
            return response("", status=403)
        try:
            wallet = Wallet.objects.get(user=request.user)
            return response("You already have a wallet.",
                            status=403)
        except Wallet.DoesNotExist:
            pass
        wallet = Wallet(user=request.user)
        wallet.points = 100000
        wallet.frozen = 0
        wallet.shares_owned = '{}'
        wallet.save()
        return response(wallet_response(request.user))
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
def execute_transfer(transfer):
    """
        Executes a single transfer. This needs to be an atomic operation.
    """
    # Lock the entire wallet. This is expensive but I can't think of a
    # better way.
    lonelock(Wallet, 0)
    buyer_wallet = Wallet.objects.get(user__pk=transfer['buyer'])
    seller_wallet = Wallet.objects.get(user__pk=transfer['seller'])
    points = transfer['points']
    shares = transfer['share_quantity']
    future = Future.objects.get(pk=transfer['share_type'])
    buyer_owned_shares = json.loads(buyer_wallet.shares_owned)
    seller_owned_shares = json.loads(seller_wallet.shares_owned)

    future_key = '%s' % future.pk
    old_buyer_owned = buyer_owned_shares.get(future_key, 0)
    buyer_owned_shares[future_key] = (old_buyer_owned + shares)
    seller_owned_shares[future_key] = (
        seller_owned_shares.get(future_key, 0) - shares)

    # Transfer some points.
    buyer_wallet.points -= points
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
if old_buyer_owned < 0:
        buyer_wallet.frozen -= (shares * SETTLEMENT_PRICE)
        if buyer_wallet.frozen < 0:
            buyer_wallet.frozen = 0

    buyer_wallet.shares_owned = json.dumps(buyer_owned_shares)
    seller_wallet.shares_owned = json.dumps(seller_owned_shares)
    buyer_wallet.save()
    seller_wallet.save()
    future.volume += shares
    future.last_buy = transfer['price']
    future.save()
    logger.debug('Updated future: %s', future)
    logger.debug('Updated buyer wallet: %s', buyer_wallet)
    logger.debug('Updated seller wallet: %s', seller_wallet)
    loneunlock(Wallet, 0)
github domino14 / Webolith / djAerolith / futures / views.py View on Github external
def wallet(request):
    if request.method == 'POST':
        # Set up a new wallet.
        if not request.user.is_authenticated():
            return response("", status=403)
        try:
            wallet = Wallet.objects.get(user=request.user)
            return response("You already have a wallet.",
                            status=403)
        except Wallet.DoesNotExist:
            pass
        wallet = Wallet(user=request.user)
        wallet.points = 100000
        wallet.frozen = 0
        wallet.shares_owned = '{}'
        wallet.save()
        return response(wallet_response(request.user))