diff --git a/daemon/migrations/20211208000000_associate_counterpart_identity_with_cfd.sql b/daemon/migrations/20211208000000_associate_counterpart_identity_with_cfd.sql new file mode 100644 index 0000000..eb1f57d --- /dev/null +++ b/daemon/migrations/20211208000000_associate_counterpart_identity_with_cfd.sql @@ -0,0 +1,25 @@ +-- Add migration script here +drop table cfd_states; +drop table cfds; + +create table if not exists cfds +( + id integer primary key autoincrement, + order_id integer unique not null, + order_uuid text unique not null, + quantity_usd text not null, + counterparty text not null, + foreign key (order_id) references orders (id) +); + +create unique index if not exists cfd_order_uuid + on cfds (order_uuid); + +create table if not exists cfd_states +( + id integer primary key autoincrement, + cfd_id integer not null, + state text not null, + foreign key (cfd_id) references cfds (id) +); + diff --git a/daemon/sqlx-data.json b/daemon/sqlx-data.json index 1218a6e..51c1aec 100644 --- a/daemon/sqlx-data.json +++ b/daemon/sqlx-data.json @@ -108,8 +108,8 @@ ] } }, - "278833084178753ca86aa8bf9ac50fad8b6fd22dd978e2c803a8b7ba79bd5e1d": { - "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n ", + "28d8b9ddd2dd85a9096200e6abd170a09ed35e1c905c081e535e19800016cd7d": { + "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd,\n counterparty\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n cfd.counterparty,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.counterparty as \"counterparty: crate::model::Identity\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n\n where ord.uuid = $1\n ", "describe": { "columns": [ { @@ -183,13 +183,18 @@ "type_info": "Text" }, { - "name": "state", + "name": "counterparty: crate::model::Identity", "ordinal": 14, "type_info": "Text" + }, + { + "name": "state", + "ordinal": 15, + "type_info": "Text" } ], "parameters": { - "Right": 0 + "Right": 1 }, "nullable": [ false, @@ -206,12 +211,31 @@ false, false, false, + false, false ] } }, - "898c1f59733397d90d39a73e160f47ab8dc68322f5b421056ffdf38c911cac2d": { - "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n\n where ord.oracle_event_id = $1\n ", + "8cbe349911b35d8e79763d64b4f5813b4bd98f12e0bba5ada84d2cae8b08ef4f": { + "query": "\n select\n id\n from cfds\n where order_uuid = $1;\n ", + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Int64" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + true + ] + } + }, + "ef4fb6c58e79051bd09ad04f59b7896df5228c6c848c999149668d7c733115c2": { + "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd,\n counterparty\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n cfd.counterparty,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.counterparty as \"counterparty: crate::model::Identity\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n ", "describe": { "columns": [ { @@ -285,13 +309,18 @@ "type_info": "Text" }, { - "name": "state", + "name": "counterparty: crate::model::Identity", "ordinal": 14, "type_info": "Text" + }, + { + "name": "state", + "ordinal": 15, + "type_info": "Text" } ], "parameters": { - "Right": 1 + "Right": 0 }, "nullable": [ false, @@ -308,30 +337,13 @@ false, false, false, + false, false ] } }, - "8cbe349911b35d8e79763d64b4f5813b4bd98f12e0bba5ada84d2cae8b08ef4f": { - "query": "\n select\n id\n from cfds\n where order_uuid = $1;\n ", - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Int64" - } - ], - "parameters": { - "Right": 1 - }, - "nullable": [ - true - ] - } - }, - "a6031f4720d1c6acf7043c9f9ee5035f5ffea0520220ad6b578b7ad0117ab58f": { - "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n\n where ord.uuid = $1\n ", + "f6ac595731d61e166b2afaa4923e9f05c75cfad6d1d734ab43e7567d0e29adec": { + "query": "\n with ord as (\n select\n id as order_id,\n uuid,\n trading_pair,\n position,\n initial_price,\n min_quantity,\n max_quantity,\n leverage,\n liquidation_price,\n creation_timestamp_seconds as ts_secs,\n settlement_time_interval_seconds as settlement_time_interval_secs,\n origin,\n oracle_event_id,\n fee_rate\n from orders\n ),\n\n cfd as (\n select\n ord.order_id,\n id as cfd_id,\n quantity_usd,\n counterparty\n from cfds\n inner join ord on ord.order_id = cfds.order_id\n ),\n\n state as (\n select\n id as state_id,\n cfd.order_id,\n cfd.quantity_usd,\n cfd.counterparty,\n state\n from cfd_states\n inner join cfd on cfd.cfd_id = cfd_states.cfd_id\n where id in (\n select\n max(id) as id\n from cfd_states\n group by (cfd_id)\n )\n )\n\n select\n ord.uuid as \"uuid: crate::model::cfd::OrderId\",\n ord.trading_pair as \"trading_pair: crate::model::TradingPair\",\n ord.position as \"position: crate::model::Position\",\n ord.initial_price as \"initial_price: crate::model::Price\",\n ord.min_quantity as \"min_quantity: crate::model::Usd\",\n ord.max_quantity as \"max_quantity: crate::model::Usd\",\n ord.leverage as \"leverage: crate::model::Leverage\",\n ord.liquidation_price as \"liquidation_price: crate::model::Price\",\n ord.ts_secs as \"ts_secs: crate::model::Timestamp\",\n ord.settlement_time_interval_secs as \"settlement_time_interval_secs: i64\",\n ord.origin as \"origin: crate::model::cfd::Origin\",\n ord.oracle_event_id as \"oracle_event_id: crate::model::BitMexPriceEventId\",\n ord.fee_rate as \"fee_rate: u32\",\n state.quantity_usd as \"quantity_usd: crate::model::Usd\",\n state.counterparty as \"counterparty: crate::model::Identity\",\n state.state\n\n from ord\n inner join state on state.order_id = ord.order_id\n\n where ord.oracle_event_id = $1\n ", "describe": { "columns": [ { @@ -405,9 +417,14 @@ "type_info": "Text" }, { - "name": "state", + "name": "counterparty: crate::model::Identity", "ordinal": 14, "type_info": "Text" + }, + { + "name": "state", + "ordinal": 15, + "type_info": "Text" } ], "parameters": { @@ -428,6 +445,7 @@ false, false, false, + false, false ] } diff --git a/daemon/src/db.rs b/daemon/src/db.rs index 0a65cd3..f677361 100644 --- a/daemon/src/db.rs +++ b/daemon/src/db.rs @@ -113,12 +113,14 @@ pub async fn insert_cfd(cfd: &Cfd, conn: &mut PoolConnection) -> anyhow: insert into cfds ( order_id, order_uuid, - quantity_usd + quantity_usd, + counterparty ) select id as order_id, uuid as order_uuid, - $2 as quantity_usd + $2 as quantity_usd, + $3 as counterparty from orders where uuid = $1; @@ -128,13 +130,14 @@ pub async fn insert_cfd(cfd: &Cfd, conn: &mut PoolConnection) -> anyhow: ) select id as cfd_id, - $3 as state + $4 as state from cfds order by id desc limit 1; "#, ) .bind(&cfd.order.id) .bind(&cfd.quantity_usd) + .bind(&cfd.counterparty) .bind(state) .execute(conn) .await?; @@ -254,7 +257,8 @@ pub async fn load_cfd_by_order_id( select ord.order_id, id as cfd_id, - quantity_usd + quantity_usd, + counterparty from cfds inner join ord on ord.order_id = cfds.order_id ), @@ -264,6 +268,7 @@ pub async fn load_cfd_by_order_id( id as state_id, cfd.order_id, cfd.quantity_usd, + cfd.counterparty, state from cfd_states inner join cfd on cfd.cfd_id = cfd_states.cfd_id @@ -290,6 +295,7 @@ pub async fn load_cfd_by_order_id( ord.oracle_event_id as "oracle_event_id: crate::model::BitMexPriceEventId", ord.fee_rate as "fee_rate: u32", state.quantity_usd as "quantity_usd: crate::model::Usd", + state.counterparty as "counterparty: crate::model::Identity", state.state from ord @@ -325,6 +331,7 @@ pub async fn load_cfd_by_order_id( order, quantity_usd: row.quantity_usd, state: serde_json::from_str(row.state.as_str())?, + counterparty: row.counterparty, }) } @@ -355,7 +362,8 @@ pub async fn load_all_cfds(conn: &mut PoolConnection) -> anyhow::Result< select ord.order_id, id as cfd_id, - quantity_usd + quantity_usd, + counterparty from cfds inner join ord on ord.order_id = cfds.order_id ), @@ -365,6 +373,7 @@ pub async fn load_all_cfds(conn: &mut PoolConnection) -> anyhow::Result< id as state_id, cfd.order_id, cfd.quantity_usd, + cfd.counterparty, state from cfd_states inner join cfd on cfd.cfd_id = cfd_states.cfd_id @@ -391,6 +400,7 @@ pub async fn load_all_cfds(conn: &mut PoolConnection) -> anyhow::Result< ord.oracle_event_id as "oracle_event_id: crate::model::BitMexPriceEventId", ord.fee_rate as "fee_rate: u32", state.quantity_usd as "quantity_usd: crate::model::Usd", + state.counterparty as "counterparty: crate::model::Identity", state.state from ord @@ -423,6 +433,7 @@ pub async fn load_all_cfds(conn: &mut PoolConnection) -> anyhow::Result< order, quantity_usd: row.quantity_usd, state: serde_json::from_str(row.state.as_str())?, + counterparty: row.counterparty, }) }) .collect::>>()?; @@ -461,7 +472,8 @@ pub async fn load_cfds_by_oracle_event_id( select ord.order_id, id as cfd_id, - quantity_usd + quantity_usd, + counterparty from cfds inner join ord on ord.order_id = cfds.order_id ), @@ -471,6 +483,7 @@ pub async fn load_cfds_by_oracle_event_id( id as state_id, cfd.order_id, cfd.quantity_usd, + cfd.counterparty, state from cfd_states inner join cfd on cfd.cfd_id = cfd_states.cfd_id @@ -497,6 +510,7 @@ pub async fn load_cfds_by_oracle_event_id( ord.oracle_event_id as "oracle_event_id: crate::model::BitMexPriceEventId", ord.fee_rate as "fee_rate: u32", state.quantity_usd as "quantity_usd: crate::model::Usd", + state.counterparty as "counterparty: crate::model::Identity", state.state from ord @@ -532,6 +546,7 @@ pub async fn load_cfds_by_oracle_event_id( order, quantity_usd: row.quantity_usd, state: serde_json::from_str(row.state.as_str())?, + counterparty: row.counterparty, }) }) .collect::>>()?; @@ -541,6 +556,11 @@ pub async fn load_cfds_by_oracle_event_id( #[cfg(test)] mod tests { + use super::*; + use crate::db::insert_order; + use crate::model::cfd::{Cfd, CfdState, Order, Origin}; + use crate::model::{Identity, Price, Usd}; + use crate::seed::Seed; use pretty_assertions::assert_eq; use rand::Rng; use rust_decimal_macros::dec; @@ -548,12 +568,6 @@ mod tests { use time::macros::datetime; use time::OffsetDateTime; - use crate::db::insert_order; - use crate::model::cfd::{Cfd, CfdState, Order, Origin}; - use crate::model::{Price, Usd}; - - use super::*; - #[tokio::test] async fn test_insert_and_load_order() { let mut conn = setup_test_db().await; @@ -753,10 +767,14 @@ mod tests { impl Cfd { fn dummy() -> Self { + let (pub_key, _) = Seed::default().derive_identity(); + let dummy_identity = Identity::new(pub_key); + Cfd::new( Order::dummy(), Usd::new(dec!(1000)), CfdState::outgoing_order_request(), + dummy_identity, ) } diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs index e0c5d97..dc2ca40 100644 --- a/daemon/src/lib.rs +++ b/daemon/src/lib.rs @@ -3,6 +3,7 @@ use crate::db::load_all_cfds; use crate::maker_cfd::{FromTaker, TakerConnected}; use crate::model::cfd::{Cfd, Order, UpdateCfdProposals}; +use crate::model::Identity; use crate::oracle::Attestation; use crate::tokio_ext::FutureExt; use address_map::Stopping; @@ -235,6 +236,7 @@ where maker_heartbeat_interval: Duration, connect_timeout: Duration, projection_actor: Address, + maker_identity: Identity, ) -> Result where F: Future>, @@ -261,6 +263,7 @@ where monitor_addr.clone(), oracle_addr, n_payouts, + maker_identity, ) .create(None) .run(); diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index fc5ac7a..2807288 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -478,6 +478,7 @@ where }, taker_id, }, + taker_id, ); insert_cfd_and_update_feed(&cfd, &mut conn, &self.projection_actor).await?; diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 67d72e9..74a4088 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -577,16 +577,19 @@ pub struct Cfd { pub order: Order, pub quantity_usd: Usd, pub state: CfdState, + + pub counterparty: Identity, /* TODO: Leverage is currently derived from the Order, but the actual leverage should be * stored in the Cfd once there is multiple choices of leverage */ } impl Cfd { - pub fn new(order: Order, quantity: Usd, state: CfdState) -> Self { + pub fn new(order: Order, quantity: Usd, state: CfdState, counterparty: Identity) -> Self { Cfd { order, quantity_usd: quantity, state, + counterparty, } } diff --git a/daemon/src/projection.rs b/daemon/src/projection.rs index 899f907..cf8fcdb 100644 --- a/daemon/src/projection.rs +++ b/daemon/src/projection.rs @@ -500,6 +500,8 @@ pub struct Cfd { #[serde(with = "::time::serde::timestamp")] pub expiry_timestamp: OffsetDateTime, + + pub counterparty: Identity, } impl From for Vec { @@ -546,6 +548,7 @@ impl From for Vec { None => cfd.order.oracle_event_id.timestamp(), Some(timestamp) => timestamp, }, + counterparty: cfd.counterparty.into(), } }) .collect::>(); diff --git a/daemon/src/setup_maker.rs b/daemon/src/setup_maker.rs index a218e1a..740c3fc 100644 --- a/daemon/src/setup_maker.rs +++ b/daemon/src/setup_maker.rs @@ -70,6 +70,7 @@ impl Actor { self.order.clone(), self.quantity, CfdState::contract_setup(), + self.taker_id, ); let (sender, receiver) = mpsc::unbounded(); diff --git a/daemon/src/setup_taker.rs b/daemon/src/setup_taker.rs index 6a327e7..4c7ea85 100644 --- a/daemon/src/setup_taker.rs +++ b/daemon/src/setup_taker.rs @@ -1,5 +1,5 @@ use crate::model::cfd::{Cfd, CfdState, Completed, Dlc, Order, OrderId, Role}; -use crate::model::Usd; +use crate::model::{Identity, Usd}; use crate::oracle::Announcement; use crate::setup_contract::{self, SetupParams}; use crate::tokio_ext::spawn_fallible; @@ -25,9 +25,11 @@ pub struct Actor { on_accepted: Box>, on_completed: Box>, setup_msg_sender: Option>, + maker_identity: Identity, } impl Actor { + #[allow(clippy::too_many_arguments)] pub fn new( (order, quantity, n_payouts): (Order, Usd, usize), (oracle_pk, announcement): (schnorrsig::PublicKey, Announcement), @@ -36,6 +38,7 @@ impl Actor { maker: xtra::Address, on_accepted: &(impl MessageChannel + 'static), on_completed: &(impl MessageChannel + 'static), + maker_identity: Identity, ) -> Self { Self { order, @@ -49,6 +52,7 @@ impl Actor { on_accepted: on_accepted.clone_channel(), on_completed: on_completed.clone_channel(), setup_msg_sender: None, + maker_identity, } } } @@ -67,6 +71,7 @@ impl Actor { self.order.clone(), self.quantity, CfdState::contract_setup(), + self.maker_identity, ); let (sender, receiver) = mpsc::unbounded::(); diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index 467d376..39a4025 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -162,6 +162,8 @@ async fn main() -> Result<()> { tokio::fs::create_dir_all(&data_dir).await?; } + let maker_identity = Identity::new(opts.maker_id); + let seed = Seed::initialize(&data_dir.join("taker_seed")).await?; let bitcoin_network = opts.network.bitcoin_network(); @@ -238,6 +240,7 @@ async fn main() -> Result<()> { HEARTBEAT_INTERVAL * 2, Duration::from_secs(10), projection_actor.clone(), + maker_identity, ) .await?; @@ -259,7 +262,7 @@ async fn main() -> Result<()> { tasks.add(connect( maker_online_status_feed_receiver.clone(), connection_actor_addr, - Identity::new(opts.maker_id), + maker_identity, possible_addresses, )); diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 2614895..be4a14d 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -2,7 +2,7 @@ use crate::address_map::{AddressMap, Stopping}; use crate::cfd_actors::{self, append_cfd_state, insert_cfd_and_update_feed}; use crate::db::{insert_order, load_cfd_by_order_id, load_order_by_id}; use crate::model::cfd::{Cfd, CfdState, CfdStateCommon, Completed, Order, OrderId, Origin, Role}; -use crate::model::{Price, Usd}; +use crate::model::{Identity, Price, Usd}; use crate::monitor::{self, MonitorParams}; use crate::{ collab_settlement_taker, connection, log_error, oracle, projection, rollover_taker, @@ -48,6 +48,7 @@ pub struct Actor { oracle_actor: Address, n_payouts: usize, tasks: Tasks, + maker_identity: Identity, } impl Actor @@ -66,6 +67,7 @@ where monitor_actor: Address, oracle_actor: Address, n_payouts: usize, + maker_identity: Identity, ) -> Self { Self { db, @@ -80,6 +82,7 @@ where collab_settlement_actors: AddressMap::default(), rollover_actors: AddressMap::default(), tasks: Tasks::default(), + maker_identity, } } } @@ -295,6 +298,7 @@ where current_order.clone(), quantity, CfdState::outgoing_order_request(), + self.maker_identity, ); insert_cfd_and_update_feed(&cfd, &mut conn, &self.projection_actor).await?; @@ -321,6 +325,7 @@ where self.conn_actor.clone(), &this, &this, + self.maker_identity, ) .create(None) .run(); diff --git a/daemon/tests/harness/mod.rs b/daemon/tests/harness/mod.rs index 69d9334..0a6d299 100644 --- a/daemon/tests/harness/mod.rs +++ b/daemon/tests/harness/mod.rs @@ -292,6 +292,7 @@ impl Taker { config.heartbeat_timeout, Duration::from_secs(10), projection_actor, + maker_identity, ) .await .unwrap(); diff --git a/maker-frontend/src/components/Types.tsx b/maker-frontend/src/components/Types.tsx index a7b7e80..60ea2a6 100644 --- a/maker-frontend/src/components/Types.tsx +++ b/maker-frontend/src/components/Types.tsx @@ -50,6 +50,8 @@ export interface Cfd { state_transition_timestamp: number; details: CfdDetails; expiry_timestamp: number; + + counterparty: string; } export interface CfdDetails { diff --git a/taker-frontend/src/types.ts b/taker-frontend/src/types.ts index ca67554..ebbf9e7 100644 --- a/taker-frontend/src/types.ts +++ b/taker-frontend/src/types.ts @@ -59,6 +59,8 @@ export interface Cfd { state_transition_timestamp: number; details: CfdDetails; expiry_timestamp: number; + + counterparty: string; } export interface CfdDetails {