Browse Source

Only pass the necessary parameters to setup/rollover

Defines the boundaries more clearly which makes it easier when transitioning to the new model.
debug-collab-settlement
Daniel Karzel 3 years ago
parent
commit
500c6a5dfe
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 17
      daemon/src/maker_cfd.rs
  2. 80
      daemon/src/setup_contract.rs
  3. 17
      daemon/src/taker_cfd.rs

17
daemon/src/maker_cfd.rs

@ -6,6 +6,7 @@ use crate::model::cfd::{
}; };
use crate::model::{Price, TakerId, Timestamp, Usd}; use crate::model::{Price, TakerId, Timestamp, Usd};
use crate::monitor::MonitorParams; use crate::monitor::MonitorParams;
use crate::setup_contract::{RolloverParams, SetupParams};
use crate::tokio_ext::FutureExt; use crate::tokio_ext::FutureExt;
use crate::{ use crate::{
log_error, maker_inc_connections, monitor, oracle, projection, setup_contract, wallet, wire, log_error, maker_inc_connections, monitor, oracle, projection, setup_contract, wallet, wire,
@ -605,7 +606,14 @@ where
}), }),
receiver, receiver,
(self.oracle_pk, offer_announcement), (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(), self.wallet.clone(),
Role::Maker, Role::Maker,
self.n_payouts, self.n_payouts,
@ -770,7 +778,12 @@ where
}), }),
receiver, receiver,
(self.oracle_pk, announcement), (self.oracle_pk, announcement),
cfd, RolloverParams::new(
cfd.order.price,
cfd.quantity_usd,
cfd.order.leverage,
cfd.refund_timelock_in_blocks(),
),
Role::Maker, Role::Maker,
dlc, dlc,
self.n_payouts, self.n_payouts,

