Skip to content

Commit 2409741

Browse files
committed
Add initial LdkLiteEventHandler structure
1 parent d4a0e9d commit 2409741

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

src/event.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use crate::{
2+
ChannelManager, EventSender, LdkLiteChainAccess, LdkLiteConfig, NetworkGraph,
3+
PaymentInfoStorage,
4+
};
5+
6+
#[allow(unused_imports)]
7+
use crate::logger::{
8+
log_error, log_given_level, log_info, log_internal, log_trace, log_warn, FilesystemLogger,
9+
Logger,
10+
};
11+
12+
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
13+
use lightning::chain::keysinterface::KeysManager;
14+
use lightning::util::events as ldk_events;
15+
16+
use bdk::database::MemoryDatabase;
17+
18+
use bitcoin::secp256k1::Secp256k1;
19+
//use std::collections::{hash_map, VecDeque};
20+
//use std::iter::Iterator;
21+
use std::sync::Arc;
22+
23+
/// An LdkLiteEvent that should be handled by the user.
24+
pub enum LdkLiteEvent {
25+
/// asdf
26+
Test,
27+
}
28+
29+
pub(crate) struct LdkLiteEventHandler {
30+
chain_access: Arc<LdkLiteChainAccess<MemoryDatabase>>,
31+
channel_manager: Arc<ChannelManager>,
32+
_network_graph: Arc<NetworkGraph>,
33+
keys_manager: Arc<KeysManager>,
34+
_inbound_payments: Arc<PaymentInfoStorage>,
35+
_outbound_payments: Arc<PaymentInfoStorage>,
36+
_event_sender: EventSender,
37+
logger: Arc<FilesystemLogger>,
38+
_config: Arc<LdkLiteConfig>,
39+
}
40+
41+
impl LdkLiteEventHandler {
42+
pub fn new(
43+
chain_access: Arc<LdkLiteChainAccess<MemoryDatabase>>,
44+
channel_manager: Arc<ChannelManager>, _network_graph: Arc<NetworkGraph>,
45+
keys_manager: Arc<KeysManager>, _inbound_payments: Arc<PaymentInfoStorage>,
46+
_outbound_payments: Arc<PaymentInfoStorage>, _event_sender: EventSender,
47+
logger: Arc<FilesystemLogger>, _config: Arc<LdkLiteConfig>,
48+
) -> Self {
49+
Self {
50+
chain_access,
51+
channel_manager,
52+
_network_graph,
53+
keys_manager,
54+
_inbound_payments,
55+
_outbound_payments,
56+
_event_sender,
57+
logger,
58+
_config,
59+
}
60+
}
61+
}
62+
63+
impl ldk_events::EventHandler for LdkLiteEventHandler {
64+
// TODO: implement error handling for events (i.e., get rid of any unwraps())
65+
fn handle_event(&self, event: &ldk_events::Event) {
66+
match event {
67+
ldk_events::Event::FundingGenerationReady {
68+
temporary_channel_id,
69+
counterparty_node_id,
70+
channel_value_satoshis,
71+
output_script,
72+
..
73+
} => {
74+
// Construct the raw transaction with one output, that is paid the amount of the
75+
// channel.
76+
77+
// TODO: what is a good default target here?
78+
let confirmation_target = ConfirmationTarget::Normal;
79+
80+
// Sign the final funding transaction and broadcast it.
81+
match self.chain_access.create_funding_transaction(
82+
&output_script,
83+
*channel_value_satoshis,
84+
confirmation_target,
85+
) {
86+
Ok(final_tx) => {
87+
// Give the funding transaction back to LDK for opening the channel.
88+
if self
89+
.channel_manager
90+
.funding_transaction_generated(
91+
&temporary_channel_id,
92+
counterparty_node_id,
93+
final_tx,
94+
)
95+
.is_err()
96+
{
97+
log_error!(self.logger, "Channel went away before we could fund it. The peer disconnected or refused the channel");
98+
}
99+
}
100+
Err(err) => {
101+
log_error!(self.logger, "Failed to create funding transaction: {}", err);
102+
}
103+
}
104+
}
105+
ldk_events::Event::PaymentReceived { .. } => {}
106+
ldk_events::Event::PaymentClaimed { .. } => {}
107+
ldk_events::Event::PaymentSent { .. } => {}
108+
ldk_events::Event::PaymentFailed { .. } => {}
109+
ldk_events::Event::PaymentPathSuccessful { .. } => {}
110+
ldk_events::Event::PaymentPathFailed { .. } => {}
111+
ldk_events::Event::ProbeSuccessful { .. } => {}
112+
ldk_events::Event::ProbeFailed { .. } => {}
113+
ldk_events::Event::HTLCHandlingFailed { .. } => {}
114+
ldk_events::Event::PendingHTLCsForwardable { .. } => {}
115+
ldk_events::Event::SpendableOutputs { outputs } => {
116+
let destination_address = self.chain_access.get_new_address().unwrap();
117+
let output_descriptors = &outputs.iter().map(|a| a).collect::<Vec<_>>();
118+
let tx_feerate =
119+
self.chain_access.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
120+
let spending_tx = self
121+
.keys_manager
122+
.spend_spendable_outputs(
123+
output_descriptors,
124+
Vec::new(),
125+
destination_address.script_pubkey(),
126+
tx_feerate,
127+
&Secp256k1::new(),
128+
)
129+
.unwrap();
130+
self.chain_access.broadcast_transaction(&spending_tx);
131+
}
132+
ldk_events::Event::OpenChannelRequest { .. } => {}
133+
ldk_events::Event::PaymentForwarded { .. } => {}
134+
ldk_events::Event::ChannelClosed { .. } => {}
135+
ldk_events::Event::DiscardFunding { .. } => {}
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)