Skip to content

Commit d0f81f7

Browse files
author
toneloc
committed
updates before split
1 parent 6b21c5b commit d0f81f7

File tree

1 file changed

+70
-43
lines changed

1 file changed

+70
-43
lines changed

src/main.rs

+70-43
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11

22
// Contents
3-
// Section 1 - Dependencies, main data structure. helper functions
4-
// Section 2 - LDK set-up
5-
// Section 3 - Price feed config and logic
6-
// Section 4 - Core stability logic
7-
// Section 5 - Program initialization and command-line-interface
3+
// 1 - Main data structure. helper types in types.rs
4+
// 2 - Price feed config and logic in price_feeds.rs
5+
// 3 - LDK set-up and initialization
6+
// 4 - Core stability logic
7+
// 5 - Program initialization and command-line-interface
88

9-
// Section 1 - Dependencies and main data structure found in src/types.rs
9+
// 1 - Main data structure. helper types in types.rs
1010
mod types;
11+
// 2 - Price feed config and logic in price_feeds.rs
1112
mod price_feeds;
1213

13-
// This is only used for the LSP node
14+
// This is used for the LSP node only; pulled from https://github.com/tnull/ldk-node-hack
1415
extern crate ldk_node_hack;
1516

1617
use types::{Bitcoin, USD, StableChannel};
1718
use price_feeds::{set_price_feeds, fetch_prices, calculate_median_price};
18-
1919
use ldk_node::bitcoin::secp256k1::PublicKey;
2020
use ldk_node::lightning::ln::ChannelId;
21-
use ldk_node::lightning::offers::offer::Offer;
2221
use ldk_node::{lightning_invoice::Bolt11Invoice, Node, Builder};
2322
use ldk_node::bitcoin::Network;
2423

2524
use std::{io::{self, Write}, sync::Arc, thread};
2625
use ldk_node::{ChannelConfig, ChannelDetails};
27-
use std::time::Duration;
2826
use reqwest::blocking::Client;
27+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
2928

30-
// Section 2 - LDK set-up and helper functions
29+
// 3 - LDK set-up and initialization
3130
fn make_hack_node(alias: &str, port: u16) -> ldk_node_hack::Node {
3231

3332
let mut builder = ldk_node_hack::Builder::new();
@@ -77,7 +76,7 @@ fn make_node(alias: &str, port: u16, lsp_pubkey:Option<PublicKey>) -> ldk_node::
7776
return node;
7877
}
7978

80-
// Section 4 - Core stability logic
79+
// 4 - Core stability logic
8180
fn check_stability(node: &Node, mut sc: StableChannel) -> StableChannel {
8281
// Fetch and update prices
8382
sc.latest_price = fetch_prices(&Client::new(), &set_price_feeds())
@@ -135,35 +134,44 @@ fn check_stability(node: &Node, mut sc: StableChannel) -> StableChannel {
135134
.iter()
136135
.find(|c| c.channel_id == sc.channel_id) {sc = update_balances(sc, Some(channel.clone()));
137136
}
137+
138+
// Print balance information
139+
println!("{:<25} {:>15}", "Expected USD:", sc.expected_usd);
140+
println!("{:<25} {:>15}", "User USD:", sc.stable_receiver_usd);
141+
println!("{:<25} {:>5}", "Percent from par:", format!("{:.2}%\n", percent_from_par));
142+
143+
println!("{:<25} {:>15}", "User BTC:", sc.stable_receiver_btc);
144+
// println!("{:<25} {:>15}", "Expected BTC ():", sc.expected_btc);
145+
println!("{:<25} {:>15}", "LSP USD:", sc.stable_provider_usd);
138146
},
139147
Action::Pay => {
140148
println!("\nPaying the difference...\n");
141149

142-
let mut amt = USD::to_msats(dollars_from_par, sc.latest_price);
150+
let amt = USD::to_msats(dollars_from_par, sc.latest_price);
143151
println!("{}", amt.to_string());
144-
145-
// // First, ensure we are connected
146-
// let address = format!("127.0.0.1:9376").parse().unwrap();
147-
// let result = node.connect(sc.counterparty, address, true);
148-
149-
// if let Err(e) = result {
150-
// println!("Failed to connect with : {}", e);
151-
// } else {
152-
// println!("Successfully connected.");
153-
// }
154-
155-
// let result = node
156-
// .spontaneous_payment()
157-
// .send(amt, sc.counterparty);
158-
// match result {
159-
// Ok(payment_id) => println!("Payment sent successfully with payment ID: {}", payment_id),
160-
// Err(e) => println!("Failed to send payment: {}", e),
161-
// }
162-
163-
let result = node.bolt12_payment().send_using_amount(&sc.counterparty_offer,Some("here ya go".to_string()),amt);
164-
152+
153+
// First, ensure we are connected
154+
let address = format!("127.0.0.1:9737").parse().unwrap();
155+
let result = node.connect(sc.counterparty, address, true);
156+
157+
// let result = node.bolt12_payment().send_using_amount(&sc.counterparty_offer,Some("here ya go".to_string()),amt);
158+
159+
if let Err(e) = result {
160+
println!("Failed to connect with : {}", e);
161+
} else {
162+
println!("Successfully connected.");
163+
}
164+
165+
let result = node
166+
.spontaneous_payment()
167+
.send(amt, sc.counterparty);
165168
match result {
166-
Ok(payment_id) => println!("Payment sent successfully with ID: {:?}", payment_id),
169+
Ok(payment_id) => println!("Payment sent successfully with payment ID: {}", payment_id),
170+
Err(e) => println!("Failed to send payment: {}", e),
171+
}
172+
173+
match result {
174+
Ok(payment_id) => println!("Payment sent successfully with ID: {:?}", payment_id.to_string()),
167175
Err(e) => eprintln!("Failed to send payment: {:?}", e),
168176
}
169177
},
@@ -202,7 +210,7 @@ fn update_balances(mut sc: StableChannel, channel_details: Option<ChannelDetails
202210
sc // Return the modified StableChannel
203211
}
204212

205-
// Section 5 - Program initialization and command-line-interface
213+
// 5 - Program initialization and command-line-interface
206214
fn main() {
207215
// Add more nodes if you need
208216
let exchange = make_node("exchange", 9735, None);
@@ -226,7 +234,7 @@ fn main() {
226234

227235
// Sample start command below:
228236
// user startstablechannel CHANNEL_ID IS_STABLE_RECEIVER EXPECTED_DOLLAR_AMOUNT EXPECTED_BTC_AMOUNT
229-
// user startstablechannel 14380d654052``43b3a63f931c3071e4b5dd8ec9458e46cf408925b6322752dea true 170.0 0
237+
// user startstablechannel 14380d654052c43b3a63f931c3071e4b5dd8ec9458e46cf408925b6322752dea true 100.0 0
230238
match (node, command, args.as_slice()) {
231239
(Some("user"), Some("startstablechannel"), [channel_id, is_stable_receiver, expected_dollar_amount, native_amount_sats]) => {
232240
let channel_id = channel_id.to_string();
@@ -271,16 +279,35 @@ fn main() {
271279
counterparty_offer: offer
272280
};
273281

274-
println!("Stable Channel created: {:?}", stable_channel.channel_id);
282+
println!("Stable Channel created: {:?}", stable_channel.channel_id.to_string());
275283

276284
loop {
285+
// Get the current time
286+
let now = SystemTime::now();
287+
let now_duration = now.duration_since(UNIX_EPOCH).unwrap();
288+
289+
let now_secs = now_duration.as_secs();
290+
291+
// Calculate the next 10-second mark
292+
let next_10_sec = ((now_secs / 10) + 1) * 10;
293+
let next_10_sec_duration = Duration::from_secs(next_10_sec);
294+
295+
// Calculate the sleep duration
296+
let sleep_duration = next_10_sec_duration
297+
.checked_sub(now_duration)
298+
.unwrap_or_else(|| Duration::from_secs(0));
299+
300+
// Sleep until the next 10-second mark
301+
std::thread::sleep(sleep_duration);
302+
303+
// Run check_stability
277304
println!();
278-
println!("\nChecking stability for channel {}...\n", stable_channel.channel_id);
279-
305+
println!(
306+
"\nChecking stability for channel {}...\n",
307+
stable_channel.channel_id
308+
);
280309
stable_channel = check_stability(&user, stable_channel);
281-
282-
thread::sleep(Duration::from_secs(20));
283-
};
310+
}
284311
},
285312
// Open to LSP
286313
(Some("exchange"), Some("openchannel"), []) => {

0 commit comments

Comments
 (0)