示例#1
0
def login():
    if "user_id" in session:
        return jsonify(
            error("Already logged in with username " + session["user_id"]))
    if request.checked_json["name"] in USERS:
        return jsonify(
            error("Username is already taken, please choose an other one"))
    session["user_id"] = request.checked_json["name"]
    USERS.add(request.checked_json["name"])
    return jsonify({"status": "login succesful"})
示例#2
0
def new_market():
    name = request.checked_json["name"]
    n_periods = request.checked_json["n_periods"]
    period_time = request.checked_json["period_time"]
    if not name.isalnum():
        return jsonify(error("Market name must be alphanumeric without space"))
    if name in MARKETS:
        return jsonify(error("Market name already taken"))
    market = Market(n_periods=n_periods, time_per_period=period_time)
    market.start()
    market.add_watch(emit_market_view(name, market))
    MARKETS[name] = market
    n_bots = max(0, min(10, request.checked_json["bots"]))
    for name in BOT_NAMES[:n_bots]:
        bot = Bot(name, market)
        bot.start()
    return jsonify({"market_id": name})
示例#3
0
def set_price(market_id, side):
    if side not in ("buy", "sell"):
        return jsonify(error("side must be 'buy' or 'sell'"))
    market = MARKETS.get(market_id)
    user_id = session["user_id"]
    market.set_price(user_id, Sides.BUY if side == "buy" else Sides.SELL,
                     request.checked_json["price"])
    return jsonify({"status": "success"})
示例#4
0
def set_open(market_id):
    market = MARKETS.get(market_id)
    user_id = session["user_id"]
    participant = market.participants[user_id]
    open_ = request.checked_json["open"]
    if open_ and (participant.bid_price < 0 or participant.ask_price < 0):
        return jsonify(error("bid and ask prices must be set before opening"))
    market.set_open(user_id, open_)
    return jsonify({"status": "success"})
示例#5
0
def join_market(market_id):
    user_id = session["user_id"]
    market = MARKETS.get(market_id)
    if user_id in market.participants:
        return jsonify(error(f"User already in market, can only join once"))
    hidden_value = market.join_market(user_id)
    participants = view_participants(market)
    return jsonify({
        "market_values": market.values,
        "hidden_value": hidden_value,
        "participants": participants
    })
示例#6
0
def take_offer(market_id):
    market = MARKETS.get(market_id)
    user_id = session["user_id"]
    participant = market.participants[user_id]
    if not participant.open:
        return jsonify(
            error("must be open to offers to be able to take offers"))
    counterparty_id, price, side = request.checked_json[
        "counterparty_id"], request.checked_json[
            "price"], request.checked_json["side"]
    if counterparty_id not in market.participants:
        return jsonify(error("counterparty is not in market"))
    counterparty = market.participants[counterparty_id]
    if not counterparty.open:
        return jsonify(error("counterparty is closed to offers"))
    counterparty_price = counterparty.bid_price if side == Sides.BUY else counterparty.ask_price
    if counterparty_price != price:
        return jsonify(error("counterparty price has changed"))
    success = market.take_price(user_id, counterparty_id, side,
                                counterparty_price)
    if not success:
        return jsonify(
            error("counterparty has closed to offers or changed price"))
    return jsonify({"status": "success"})