Esempio n. 1
0
def public_orderbook_service_get_orderbook(channel):
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    stub = orderbook_pb2_grpc.PublicOrderbookServiceStub(channel)

    # Create a request for the BTC_EUR orderbook, with the greatest precision, largest length,
    # and highest update frequency
    # See TBD for semantics of Precision and Length
    request = orderbook_pb2.GetOrderbookRequest(
        market=orders_pb2.BTC_EUR,
        precision=orderbook_pb2.P0,
        length=orderbook_pb2.NUM_ENTRIES_25,
    )

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.GetOrderbook(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PublicOrderbookService.GetOrderbook error: " + str(e),
              file=sys.stderr)
    # CODEINCLUDE-END-MARKER: ref-code-example-request

    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-response
    result_string = "{}\n".format(type(response).__name__)
    for orderbook_entry in response.entries:
        result_string += "\t{} {} {} orders @ {} total {}\n".format(
            type(orderbook_entry).__name__,
            orders_pb2.Side.Name(orderbook_entry.side),
            orderbook_entry.orders_at_price_level,
            orderbook_entry.price_level,
            orderbook_entry.amount,
        )
    print(result_string)
def public_orderbook_service_get_orderbook(channel):
    stub = orderbook_pb2_grpc.PublicOrderbookServiceStub(channel)

    # Create a request for the BTC_EUR orderbook, with the greatest precision, largest length,
    # and highest update frequency
    # See TBD for semantics of Precision and Length
    request = orderbook_pb2.GetOrderbookRequest(
        market=orders_pb2.BTC_EUR,
        precision=orderbook_pb2.P0,
        length=orderbook_pb2.L0,
    )

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.GetOrderbook(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PublicOrderbookService.GetOrderbook error: " + str(e),
              file=sys.stderr)
Esempio n. 3
0
def public_orderbook_service_stream_orderbook(channel):
    stub = orderbook_pb2_grpc.PublicOrderbookServiceStub(channel)

    # Create a request for streaming the BTC_EUR orderbook, with the greatest precision, largest length,
    # and highest update frequency
    # See TBD for semantics of Precision, Length and Frequency
    request = orderbook_pb2.StreamOrderbookRequest(
        market=orders_pb2.BTC_EUR,
        precision=orderbook_pb2.P3,
        length=orderbook_pb2.NUM_ENTRIES_100,
        frequency=orderbook_pb2.BEST_EFFORT,
    )

    try:
        # Make the request synchronously and iterate over the received orderbook entries
        for response in stub.StreamOrderbook(request):
            print(response)
    except grpc.RpcError as e:
        print("PublicOrderbookService.StreamOrderbook error: " + str(e),
              file=sys.stderr)
Esempio n. 4
0
# Subscribe to a public orderbook stream and set a new order
if __name__ == '__main__':
    creds = grpc.ssl_channel_credentials()

    with grpc.secure_channel('mockgrpc.test.tulipsolutions.nl:443',
                             creds) as channel:
        # Create a SHA256 HMAC with the base64 decoded 'secret' string as its key
        dummy_secret = base64.standard_b64decode("secret==")
        dummy_jwt = "eyJraWQiOiI2YzY4OTIzMi03YTcxLTQ3NGItYjBlMi1lMmI1MzMyNDQzOWUiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMjM0In0.IL9QJQl55qn3oPsT7sFa7iwd5g1GsEQVr0IO7gCe1UmQdjT7jCIc-pUfjyYUgptPR8HBQl5ncXuBnxwjdXqOMwW1WhPmi_B3BRHQh3Sfu0zNXqKhkuz2-6DffXK1ek3DmK1NpaSikXtg2ruSQ4Uk5xHcnxmXY_SwEij0yot_JRKYEs-0RbyD5Z4jOFKcsbEW46WQmiWdgG3PUKiJT5TfdFd55JM55BwzSOdPIP1S_3dQ4VTDo30mWqAs1KaVbcPqCQmjT1PL0QScTp4w8-YPDcajcafIj98ve9LUoLBLraCIAX34D-hOxu643h9DoG2kIPFfZyXbkDTiUKOl7t-Ykg"

        # Create an interceptor that signs messages with the provided secret.
        # Only messages to the private API that have a 'signed' field will be signed.
        message_auth_interceptor = message_authentication_interceptor.create(
            dummy_secret)
        # Create an interceptor that adds a JWT token when a request to a private service is made.
        jwt_interceptor = jwt_interceptor.create(dummy_jwt)
        # Add interceptors to all requests over the channel
        channel = grpc.intercept_channel(channel, jwt_interceptor,
                                         message_auth_interceptor)

        # Construct clients for accessing PublicOrderbookService and PrivateOrderService using the existing connection.
        # Add a deadline to all requests to the PrivateOrderService
        public_orderbook_service_stub = orderbook_pb2_grpc.PublicOrderbookServiceStub(
            channel)
        private_order_service_stub = order_pb2_grpc.PrivateOrderServiceStub(
            channel)

        with ThreadPoolExecutor(max_workers=2) as executor:
            executor.submit(stream_orderbook, public_orderbook_service_stub),
            executor.submit(create_order, private_order_service_stub),