def login(): # Handles a user login if "user" in session: flash("You are already logged in", "warning") return redirect("/trade") if request.method == 'POST': session["widget"] = 859 user = User() success_flag = user.login() if success_flag: session.permanent = True user = session['user'] session["username"] = udb.get_user_name(user) session["balance"] = udb.get_user_balance(user) session["id"] = udb.get_user_id(user) session["starting_balance"] = udb.get_user_starting_balance(user) session["currency"] = udb.get_user_currency(user) session["wallet"] = udb.get_user_wallet(user) flash('Your were logged in successfully!', "success") return redirect('/trade') else: flash("Log in error - Bad username/password", 'danger') return redirect('/login') # Default GET route return render_template('login.html')
def calculate_profit_leaderboard(user): balance = udb.get_user_balance(user) currency = udb.get_user_currency(user).lower() starting_balance = udb.get_user_starting_balance(user) wallet = udb.get_user_wallet(user) coin_balance = 0 for coin in wallet: temp_coin_balance = wallet[coin] temp_coin_balance *= cg.get_price( ids=coin, vs_currencies=currency).get(coin).get(currency) coin_balance += temp_coin_balance balance += coin_balance percent_profit = balance / starting_balance return [balance, percent_profit]
def sell(user, sold_price, coin_amount, coin_id): balance = session["balance"] wallet = udb.get_user_wallet(user) _id = udb.get_user_id(user) coin_balance = wallet.get(coin_id) if coin_amount > coin_balance: print("Error: Not enough coins") return False else: coin_balance -= coin_amount wallet[coin_id] = coin_balance new_balance = balance + sold_price udb.collection.update_one( {"_id": _id}, {"$set": { "balance": new_balance, "wallet": wallet }}) session["balance"] = new_balance return True
def purchase(user, purchase, coin_amount, coin_id): balance = session["balance"] wallet = udb.get_user_wallet(user) _id = udb.get_user_id(user) if balance < purchase: print("Error: Balance cannot be less than purchase") return False else: new_balance = balance - purchase if coin_id in wallet: wallet[coin_id] += coin_amount else: wallet[coin_id] = coin_amount udb.collection.update_one( {"_id": _id}, {"$set": { "balance": new_balance, "wallet": wallet }}) session["balance"] = new_balance return True
def trade(): if "user" in session: user = session["user"] widget_id = session["widget"] username = udb.get_user_name(user) balance = session["balance"] wallet = udb.get_user_wallet(user) currency = udb.get_user_currency(user) balance_total = td.calculate_profit(user)[0] percent_profit = td.calculate_profit(user)[1] if request.method == "POST": print("called from POST /trade") # Collects buy information from user coin_id = request.form["coin_id"] coin_id = coin_id.lower() coin_amount = request.form["coin_amount"] coin_amount = float(coin_amount) # #Info from sell form # coin_id_sell = request.form["coin_id_sell"] # coin_id_sell = coin_id_sell.lower() # coin_amount_sell = request.form["coin_amount_sell"] # coin_amount_sell = float(coin_amount_sell) # handles a buy if "confirm_buy" in request.form: print("performing a buy") if not td.check_coin(coin_id): flash("TRADE FAILED - UNKNOWN COIN", "warning") return redirect(url_for("trade")) elif currency not in cg.get_supported_vs_currencies(): flash("PRICE CHECK FAILED - UNKNOWN CURRENCY", "warning") return redirect(url_for("trade")) price = td.get_price(coin_id, currency) purchase = float(price) * coin_amount status = td.purchase( user, purchase, coin_amount, coin_id) if status: flash( f"Your purchase of {coin_amount} {coin_id.capitalize()} @ {price} totaling {round(purchase, 3)} has completed successfully", "success") return redirect(url_for("trade")) else: flash("TRADE FAILED - INSUFFICIENT FUNDS", "danger") # handles a sell elif "confirm_sell" in request.form: print("Getting ready to perform a sell") if not td.check_coin(coin_id): flash("TRADE FAILED - UNKNOWN COIN", "danger") return redirect(url_for("trade")) elif currency not in cg.get_supported_vs_currencies(): flash("PRICE CHECK FAILED - UNKNOWN CURRENCY", "danger") return redirect(url_for("sell")) price = td.get_price(coin_id, currency) sold_price = float(price) * coin_amount status = td.sell( session["user"], sold_price, coin_amount, coin_id) if status: flash( f"Your sale of {coin_amount} {coin_id.capitalize()} @ {price} totalling {round(sold_price, 3)} has completed successfully", "success") return redirect(url_for("trade")) else: flash( "TRADE FAILED - INSUFFICIENT COIN BALANCE", "danger") return redirect(url_for("trade")) return render_template( "trade.html", user=username, balance=round(balance, 2), wallet=wallet, balance_total=balance_total, percent_profit=percent_profit, widget_id=widget_id) if request.method == "GET": if "user" in session: return render_template( "trade.html", user=username, balance=round(balance, 2), wallet=wallet, balance_total=balance_total, percent_profit=percent_profit, widget_id=widget_id) else: flash("You are not logged in", "warning") return redirect('/home') else: return redirect(url_for("login"))