|
| 1 | +# Opening a Channel |
| 2 | + |
| 3 | +Channels are the basic building blocks of the Lightning Network. With channels, you can transact not only with your immediate peers but with others on the network. Let's explore how to open a channel with LDK. |
| 4 | + |
| 5 | +Now that you have a peer, you can open a channel with them using `ChannelManager`. You'll need the peer's pubkey as before along with: |
| 6 | + |
| 7 | +- the amount in sats to use when funding the channel, |
| 8 | +- any msats to push to your peer, |
| 9 | +- an id which is given back in the `FundingGenerationReady` event, and |
| 10 | +- an optional `UserConfig` for overriding `ChannelManager` defaults |
| 11 | + |
| 12 | +Channels can be announced to the network or can remain private, which is controlled via `UserConfig::announced_channel`. |
| 13 | + |
| 14 | +<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}"> |
| 15 | + <template v-slot:rust> |
| 16 | + |
| 17 | +```rust |
| 18 | +let amount = 10_000; |
| 19 | +let push_msat = 1_000; |
| 20 | +let user_id = 42; |
| 21 | +let config = UserConfig { |
| 22 | + channel_options: ChannelConfig { announced_channel: true, ..Default::default() }, |
| 23 | + ..Default::default() |
| 24 | +}; |
| 25 | +match channel_manager.create_channel(pubkey, amount, push_msat, user_id, Some(config)) { |
| 26 | + Ok(_) => println!("EVENT: initiated channel with peer {}", pubkey), |
| 27 | + Err(e) => panic!("ERROR: failed to open channel: {:?}", e), |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | + </template> |
| 32 | + <template v-slot:java> |
| 33 | + |
| 34 | +```java |
| 35 | +long amount = 10_000L; |
| 36 | +long push_msat = 1_000L; |
| 37 | +long user_id = 42L; |
| 38 | +UserConfig config = UserConfig.with_defaults(); |
| 39 | +config.get_channel_options().set_announced_channel(true); |
| 40 | + |
| 41 | +Result_NoneAPIErrorZ create_channel_result = channel_manager.create_channel( |
| 42 | + pubkey, amount, push_msat, user_id, config); |
| 43 | +assert create_channel_result instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK; |
| 44 | +``` |
| 45 | + |
| 46 | + </template> |
| 47 | + <template v-slot:kotlin> |
| 48 | + |
| 49 | +```kotlin |
| 50 | +val amount = 100_000L |
| 51 | +val pushMsat = 1_000L |
| 52 | +val userId = 42L |
| 53 | + |
| 54 | +// public aka announced channel |
| 55 | +val userConfig = UserConfig.with_default() |
| 56 | + |
| 57 | +val channelHandshakeConfig = ChannelHandshakeConfig.with_default() |
| 58 | +channelHandshakeConfig._announced_channel = true |
| 59 | + |
| 60 | +userConfig._channel_handshake_config = channelHandshakeConfig |
| 61 | + |
| 62 | +val createChannelResult = channelManager.create_channel( |
| 63 | + pubKey.toByteArray(), amount, pushMsat, userId, userConfig |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | + </template> |
| 68 | +</CodeSwitcher> |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments