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)),
Origin::Theirs,
BitMexPriceEventId::with_20_digits(OffsetDateTime::now_utc()),
time::Duration::hours(24),
)
.unwrap()
}

8
daemon/src/maker.rs

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

9
daemon/src/maker_cfd.rs

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

5
daemon/src/model/cfd.rs

@ -125,14 +125,13 @@ pub struct Order {
}
impl Order {
pub const TERM: Duration = Duration::hours(24);
pub fn new(
price: Usd,
min_quantity: Usd,
max_quantity: Usd,
origin: Origin,
oracle_event_id: BitMexPriceEventId,
term: Duration,
) -> Result<Self> {
let leverage = Leverage(2);
let maintenance_margin_rate = dec!(0.005);
@ -149,7 +148,7 @@ impl Order {
liquidation_price,
position: Position::Sell,
creation_timestamp: SystemTime::now(),
term: Self::TERM,
term,
origin,
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)
}
let order = load_order_by_id(order_id, &mut self.db.acquire().await?).await?;
let proposal = RollOverProposal {
order_id,
timestamp: SystemTime::now(),
@ -357,7 +359,7 @@ where
// we are likely going to need this one
self.oracle_actor
.send(oracle::FetchAnnouncement(oracle::next_announcement_after(
OffsetDateTime::now_utc() + Order::TERM,
OffsetDateTime::now_utc() + order.term,
)?))
.await?;

Loading…
Cancel
Save