diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 6738f33..1a5ea9c 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -6,6 +6,7 @@ use crate::model::cfd::{ }; use crate::model::{Price, TakerId, Timestamp, Usd}; use crate::monitor::MonitorParams; +use crate::setup_contract::{RolloverParams, SetupParams}; use crate::tokio_ext::FutureExt; use crate::{ log_error, maker_inc_connections, monitor, oracle, projection, setup_contract, wallet, wire, @@ -605,7 +606,14 @@ where }), receiver, (self.oracle_pk, offer_announcement), - cfd, + SetupParams::new( + cfd.margin()?, + cfd.counterparty_margin()?, + cfd.order.price, + cfd.quantity_usd, + cfd.order.leverage, + cfd.refund_timelock_in_blocks(), + ), self.wallet.clone(), Role::Maker, self.n_payouts, @@ -770,7 +778,12 @@ where }), receiver, (self.oracle_pk, announcement), - cfd, + RolloverParams::new( + cfd.order.price, + cfd.quantity_usd, + cfd.order.leverage, + cfd.refund_timelock_in_blocks(), + ), Role::Maker, dlc, self.n_payouts, diff --git a/daemon/src/setup_contract.rs b/daemon/src/setup_contract.rs index 515ada0..e589e87 100644 --- a/daemon/src/setup_contract.rs +++ b/daemon/src/setup_contract.rs @@ -1,4 +1,5 @@ -use crate::model::cfd::{Cet, Cfd, Dlc, RevokedCommit, Role}; +use crate::model::cfd::{Cet, Dlc, RevokedCommit, Role}; +use crate::model::{Leverage, Price, Usd}; use crate::tokio_ext::FutureExt; use crate::wire::{ Msg0, Msg1, Msg2, RollOverMsg, RollOverMsg0, RollOverMsg1, RollOverMsg2, SetupMsg, @@ -24,13 +25,42 @@ use std::ops::RangeInclusive; use std::time::Duration; use xtra::Address; +pub struct SetupParams { + margin: Amount, + counterparty_margin: Amount, + price: Price, + quantity: Usd, + leverage: Leverage, + refund_timelock: u32, +} + +impl SetupParams { + pub fn new( + margin: Amount, + counterparty_margin: Amount, + price: Price, + quantity: Usd, + leverage: Leverage, + refund_timelock: u32, + ) -> Self { + Self { + margin, + counterparty_margin, + price, + quantity, + leverage, + refund_timelock, + } + } +} + /// Given an initial set of parameters, sets up the CFD contract with /// the other party. pub async fn new( mut sink: impl Sink + Unpin, mut stream: impl FusedStream + Unpin, (oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement), - cfd: Cfd, + setup_params: SetupParams, wallet: Address, role: Role, n_payouts: usize, @@ -42,10 +72,9 @@ where let (rev_sk, rev_pk) = crate::keypair::new(&mut rand::thread_rng()); let (publish_sk, publish_pk) = crate::keypair::new(&mut rand::thread_rng()); - let margin = cfd.margin().context("Failed to calculate margin")?; let own_params = wallet .send(wallet::BuildPartyParams { - amount: margin, + amount: setup_params.margin, identity_pk: pk, }) .await @@ -74,10 +103,10 @@ where let params = AllParams::new(own_params, own_punish, other, other_punish, role); - if params.other.lock_amount != cfd.counterparty_margin()? { + if params.other.lock_amount != setup_params.counterparty_margin { anyhow::bail!( "Amounts sent by counterparty don't add up, expected margin {} but got {}", - cfd.counterparty_margin()?, + setup_params.counterparty_margin, params.other.lock_amount ) } @@ -85,9 +114,9 @@ where let payouts = HashMap::from_iter([( announcement.into(), payout_curve::calculate( - cfd.order.price, - cfd.quantity_usd, - cfd.order.leverage, + setup_params.price, + setup_params.quantity, + setup_params.leverage, n_payouts, )?, )]); @@ -96,10 +125,7 @@ where (params.maker().clone(), *params.maker_punish()), (params.taker().clone(), *params.taker_punish()), oracle_pk, - ( - model::cfd::Cfd::CET_TIMELOCK, - cfd.refund_timelock_in_blocks(), - ), + (model::cfd::Cfd::CET_TIMELOCK, setup_params.refund_timelock), payouts, sk, ) @@ -266,11 +292,29 @@ where }) } +pub struct RolloverParams { + price: Price, + quantity: Usd, + leverage: Leverage, + refund_timelock: u32, +} + +impl RolloverParams { + pub fn new(price: Price, quantity: Usd, leverage: Leverage, refund_timelock: u32) -> Self { + Self { + price, + quantity, + leverage, + refund_timelock, + } + } +} + pub async fn roll_over( mut sink: impl Sink + Unpin, mut stream: impl FusedStream + Unpin, (oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement), - cfd: Cfd, + rollover_params: RolloverParams, our_role: Role, dlc: Dlc, n_payouts: usize, @@ -309,9 +353,9 @@ pub async fn roll_over( nonce_pks: announcement.nonce_pks.clone(), }, payout_curve::calculate( - cfd.order.price, - cfd.quantity_usd, - cfd.order.leverage, + rollover_params.price, + rollover_params.quantity, + rollover_params.leverage, n_payouts, )?, )]); @@ -356,7 +400,7 @@ pub async fn roll_over( oracle_pk, ( model::cfd::Cfd::CET_TIMELOCK, - cfd.refund_timelock_in_blocks(), + rollover_params.refund_timelock, ), payouts, sk, diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index ecdbf87..e8a5dd9 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -6,6 +6,7 @@ use crate::model::cfd::{ }; use crate::model::{BitMexPriceEventId, Price, Timestamp, Usd}; use crate::monitor::{self, MonitorParams}; +use crate::setup_contract::{RolloverParams, SetupParams}; use crate::tokio_ext::FutureExt; use crate::wire::{MakerToTaker, RollOverMsg, SetupMsg}; use crate::{log_error, oracle, projection, setup_contract, wallet, wire}; @@ -495,7 +496,14 @@ where .with(|msg| future::ok(wire::TakerToMaker::Protocol(msg))), receiver, (self.oracle_pk, offer_announcement), - cfd, + SetupParams::new( + cfd.margin()?, + cfd.counterparty_margin()?, + cfd.order.price, + cfd.quantity_usd, + cfd.order.leverage, + cfd.refund_timelock_in_blocks(), + ), self.wallet.clone(), Role::Taker, self.n_payouts, @@ -562,7 +570,12 @@ where .with(|msg| future::ok(wire::TakerToMaker::RollOverProtocol(msg))), receiver, (self.oracle_pk, announcement), - cfd, + RolloverParams::new( + cfd.order.price, + cfd.quantity_usd, + cfd.order.leverage, + cfd.refund_timelock_in_blocks(), + ), Role::Taker, dlc, self.n_payouts,