diff --git a/daemon/src/actors.rs b/daemon/src/actors.rs index ba9846d..c97159d 100644 --- a/daemon/src/actors.rs +++ b/daemon/src/actors.rs @@ -1,4 +1,5 @@ /// Wrapper for handlers to log errors +#[macro_export] macro_rules! log_error { ($future:expr) => { if let Err(e) = $future.await { @@ -6,4 +7,3 @@ macro_rules! log_error { } }; } -pub(crate) use log_error; diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs new file mode 100644 index 0000000..ad71158 --- /dev/null +++ b/daemon/src/lib.rs @@ -0,0 +1,24 @@ +pub mod actors; +pub mod auth; +pub mod bitmex_price_feed; +pub mod db; +pub mod housekeeping; +pub mod keypair; +pub mod logger; +pub mod maker_cfd; +pub mod maker_inc_connections; +pub mod model; +pub mod monitor; +pub mod olivia; +pub mod oracle; +pub mod payout_curve; +pub mod routes; +pub mod seed; +pub mod send_to_socket; +pub mod setup_contract; +pub mod taker_cfd; +pub mod to_sse_event; +pub mod tokio_ext; +pub mod wallet; +pub mod wallet_sync; +pub mod wire; diff --git a/daemon/src/maker.rs b/daemon/src/maker.rs index 7a18042..6dc0594 100644 --- a/daemon/src/maker.rs +++ b/daemon/src/maker.rs @@ -1,14 +1,17 @@ -use crate::auth::MAKER_USERNAME; -use crate::db::load_all_cfds; -use crate::model::cfd::UpdateCfdProposals; -use crate::seed::Seed; -use crate::wallet::Wallet; use anyhow::{Context, Result}; use bdk::bitcoin; use bdk::bitcoin::secp256k1::schnorrsig; use clap::Clap; -use model::cfd::Order; -use model::WalletInfo; +use daemon::auth::{self, MAKER_USERNAME}; +use daemon::db::{self, load_all_cfds}; +use daemon::model::cfd::{Order, UpdateCfdProposals}; +use daemon::model::WalletInfo; +use daemon::seed::Seed; +use daemon::wallet::Wallet; +use daemon::{ + bitmex_price_feed, housekeeping, logger, maker_cfd, maker_inc_connections, monitor, oracle, + wallet_sync, +}; use rocket::fairing::AdHoc; use rocket_db_pools::Database; use std::collections::HashMap; @@ -22,30 +25,7 @@ use tracing_subscriber::filter::LevelFilter; use xtra::prelude::*; use xtra::spawn::TokioGlobalSpawnExt; -mod actors; -mod auth; -mod bitmex_price_feed; -mod db; -mod housekeeping; -mod keypair; -mod logger; -mod maker_cfd; -mod maker_inc_connections; -mod model; -mod monitor; -mod olivia; -mod oracle; -mod payout_curve; -mod routes; mod routes_maker; -mod seed; -mod send_to_socket; -mod setup_contract; -mod to_sse_event; -mod tokio_ext; -mod wallet; -mod wallet_sync; -mod wire; #[derive(Database)] #[database("maker")] @@ -332,7 +312,3 @@ async fn main() -> Result<()> { Ok(()) } - -impl xtra::Message for wire::MakerToTaker { - type Result = (); -} diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index c5362e5..67a2024 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -1,4 +1,3 @@ -use crate::actors::log_error; use crate::db::{ insert_cfd, insert_new_cfd_state_by_order_id, insert_order, load_all_cfds, load_cfd_by_order_id, load_cfds_by_oracle_event_id, load_order_by_id, @@ -12,7 +11,7 @@ use crate::model::cfd::{ use crate::model::{TakerId, Usd}; use crate::monitor::MonitorParams; use crate::wallet::Wallet; -use crate::{maker_inc_connections, monitor, oracle, setup_contract, wire}; +use crate::{log_error, maker_inc_connections, monitor, oracle, setup_contract, wire}; use anyhow::{bail, Context as _, Result}; use async_trait::async_trait; use bdk::bitcoin::secp256k1::schnorrsig; diff --git a/daemon/src/maker_inc_connections.rs b/daemon/src/maker_inc_connections.rs index 9233b85..70be8ae 100644 --- a/daemon/src/maker_inc_connections.rs +++ b/daemon/src/maker_inc_connections.rs @@ -1,7 +1,6 @@ -use crate::actors::log_error; use crate::model::cfd::{Order, OrderId}; use crate::model::{BitMexPriceEventId, TakerId}; -use crate::{maker_cfd, send_to_socket, wire}; +use crate::{log_error, maker_cfd, send_to_socket, wire}; use anyhow::{Context as AnyhowContext, Result}; use async_trait::async_trait; use futures::StreamExt; @@ -162,7 +161,7 @@ impl Actor { tracing::info!("New taker {} connected on {}", taker_id, address); let (read, write) = stream.into_split(); - let read = FramedRead::new(read, wire::JsonCodec::new()) + let read = FramedRead::new(read, wire::JsonCodec::default()) .map(move |item| maker_cfd::TakerStreamMessage { taker_id, item }); tokio::spawn(self.cfd_actor.clone().attach_stream(read)); diff --git a/daemon/src/monitor.rs b/daemon/src/monitor.rs index 5cecc65..09ffe23 100644 --- a/daemon/src/monitor.rs +++ b/daemon/src/monitor.rs @@ -1,8 +1,7 @@ -use crate::actors::log_error; use crate::model::cfd::{CetStatus, Cfd, CfdState, Dlc, OrderId}; use crate::model::BitMexPriceEventId; use crate::oracle::Attestation; -use crate::{model, oracle}; +use crate::{log_error, model, oracle}; use anyhow::{Context, Result}; use async_trait::async_trait; use bdk::bitcoin::{PublicKey, Script, Txid}; diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index d96f6f0..125bc24 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -1,7 +1,6 @@ -use crate::actors::log_error; use crate::model::cfd::{Cfd, CfdState}; use crate::model::BitMexPriceEventId; -use crate::tokio_ext; +use crate::{log_error, tokio_ext}; use anyhow::{Context, Result}; use async_trait::async_trait; use cfd_protocol::secp256k1_zkp::{schnorrsig, SecretKey}; diff --git a/daemon/src/payout_curve.rs b/daemon/src/payout_curve.rs index f5e2b98..7b3bb5b 100644 --- a/daemon/src/payout_curve.rs +++ b/daemon/src/payout_curve.rs @@ -19,13 +19,7 @@ mod curve_factory; mod splineobject; mod utils; -/// function to generate an iterator of values, heuristically viewed as: -/// -/// `[left_price_boundary, right_price_boundary], maker_payout_value` -/// -/// with units -/// -/// `[Usd, Usd], bitcoin::Amount` +/// Generate a list of [`Payout`]s. /// /// A key item to note is that although the POC logic has been to imposed /// that maker goes short every time, there is no reason to make the math @@ -39,7 +33,7 @@ mod utils; /// in the call to PayoutCurve::new(), so this behaviour can be changed in /// the future trivially. /// -/// ### Paramters +/// ### Parameters /// /// * price: BTC-USD exchange rate used to create CFD contract /// * quantity: Interger number of one-dollar USD contracts contained in the diff --git a/daemon/src/routes_maker.rs b/daemon/src/routes_maker.rs index 662772c..404b55f 100644 --- a/daemon/src/routes_maker.rs +++ b/daemon/src/routes_maker.rs @@ -1,11 +1,11 @@ -use crate::auth::Authenticated; -use crate::model::cfd::{Cfd, Order, OrderId, Role, UpdateCfdProposals}; -use crate::model::{Usd, WalletInfo}; -use crate::routes::EmbeddedFileExt; -use crate::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent}; -use crate::{bitmex_price_feed, maker_cfd}; use anyhow::Result; use bdk::bitcoin::Network; +use daemon::auth::Authenticated; +use daemon::model::cfd::{Cfd, Order, OrderId, Role, UpdateCfdProposals}; +use daemon::model::{Usd, WalletInfo}; +use daemon::routes::EmbeddedFileExt; +use daemon::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent}; +use daemon::{bitmex_price_feed, maker_cfd}; use rocket::http::{ContentType, Header, Status}; use rocket::response::stream::EventStream; use rocket::response::{status, Responder}; @@ -231,7 +231,7 @@ pub fn index<'r>(_paths: PathBuf, _auth: Authenticated) -> impl Responder<'r, 's #[cfg(test)] mod tests { use super::*; - use crate::auth::Password; + use daemon::auth::Password; use rocket::http::{Header, Status}; use rocket::local::blocking::Client; use rocket::{Build, Rocket}; diff --git a/daemon/src/routes_taker.rs b/daemon/src/routes_taker.rs index 62d257a..f8c26de 100644 --- a/daemon/src/routes_taker.rs +++ b/daemon/src/routes_taker.rs @@ -1,9 +1,9 @@ -use crate::model::cfd::{calculate_buy_margin, Cfd, Order, OrderId, Role, UpdateCfdProposals}; -use crate::model::{Leverage, Usd, WalletInfo}; -use crate::routes::EmbeddedFileExt; -use crate::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent}; -use crate::{bitmex_price_feed, taker_cfd}; use bdk::bitcoin::{Amount, Network}; +use daemon::model::cfd::{calculate_buy_margin, Cfd, Order, OrderId, Role, UpdateCfdProposals}; +use daemon::model::{Leverage, Usd, WalletInfo}; +use daemon::routes::EmbeddedFileExt; +use daemon::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent}; +use daemon::{bitmex_price_feed, taker_cfd}; use rocket::http::{ContentType, Status}; use rocket::response::stream::EventStream; use rocket::response::{status, Responder}; diff --git a/daemon/src/send_to_socket.rs b/daemon/src/send_to_socket.rs index ac82e32..8075259 100644 --- a/daemon/src/send_to_socket.rs +++ b/daemon/src/send_to_socket.rs @@ -1,4 +1,4 @@ -use crate::wire::JsonCodec; +use crate::wire::{self, JsonCodec}; use futures::SinkExt; use serde::Serialize; use std::fmt; @@ -13,7 +13,7 @@ pub struct Actor { impl Actor { pub fn new(write: OwnedWriteHalf) -> Self { Self { - write: FramedWrite::new(write, JsonCodec::new()), + write: FramedWrite::new(write, JsonCodec::default()), } } } @@ -34,3 +34,11 @@ where } impl xtra::Actor for Actor {} + +impl xtra::Message for wire::MakerToTaker { + type Result = (); +} + +impl xtra::Message for wire::TakerToMaker { + type Result = (); +} diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index 7dfae56..d77246f 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -1,16 +1,19 @@ -use crate::db::load_all_cfds; -use crate::model::cfd::UpdateCfdProposals; -use crate::model::WalletInfo; -use crate::wallet::Wallet; use anyhow::{Context, Result}; use bdk::bitcoin; use bdk::bitcoin::secp256k1::schnorrsig; use clap::Clap; +use daemon::db::{self, load_all_cfds}; +use daemon::model::cfd::{Order, UpdateCfdProposals}; +use daemon::model::WalletInfo; +use daemon::seed::Seed; +use daemon::wallet::Wallet; +use daemon::{ + bitmex_price_feed, housekeeping, logger, monitor, oracle, send_to_socket, taker_cfd, + wallet_sync, wire, +}; use futures::StreamExt; -use model::cfd::Order; use rocket::fairing::AdHoc; use rocket_db_pools::Database; -use seed::Seed; use std::collections::HashMap; use std::net::SocketAddr; use std::path::PathBuf; @@ -20,32 +23,10 @@ use std::time::Duration; use tokio::sync::watch; use tokio_util::codec::FramedRead; use tracing_subscriber::filter::LevelFilter; -use wire::TakerToMaker; use xtra::spawn::TokioGlobalSpawnExt; use xtra::Actor; -mod actors; -mod bitmex_price_feed; -mod db; -mod housekeeping; -mod keypair; -mod logger; -mod model; -mod monitor; -mod olivia; -mod oracle; -mod payout_curve; -mod routes; mod routes_taker; -mod seed; -mod send_to_socket; -mod setup_contract; -mod taker_cfd; -mod to_sse_event; -mod tokio_ext; -mod wallet; -mod wallet_sync; -mod wire; const CONNECTION_RETRY_INTERVAL: Duration = Duration::from_secs(5); @@ -246,7 +227,7 @@ async fn main() -> Result<()> { .create(None) .spawn_global(); - let read = FramedRead::new(read, wire::JsonCodec::new()) + let read = FramedRead::new(read, wire::JsonCodec::default()) .map(move |item| taker_cfd::MakerStreamMessage { item }); tokio::spawn(cfd_actor_inbox.clone().attach_stream(read)); @@ -305,7 +286,3 @@ async fn main() -> Result<()> { Ok(()) } - -impl xtra::Message for TakerToMaker { - type Result = (); -} diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index f6d8666..5d42921 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -1,4 +1,3 @@ -use crate::actors::log_error; use crate::db::{ insert_cfd, insert_new_cfd_state_by_order_id, insert_order, load_all_cfds, load_cfd_by_order_id, load_cfds_by_oracle_event_id, load_order_by_id, @@ -12,7 +11,7 @@ use crate::model::{BitMexPriceEventId, Usd}; use crate::monitor::{self, MonitorParams}; use crate::wallet::Wallet; use crate::wire::{MakerToTaker, RollOverMsg, SetupMsg}; -use crate::{oracle, send_to_socket, setup_contract, wire}; +use crate::{log_error, oracle, send_to_socket, setup_contract, wire}; use anyhow::{bail, Context as _, Result}; use async_trait::async_trait; use bdk::bitcoin::secp256k1::schnorrsig; diff --git a/daemon/src/wallet_sync.rs b/daemon/src/wallet_sync.rs index 8159d16..5c33fed 100644 --- a/daemon/src/wallet_sync.rs +++ b/daemon/src/wallet_sync.rs @@ -1,5 +1,5 @@ +use crate::model::WalletInfo; use crate::wallet::Wallet; -use crate::WalletInfo; use std::time::Duration; use tokio::sync::watch; use tokio::time::sleep; diff --git a/daemon/src/wire.rs b/daemon/src/wire.rs index 627394d..60d3550 100644 --- a/daemon/src/wire.rs +++ b/daemon/src/wire.rs @@ -1,6 +1,5 @@ -use crate::model::cfd::OrderId; +use crate::model::cfd::{Order, OrderId}; use crate::model::{BitMexPriceEventId, Usd}; -use crate::Order; use anyhow::{bail, Result}; use bdk::bitcoin::secp256k1::Signature; use bdk::bitcoin::util::psbt::PartiallySignedTransaction; @@ -99,8 +98,8 @@ pub struct JsonCodec { inner: LengthDelimitedCodec, } -impl JsonCodec { - pub fn new() -> Self { +impl Default for JsonCodec { + fn default() -> Self { Self { _type: PhantomData, inner: LengthDelimitedCodec::new(),