Skip to content

Commit

Permalink
chore: extract logic from api layer into own module
Browse files Browse the repository at this point in the history
Signed-off-by: Philipp Hoenisch <[email protected]>
  • Loading branch information
bonomat committed Jan 12, 2024
1 parent aeb1673 commit 3430d9f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
46 changes: 2 additions & 44 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::calculations;
use crate::channel_trade_constraints;
use crate::commons::api::ChannelInfo;
use crate::commons::api::Price;
use crate::config;
Expand Down Expand Up @@ -439,50 +440,7 @@ pub struct TradeConstraints {
}

pub fn channel_trade_constraints() -> Result<TradeConstraints> {
let lsp_config =
crate::state::try_get_lsp_config().context("We can't trade without LSP config")?;

// TODO(bonomat): retrieve these values from the coordinator. This can come from the liquidity
// options.
let min_quantity = 100;

// TODO(bonomat): this logic should be removed once we have our liquidity options again and the
// on-boarding logic. For now we take the highest liquidity option
let option = lsp_config
.liquidity_options
.iter()
.filter(|option| option.active)
.max_by_key(|option| &option.trade_up_to_sats)
.context("we need at least one liquidity option")?;
let coordinator_leverage = option.coordinator_leverage;

let dlc_channels = ln_dlc::get_dlc_channels()?;

let maybe_channel = dlc_channels.first();

let trade_constraints = match maybe_channel {
None => {
let balance = ln_dlc::get_onchain_balance()?;
let counterparty_margin_sats = option.trade_up_to_sats;
TradeConstraints {
max_local_margin_sats: balance.confirmed
+ balance.trusted_pending
+ balance.untrusted_pending,

max_counterparty_margin_sats: counterparty_margin_sats,
coordinator_leverage,
min_quantity,
is_channel_balance: false,
}
}
Some(channel) => TradeConstraints {
max_local_margin_sats: channel.own_params.collateral,
max_counterparty_margin_sats: channel.counter_params.collateral,
coordinator_leverage,
min_quantity,
is_channel_balance: true,
},
};
let trade_constraints = channel_trade_constraints::channel_trade_constraints()?;
Ok(trade_constraints)
}

Expand Down
52 changes: 52 additions & 0 deletions mobile/native/src/channel_trade_constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::api::TradeConstraints;
use crate::ln_dlc;
use anyhow::Context;
use anyhow::Result;

pub fn channel_trade_constraints() -> Result<TradeConstraints> {
let lsp_config =
crate::state::try_get_lsp_config().context("We can't trade without LSP config")?;

// TODO(bonomat): retrieve these values from the coordinator. This can come from the liquidity
// options.
let min_quantity = 100;

// TODO(bonomat): this logic should be removed once we have our liquidity options again and the
// on-boarding logic. For now we take the highest liquidity option
let option = lsp_config
.liquidity_options
.iter()
.filter(|option| option.active)
.max_by_key(|option| &option.trade_up_to_sats)
.context("we need at least one liquidity option")?;
let coordinator_leverage = option.coordinator_leverage;

let dlc_channels = ln_dlc::get_dlc_channels()?;

let maybe_channel = dlc_channels.first();

let trade_constraints = match maybe_channel {
None => {
let balance = ln_dlc::get_onchain_balance()?;
let counterparty_margin_sats = option.trade_up_to_sats;
TradeConstraints {
max_local_margin_sats: balance.confirmed
+ balance.trusted_pending
+ balance.untrusted_pending,

max_counterparty_margin_sats: counterparty_margin_sats,
coordinator_leverage,
min_quantity,
is_channel_balance: false,
}
}
Some(channel) => TradeConstraints {
max_local_margin_sats: channel.own_params.collateral,
max_counterparty_margin_sats: channel.counter_params.collateral,
coordinator_leverage,
min_quantity,
is_channel_balance: true,
},
};
Ok(trade_constraints)
}
1 change: 1 addition & 0 deletions mobile/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod orderbook;
unused_qualifications
)]
mod bridge_generated;
mod channel_trade_constraints;
mod cipher;
mod destination;
mod storage;

0 comments on commit 3430d9f

Please sign in to comment.