Ejemplo n.º 1
0
def main(argv=sys.argv):
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    storeData = StoreOrderBookData(settings['kafka.url'])
Ejemplo n.º 2
0
    def __init__(self, kafka_url: str):
        self.log.info("Initialized with kafka_url=" + kafka_url)
        self.consumer = KafkaConsumer(
            bootstrap_servers=kafka_url,
            value_deserializer=lambda v: json.loads(v))

        self.consumer.subscribe(['orderbook'])
        self.log.debug("Init OK")
        while (1):
            with transaction.manager:
                self.log.debug("Polling records")
                records = self.consumer.poll(1000)
                orderbook = None
                for key in records:
                    self.log.debug("Got " + str(len(records[key])) +
                                   " records")
                    for record in records[key]:
                        orderbook = OrderBook(json.loads(record.value))

                if orderbook != None:
                    self.log.debug("Got latest orderbook=" + repr(orderbook))
                    storage: OrderBookStorage = DBSession.query(
                        OrderBookStorage).filter_by(id=1).one()
                    storage.asks = orderbook.asks
                    storage.bids = orderbook.bids
                    self.log.debug("Stored latest orderbook status")
Ejemplo n.º 3
0
def put_place_limit_order(request: Request):

    user = map_user(request)
    if user == None:
        return {"error": "no auth"}
    else:
        user_id = user.id
    amount = float(request.matchdict['amount'])
    price = float(request.matchdict['price'])
    type = request.matchdict['type']
    log.debug("New order for data:" + json.dumps({"amount":amount, "price":price, "type":type, "owner_id":user_id}))
    with transaction.manager:
        user = DBSession.query(User).filter_by(id=user_id).one()
        if type == "ASK":
            if user.stuff >= amount:
                user.stuff = float(user.stuff) - amount
            else:
                log.debug("User does not have enough stuff")
                return None
        elif type == "BID":
            if user.balance >= price*amount:
                user.balance = float(user.balance) - price*amount
            else:
                log.debug("User does not have enough funds")
                return None
        order = {"id": str(uuid4()), "amount": amount, "price": price, "operation": "ADD", "type": type, "owner_id": user_id}
        producer: KafkaProducer = request.registry.kafka_producer
        log.debug("Placing order into queue" + json.dumps(order))
        producer.send(topic='limit_order', value=order)
        log.debug("Order successfully placed in the queue")
        return order
Ejemplo n.º 4
0
def get_user_details(request):
    user = map_user(request)
    if user == None:
        return {"error": "no auth"}
    else:
        user_id = user.id
    user = DBSession.query(User).filter_by(id=user_id).one()
    return user.toDict()
Ejemplo n.º 5
0
def map_user(request):
    if (request.authorization is not None):
        if (JWSig().unpack(request.authorization.params).valid()):
            username = JWT().unpack(request.authorization.params).payload()['preferred_username']
            #map username to userid
            user = DBSession.query(User).filter_by(name=username).first()
            return user
    return None
Ejemplo n.º 6
0
def get_user_orders(request):
    user = map_user(request)
    if user == None:
        return {"error": "no auth"}
    else:
        user_id = user.id
    orders = DBSession.query(Order).filter_by(user_id=user_id).all()
    orders_array = []
    for order in orders:
        orders_array.append(order.toDict())
    return orders_array
Ejemplo n.º 7
0
def get_orderbook(request):
    log.debug("Requested BID orderbooks")
    return DBSession.query(OrderBookStorage).one().bids