diff --git a/Cargo.lock b/Cargo.lock index 29a2134cc..5cdb12be6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1550,7 +1550,6 @@ dependencies = [ "clap", "diesel", "diesel_migrations", - "dlc-manager", "futures", "hex 0.4.3", "ln-dlc-node", @@ -1667,7 +1666,6 @@ dependencies = [ "bdk", "diesel", "diesel_migrations", - "dlc-manager", "flutter_rust_bridge", "lightning-invoice", "ln-dlc-node", diff --git a/coordinator/Cargo.toml b/coordinator/Cargo.toml index 8e5ad199d..63897f713 100644 --- a/coordinator/Cargo.toml +++ b/coordinator/Cargo.toml @@ -12,7 +12,7 @@ bitcoin = "0.29" clap = { version = "4", features = ["derive"] } diesel = { version = "2.0.0", features = ["r2d2", "postgres"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.4.0", features = ["use-serde"] } +dlc-manager = { version = "0.4.0", features = ["use-serde"] } # required for the ContractInput type futures = "0.3" ln-dlc-node = { path = "../crates/ln-dlc-node" } rand = "0.8.5" diff --git a/coordinator/src/routes.rs b/coordinator/src/routes.rs index 582f9c980..4fd04133b 100644 --- a/coordinator/src/routes.rs +++ b/coordinator/src/routes.rs @@ -20,7 +20,6 @@ use diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; use diesel::PgConnection; use dlc_manager::contract::contract_input::ContractInput; -use dlc_manager::Wallet; use ln_dlc_node::node::Node; use serde::Deserialize; use serde::Serialize; @@ -88,10 +87,10 @@ pub async fn post_fake_scid( pub async fn get_new_address( State(app_state): State>, ) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; Ok(Json(address.to_string())) } diff --git a/crates/ln-dlc-node/src/lib.rs b/crates/ln-dlc-node/src/lib.rs index 42ebaf69d..04a463126 100644 --- a/crates/ln-dlc-node/src/lib.rs +++ b/crates/ln-dlc-node/src/lib.rs @@ -38,6 +38,8 @@ pub mod seed; #[cfg(test)] mod tests; +pub use node::dlc_channel::Dlc; + type ConfirmableMonitor = ( ChannelMonitor, Arc, diff --git a/crates/ln-dlc-node/src/node/dlc_channel.rs b/crates/ln-dlc-node/src/node/dlc_channel.rs index 1f7bf2def..ff1d14401 100644 --- a/crates/ln-dlc-node/src/node/dlc_channel.rs +++ b/crates/ln-dlc-node/src/node/dlc_channel.rs @@ -27,6 +27,12 @@ use std::time::UNIX_EPOCH; use trade::cfd::calculate_margin; use trade::TradeParams; +pub struct Dlc { + pub id: [u8; 32], + pub offer_collateral: u64, + pub accept_collateral: u64, +} + impl Node { // TODO: This API doesn't belong in this crate! pub fn trade(&self, trade_params: TradeParams) -> Result { @@ -130,11 +136,29 @@ impl Node { Ok(()) } - pub fn get_dlcs(&self) -> Result> { - self.dlc_manager + pub fn get_confirmed_dlcs(&self) -> Result> { + let confimed_dlcs = self + .dlc_manager .get_store() .get_contracts() - .map_err(|e| anyhow!("Unable to get contracts from manager: {e:#}")) + .map_err(|e| anyhow!("Unable to get contracts from manager: {e:#}"))? + .iter() + .filter_map(|contract| match contract { + Contract::Confirmed(signed) => Some((contract.get_id(), signed)), + _ => None, + }) + .map(|(id, signed)| Dlc { + id, + offer_collateral: signed + .accepted_contract + .offered_contract + .offer_params + .collateral, + accept_collateral: signed.accepted_contract.accept_params.collateral, + }) + .collect(); + + Ok(confimed_dlcs) } pub fn process_incoming_messages(&self) -> Result<()> { diff --git a/crates/ln-dlc-node/src/node/mod.rs b/crates/ln-dlc-node/src/node/mod.rs index b17608486..c4be28f24 100644 --- a/crates/ln-dlc-node/src/node/mod.rs +++ b/crates/ln-dlc-node/src/node/mod.rs @@ -57,7 +57,7 @@ use std::time::SystemTime; mod channel_manager; mod connection; -mod dlc_channel; +pub(crate) mod dlc_channel; mod dlc_manager; pub(crate) mod invoice; mod ln_channel; @@ -76,7 +76,7 @@ const BROADCAST_NODE_ANNOUNCEMENT_INTERVAL: Duration = Duration::from_secs(60); pub struct Node { network: Network, - pub wallet: Arc, + pub(crate) wallet: Arc, pub(crate) peer_manager: Arc, invoice_payer: Arc>, pub(crate) channel_manager: Arc, diff --git a/crates/ln-dlc-node/src/node/wallet.rs b/crates/ln-dlc-node/src/node/wallet.rs index 181021877..eb7859e66 100644 --- a/crates/ln-dlc-node/src/node/wallet.rs +++ b/crates/ln-dlc-node/src/node/wallet.rs @@ -1,6 +1,8 @@ use crate::node::Node; use anyhow::anyhow; use anyhow::Result; +use bdk::wallet::AddressIndex; +use bitcoin::Address; use lightning::chain::Confirm; #[derive(Debug, Clone)] @@ -19,6 +21,17 @@ impl Node { self.wallet.inner().sync(confirmables).unwrap(); } + pub fn get_new_address(&self) -> Result
{ + let address = self + .wallet + .inner() + .get_wallet() + .unwrap() + .get_address(AddressIndex::New)?; + + Ok(address.address) + } + pub fn get_on_chain_balance(&self) -> Result { self.wallet.inner().get_balance().map_err(|e| anyhow!(e)) } diff --git a/maker/Cargo.toml b/maker/Cargo.toml index e68deeb57..f498b363f 100644 --- a/maker/Cargo.toml +++ b/maker/Cargo.toml @@ -12,7 +12,6 @@ bitcoin = "0.29" clap = { version = "4", features = ["derive"] } diesel = { version = "2.0.0", features = ["r2d2", "postgres"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.4.0", features = ["use-serde"] } futures = "0.3" hex = "0.4.3" ln-dlc-node = { path = "../crates/ln-dlc-node" } diff --git a/maker/src/routes.rs b/maker/src/routes.rs index 1424e3d25..e03d2883c 100644 --- a/maker/src/routes.rs +++ b/maker/src/routes.rs @@ -13,7 +13,6 @@ use diesel::r2d2; use diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; use diesel::PgConnection; -use dlc_manager::Wallet; use ln_dlc_node::node::Node; use ln_dlc_node::node::NodeInfo; use serde::Deserialize; @@ -54,10 +53,10 @@ pub struct Invoice { } pub async fn index(State(app_state): State>) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; let offchain = app_state.node.get_ldk_balance(); let onchain = app_state @@ -87,10 +86,10 @@ pub async fn index(State(app_state): State>) -> Result pub async fn get_new_address( State(app_state): State>, ) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; Ok(Json(address.to_string())) } diff --git a/mobile/native/Cargo.toml b/mobile/native/Cargo.toml index faf19832d..278f27288 100644 --- a/mobile/native/Cargo.toml +++ b/mobile/native/Cargo.toml @@ -11,7 +11,6 @@ anyhow = "1" bdk = { version = "0.24.0", features = ["key-value-db"] } diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.4.0", features = ["use-serde"] } # required for the dlc_manager::Wallet trait flutter_rust_bridge = "1.63.1" lightning-invoice = { version = "0.21" } ln-dlc-node = { path = "../../crates/ln-dlc-node" } diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index 5569e463b..fd4fe6dfe 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -15,12 +15,11 @@ use bdk::bitcoin::secp256k1::rand::thread_rng; use bdk::bitcoin::secp256k1::rand::RngCore; use bdk::bitcoin::Network; use bdk::bitcoin::XOnlyPublicKey; -use dlc_manager::contract::Contract; -use dlc_manager::Wallet; use lightning_invoice::Invoice; use ln_dlc_node::node::Node; use ln_dlc_node::node::NodeInfo; use ln_dlc_node::seed::Bip39Seed; +use ln_dlc_node::Dlc; use state::Storage; use std::net::TcpListener; use std::path::Path; @@ -134,7 +133,7 @@ pub fn run(data_dir: String) -> Result<()> { // periodically update for positions runtime.spawn(async move { loop { - let contracts = match node_cloned.get_dlcs() { + let contracts = match node_cloned.get_confirmed_dlcs() { Ok(contracts) => contracts, Err(e) => { tracing::error!("Failed to retrieve DLCs from node: {e:#}"); @@ -144,7 +143,10 @@ pub fn run(data_dir: String) -> Result<()> { }; // Assumes that there is only one contract, i.e. one position - if let Some(Contract::Confirmed(contract)) = contracts.get(0) { + if let Some(Dlc { + offer_collateral, .. + }) = contracts.get(0) + { // TODO: Load position data from database and fill in the values; the collateral // can be taken from the DLC event::publish(&EventInternal::PositionUpdateNotification(PositionTrade { @@ -156,11 +158,7 @@ pub fn run(data_dir: String) -> Result<()> { liquidation_price: 0.0, unrealized_pnl: 0, position_state: PositionStateTrade::Open, - collateral: contract - .accepted_contract - .offered_contract - .offer_params - .collateral, + collateral: *offer_collateral, })); } @@ -177,7 +175,6 @@ pub fn run(data_dir: String) -> Result<()> { pub fn get_new_address() -> Result { let node = NODE.try_get().context("failed to get ln dlc node")?; let address = node - .wallet .get_new_address() .map_err(|e| anyhow!("Failed to get new address: {e}"))?; Ok(address.to_string())