# Closing a Channel
Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs will be accepted on the given channel, and after additional timeout/the closing of all pending HTLCs, the channel will be closed on chain.
- Rust
- Kotlin
- Swift
let channel_id = channel_manager
.list_channels()
.iter()
.find(|channel| channel.user_id == user_id)
.expect("ERROR: Channel not found")
.channel_id;
// Example: Cooperative close
channel_manager.close_channel(&channel_id).expect("ERROR: Failed to close channel");
// Example: Unilateral close
channel_manager.force_close_channel(&channel_id).expect("ERROR: Failed to close channel");
To claim Funds directly into a custom wallet like BDK wallet using a custom KeysManager
see the Key Management guide for more info.
# SpendableOutputs Event Handling
- Rust
- Kotlin
- Swift
Event::SpendableOutputs { outputs, channel_id: _ } => {
// SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
// of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
// other descriptors are not currently ever regenerated for you by LDK. Once we return
// from this method, the descriptor will be gone, and you may lose track of some funds.
//
// Here we simply persist them to disk, with a background task running which will try
// to spend them regularly (possibly duplicatively/RBF'ing them). These can just be
// treated as normal funds where possible - they are only spendable by us and there is
// no rush to claim them.
for output in outputs {
let key = hex_utils::hex_str(&keys_manager.get_secure_random_bytes());
// Note that if the type here changes our read code needs to change as well.
let output: SpendableOutputDescriptor = output;
fs_store.write(PENDING_SPENDABLE_OUTPUT_DIR, "", &key, &output.encode()).unwrap();
}
}
References: Rust SpendableOutputs
docs (opens new window), Java/Kotlin SpendableOutputs
bindings (opens new window)