Skip to content

Commit

Permalink
chore: get counterparty max margin from liquidity options
Browse files Browse the repository at this point in the history
Note: right now we simply take the highest liquidity option. This needs to be changed once we have our on-boarding flow again and link channels with liquidity options.
Signed-off-by: Philipp Hoenisch <[email protected]>
  • Loading branch information
bonomat committed Jan 10, 2024
1 parent 0068474 commit cf1aecb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
20 changes: 15 additions & 5 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ pub struct TradeConstraints {
/// the counterparty is willing to provide.
pub max_counterparty_margin_sats: u64,
/// The leverage the coordinator will take
pub coordinator_leverage: f64,
pub coordinator_leverage: f32,
/// Smallest allowed amount of contracts
pub min_quantity: u64,
/// If true it means that the user has a channel and hence the max amount is limited by what he
Expand All @@ -439,21 +439,31 @@ 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 coordinator_leverage = 2.0;
let min_quantity = 1;

// 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_channel()?;

let maybe_channel = dlc_channels.first();

let trade_constraints = match maybe_channel {
None => {
let balance = ln_dlc::get_onchain_balance()?;
// TODO: get this value from the coordinator, for now we just take what the user has
let counterparty_margin_sats =
balance.confirmed + balance.trusted_pending + balance.untrusted_pending;
let counterparty_margin_sats = option.trade_up_to_sats;
TradeConstraints {
max_local_margin_sats: balance.confirmed
+ balance.trusted_pending
Expand Down
2 changes: 2 additions & 0 deletions mobile/native/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::event::EventInternal;
use crate::event::TaskStatus;
use crate::health::ServiceStatus;
use crate::ln_dlc;
use crate::state;
use crate::trade::position;
use anyhow::bail;
use anyhow::Context;
Expand Down Expand Up @@ -209,6 +210,7 @@ async fn handle_orderbook_message(
match msg {
Message::Authenticated(lsp_config) => {
tracing::info!("Successfully logged in to 10101 websocket api!");
state::set_lsp_config(lsp_config.clone());
event::publish(&EventInternal::Authenticated(lsp_config));
}
Message::Rollover(contract_id) => {
Expand Down
17 changes: 17 additions & 0 deletions mobile/native/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::ln_dlc::node::Node;
use crate::logger::LogEntry;
use crate::storage::TenTenOneNodeStorage;
use anyhow::Result;
use commons::LspConfig;
use commons::OrderbookRequest;
use flutter_rust_bridge::StreamSink;
use ln_dlc_node::seed::Bip39Seed;
Expand All @@ -24,6 +25,7 @@ static STORAGE: Storage<RwLock<TenTenOneNodeStorage>> = Storage::new();
static RUNTIME: Storage<Runtime> = Storage::new();
static WEBSOCKET: Storage<RwLock<Sender<OrderbookRequest>>> = Storage::new();
static LOG_STREAM_SINK: Storage<RwLock<Arc<StreamSink<LogEntry>>>> = Storage::new();
static LSP_CONFIG: Storage<RwLock<LspConfig>> = Storage::new();

pub fn set_config(config: ConfigInternal) {
match CONFIG.try_get() {
Expand Down Expand Up @@ -129,3 +131,18 @@ pub fn set_log_stream_sink(sink: Arc<StreamSink<LogEntry>>) {
pub fn try_get_log_stream_sink() -> Option<Arc<StreamSink<LogEntry>>> {
LOG_STREAM_SINK.try_get().map(|l| l.read().clone())
}

pub fn set_lsp_config(lsp_config: LspConfig) {
match LSP_CONFIG.try_get() {
None => {
LSP_CONFIG.set(RwLock::new(lsp_config));
}
Some(s) => {
*s.write() = lsp_config;
}
}
}

pub fn try_get_lsp_config() -> Option<LspConfig> {
LSP_CONFIG.try_get().map(|w| w.read().clone())
}

0 comments on commit cf1aecb

Please sign in to comment.