80
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::tokio_ext::FutureExt;
use crate::wire::{ use crate::wire::{
Msg0, Msg1, Msg2, RollOverMsg, RollOverMsg0, RollOverMsg1, RollOverMsg2, SetupMsg, Msg0, Msg1, Msg2, RollOverMsg, RollOverMsg0, RollOverMsg1, RollOverMsg2, SetupMsg,
@ -24,13 +25,42 @@ use std::ops::RangeInclusive;
use std::time::Duration; use std::time::Duration;
use xtra::Address; 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 /// Given an initial set of parameters, sets up the CFD contract with
/// the other party. /// the other party.
pub async fn new<W>( pub async fn new<W>(
mut sink: impl Sink<SetupMsg, Error = anyhow::Error> + Unpin, mut sink: impl Sink<SetupMsg, Error = anyhow::Error> + Unpin,
mut stream: impl FusedStream<Item = SetupMsg> + Unpin, mut stream: impl FusedStream<Item = SetupMsg> + Unpin,
(oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement), (oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement),
cfd: Cfd, setup_params: SetupParams,
wallet: Address<W>, wallet: Address<W>,
role: Role, role: Role,
n_payouts: usize, n_payouts: usize,
@ -42,10 +72,9 @@ where
let (rev_sk, rev_pk) = crate::keypair::new(&mut rand::thread_rng()); 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 (publish_sk, publish_pk) = crate::keypair::new(&mut rand::thread_rng());
let margin = cfd.margin().context("Failed to calculate margin")?;
let own_params = wallet let own_params = wallet
.send(wallet::BuildPartyParams { .send(wallet::BuildPartyParams {
amount: margin, amount: setup_params.margin,
identity_pk: pk, identity_pk: pk,
}) })
.await .await
@ -74,10 +103,10 @@ where
let params = AllParams::new(own_params, own_punish, other, other_punish, role); 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!( anyhow::bail!(
"Amounts sent by counterparty don't add up, expected margin {} but got {}", "Amounts sent by counterparty don't add up, expected margin {} but got {}",
cfd.counterparty_margin()?, setup_params.counterparty_margin,
params.other.lock_amount params.other.lock_amount
) )
} }
@ -85,9 +114,9 @@ where
let payouts = HashMap::from_iter([( let payouts = HashMap::from_iter([(
announcement.into(), announcement.into(),
payout_curve::calculate( payout_curve::calculate(
cfd.order.price, setup_params.price,
cfd.quantity_usd, setup_params.quantity,
cfd.order.leverage, setup_params.leverage,
n_payouts, n_payouts,
)?, )?,
)]); )]);
@ -96,10 +125,7 @@ where
(params.maker().clone(), *params.maker_punish()), (params.maker().clone(), *params.maker_punish()),
(params.taker().clone(), *params.taker_punish()), (params.taker().clone(), *params.taker_punish()),
oracle_pk, oracle_pk,
( (model::cfd::Cfd::CET_TIMELOCK, setup_params.refund_timelock),
model::cfd::Cfd::CET_TIMELOCK,
cfd.refund_timelock_in_blocks(),
),
payouts, payouts,
sk, 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( pub async fn roll_over(
mut sink: impl Sink<RollOverMsg, Error = anyhow::Error> + Unpin, mut sink: impl Sink<RollOverMsg, Error = anyhow::Error> + Unpin,
mut stream: impl FusedStream<Item = RollOverMsg> + Unpin, mut stream: impl FusedStream<Item = RollOverMsg> + Unpin,
(oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement), (oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement),
cfd: Cfd, rollover_params: RolloverParams,
our_role: Role, our_role: Role,
dlc: Dlc, dlc: Dlc,
n_payouts: usize, n_payouts: usize,
@ -309,9 +353,9 @@ pub async fn roll_over(
nonce_pks: announcement.nonce_pks.clone(), nonce_pks: announcement.nonce_pks.clone(),
}, },
payout_curve::calculate( payout_curve::calculate(
cfd.order.price, rollover_params.price,
cfd.quantity_usd, rollover_params.quantity,
cfd.order.leverage, rollover_params.leverage,
n_payouts, n_payouts,
)?, )?,
)]); )]);
@ -356,7 +400,7 @@ pub async fn roll_over(
oracle_pk, oracle_pk,
( (
model::cfd::Cfd::CET_TIMELOCK, model::cfd::Cfd::CET_TIMELOCK,
cfd.refund_timelock_in_blocks(), rollover_params.refund_timelock,
), ),
payouts, payouts,
sk, sk,

17
daemon/src/taker_cfd.rs

@ -6,6 +6,7 @@ use crate::model::cfd::{
}; };
use crate::model::{BitMexPriceEventId, Price, Timestamp, Usd}; use crate::model::{BitMexPriceEventId, Price, Timestamp, Usd};
use crate::monitor::{self, MonitorParams}; use crate::monitor::{self, MonitorParams};
use crate::setup_contract::{RolloverParams, SetupParams};
use crate::tokio_ext::FutureExt; use crate::tokio_ext::FutureExt;
use crate::wire::{MakerToTaker, RollOverMsg, SetupMsg}; use crate::wire::{MakerToTaker, RollOverMsg, SetupMsg};
use crate::{log_error, oracle, projection, setup_contract, wallet, wire}; use crate::{log_error, oracle, projection, setup_contract, wallet, wire};
@ -495,7 +496,14 @@ where
.with(|msg| future::ok(wire::TakerToMaker::Protocol(msg))), .with(|msg| future::ok(wire::TakerToMaker::Protocol(msg))),
receiver, receiver,
(self.oracle_pk, offer_announcement), (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(), self.wallet.clone(),
Role::Taker, Role::Taker,
self.n_payouts, self.n_payouts,
@ -562,7 +570,12 @@ where
.with(|msg| future::ok(wire::TakerToMaker::RollOverProtocol(msg))), .with(|msg| future::ok(wire::TakerToMaker::RollOverProtocol(msg))),
receiver, receiver,
(self.oracle_pk, announcement), (self.oracle_pk, announcement),
cfd, RolloverParams::new(
cfd.order.price,
cfd.quantity_usd,
cfd.order.leverage,
cfd.refund_timelock_in_blocks(),
),
Role::Taker, Role::Taker,
dlc, dlc,
self.n_payouts, self.n_payouts,

Loading…
Cancel
Save