diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2fba998..836d30f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -269,6 +269,24 @@ jobs: working-directory: snippets/python run: python3 -m compileall src + check-swift: + + name: Check Swift snippets + runs-on: macos-13 + + steps: + - uses: actions/checkout@v4 + + - name: Set up Swift + uses: swift-actions/setup-swift@v1 + with: + swift-version: "5" + + - name: Build + working-directory: snippets/swift/BreezSDKExamples + run: | + swift build + build: name: Build mdbook runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index e857aae..d4809f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ book .DS_Store .idea - -*.nupkg \ No newline at end of file +*.nupkg diff --git a/snippets/swift/BreezSDKExamples/.gitignore b/snippets/swift/BreezSDKExamples/.gitignore new file mode 100644 index 0000000..d230f4e --- /dev/null +++ b/snippets/swift/BreezSDKExamples/.gitignore @@ -0,0 +1,5 @@ +storage.sql +sync/ +sync_storage.sql +.swiftpm +.build diff --git a/snippets/swift/BreezSDKExamples/Package.resolved b/snippets/swift/BreezSDKExamples/Package.resolved new file mode 100644 index 0000000..3952624 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Package.resolved @@ -0,0 +1,23 @@ +{ + "pins" : [ + { + "identity" : "breez-sdk-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/breez/breez-sdk-swift", + "state" : { + "revision" : "dcbb39d45bc6797447bf20f52bc22344026d0662", + "version" : "0.2.9" + } + }, + { + "identity" : "swift-argument-parser", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-argument-parser", + "state" : { + "revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531", + "version" : "1.2.3" + } + } + ], + "version" : 2 +} diff --git a/snippets/swift/BreezSDKExamples/Package.swift b/snippets/swift/BreezSDKExamples/Package.swift new file mode 100644 index 0000000..27e6c84 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Package.swift @@ -0,0 +1,24 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "BreezSDKDocs", + platforms: [.macOS(.v12)], + dependencies: [ + .package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.3"), + .package(url: "https://github.com/breez/breez-sdk-swift", from:"0.2.9") + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "BreezSDKDocs", + dependencies: [ + .product(name: "ArgumentParser", package: "swift-argument-parser"), + .product(name: "BreezSDK", package: "breez-sdk-swift"), + ], + path: "Sources"), + ] +) diff --git a/snippets/swift/BreezSDKExamples/Sources/BuyBtc.swift b/snippets/swift/BreezSDKExamples/Sources/BuyBtc.swift new file mode 100644 index 0000000..b59e14e --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/BuyBtc.swift @@ -0,0 +1,17 @@ +// +// BuyBtc.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func buy(sdk: BlockingBreezServices) -> BuyBitcoinResponse? { + // ANCHOR: buy-btc + let buyBitcoinResponse = try? sdk.buyBitcoin( + req: BuyBitcoinRequest(provider: .moonpay)) + // ANCHOR_END: buy-btc + return buyBitcoinResponse +} diff --git a/snippets/swift/BreezSDKExamples/Sources/ConnectingLsp.swift b/snippets/swift/BreezSDKExamples/Sources/ConnectingLsp.swift new file mode 100644 index 0000000..367c9c5 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/ConnectingLsp.swift @@ -0,0 +1,25 @@ +// +// ConnectingLsp.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func getLspInfo(sdk: BlockingBreezServices) -> LspInformation?{ + // ANCHOR: get-lsp-info + let lspId = try? sdk.lspId() + let lspInfo = try? sdk.lspInfo() + // ANCHOR_END: get-lsp-info + return lspInfo +} + + +func connectLsp(sdk: BlockingBreezServices, lspId: String) { + // ANCHOR: connect-lsp + try? sdk.connectLsp(lspId: lspId) + // ANCHOR_END: connect-lsp + +} diff --git a/snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift b/snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift new file mode 100644 index 0000000..bdca755 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift @@ -0,0 +1,48 @@ +// +// FiatCurrencies.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func listSupportedFiatCurrencies(sdk: BlockingBreezServices) -> [FiatCurrency]? { + // ANCHOR: list-fiat-currencies + let supportedFiatCurrencies = try? sdk.listFiatCurrencies() + // ANCHOR_END: list-fiat-currencies + return supportedFiatCurrencies +} + +func getCurrentRates(sdk:BlockingBreezServices) -> [Rate]? { + // ANCHOR: fetch-fiat-rates + let fiatRates = try? sdk.fetchFiatRates() + // ANCHOR_END: fetch-fiat-rates + return fiatRates +} + +func getFiatCurrencyAndRates(sdk: BlockingBreezServices) -> [(FiatCurrency,Rate)]? { + // ANCHOR: get-fiat-currencies-and-rates + var ratesMap = [String:Rate]() + if let supportedFiatCurrencies = try? sdk.listFiatCurrencies(), let fiatRates = try? sdk.fetchFiatRates() { + for fiatRate in fiatRates { + ratesMap[fiatRate.coin.lowercased()] = fiatRate + } + + let sorted = supportedFiatCurrencies.sorted(by: { f1, f2 in + f1.id BlockingBreezServices? { + // Create the default config + let seed = try? mnemonicToSeed(phrase: "") + + let inviteCode = "" + let apiKey = "" + var config = defaultConfig(envType: EnvironmentType.production, apiKey: apiKey, + nodeConfig: NodeConfig.greenlight( + config: GreenlightNodeConfig(partnerCredentials: nil, inviteCode: inviteCode))) + + // Customize the config object according to your needs + config.workingDir = "path to an existing directory" + + + // Connect to the Breez SDK make it ready for use + guard seed != nil else { + return nil + } + let sdk = try? connect(config: config, seed: seed!, listener: SDKListener()) + + return sdk +} +// ANCHOR_END: init-sdk + +func gettingStartedNodeInfo(sdk: BlockingBreezServices) { + // ANCHOR: fetch-balance + if let nodeInfo = try? sdk.nodeInfo() { + let lnBalance = nodeInfo.channelsBalanceMsat + let onchainBalance = nodeInfo.onchainBalanceMsat + } + // ANCHOR_END: fetch-balance +} + + diff --git a/snippets/swift/BreezSDKExamples/Sources/ListPayments.swift b/snippets/swift/BreezSDKExamples/Sources/ListPayments.swift new file mode 100644 index 0000000..ae5b9ec --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/ListPayments.swift @@ -0,0 +1,27 @@ +// +// ListPayments.swift +// +// +// Created by ruben on 13/11/2023. +// + +import Foundation +import BreezSDK + +func ListPayments(sdk: BlockingBreezServices) -> [Payment]? { + // ANCHOR: list-payments + let payments = try? sdk.listPayments(req: ListPaymentsRequest()) + // ANCHOR_END: list-payments + return payments +} + +func ListPaymentsFiltered(sdk: BlockingBreezServices) -> [Payment]? { + // ANCHOR: list-payments-filtered + let payments = try? sdk.listPayments( + req: ListPaymentsRequest( + filters: [.sent], + fromTimestamp: 1696880000, + includeFailures: true)) + // ANCHOR_END: list-payments-filtered + return payments +} diff --git a/snippets/swift/BreezSDKExamples/Sources/LnurlAuth.swift b/snippets/swift/BreezSDKExamples/Sources/LnurlAuth.swift new file mode 100644 index 0000000..dbc3074 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/LnurlAuth.swift @@ -0,0 +1,32 @@ +// +// LnurlAuth.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func auth(sdk: BlockingBreezServices) { + // ANCHOR: lnurl-withdraw + // Endpoint can also be of the form: + // keyauth://domain.com/auth?key=val + let lnurlAuthUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu" + + + if let inputType = try? parseInput(s: lnurlAuthUrl) { + if case .lnUrlAuth(let `data`) = inputType { + let result = try? sdk.lnurlAuth(reqData: data) + switch result { + case .ok: + print("Successfully authenticated") + case .errorStatus(let error): + print("Failed to authenticate: \(error)") + case .none: + print("Failed to authenticate") + } + } + } + // ANCHOR_END: lnurl-withdraw +} diff --git a/snippets/swift/BreezSDKExamples/Sources/LnurlPay.swift b/snippets/swift/BreezSDKExamples/Sources/LnurlPay.swift new file mode 100644 index 0000000..40104d9 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/LnurlPay.swift @@ -0,0 +1,26 @@ +// +// LnurlPay.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func pay(sdk: BlockingBreezServices) -> LnUrlPayResult? { + // ANCHOR: lnurl-pay + // Endpoint can also be of the form: + // lnurlp://domain.com/lnurl-pay?key=val + // lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf + var response: LnUrlPayResult? + let lnurlPayUrl = "lightning@address.com" + if let inputType = try? parseInput(s: lnurlPayUrl) { + if case.lnUrlPay(let `data`) = inputType { + let amountMSat = data.minSendable + response = try? sdk.payLnurl(req: LnUrlPayRequest(data: data, amountMsat: amountMSat, comment: "comment")) + } + } + // ANCHOR_END: lnurl-pay + return response +} diff --git a/snippets/swift/BreezSDKExamples/Sources/LnurlWithdraw.swift b/snippets/swift/BreezSDKExamples/Sources/LnurlWithdraw.swift new file mode 100644 index 0000000..d0d5b86 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/LnurlWithdraw.swift @@ -0,0 +1,27 @@ +// +// LnurlWithdraw.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func withdraw(sdk: BlockingBreezServices) -> LnUrlWithdrawResult? { + // ANCHOR: lnurl-withdraw + // Endpoint can also be of the form: + // lnurlw://domain.com/lnurl-withdraw?key=val + var response: LnUrlWithdrawResult? + let lnurlWithdrawUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk" + + if let inputType = try? parseInput(s: lnurlWithdrawUrl){ + if case.lnUrlWithdraw(let `data`) = inputType { + let amountMsat = data.maxWithdrawable + let description = "Test withdraw" + response = try? sdk.withdrawLnurl(request: LnUrlWithdrawRequest(data: data, amountMsat: amountMsat)) + } + } + // ANCHOR_END: lnurl-withdraw + return response +} diff --git a/snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift b/snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift new file mode 100644 index 0000000..ebffdc9 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift @@ -0,0 +1,48 @@ +// +// ReceiveOnchain.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func generateReceiveOnchainAddress(sdk: BlockingBreezServices) -> String? { + // ANCHOR: generate-receive-onchain-address + let swapInfo = try? sdk.receiveOnchain(req: ReceiveOnchainRequest()) + + // Send your funds to the bellow bitcoin address + let address = swapInfo?.bitcoinAddress + // ANCHOR_END: generate-receive-onchain-address + + return address +} + +func getSwapInProgress(sdk: BlockingBreezServices) -> SwapInfo? { + // ANCHOR: in-progress-swap + let swapInfo = try? sdk.inProgressSwap() + // ANCHOR_END: in-progress-swap + return swapInfo +} + +func listRefundables(sdk:BlockingBreezServices) -> [SwapInfo]? { + // ANCHOR: list-refundables + let refundables = try? sdk.listRefundables() + // ANCHOR_END: list-refundables + return refundables +} +func executeRefund(sdk: BlockingBreezServices, refundables: SwapInfo,satPerVbyte: UInt32) -> RefundResponse? { + // ANCHOR: execute-refund + let destinationAddress = "..." + let response = try? sdk.refund(req: RefundRequest(swapAddress: refundables.bitcoinAddress, toAddress: destinationAddress, satPerVbyte: satPerVbyte)) + // ANCHOR_END: execute-refund + return response +} + +func getChannelOpeningFees(sdk: BlockingBreezServices, amountMsat: UInt64) -> OpenChannelFeeResponse? { + // ANCHOR: get-channel-opening-fees + let response = try? sdk.openChannelFee(req: OpenChannelFeeRequest(amountMsat: amountMsat)) + // ANCHOR_END: get-channel-opening-fees + return response +} diff --git a/snippets/swift/BreezSDKExamples/Sources/ReceivePayment.swift b/snippets/swift/BreezSDKExamples/Sources/ReceivePayment.swift new file mode 100644 index 0000000..a8e1bc7 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/ReceivePayment.swift @@ -0,0 +1,18 @@ +// +// ReceivePayment.swift +// +// +// Created by ruben on 13/11/2023. +// + +import BreezSDK + +func receivePayment(sdk: BlockingBreezServices) -> ReceivePaymentResponse? { + // ANCHOR: receive-payment + let repsonse = try? sdk.receivePayment( + req: ReceivePaymentRequest( + amountMsat: 3_000_000, + description: "Invoice for 3 000 000 sats")) + // ANCHOR_END: receive-payment + return repsonse +} diff --git a/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift b/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift new file mode 100644 index 0000000..01ae2d0 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift @@ -0,0 +1,46 @@ +// +// SendOnchain.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func GetCurrentFees(sdk: BlockingBreezServices) -> ReverseSwapPairInfo? { + // ANCHOR: estimate-current-reverse-swap-total-fees + let sendAmountSat:UInt64 = 50_000 + let currentFees = try? sdk.fetchReverseSwapFees(req: ReverseSwapFeesRequest(sendAmountSat: sendAmountSat)) + print("Total estimated fees for reverse swap: \(String(describing: currentFees?.totalEstimatedFees))") + // ANCHOR_END: estimate-current-reverse-swap-total-fees + return currentFees +} + +func ListCurrentFees(currentFees: ReverseSwapPairInfo) { + // ANCHOR: get-current-reverse-swap-min-max + print("Minimum amount, in sats: \(currentFees.min)") + print("Maximum amount, in sats: \(currentFees.max)") + // ANCHOR_END: get-current-reverse-swap-min-max +} + +func StartReverseSwap(sdk: BlockingBreezServices, currentFees: ReverseSwapPairInfo) -> SendOnchainResponse? { + // ANCHOR: start-reverse-swap + let destinationAddress = "bc1.." + let amountSat = currentFees.min + let satPerVbyte: UInt32 = 5 + + let response = try? sdk.sendOnchain(req: SendOnchainRequest(amountSat: amountSat, onchainRecipientAddress: destinationAddress, pairHash: currentFees.feesHash, satPerVbyte: satPerVbyte)) + // ANCHOR_END: start-reverse-swap + return response +} + +func checkReverseSwap(sdk: BlockingBreezServices) { + // ANCHOR: check-reverse-swaps-status + if let inProgressReverseSwaps = try? sdk.inProgressReverseSwaps() { + for rs in inProgressReverseSwaps { + print("Reverse swap \(rs.id) in progress, status is \(rs.status)") + } + } + // ANCHOR_END: check-reverse-swaps-status +} diff --git a/snippets/swift/BreezSDKExamples/Sources/SendPayment.swift b/snippets/swift/BreezSDKExamples/Sources/SendPayment.swift new file mode 100644 index 0000000..6bdcb99 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/SendPayment.swift @@ -0,0 +1,20 @@ +// +// SendPayment.swift +// +// +// Created by ruben on 13/11/2023. +// + +import BreezSDK + +func sendPayment(sdk: BlockingBreezServices) -> SendPaymentResponse? { + // ANCHOR: send-payment + // The `amountMsat` param is optional and should only passed if the bolt11 doesn't specify an amount. + // The amountMsat is required in case an amount is not specified in the bolt11 invoice'. + let req = SendPaymentRequest(bolt11: "...", amountMsat: 3_000_000) + let response = try? sdk.sendPayment(req: req) + // ANCHOR_END: send-payment + return response + + +} diff --git a/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift b/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift new file mode 100644 index 0000000..a5302eb --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift @@ -0,0 +1,21 @@ +// +// SendSpontaneous.swift +// +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + + +func sendSpontaneousPayment(sdk: BlockingBreezServices) -> SendPaymentResponse? { + // ANCHOR: send-spontaneous-payment + let response = try? sdk.sendSpontaneousPayment( + req: SendSpontaneousPaymentRequest( + nodeId: "...", + amountMsat: 3_000_000)) + // ANCHOR_END: send-spontaneous-payment + return response + +} diff --git a/snippets/swift/BreezSDKExamples/Sources/StaticChannelBackup.swift b/snippets/swift/BreezSDKExamples/Sources/StaticChannelBackup.swift new file mode 100644 index 0000000..f6ca235 --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/StaticChannelBackup.swift @@ -0,0 +1,16 @@ +// +// StaticChannelBackup.swift +// BreezSDKDocs +// +// Created by ruben on 14/11/2023. +// + +import Foundation +import BreezSDK + +func retrieveBackupFiles() -> StaticBackupResponse? { + // ANCHOR: static-channel-backup + let backupData = try? staticBackup(req:StaticBackupRequest(workingDir: "")) + // ANCHOR_END: static-channel-backup + return backupData +} diff --git a/snippets/swift/BreezSDKExamples/Sources/main.swift b/snippets/swift/BreezSDKExamples/Sources/main.swift new file mode 100644 index 0000000..41c5e6e --- /dev/null +++ b/snippets/swift/BreezSDKExamples/Sources/main.swift @@ -0,0 +1,13 @@ +// The Swift Programming Language +// https://docs.swift.org/swift-book + +print("Hello, tester!") +if let sdk = try gettingStarted(){ + if let fiarRates = getFiatCurrencyAndRates(sdk: sdk) { + for fiat in fiarRates { + print("rate \(fiat)\n") + print("------------------------") + } + } +} + diff --git a/snippets/swift/README.md b/snippets/swift/README.md new file mode 100644 index 0000000..d9e62d0 --- /dev/null +++ b/snippets/swift/README.md @@ -0,0 +1,8 @@ +# How to run +``` +cd BreezSDKExamples + +swift build + +swift run +``` diff --git a/src/guide/buy_btc.md b/src/guide/buy_btc.md index d5cd290..bf15a45 100644 --- a/src/guide/buy_btc.md +++ b/src/guide/buy_btc.md @@ -20,12 +20,7 @@ Once the buy is completed, the provider will transfer the Bitcoin to the generat
```swift,ignore -do { - let buyBitcoinResponse = try sdk.buyBitcoin( - req: BuyBitcoinRequest(provider: .moonpay)) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/BuyBtc.swift:buy-btc}} ```
diff --git a/src/guide/connecting_lsp.md b/src/guide/connecting_lsp.md index 699eba4..98e68fd 100644 --- a/src/guide/connecting_lsp.md +++ b/src/guide/connecting_lsp.md @@ -15,12 +15,7 @@ Based on the API key provided to the Breez SDK, a default LSP is selected for yo
```swift,ignore -do { - let lspId = try sdk.lspId() - let lspInfo = try sdk.lspInfo() -} catch { - // Handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ConnectingLsp.swift:get-lsp-info}} ```
@@ -97,11 +92,7 @@ When you have selected an LSP you may then connect to it.
```swift,ignore -do { - try sdk.connectLsp(lspId: lspId!) -} catch { - // Handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ConnectingLsp.swift:connect-lsp}} ```
diff --git a/src/guide/fiat_currencies.md b/src/guide/fiat_currencies.md index 3071e3a..ea0e8ce 100644 --- a/src/guide/fiat_currencies.md +++ b/src/guide/fiat_currencies.md @@ -11,6 +11,14 @@ In order to list the available fiat currencies: ``` +
Swift
+
+ +```swift,ignore +{{#include ../../snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift:list-fiat-currencies}} +``` +
+
Kotlin
@@ -75,6 +83,14 @@ To get the current BTC rate for the currencies: ```
+
Swift
+
+ +```swift,ignore +{{#include ../../snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift:fetch-fiat-rates}} +``` +
+
Kotlin
@@ -143,6 +159,14 @@ At the example project you can see these methods combined: ```
+
Swift
+
+ +```swift,ignore +{{#include ../../snippets/swift/BreezSDKExamples/Sources/FiatCurrencies.swift:get-fiat-currencies-and-rates}} +``` +
+
Kotlin
diff --git a/src/guide/getting_started.md b/src/guide/getting_started.md index d5f1a4d..4386d3e 100644 --- a/src/guide/getting_started.md +++ b/src/guide/getting_started.md @@ -51,30 +51,7 @@ Now you are ready to interact with the SDK.
```swift,ignore -// SDK events listener -class SDKListener: EventListener { - func onEvent(e: BreezEvent) { - print("received event ", e) - } -} - -// Create the default config -let seed = try mnemonicToSeed(phrase: "") -let inviteCode = "" -let apiKey = "" -let config = breez_sdk.defaultConfig(envType: EnvironmentType.production, apiKey: apiKey, - nodeConfig: NodeConfig.greenlight( - config: GreenlightNodeConfig(partnerCredentials: nil, inviteCode: inviteCode))); - -// Customize the config object according to your needs -config.workingDir = "path to an existing directory" - -do { - // Connect to the Breez SDK make it ready for use - let sdk = try connect(config: config, seed: seed, listener: SDKListener()); -} catch{ - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/GettingStarted.swift:init-sdk}} ```
@@ -170,13 +147,7 @@ At any point we can fetch our balance from the Greenlight node:
```swift,ignore -do { - let nodeInfo = try sdk.nodeInfo() - let lnBalance = nodeInfo?.channelsBalanceMsat - let onchainBalance = nodeInfo?.onchainBalanceMsat -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/GettingStarted.swift:fetch-balance}} ```
diff --git a/src/guide/list_payments.md b/src/guide/list_payments.md index 010c2b7..29f8aaf 100644 --- a/src/guide/list_payments.md +++ b/src/guide/list_payments.md @@ -15,11 +15,7 @@ To view your payment history you can list and filter all the sent and received p
```swift,ignore -do { - let payments = try sdk.listPayments(req: ListPaymentsRequest(filter: PaymentTypeFilter.all)) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ListPayments.swift:list-payments}} ```
@@ -91,17 +87,7 @@ You can optionally filter payments by timestamp and include failed payments.
```swift,ignore -do { - let payments = try sdk.listPayments( - req: ListPaymentsRequest( - filter: PaymentTypeFilter.sent, - fromTimestamp: 1696880000, - includeFailures: true, - ) - ) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ListPayments.swift:list-payments-filtered}} ```
diff --git a/src/guide/lnurl_auth.md b/src/guide/lnurl_auth.md index f266e7e..2d93a93 100644 --- a/src/guide/lnurl_auth.md +++ b/src/guide/lnurl_auth.md @@ -14,24 +14,7 @@
```swift,ignore -// Endpoint can also be of the form: -// keyauth://domain.com/auth?key=val -let lnurlAuthUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu" - -do { - let inputType = try parseInput(s: lnurlAuthUrl) - if case .lnUrlAuth(let data) = inputType { - let result = try sdk.lnurlAuth(reqData: data) - switch result { - case .ok: - print("Successfully authenticated") - case .errorStatus(let data): - print("Failed to authenticate") - } - } -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/LnurlAuth.swift:lnurl-withdraw}} ```
diff --git a/src/guide/lnurl_pay.md b/src/guide/lnurl_pay.md index 157d85e..f6c8ebf 100644 --- a/src/guide/lnurl_pay.md +++ b/src/guide/lnurl_pay.md @@ -15,22 +15,7 @@
```swift,ignore -// Endpoint can also be of the form: -// lnurlp://domain.com/lnurl-pay?key=val -// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf -let lnurlPayUrl = "lightning@address.com"; -do { - let inputType = try parseInput(s: lnurlPayUrl) - if case .lnUrlPay(let data) = inputType { - let amountMsat = data.minSendable; - try sdk.payLnurl(req: LnUrlPayRequest( - data: data, - amountMsat: amountMsat, - comment: "comment")) - } -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/LnurlPay.swift:lnurl-pay}} ```
diff --git a/src/guide/lnurl_withdraw.md b/src/guide/lnurl_withdraw.md index 10b9fd7..d6a3150 100644 --- a/src/guide/lnurl_withdraw.md +++ b/src/guide/lnurl_withdraw.md @@ -16,24 +16,7 @@
```swift,ignore -// Endpoint can also be of the form: -// lnurlw://domain.com/lnurl-withdraw?key=val -let lnurlWithdrawUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk" - -do { - let inputType = try parseInput(s: lnurlWithdrawUrl) - if case .lnUrlWithdraw(let data) = inputType { - let amountMsat = data.minWithdrawable - let description = "Test withdraw" - req: ListPaymentsRequest(filter: PaymentTypeFilter.all) - try sdk.withdrawLnurl(req: LnUrlWithdrawRequest( - data: data, - amountMsat: amountMsat, - description: "comment")) - } -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/LnurlWithdraw.swift:lnurl-withdraw}} ```
diff --git a/src/guide/receive_onchain.md b/src/guide/receive_onchain.md index ccb1d04..86f784a 100644 --- a/src/guide/receive_onchain.md +++ b/src/guide/receive_onchain.md @@ -18,14 +18,7 @@ In order to receive funds you first have to be connected to an [LSP](connecting_
```swift,ignore -do { - let swapInfo = try sdk.receiveOnchain(req: ReceiveOnchainRequest()) - - // Send your funds to the bellow bitcoin address - let address = swapInfo.bitcoinAddress; -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift:generate-receive-onchain-address}} ```
@@ -99,11 +92,7 @@ Once you've sent the funds to the above address, the SDK will monitor this addre
```swift,ignore -do { - let swapInfo = try sdk.inProgressSwap() -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift:in-progress-swap}} ```
@@ -181,11 +170,7 @@ In order to execute a refund, you need to supply an on-chain address to where th
```swift,ignore -do { - let refundables = try sdk.listRefundables() -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift:list-refundables}} ```
@@ -257,17 +242,7 @@ Once you have a refundable swap in hand, use the following code to execute a ref
```swift,ignore -let destinationAddress = "..." -let satPerVbyte = - -do { - try sdk.refund( - swapAddress: refundable?.bitcoinAddress, - toAddress: "...", - satPerVbyte: satPerVbyte) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift:execute-refund}} ```
@@ -346,13 +321,7 @@ To calculate the fees for a channel being opened by the LSP:
```swift,ignore -let amountMsat = -do { - let channelFees = try sdk.openChannelFee( - req: OpenChannelFeeRequest(amountMsat: amountMsat)) -} catch { - // Handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceiveOnchain.swift:get-channel-opening-fees}} ```
diff --git a/src/guide/receive_payment.md b/src/guide/receive_payment.md index 572b280..5f780cc 100644 --- a/src/guide/receive_payment.md +++ b/src/guide/receive_payment.md @@ -16,14 +16,7 @@ The Breez SDK automatically connects your node to the LSP peer and you can now r
```swift,ignore -do { - let invoice = try sdk.receivePayment( - req: ReceivePaymentRequest( - amountMsat: 3_000_000, - description: "Invoice for 3000 sats")) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/ReceivePayment.swift:receive-payment}} ```
diff --git a/src/guide/send_onchain.md b/src/guide/send_onchain.md index fa313a6..260003b 100644 --- a/src/guide/send_onchain.md +++ b/src/guide/send_onchain.md @@ -17,14 +17,7 @@ First, fetch the current reverse swap fees:
```swift,ignore -let sendAmountSat:UInt64? = 50000 -try { - let currentFees = try sdk.fetchReverseSwapFees( - req: ReverseSwapFeesRequest(sendAmountSat: sendAmountSat)) - print("Total estimated fees for reverse swap: \(currentFees.totalEstimatedFees)") -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift:estimate-current-reverse-swap-total-fees}} ```
@@ -101,8 +94,7 @@ Fetching the fees also tells you what is the range of amounts you can send:
```swift,ignore -println("Minimum amount, in sats: \(current_fees.min)") -println("Maximum amount, in sats: \(current_fees.max)") +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift:get-current-reverse-swap-min-max}} ```
@@ -171,18 +163,7 @@ Once you checked the fees are acceptable, you can start the reverse swap:
```swift,ignore -let destinationAddress = "bc1.." -let amountSat = currentFees.min -let satPerVbyte = -try { - try sdk.sendOnchain( - amountSat: amountSat, - onchainRecipientAddress: destinationAddress, - pairHash: currentFees.feesHash, - satPerVbyte: satPerVbyte) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift:start-reverse-swap}} ```
@@ -261,9 +242,7 @@ You can check its status with:
```swift,ignore -for rs in sdk.inProgressReverseSwaps() { - println("Reverse swap \(rs.id) in progress, status is \(rs.status)") -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendOnchain.swift:check-reverse-swaps-status}} ```
diff --git a/src/guide/send_payment.md b/src/guide/send_payment.md index b8aad56..aadc01d 100644 --- a/src/guide/send_payment.md +++ b/src/guide/send_payment.md @@ -15,14 +15,7 @@ Once you have outbound liquidity you can start sending payments too.
```swift,ignore -do { - // The `amountMsat` param is optional and should only passed if the bolt11 doesn't specify an amount. - // The amountMsat is required in case an amount is not specified in the bolt11 invoice'. - let req = SendPaymentRequest(bolt11: "...", amountMsat: 3000000) - let response = try sdk.sendPayment(req: req) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendPayment.swift:send-payment}} ```
diff --git a/src/guide/send_spontaneous_payment.md b/src/guide/send_spontaneous_payment.md index b5cba79..bdd4257 100644 --- a/src/guide/send_spontaneous_payment.md +++ b/src/guide/send_spontaneous_payment.md @@ -15,15 +15,7 @@ They can even be spontaneous payments to a node without a bolt11 invoice.
```swift,ignore -let nodeId = "..."; -do { - let response = try sdk.sendSpontaneousPayment( - req: SendSpontaneousPaymentRequest( - nodeId: "...", - amountMsat: 3000000)) -} catch { - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift:send-spontaneous-payment}} ```
diff --git a/src/guide/static_channel_backup.md b/src/guide/static_channel_backup.md index 7eb0f62..23acee7 100644 --- a/src/guide/static_channel_backup.md +++ b/src/guide/static_channel_backup.md @@ -21,11 +21,7 @@ In order to use the recoverchannel method, the user needs to provide the static
```swift,ignore -do { - let backupData = breez_sdk.staticBackup(request: StaticBackupRequest(workingDir: "")); -} catch{ - // handle error -} +{{#include ../../snippets/swift/BreezSDKExamples/Sources/StaticChannelBackup.swift:static-channel-backup}} ```