def getjsonorders(instrument, t): """ Returns open orders from redis orderbook. """ orders = [] if config.is_valid_instrument(instrument): if t == "bid": bids = redis.zrange(instrument + "/bid", 0, -1, withscores=True) for bid in bids: orders.append({ "price": bid[1], "amount": redis.hget(bid[0], "amount") }) else: asks = redis.zrange(instrument + "/ask", 0, -1, withscores=True) for ask in asks: orders.append({ "price": ask[1], "amount": redis.hget(ask[0], "amount") }) # So prices are not quoted in satoshis # TODO: move this client side? for order in orders: order['amount'] = float(order['amount']) / config.get_multiplier( instrument.split("_")[0]) else: orders.append("Invalid trade pair!") jo = json.dumps(orders) return Response(jo, mimetype='application/json')
def withdraw(currency): if not is_logged_in(session): flash("Please log in to perform that action.", "error") return home_page("ltc_btc") if not config.is_valid_currency(currency): flash("Invalid Currency!", "error") return account_page() if request.method == 'GET': return account_page(withdraw=currency) elif request.method == 'POST': if 'amount' not in request.form or 'address' not in request.form: flash("Please enter an address and an amount!", "error") return account_page() try: total = string_to_currency_unit( request.form['amount'], config.get_multiplier(currency)) except: flash("Invalid amount!", "error") return account_page() if check_balance(currency, session['userid']) < total or total < 0: flash("Balance too low to execute withdrawal!", "error") return account_page() # TODO: add valid address checking adjustbalance(currency, session['userid'], -1 * total) co = CompletedOrder( currency + "_" + currency, "WITHDRAWAL", total, 0, session['userid'], is_withdrawal=True, withdrawal_address=request.form['address']) db_session.add(co) db_session.commit() flash("Deposit to " + request.form['address'] + " completed!", "success") return account_page()
def getjsonorders(instrument, t): """ Returns open orders from redis orderbook. """ orders = [] if config.is_valid_instrument(instrument): if t == "bid": bids = redis.zrange(instrument + "/bid", 0, -1, withscores=True) for bid in bids: orders.append( {"price": bid[1], "amount": redis.hget(bid[0], "amount")}) else: asks = redis.zrange(instrument + "/ask", 0, -1, withscores=True) for ask in asks: orders.append( {"price": ask[1], "amount": redis.hget(ask[0], "amount")}) # So prices are not quoted in satoshis # TODO: move this client side? for order in orders: order['amount'] = float( order['amount']) / config.get_multiplier(instrument.split("_")[0]) else: orders.append("Invalid trade pair!") jo = json.dumps(orders) return Response(jo, mimetype='application/json')
def addorder(): """ Checks balance and essential stuff, generates an order ID then adds order to a redis queue. """ instrument = request.form['currency_pair'] if not is_logged_in(session): flash("Please log in to perform that action.", "error") return home_page(instrument) # They shouldn't be able to modify the trade pair, if it isnt valid either # I messed up somewhere or they are trying to do something wrong if not config.is_valid_instrument(instrument): flash("Unknown Error, contact the administrator!", "error") return home_page("ltc_btc") base_currency = request.form['currency_pair'].split("_")[0] quote_currency = request.form['currency_pair'].split("_")[1] try: rprice = Decimal(request.form['price']) ramount = string_to_currency_unit( request.form['amount'], config.get_multiplier(base_currency)) print(ramount) except Exception as e: print(e) flash("Please enter numerical values for price and amount!", "error") return home_page(instrument) if ramount < 1: # TODO: find a good amount for this flash("Transaction amount too low!", "error") return home_page(instrument) if rprice <= 0: flash("Price must be greater than 0!", "error") return home_page(instrument) getcontext().prec = 6 whole, dec = ExtendedContext.divmod( rprice * ramount / config.get_multiplier(base_currency), Decimal(1)) total = int( whole * config.get_multiplier(base_currency) + dec * config.get_multiplier(base_currency)) print("total: " + str(total)) uid = session['userid'] orderid = generate_password_hash(str(random.random())) instrument = request.form['currency_pair'] bidtable = instrument + "/bid" asktable = instrument + "/ask" if request.form['ordertype'] == 'buy': currency = quote_currency if check_balance(currency, session['userid']) < total: flash("Balance too low to execute order!", "error") return home_page(instrument) else: adjustbalance(currency, session['userid'], -1 * total) elif request.form['ordertype'] == 'sell': currency = base_currency if check_balance(currency, uid) < ramount: flash("Balance too low to execute order!", "error") return home_page(instrument) else: adjustbalance(currency, uid, -1 * ramount) else: # invalid order type, they must have been messing around flash("Unknown Error, contact the administrator!", "error") return home_page(instrument) redis.hmset(orderid, {"ordertype": request.form['ordertype'], "instrument": request.form['currency_pair'], "amount": ramount, "uid": uid, "price": rprice}) redis.rpush("order_queue", orderid) redis.sadd(str(uid) + "/orders", orderid) flash("Order placed successfully!","dismissable") return home_page(instrument)
def openorders(uid): if not is_logged_in(session): return home_page("ltc_btc",danger="Please log in to perform that action.") orders = redis.smembers(str(uid)+"/orders") r = [] for o in orders: c = redis.hgetall(o) base_currency = c['instrument'][0:c['instrument'].find("_")] quote_currency = c['instrument'][c['instrument'].find("_")+1:] instrument = (base_currency+"/"+quote_currency).upper() r.append([instrument, c['ordertype'], c['price'] + " " + instrument, str(float(c['amount'])/config.get_multiplier(base_currency)) + " " + base_currency.upper(), o]) return r
def tradehistory(currency, uid): if not is_logged_in(session): return home_page("ltc_btc",danger="Please log in to perform that action.") orders = CompletedOrder.query.filter(CompletedOrder.user == uid).filter(CompletedOrder.base_currency==str(currency)).all() orders2 = [] for o in orders: if o.is_deposit: orders2.append(["DEPOSIT", o.order_type, o.price, str(o.amount) + " " + str(o.base_currency).upper()]) elif o.is_withdrawal: orders2.append(["WITHDRAWAL", o.order_type, o.price, str(o.amount) + " " + str(o.base_currency).upper()]) else: orders2.append([o.currency_pair, o.order_type, (str(o.price) + " " + o.quote_currency + "/" + o.base_currency).upper(), str(float(o.amount)/config.get_multiplier(currency))+ " " + str(o.base_currency).upper()]) return orders2