2023-03-13 09:51:30 -07:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2023-05-14 01:09:12 -07:00
|
|
|
use deadpool_postgres::Pool;
|
2023-03-05 22:52:42 -08:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
|
2023-03-12 22:03:58 -07:00
|
|
|
use crate::structs::markets::MarketInfo;
|
|
|
|
|
2023-03-05 22:52:42 -08:00
|
|
|
pub trait AnyhowWrap {
|
|
|
|
type Value;
|
|
|
|
fn map_err_anyhow(self) -> anyhow::Result<Self::Value>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: std::fmt::Debug> AnyhowWrap for Result<T, E> {
|
|
|
|
type Value = T;
|
|
|
|
fn map_err_anyhow(self) -> anyhow::Result<Self::Value> {
|
|
|
|
self.map_err(|err| anyhow::anyhow!("{:?}", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 09:11:17 -07:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
pub struct PgConfig {
|
|
|
|
pub pg: deadpool_postgres::Config,
|
|
|
|
pub pg_max_pool_connections: usize,
|
|
|
|
pub pg_use_ssl: bool,
|
|
|
|
pub pg_ca_cert_path: Option<String>,
|
|
|
|
pub pg_client_key_path: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PgConfig {
|
|
|
|
pub fn from_env() -> Result<Self, config::ConfigError> {
|
|
|
|
config::Config::builder()
|
|
|
|
.add_source(config::Environment::default().separator("_"))
|
2023-05-18 11:41:17 -07:00
|
|
|
.add_source(config::Environment::default())
|
2023-05-17 09:11:17 -07:00
|
|
|
.build()?
|
|
|
|
.try_deserialize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:52:42 -08:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct Config {
|
2023-03-07 22:45:37 -08:00
|
|
|
pub rpc_url: String,
|
2023-03-05 22:52:42 -08:00
|
|
|
}
|
|
|
|
|
2023-03-12 22:03:58 -07:00
|
|
|
pub struct WebContext {
|
2023-05-05 18:41:17 -07:00
|
|
|
pub rpc_url: String,
|
2023-03-12 22:03:58 -07:00
|
|
|
pub markets: Vec<MarketInfo>,
|
2023-05-14 01:09:12 -07:00
|
|
|
pub pool: Pool,
|
2023-03-09 20:54:51 -08:00
|
|
|
}
|
|
|
|
|
2023-05-05 18:41:17 -07:00
|
|
|
#[allow(deprecated)]
|
2023-03-13 09:51:30 -07:00
|
|
|
pub fn to_timestampz(seconds: u64) -> chrono::DateTime<Utc> {
|
|
|
|
chrono::DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(seconds as i64, 0), Utc)
|
|
|
|
}
|
2023-05-14 00:15:10 -07:00
|
|
|
|
|
|
|
pub(crate) fn f64_max(a: f64, b: f64) -> f64 {
|
|
|
|
if a >= b {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn f64_min(a: f64, b: f64) -> f64 {
|
|
|
|
if a < b {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|