Browse Source

Merge #686

686: Only pass the necessary parameters to setup/rollover r=da-kami a=da-kami

Defines the boundaries more clearly which makes it easier when transitioning to the new model.

Co-authored-by: Daniel Karzel <daniel@comit.network>
debug-collab-settlement
bors[bot] 3 years ago
committed by GitHub
parent
commit
1b6f1e7bc6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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::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,

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::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<W>(
mut sink: impl Sink<SetupMsg, Error = anyhow::Error> + Unpin,
mut stream: impl FusedStream<Item = SetupMsg> + Unpin,
(oracle_pk, announcement): (schnorrsig::PublicKey, oracle::Announcement),
cfd: Cfd,
setup_params: SetupParams,
wallet: Address<W>,
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<RollOverMsg, Error = anyhow::Error> + Unpin,
mut stream: impl FusedStream<Item = RollOverMsg> + 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,

17
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,

Loading…
Cancel
Save