feat: finish /tickers route

This commit is contained in:
dboures 2023-05-06 20:06:42 -05:00
parent 3aafa73d45
commit be1424b1e1
No known key found for this signature in database
GPG Key ID: AB3790129D478852
3 changed files with 36 additions and 31 deletions

6
src/backfill/main.rs Normal file
View File

@ -0,0 +1,6 @@
#[tokio::main]
async fn main() -> anyhow::Result<()> {
Ok(())
}

View File

@ -1,14 +1,12 @@
use std::str::FromStr;
use crate::server_error::ServerError; use crate::server_error::ServerError;
use actix_web::{get, web, HttpResponse, Scope}; use actix_web::{get, web, HttpResponse, Scope};
use anchor_lang::prelude::Pubkey;
use futures::join; use futures::join;
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
use openbook_candles::{ use openbook_candles::{
database::fetch::{fetch_coingecko_24h_high_low, fetch_coingecko_24h_volume}, database::fetch::{fetch_coingecko_24h_high_low, fetch_coingecko_24h_volume},
structs::coingecko::{ structs::{
CoinGecko24HourVolume, CoinGeckoPair, CoinGeckoTicker, PgCoinGecko24HighLow, coingecko::{CoinGecko24HourVolume, CoinGeckoPair, CoinGeckoTicker, PgCoinGecko24HighLow},
slab::{get_best_bids_and_asks},
}, },
utils::WebContext, utils::WebContext,
}; };
@ -40,16 +38,26 @@ pub async fn pairs(context: web::Data<WebContext>) -> Result<HttpResponse, Serve
#[get("/tickers")] #[get("/tickers")]
pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> { pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> {
let markets = context.markets.clone(); let client = RpcClient::new(context.rpc_url.clone());
let markets = &context.markets;
// rpc get bid ask liquidity let mut c1 = context.pool.acquire().await.unwrap();
let mut c2 = context.pool.acquire().await.unwrap();
let bba_fut = get_best_bids_and_asks(client, markets);
let volume_fut = fetch_coingecko_24h_volume(&mut c1);
let high_low_fut = fetch_coingecko_24h_high_low(&mut c2);
let mut conn = context.pool.acquire().await.unwrap(); let ((best_bids, best_asks), volume_query, high_low_quey) = join!(
let raw_volumes = match fetch_coingecko_24h_volume(&mut conn).await { bba_fut,
volume_fut,
high_low_fut,
);
let raw_volumes = match volume_query {
Ok(c) => c, Ok(c) => c,
Err(_) => return Err(ServerError::DbQueryError), Err(_) => return Err(ServerError::DbQueryError),
}; };
let high_low = match fetch_coingecko_24h_high_low(&mut conn).await { let high_low = match high_low_quey {
Ok(c) => c, Ok(c) => c,
Err(_) => return Err(ServerError::DbQueryError), Err(_) => return Err(ServerError::DbQueryError),
}; };
@ -62,7 +70,8 @@ pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, Ser
.collect(); .collect();
let tickers = markets let tickers = markets
.iter() .iter()
.map(|m| { .enumerate()
.map(|(index, m)| {
let name = m.name.clone(); let name = m.name.clone();
let high_low = high_low let high_low = high_low
.iter() .iter()
@ -79,9 +88,8 @@ pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, Ser
last_price: high_low.close.to_f64().unwrap(), last_price: high_low.close.to_f64().unwrap(),
base_volume: volume.base_volume.to_f64().unwrap(), base_volume: volume.base_volume.to_f64().unwrap(),
target_volume: volume.target_volume.to_f64().unwrap(), target_volume: volume.target_volume.to_f64().unwrap(),
liquidity_in_usd: 0.0, bid: best_bids[index].to_f64().unwrap(),
bid: 0.0, ask: best_asks[index].to_f64().unwrap(),
ask: 0.0,
high: high_low.high.to_f64().unwrap(), high: high_low.high.to_f64().unwrap(),
low: high_low.low.to_f64().unwrap(), low: high_low.low.to_f64().unwrap(),
} }
@ -95,26 +103,17 @@ pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, Ser
pub async fn orderbook(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> { pub async fn orderbook(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> {
let client = RpcClient::new(context.rpc_url.clone()); let client = RpcClient::new(context.rpc_url.clone());
let markets = context.markets.clone(); let markets = &context.markets;
let bid_keys = markets
.iter()
.map(|m| Pubkey::from_str(&m.bids_key).unwrap())
.collect::<Vec<Pubkey>>();
let ask_keys = markets
.iter()
.map(|m| Pubkey::from_str(&m.asks_key).unwrap())
.collect::<Vec<Pubkey>>();
// client.get_multiple_accounts(&bid_keys) let (best_bids, best_asks) = get_best_bids_and_asks(client, markets).await;
let (bid_results, _ask_results) = join!( // let bids = bid_bytes.into_iter().map(|mut x| Slab::new(x.as_mut_slice())).collect::<Vec<_>>();
client.get_multiple_accounts(&bid_keys), // Slab::new(&mut x.data)
client.get_multiple_accounts(&ask_keys)
);
let x = bid_results.unwrap(); // let mut bb = bid_bytes[0].clone();
// let data_end = bb.len() - 7;
println!("{:?}", x); // let goo = Slab::new(&mut bb[13..data_end]);
// decode results // decode results

View File

@ -4,6 +4,6 @@ use openbook_candles::utils::WebContext;
#[get("/markets")] #[get("/markets")]
pub async fn get_markets(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> { pub async fn get_markets(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> {
let markets = context.markets.clone(); let markets = &context.markets;
Ok(HttpResponse::Ok().json(markets)) Ok(HttpResponse::Ok().json(markets))
} }