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): return home_page(instrument, danger="Please log in to perform that action.") #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): return home_page("ltc_btc", danger="Unknown Error, contact the administrator!") 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) return home_page(instrument, danger="Please enter numerical values for price and amount!") if ramount < 1: #TODO: find a good amount for this return home_page(instrument, danger="Transaction amount too low!") if rprice <= 0: return home_page(instrument, danger="Price must be greater than 0!") 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: return home_page(instrument, danger="Balance too low to execute order!") else: adjustbalance(currency,session['userid'],-1 * total) elif request.form['ordertype'] == 'sell': currency = base_currency if check_balance(currency, uid) < ramount: return home_page(instrument, danger="Balance too low to execute order!") else: adjustbalance(currency, uid, -1 * ramount) else: return home_page(instrument, danger="Unknown Error, contact the administrator!") #invalid order type, they must have been messing around 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) return home_page(instrument, dismissable="Order placed successfully!")
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): return home_page(instrument, danger="Please log in to perform that action.") #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): return home_page("ltc_btc", danger="Unknown Error, contact the administrator!") base_currency = request.form['currency_pair'].split("_")[0] quote_currency = request.form['currency_pair'].split("_")[1] try: rprice = float(request.form['price']) ramount = int(float(request.form['amount'])* config.get_multiplier(base_currency)) except: return home_page(instrument, danger="Please enter numerical values for price and amount!") if ramount < 1: #TODO: find a good amount for this return home_page(instrument, danger="Transaction amount too low!") if rprice <= 0: return home_page(instrument, danger="Price must be greater than 0!") total = int(rprice * ramount) 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: return home_page(instrument, danger="Balance too low to execute order!") else: adjustbalance(currency,session['userid'],-1 * total) elif request.form['ordertype'] == 'sell': currency = base_currency if check_balance(currency, uid) < ramount: return home_page(instrument, danger="Balance too low to execute order!") else: adjustbalance(currency, uid, -1 * ramount) else: return home_page(instrument, danger="Unknown Error, contact the administrator!") #invalid order type, they must have been messing around 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) return home_page(instrument, dismissable="Order placed successfully!")
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 trade_page(instrument): if not config.is_valid_instrument(instrument): return home_page("ltc_btc", danger="Invalid trade pair!") return home_page(instrument)