Browse Source

Merge #412

412: All the term to be configurable on the maker's commandline r=thomaseizinger a=thomaseizinger

- Use the original order's term for rolling over
- Allow term to be configurable on the maker's commandline


Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
contact-taker-before-changing-cfd-state
bors[bot] 3 years ago
committed by GitHub
parent
commit
205d99e19b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      daemon/src/db.rs
  2. 8
      daemon/src/maker.rs
  3. 9
      daemon/src/maker_cfd.rs
  4. 5
      daemon/src/model/cfd.rs
  5. 4
      daemon/src/taker_cfd.rs

1
daemon/src/db.rs

@ -812,6 +812,7 @@ mod tests {
Usd::new(dec!(1000)), Usd::new(dec!(1000)),
Origin::Theirs, Origin::Theirs,
BitMexPriceEventId::with_20_digits(OffsetDateTime::now_utc()), BitMexPriceEventId::with_20_digits(OffsetDateTime::now_utc()),
time::Duration::hours(24),
) )
.unwrap() .unwrap()
} }

8
daemon/src/maker.rs

@ -53,6 +53,10 @@ struct Opts {
#[clap(short, long)] #[clap(short, long)]
json: bool, json: bool,
/// The term length of each CFD in hours.
#[clap(long, default_value = "24")]
term: u8,
#[clap(subcommand)] #[clap(subcommand)]
network: Network, network: Network,
} }
@ -203,6 +207,7 @@ async fn main() -> Result<()> {
}, },
|channel0, channel1| maker_inc_connections::Actor::new(channel0, channel1), |channel0, channel1| maker_inc_connections::Actor::new(channel0, channel1),
listener, listener,
time::Duration::hours(opts.term as i64),
) )
.await?; .await?;
@ -265,6 +270,7 @@ where
+ xtra::Handler<maker_inc_connections::BroadcastOrder> + xtra::Handler<maker_inc_connections::BroadcastOrder>
+ xtra::Handler<maker_inc_connections::ListenerMessage>, + xtra::Handler<maker_inc_connections::ListenerMessage>,
{ {
#[allow(clippy::too_many_arguments)]
pub async fn new<F>( pub async fn new<F>(
db: SqlitePool, db: SqlitePool,
wallet: Wallet, wallet: Wallet,
@ -276,6 +282,7 @@ where
Box<dyn MessageChannel<FromTaker>>, Box<dyn MessageChannel<FromTaker>>,
) -> T, ) -> T,
listener: TcpListener, listener: TcpListener,
term: time::Duration,
) -> Result<Self> ) -> Result<Self>
where where
F: Future<Output = Result<M>>, F: Future<Output = Result<M>>,
@ -296,6 +303,7 @@ where
let cfd_actor_addr = maker_cfd::Actor::new( let cfd_actor_addr = maker_cfd::Actor::new(
db, db,
wallet, wallet,
term,
oracle_pk, oracle_pk,
cfd_feed_sender, cfd_feed_sender,
order_feed_sender, order_feed_sender,

9
daemon/src/maker_cfd.rs

@ -21,6 +21,7 @@ use sqlx::pool::PoolConnection;
use sqlx::Sqlite; use sqlx::Sqlite;
use std::collections::HashMap; use std::collections::HashMap;
use std::time::SystemTime; use std::time::SystemTime;
use time::Duration;
use tokio::sync::watch; use tokio::sync::watch;
use xtra::prelude::*; use xtra::prelude::*;
@ -62,6 +63,7 @@ pub struct FromTaker {
pub struct Actor<O, M, T> { pub struct Actor<O, M, T> {
db: sqlx::SqlitePool, db: sqlx::SqlitePool,
wallet: Wallet, wallet: Wallet,
term: Duration,
oracle_pk: schnorrsig::PublicKey, oracle_pk: schnorrsig::PublicKey,
cfd_feed_actor_inbox: watch::Sender<Vec<Cfd>>, cfd_feed_actor_inbox: watch::Sender<Vec<Cfd>>,
order_feed_sender: watch::Sender<Option<Order>>, order_feed_sender: watch::Sender<Option<Order>>,
@ -98,6 +100,7 @@ impl<O, M, T> Actor<O, M, T> {
pub fn new( pub fn new(
db: sqlx::SqlitePool, db: sqlx::SqlitePool,
wallet: Wallet, wallet: Wallet,
term: Duration,
oracle_pk: schnorrsig::PublicKey, oracle_pk: schnorrsig::PublicKey,
cfd_feed_actor_inbox: watch::Sender<Vec<Cfd>>, cfd_feed_actor_inbox: watch::Sender<Vec<Cfd>>,
order_feed_sender: watch::Sender<Option<Order>>, order_feed_sender: watch::Sender<Option<Order>>,
@ -109,6 +112,7 @@ impl<O, M, T> Actor<O, M, T> {
Self { Self {
db, db,
wallet, wallet,
term,
oracle_pk, oracle_pk,
cfd_feed_actor_inbox, cfd_feed_actor_inbox,
order_feed_sender, order_feed_sender,
@ -637,7 +641,7 @@ where
max_quantity: Usd, max_quantity: Usd,
) -> Result<()> { ) -> Result<()> {
let oracle_event_id = let oracle_event_id =
oracle::next_announcement_after(time::OffsetDateTime::now_utc() + Order::TERM)?; oracle::next_announcement_after(time::OffsetDateTime::now_utc() + self.term)?;
self.oracle_actor self.oracle_actor
.do_send_async(oracle::FetchAnnouncement(oracle_event_id)) .do_send_async(oracle::FetchAnnouncement(oracle_event_id))
@ -649,6 +653,7 @@ where
max_quantity, max_quantity,
Origin::Ours, Origin::Ours,
oracle_event_id, oracle_event_id,
self.term,
)?; )?;
// 1. Save to DB // 1. Save to DB
@ -751,7 +756,7 @@ where
let dlc = cfd.open_dlc().context("CFD was in wrong state")?; let dlc = cfd.open_dlc().context("CFD was in wrong state")?;
let oracle_event_id = let oracle_event_id =
oracle::next_announcement_after(time::OffsetDateTime::now_utc() + Order::TERM)?; oracle::next_announcement_after(time::OffsetDateTime::now_utc() + cfd.order.term)?;
let announcement = self let announcement = self
.oracle_actor .oracle_actor
.send(oracle::GetAnnouncement(oracle_event_id)) .send(oracle::GetAnnouncement(oracle_event_id))

5
daemon/src/model/cfd.rs

@ -125,14 +125,13 @@ pub struct Order {
} }
impl Order { impl Order {
pub const TERM: Duration = Duration::hours(24);
pub fn new( pub fn new(
price: Usd, price: Usd,
min_quantity: Usd, min_quantity: Usd,
max_quantity: Usd, max_quantity: Usd,
origin: Origin, origin: Origin,
oracle_event_id: BitMexPriceEventId, oracle_event_id: BitMexPriceEventId,
term: Duration,
) -> Result<Self> { ) -> Result<Self> {
let leverage = Leverage(2); let leverage = Leverage(2);
let maintenance_margin_rate = dec!(0.005); let maintenance_margin_rate = dec!(0.005);
@ -149,7 +148,7 @@ impl Order {
liquidation_price, liquidation_price,
position: Position::Sell, position: Position::Sell,
creation_timestamp: SystemTime::now(), creation_timestamp: SystemTime::now(),
term: Self::TERM, term,
origin, origin,
oracle_event_id, oracle_event_id,
}) })

4
daemon/src/taker_cfd.rs

@ -340,6 +340,8 @@ where
anyhow::bail!("An update for order id {} is already in progress", order_id) anyhow::bail!("An update for order id {} is already in progress", order_id)
} }
let order = load_order_by_id(order_id, &mut self.db.acquire().await?).await?;
let proposal = RollOverProposal { let proposal = RollOverProposal {
order_id, order_id,
timestamp: SystemTime::now(), timestamp: SystemTime::now(),
@ -357,7 +359,7 @@ where
// we are likely going to need this one // we are likely going to need this one
self.oracle_actor self.oracle_actor
.send(oracle::FetchAnnouncement(oracle::next_announcement_after( .send(oracle::FetchAnnouncement(oracle::next_announcement_after(
OffsetDateTime::now_utc() + Order::TERM, OffsetDateTime::now_utc() + order.term,
)?)) )?))
.await?; .await?;

Loading…
Cancel
Save