Hi
I got this basic app that pulls orderbook streams from the websockets of two exchanges (Binance & bitstamp): GitHub - oo92/Currency-exchange
At the moment, that's all it does.
I am trying to add two more things:
- Merge, sort and combine the orderbooks.
- From the combined orderbook, publish the spread, top 10 bids, top 10 asks, as stream through a gRPC server.
Unfortunately, I couldn't figure out how to go about adding these.
This is an example gRPC server that I managed to write:
main.rs:
use tonic::{transport::Server, Request, Response, Status};
use bookstore::bookstore_server::{Bookstore, BookstoreServer};
use bookstore::{GetBookRequest, GetBookResponse};
mod bookstore {
include!("bookstore.rs");
}
#[derive(Default)]
pub struct BookStoreImpl {}
#[tonic::async_trait]
impl Bookstore for BookStoreImpl {
async fn get_book(
&self,
request: Request<GetBookRequest>,
) -> Result<Response<GetBookResponse>, Status> {
println!("Request from {:?}", request.remote_addr());
let response = GetBookResponse {
id: request.into_inner().id,
author: "Peter".to_owned(),
name: "Zero to One".to_owned(),
year: 2014,
};
Ok(Response::new(response))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let bookstore = BookStoreImpl::default();
println!("Bookstore server listening on {}", addr);
Server::builder()
.add_service(BookstoreServer::new(bookstore))
.serve(addr)
.await?;
Ok(())
}
build.rs:
use std::{env, path::PathBuf};
fn main() {
let proto_file = "./proto/bookstore.proto";
tonic_build::configure()
.build_server(true)
.out_dir("./src")
.compile(&[proto_file], &["."])
.unwrap_or_else(|e| panic!("protobuf compile error: {}", e));
println!("cargo:rerun-if-changed={}", proto_file);
}
bookstore.proto
syntax = "proto3";
package bookstore;
// The book store service definition.
service Bookstore {
// Retrieve a book
rpc GetBook(GetBookRequest) returns (GetBookResponse) {}
}
// The request with a id of the book
message GetBookRequest {
string id = 1;
}
// The response details of a book
message GetBookResponse {
string id = 1;
string name = 2;
string author = 3;
int32 year = 4;
}