diff --git a/snippets/csharp/SendOnchain.cs b/snippets/csharp/SendOnchain.cs index bc97cd9..0374f95 100644 --- a/snippets/csharp/SendOnchain.cs +++ b/snippets/csharp/SendOnchain.cs @@ -28,6 +28,22 @@ public class SendOnchainSnippets // ANCHOR_END: get-current-reverse-swap-min-max } + public void MaxReverseSwapAmount(BlockingBreezServices sdk) + { + // ANCHOR: max-reverse-swap-amount + try + { + var maxAmountResponse = sdk.MaxReverseSwapAmount(); + Console.WriteLine( + $"Max reverse swap amount {maxAmountResponse.totalSat}"); + } + catch (Exception) + { + // Handle error + } + // ANCHOR_END: max-reverse-swap-amount + } + public void StartReverseSwap(BlockingBreezServices sdk, ReverseSwapPairInfo currentFees, uint feeRate) { // ANCHOR: start-reverse-swap diff --git a/snippets/dart_snippets/lib/send_onchain.dart b/snippets/dart_snippets/lib/send_onchain.dart index cbfb869..fd3ec65 100644 --- a/snippets/dart_snippets/lib/send_onchain.dart +++ b/snippets/dart_snippets/lib/send_onchain.dart @@ -17,6 +17,14 @@ void listCurrentFees({required ReverseSwapPairInfo currentFees}) { // ANCHOR_END: get-current-reverse-swap-min-max } +Future maxReverseSwapAmount() async { + // ANCHOR: max-reverse-swap-amount + MaxReverseSwapAmountResponse maxAmount = await BreezSDK().maxReverseSwapAmount(); + print("Max reverse swap amount: ${maxAmount.totalSat}"); + // ANCHOR_END: max-reverse-swap-amount + return maxAmount; +} + Future startReverseSwap({ required int amountSat, required String onchainRecipientAddress, diff --git a/snippets/go/send_onchain.go b/snippets/go/send_onchain.go index a98fdd4..eb99246 100644 --- a/snippets/go/send_onchain.go +++ b/snippets/go/send_onchain.go @@ -25,6 +25,14 @@ func ListCurrentFees(currentFees breez_sdk.ReverseSwapPairInfo) { // ANCHOR_END: get-current-reverse-swap-min-max } +func MaxReverseSwapAmount() { + // ANCHOR: max-reverse-swap-amount + if maxAmount, err := sdk.MaxReverseSwapAmount(); err == nil { + log.Printf("Max reverse swap amount: %v", maxAmount.TotalSat) + } + // ANCHOR_END: max-reverse-swap-amount +} + func StartReverseSwap() { // ANCHOR: start-reverse-swap destinationAddress := "bc1.." diff --git a/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendOnchain.kt b/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendOnchain.kt index 36987af..c6dc8fd 100644 --- a/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendOnchain.kt +++ b/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendOnchain.kt @@ -20,6 +20,17 @@ class SendOnchain { // ANCHOR_END: get-current-reverse-swap-min-max } + fun max_reverse_swap_amount(sdk: BlockingBreezServices) { + // ANCHOR: max-reverse-swap-amount + try { + val maxAmount = sdk.maxReverseSwapAmount() + // Log.v("Breez", "Max reverse swap amount: ${maxAmount.totalSat}") + } catch (e: Exception) { + // handle error + } + // ANCHOR_END: max-reverse-swap-amount + } + fun start_reverse_swap(sdk: BlockingBreezServices, fees: ReverseSwapPairInfo) { // ANCHOR: start-reverse-swap val address = "bc1.." diff --git a/snippets/python/src/send_onchain.py b/snippets/python/src/send_onchain.py index a35e5f5..d3f9171 100644 --- a/snippets/python/src/send_onchain.py +++ b/snippets/python/src/send_onchain.py @@ -18,6 +18,17 @@ def list_current_fees(current_fees): print("Maximum amount, in sats: ", current_fees.max) # ANCHOR_END: get-current-reverse-swap-min-max +def max_reverse_swap_amount(sdk_services): + try: + # ANCHOR: max-reverse-swap-amount + max_amount = sdk_services.max_reverse_swap_amount() + print("Max reverse swap amount: ", max_amount.totalSat) + # ANCHOR_END: max-reverse-swap-amount + return max_amount + except Exception as error: + print(error) + raise + def start_reverse_swap(sdk_services, current_fees,fee_rate): # ANCHOR: start-reverse-swap destination_address = "bc1.." diff --git a/snippets/react-native/send_onchain.ts b/snippets/react-native/send_onchain.ts index 32205af..95a98af 100644 --- a/snippets/react-native/send_onchain.ts +++ b/snippets/react-native/send_onchain.ts @@ -1,49 +1,60 @@ import { - type ReverseSwapPairInfo, - fetchReverseSwapFees, - inProgressReverseSwaps, - sendOnchain + type ReverseSwapPairInfo, + fetchReverseSwapFees, + inProgressReverseSwaps, + sendOnchain } from '@breeztech/react-native-breez-sdk' const exampleFetchReverseSwapFees = async () => { - // ANCHOR: estimate-current-reverse-swap-total-fees - const currentFees = await fetchReverseSwapFees({ sendAmountSat: 50000 }) + // ANCHOR: estimate-current-reverse-swap-total-fees + const currentFees = await fetchReverseSwapFees({ sendAmountSat: 50000 }) - console.log( + console.log( `Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}` - ) - // ANCHOR_END: estimate-current-reverse-swap-total-fees + ) + // ANCHOR_END: estimate-current-reverse-swap-total-fees } const exampleListCurrentFees = (currentFees: ReverseSwapPairInfo) => { - // ANCHOR: get-current-reverse-swap-min-max - console.log(`Minimum amount, in sats: ${currentFees.min}`) - console.log(`Maximum amount, in sats: ${currentFees.max}`) - // ANCHOR_END: get-current-reverse-swap-min-max + // ANCHOR: get-current-reverse-swap-min-max + console.log(`Minimum amount, in sats: ${currentFees.min}`) + console.log(`Maximum amount, in sats: ${currentFees.max}`) + // ANCHOR_END: get-current-reverse-swap-min-max } -const exampleSendOnchain = async (currentFees: ReverseSwapPairInfo) => { - // ANCHOR: start-reverse-swap - const onchainRecipientAddress = 'bc1..' - const amountSat = currentFees.min - const satPerVbyte = 5 +const maxReverseSwapAmount = async () => { + // ANCHOR: max-reverse-swap-amount + const maxAmount = await maxReverseSwapAmount() - const reverseSwapInfo = await sendOnchain({ - amountSat, - onchainRecipientAddress, - pairHash: currentFees.feesHash, - satPerVbyte - }) - // ANCHOR_END: start-reverse-swap + console.log( + `Max reverse swap amount: ${maxAmount.totalSat}` + ) + // ANCHOR_END: max-reverse-swap-amount +} + + +const exampleSendOnchain = async (currentFees: ReverseSwapPairInfo) => { + // ANCHOR: start-reverse-swap + const onchainRecipientAddress = 'bc1..' + const amountSat = currentFees.min + const satPerVbyte = 5 + + const reverseSwapInfo = await sendOnchain({ + amountSat, + onchainRecipientAddress, + pairHash: currentFees.feesHash, + satPerVbyte + }) + // ANCHOR_END: start-reverse-swap } const exampleInProgressReverseSwaps = async () => { - // ANCHOR: check-reverse-swaps-status - const swaps = await inProgressReverseSwaps() - for (const swap of swaps) { - console.log( + // ANCHOR: check-reverse-swaps-status + const swaps = await inProgressReverseSwaps() + for (const swap of swaps) { + console.log( `Reverse swap ${swap.id} in progress, status is ${swap.status}` - ) - } - // ANCHOR_END: check-reverse-swaps-status + ) + } + // ANCHOR_END: check-reverse-swaps-status } diff --git a/snippets/rust/src/send_onchain.rs b/snippets/rust/src/send_onchain.rs index 4b450d0..c793f99 100644 --- a/snippets/rust/src/send_onchain.rs +++ b/snippets/rust/src/send_onchain.rs @@ -30,6 +30,21 @@ async fn list_current_fees(current_fees: ReverseSwapPairInfo) -> Result<()> { Ok(()) } +async fn max_reverse_swap_amount(sdk: Arc) -> Result<()> { + // ANCHOR: max-reverse-swap-amount + let max_amount = sdk + .max_reverse_swap_amount() + .await?; + + info!( + "Max reverse swap amount: {:?}", + max_amount.totalSat + ); + // ANCHOR_END: max-reverse-swap-amount + + Ok(()) +} + async fn start_reverse_swap( sdk: Arc, current_fees: ReverseSwapPairInfo, diff --git a/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift b/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift index 01ae2d0..50f3948 100644 --- a/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift +++ b/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift @@ -24,6 +24,14 @@ func ListCurrentFees(currentFees: ReverseSwapPairInfo) { // ANCHOR_END: get-current-reverse-swap-min-max } +func MaxReverseSwapAmount(sdk: BlockingBreezServices) -> MaxReverseSwapAmountResponse? { + // ANCHOR: max-reverse-swap-amount + let maxAmount = try? sdk.MaxReverseSwapAmount() + print("Max reverse swap amount: \(String(describing: maxAmount?.totalSat))") + // ANCHOR_END: max-reverse-swap-amount + return maxAmount +} + func StartReverseSwap(sdk: BlockingBreezServices, currentFees: ReverseSwapPairInfo) -> SendOnchainResponse? { // ANCHOR: start-reverse-swap let destinationAddress = "bc1.." diff --git a/src/guide/send_onchain.md b/src/guide/send_onchain.md index 061247c..43a1a58 100644 --- a/src/guide/send_onchain.md +++ b/src/guide/send_onchain.md @@ -74,7 +74,7 @@ The reverse swap will involve two on-chain transactions, for which the mining fe automatically once the process is started, but the last two values above are these estimates to help you get a picture of the total costs. -Fetching the fees also tells you what is the range of amounts you can send: +Fetching the fees also tells you what is the range of amounts the service allows:
Rust
@@ -142,7 +142,75 @@ Fetching the fees also tells you what is the range of amounts you can send:
-Once you checked the fees are acceptable, you can start the reverse swap: +In case you want to drain your channels you need to know the maximum sendable amount to an on-chain address: + + +
Rust
+
+ +```rust,ignore +{{#include ../../snippets/rust/src/send_onchain.rs:max-reverse-swap-amount}} +``` +
+ +
Swift
+
+ +```swift,ignore +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift:max-reverse-swap-amount}} +``` +
+ +
Kotlin
+
+ +```kotlin,ignore +{{#include ../../snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendOnchain.kt:max-reverse-swap-amount}} +``` +
+ +
React Native
+
+ +```typescript +{{#include ../../snippets/react-native/send_onchain.ts:max-reverse-swap-amount}} +``` +
+ +
Dart
+
+ +```dart,ignore +{{#include ../../snippets/dart_snippets/lib/send_onchain.dart:max-reverse-swap-amount}} +``` +
+ +
Python
+
+ +```python,ignore +{{#include ../../snippets/python/src/send_onchain.py:max-reverse-swap-amount}} +``` +
+ +
Go
+
+ +```go,ignore +{{#include ../../snippets/go/send_onchain.go:max-reverse-swap-amount}} +``` +
+ +
C#
+
+ +```cs,ignore +{{#include ../../snippets/csharp/SendOnchain.cs:max-reverse-swap-amount}} +``` +
+
+ +Once you decided about the amount and checked the fees are acceptable, you can start the reverse swap:
Rust