1
1
2
2
// 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
8
8
9
- // Section 1 - Dependencies and main data structure found in src/ types.rs
9
+ // 1 - Main data structure. helper types in types.rs
10
10
mod types;
11
+ // 2 - Price feed config and logic in price_feeds.rs
11
12
mod price_feeds;
12
13
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
14
15
extern crate ldk_node_hack;
15
16
16
17
use types:: { Bitcoin , USD , StableChannel } ;
17
18
use price_feeds:: { set_price_feeds, fetch_prices, calculate_median_price} ;
18
-
19
19
use ldk_node:: bitcoin:: secp256k1:: PublicKey ;
20
20
use ldk_node:: lightning:: ln:: ChannelId ;
21
- use ldk_node:: lightning:: offers:: offer:: Offer ;
22
21
use ldk_node:: { lightning_invoice:: Bolt11Invoice , Node , Builder } ;
23
22
use ldk_node:: bitcoin:: Network ;
24
23
25
24
use std:: { io:: { self , Write } , sync:: Arc , thread} ;
26
25
use ldk_node:: { ChannelConfig , ChannelDetails } ;
27
- use std:: time:: Duration ;
28
26
use reqwest:: blocking:: Client ;
27
+ use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
29
28
30
- // Section 2 - LDK set-up and helper functions
29
+ // 3 - LDK set-up and initialization
31
30
fn make_hack_node ( alias : & str , port : u16 ) -> ldk_node_hack:: Node {
32
31
33
32
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::
77
76
return node;
78
77
}
79
78
80
- // Section 4 - Core stability logic
79
+ // 4 - Core stability logic
81
80
fn check_stability ( node : & Node , mut sc : StableChannel ) -> StableChannel {
82
81
// Fetch and update prices
83
82
sc. latest_price = fetch_prices ( & Client :: new ( ) , & set_price_feeds ( ) )
@@ -135,35 +134,44 @@ fn check_stability(node: &Node, mut sc: StableChannel) -> StableChannel {
135
134
. iter ( )
136
135
. find ( |c| c. channel_id == sc. channel_id ) { sc = update_balances ( sc, Some ( channel. clone ( ) ) ) ;
137
136
}
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) ;
138
146
} ,
139
147
Action :: Pay => {
140
148
println ! ( "\n Paying the difference...\n " ) ;
141
149
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 ) ;
143
151
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 ) ;
165
168
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( ) ) ,
167
175
Err ( e) => eprintln ! ( "Failed to send payment: {:?}" , e) ,
168
176
}
169
177
} ,
@@ -202,7 +210,7 @@ fn update_balances(mut sc: StableChannel, channel_details: Option<ChannelDetails
202
210
sc // Return the modified StableChannel
203
211
}
204
212
205
- // Section 5 - Program initialization and command-line-interface
213
+ // 5 - Program initialization and command-line-interface
206
214
fn main ( ) {
207
215
// Add more nodes if you need
208
216
let exchange = make_node ( "exchange" , 9735 , None ) ;
@@ -226,7 +234,7 @@ fn main() {
226
234
227
235
// Sample start command below:
228
236
// 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
230
238
match ( node, command, args. as_slice ( ) ) {
231
239
( Some ( "user" ) , Some ( "startstablechannel" ) , [ channel_id, is_stable_receiver, expected_dollar_amount, native_amount_sats] ) => {
232
240
let channel_id = channel_id. to_string ( ) ;
@@ -271,16 +279,35 @@ fn main() {
271
279
counterparty_offer : offer
272
280
} ;
273
281
274
- println ! ( "Stable Channel created: {:?}" , stable_channel. channel_id) ;
282
+ println ! ( "Stable Channel created: {:?}" , stable_channel. channel_id. to_string ( ) ) ;
275
283
276
284
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
277
304
println ! ( ) ;
278
- println ! ( "\n Checking stability for channel {}...\n " , stable_channel. channel_id) ;
279
-
305
+ println ! (
306
+ "\n Checking stability for channel {}...\n " ,
307
+ stable_channel. channel_id
308
+ ) ;
280
309
stable_channel = check_stability ( & user, stable_channel) ;
281
-
282
- thread:: sleep ( Duration :: from_secs ( 20 ) ) ;
283
- } ;
310
+ }
284
311
} ,
285
312
// Open to LSP
286
313
( Some ( "exchange" ) , Some ( "openchannel" ) , [ ] ) => {
0 commit comments