Skip to content

Commit

Permalink
Use a single instance of BreezSDK on Dart snippets
Browse files Browse the repository at this point in the history
- Add documentation that recommends using single instance of BreezSDK and possible options on how.
- Add documentation on how to use initialize() in light of recent SDK stream management changes.
  • Loading branch information
erdemyerebasmaz committed Jun 3, 2024
1 parent b4bd996 commit b8bdd1d
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 135 deletions.
4 changes: 2 additions & 2 deletions snippets/dart_snippets/bin/dart_snippets.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';

void main(List<String> arguments) {
BreezSDK().initialize();
breezSDK.initialize();
}
4 changes: 2 additions & 2 deletions snippets/dart_snippets/lib/buy_btc.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:breez_sdk/bridge_generated.dart';
import 'package:dart_snippets/sdk_instance.dart';

Future<BuyBitcoinResponse> buyBitcoin() async {
// ANCHOR: buy-btc
BuyBitcoinRequest req = const BuyBitcoinRequest(provider: BuyBitcoinProvider.Moonpay);
BuyBitcoinResponse resp = await BreezSDK().buyBitcoin(req: req);
BuyBitcoinResponse resp = await breezSDK.buyBitcoin(req: req);
// ANCHOR_END: buy-btc
return resp;
}
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/closed_channel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<PrepareRedeemOnchainFundsResponse> prepareRedeemOnchainFunds(
Expand All @@ -8,7 +8,7 @@ Future<PrepareRedeemOnchainFundsResponse> prepareRedeemOnchainFunds(
toAddress: "bc1..",
satPerVbyte: satPerVbyte,
);
final resp = await BreezSDK().prepareRedeemOnchainFunds(req: req);
final resp = await breezSDK.prepareRedeemOnchainFunds(req: req);
// ANCHOR_END: prepare-redeem-onchain-funds
return resp;
}
Expand All @@ -20,7 +20,7 @@ Future<RedeemOnchainFundsResponse> redeemOnchainFunds(
toAddress: "bc1..",
satPerVbyte: satPerVbyte,
);
final resp = await BreezSDK().redeemOnchainFunds(req: req);
final resp = await breezSDK.redeemOnchainFunds(req: req);
// ANCHOR_END: redeem-onchain-funds
return resp;
}
10 changes: 5 additions & 5 deletions snippets/dart_snippets/lib/communicating_fees.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> getFeeInfoBeforeInvoiceCreated() async {
// ANCHOR: get-fee-info-before-receiving-payment
NodeState? nodeInfo = await BreezSDK().nodeInfo();
NodeState? nodeInfo = await breezSDK.nodeInfo();
if (nodeInfo != null) {
int inboundLiquiditySat = nodeInfo.inboundLiquidityMsats ~/ 1000;

OpenChannelFeeResponse openingFeeResponse = await BreezSDK().openChannelFee(req: OpenChannelFeeRequest());
OpenChannelFeeResponse openingFeeResponse = await breezSDK.openChannelFee(req: OpenChannelFeeRequest());

OpeningFeeParams openingFees = openingFeeResponse.feeParams;
double feePercentage = (openingFees.proportional * 100) / 1000000;
Expand All @@ -31,12 +31,12 @@ Future<void> getFeeInfoAfterInvoiceCreated({required ReceivePaymentResponse rece

Future<void> getFeeInfoReceiveOnchain() async {
// ANCHOR: get-fee-info-receive-onchain
SwapInfo swapInfo = await BreezSDK().receiveOnchain(req: ReceiveOnchainRequest());
SwapInfo swapInfo = await breezSDK.receiveOnchain(req: ReceiveOnchainRequest());

int minDepositSat = swapInfo.minAllowedDeposit;
int maxDepositSat = swapInfo.maxAllowedDeposit;

NodeState? nodeInfo = await BreezSDK().nodeInfo();
NodeState? nodeInfo = await breezSDK.nodeInfo();
if (nodeInfo != null) {
int inboundLiquiditySat = nodeInfo.inboundLiquidityMsats ~/ 1000;

Expand Down
10 changes: 5 additions & 5 deletions snippets/dart_snippets/lib/connecting_lsp.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> getLspInfo() async {
// ANCHOR: get-lsp-info
String? lspId = await BreezSDK().lspId();
LspInformation? lspInfo = await BreezSDK().lspInfo();
String? lspId = await breezSDK.lspId();
LspInformation? lspInfo = await breezSDK.lspInfo();
print(lspId);
print(lspInfo);
// ANCHOR_END: get-lsp-info
}

Future<List<LspInformation>> listLsps() async {
// ANCHOR: list-lsps
List<LspInformation> availableLsps = await BreezSDK().listLsps();
List<LspInformation> availableLsps = await breezSDK.listLsps();
// ANCHOR_END: list-lsps
return availableLsps;
}

Future<void> connectLsp(String lspId) async {
// ANCHOR: connect-lsp
await BreezSDK().connectLSP(lspId);
await breezSDK.connectLSP(lspId);
// ANCHOR_END: connect-lsp
}
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/fiat_currencies.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<List<FiatCurrency>> listFiatCurrencies() async {
// ANCHOR: list-fiat-currencies
List<FiatCurrency> fiatCurrencyList = await BreezSDK().listFiatCurrencies();
List<FiatCurrency> fiatCurrencyList = await breezSDK.listFiatCurrencies();
// ANCHOR_END: list-fiat-currencies
return fiatCurrencyList;
}

Future<Map<String, Rate>> fetchFiatRates(String lspId) async {
// ANCHOR: fetch-fiat-rates
Map<String, Rate> fiatRatesMap = await BreezSDK().fetchFiatRates();
Map<String, Rate> fiatRatesMap = await breezSDK.fetchFiatRates();
// print your desired rate
print(fiatRatesMap["USD"]?.value);
// ANCHOR_END: fetch-fiat-rates
Expand Down
25 changes: 18 additions & 7 deletions snippets/dart_snippets/lib/getting_started.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import 'dart:typed_data';

import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> initializeSDK() async {
// ANCHOR: init-sdk

// Initialize SDK logs listener
BreezSDK().initialize();
// It is recommended to use a single instance of BreezSDK across your Dart/Flutter app.
//
// All of the snippets assume a BreezSDK object is created on entrypoint of the app as such:
//
// BreezSDK breezSDK = BreezSDK();
//
// and is accessible throughout the app. There are various approaches on how to achieve this; creating a Singleton class using factory constructor, using state management libraries such as 'provider', 'GetX', 'Riverpod' and 'Redux' to name a few.

// Initializes SDK events & log streams.
//
// Call once on your Dart entrypoint file, e.g.; `lib/main.dart`.
breezSDK.initialize();

// Create the default config
Uint8List seed = await BreezSDK().mnemonicToSeed("<mnemonic words>");
Uint8List seed = await breezSDK.mnemonicToSeed("<mnemonic words>");
String inviteCode = "<invite code>";
String apiKey = "<api key>";
NodeConfig nodeConfig = NodeConfig.greenlight(
Expand All @@ -19,7 +30,7 @@ Future<void> initializeSDK() async {
inviteCode: inviteCode,
),
);
Config config = await BreezSDK().defaultConfig(
Config config = await breezSDK.defaultConfig(
envType: EnvironmentType.Production,
apiKey: apiKey,
nodeConfig: nodeConfig,
Expand All @@ -30,20 +41,20 @@ Future<void> initializeSDK() async {

// Connect to the Breez SDK make it ready for use
ConnectRequest connectRequest = ConnectRequest(config: config, seed: seed);
return await BreezSDK().connect(req: connectRequest);
return await breezSDK.connect(req: connectRequest);
// ANCHOR_END: init-sdk
}

Future<void> connectRestoreOnly(Config config, Uint8List seed) async {
// ANCHOR: init-sdk-restore-only
ConnectRequest connectRequest = ConnectRequest(config: config, seed: seed, restoreOnly: true);
return await BreezSDK().connect(req: connectRequest);
return await breezSDK.connect(req: connectRequest);
// ANCHOR_END: init-sdk-restore-only
}

Future<void> fetchBalance(String lspId) async {
// ANCHOR: fetch-balance
NodeState? nodeInfo = await BreezSDK().nodeInfo();
NodeState? nodeInfo = await breezSDK.nodeInfo();
if (nodeInfo != null) {
int lnBalance = nodeInfo.channelsBalanceMsat;
int onchainBalance = nodeInfo.onchainBalanceMsat;
Expand Down
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/list_payments.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<List<Payment>> listPayments() async {
// ANCHOR: list-payments
ListPaymentsRequest req = ListPaymentsRequest();
List<Payment> paymentsList = await BreezSDK().listPayments(req: req);
List<Payment> paymentsList = await breezSDK.listPayments(req: req);
print(paymentsList);
// ANCHOR_END: list-payments
return paymentsList;
Expand All @@ -29,7 +29,7 @@ Future<List<Payment>> listPaymentsFiltered({
offset: offset,
limit: limit,
);
List<Payment> paymentsList = await BreezSDK().listPayments(req: req);
List<Payment> paymentsList = await breezSDK.listPayments(req: req);
print(paymentsList);
// ANCHOR_END: list-payments-filtered
return paymentsList;
Expand Down
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/lnurl_auth.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> lnurlAuth() async {
Expand All @@ -8,9 +8,9 @@ Future<void> lnurlAuth() async {
String lnurlAuthUrl =
"lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu";

InputType inputType = await BreezSDK().parseInput(input: lnurlAuthUrl);
InputType inputType = await breezSDK.parseInput(input: lnurlAuthUrl);
if (inputType is InputType_LnUrlAuth) {
LnUrlCallbackStatus result = await BreezSDK().lnurlAuth(reqData: inputType.data);
LnUrlCallbackStatus result = await breezSDK.lnurlAuth(reqData: inputType.data);
if (result is LnUrlCallbackStatus_Ok) {
print("Successfully authenticated");
} else {
Expand Down
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/lnurl_pay.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> lnurlPay() async {
Expand All @@ -8,7 +8,7 @@ Future<void> lnurlPay() async {
/// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf
String lnurlPayUrl = "[email protected]";

InputType inputType = await BreezSDK().parseInput(input: lnurlPayUrl);
InputType inputType = await breezSDK.parseInput(input: lnurlPayUrl);
if (inputType is InputType_LnUrlPay) {
int amountMsat = inputType.data.minSendable;
String optionalComment = "<comment>";
Expand All @@ -19,7 +19,7 @@ Future<void> lnurlPay() async {
comment: optionalComment,
paymentLabel: optionalPaymentLabel,
);
LnUrlPayResult result = await BreezSDK().lnurlPay(req: req);
LnUrlPayResult result = await breezSDK.lnurlPay(req: req);
print(result.data);
}
// ANCHOR_END: lnurl-pay
Expand Down
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/lnurl_withdraw.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> lnurlWithdraw() async {
Expand All @@ -8,15 +8,15 @@ Future<void> lnurlWithdraw() async {
String lnurlWithdrawUrl =
"lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk";

InputType inputType = await BreezSDK().parseInput(input: lnurlWithdrawUrl);
InputType inputType = await breezSDK.parseInput(input: lnurlWithdrawUrl);
if (inputType is InputType_LnUrlWithdraw) {
int amountMsat = inputType.data.minWithdrawable;
LnUrlWithdrawRequest req = LnUrlWithdrawRequest(
data: inputType.data,
amountMsat: amountMsat,
description: "<description>",
);
LnUrlWithdrawResult result = await BreezSDK().lnurlWithdraw(req: req);
LnUrlWithdrawResult result = await breezSDK.lnurlWithdraw(req: req);
print(result.data);
}
// ANCHOR_END: lnurl-withdraw
Expand Down
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/metadata.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<void> setPaymentMetadata({required String paymentHash, required String metadata}) async {
// ANCHOR: set-payment-metadata
await BreezSDK().setPaymentMetadata(hash: "target-payment-hash", metadata: '{"myCustomValue":true}');
await breezSDK.setPaymentMetadata(hash: "target-payment-hash", metadata: '{"myCustomValue":true}');
// ANCHOR_END: set-payment-metadata
}

Expand All @@ -16,7 +16,7 @@ Future<void> filterPaymentMetadata() async {
),
];

await BreezSDK().listPayments(
await breezSDK.listPayments(
req: ListPaymentsRequest(
metadataFilters: metadataFilters
));
Expand Down
10 changes: 5 additions & 5 deletions snippets/dart_snippets/lib/pay_onchain.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<OnchainPaymentLimitsResponse> getCurrentLimits() async {
// ANCHOR: get-current-reverse-swap-limits
OnchainPaymentLimitsResponse currentLimits = await BreezSDK().onchainPaymentLimits();
OnchainPaymentLimitsResponse currentLimits = await breezSDK.onchainPaymentLimits();
print("Minimum amount, in sats: ${currentLimits.minSat}");
print("Maximum amount, in sats: ${currentLimits.maxSat}");
// ANCHOR_END: get-current-reverse-swap-limits
Expand All @@ -20,7 +20,7 @@ Future<PrepareOnchainPaymentResponse> preparePayOnchain({
amountType: SwapAmountType.Send,
claimTxFeerate: satPerVbyte,
);
PrepareOnchainPaymentResponse prepareRes = await BreezSDK().prepareOnchainPayment(req: req);
PrepareOnchainPaymentResponse prepareRes = await breezSDK.prepareOnchainPayment(req: req);
print("Sender amount: ${prepareRes.senderAmountSat} sats");
print("Recipient amount: ${prepareRes.recipientAmountSat} sats");
print("Total fees: ${prepareRes.totalFees} sats");
Expand All @@ -37,14 +37,14 @@ Future<PayOnchainResponse> startReverseSwap({
recipientAddress: onchainRecipientAddress,
prepareRes: prepareRes,
);
PayOnchainResponse res = await BreezSDK().payOnchain(req: req);
PayOnchainResponse res = await breezSDK.payOnchain(req: req);
// ANCHOR_END: start-reverse-swap
return res;
}

Future<List<ReverseSwapInfo>> checkReverseSwapStatus() async {
// ANCHOR: check-reverse-swaps-status
List<ReverseSwapInfo> inProgOnchainPaymentList = await BreezSDK().inProgressOnchainPayments();
List<ReverseSwapInfo> inProgOnchainPaymentList = await breezSDK.inProgressOnchainPayments();
for (var inProgOnchainPayment in inProgOnchainPaymentList) {
print("Onchain payment ${inProgOnchainPayment.id} in progress, status is ${inProgOnchainPayment.status.name}");
}
Expand Down
14 changes: 7 additions & 7 deletions snippets/dart_snippets/lib/receive_onchain.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:dart_snippets/sdk_instance.dart';
import 'package:breez_sdk/bridge_generated.dart';

Future<SwapInfo> generateReceiveOnchainAddress() async {
// ANCHOR: generate-receive-onchain-address
ReceiveOnchainRequest req = const ReceiveOnchainRequest();
SwapInfo swapInfo = await BreezSDK().receiveOnchain(req: req);
SwapInfo swapInfo = await breezSDK.receiveOnchain(req: req);

// Send your funds to the below bitcoin address
String address = swapInfo.bitcoinAddress;
Expand All @@ -17,15 +17,15 @@ Future<SwapInfo> generateReceiveOnchainAddress() async {

Future<SwapInfo?> getInProgressSwap() async {
// ANCHOR: in-progress-swap
SwapInfo? swapInfo = await BreezSDK().inProgressSwap();
SwapInfo? swapInfo = await breezSDK.inProgressSwap();
print(swapInfo);
// ANCHOR_END: in-progress-swap
return swapInfo;
}

Future<List<SwapInfo>> listRefundables() async {
// ANCHOR: list-refundables
List<SwapInfo> refundables = await BreezSDK().listRefundables();
List<SwapInfo> refundables = await breezSDK.listRefundables();
for (var refundable in refundables) {
print(refundable.bitcoinAddress);
}
Expand All @@ -44,7 +44,7 @@ Future<RefundResponse> executeRefund({
toAddress: toAddress,
satPerVbyte: satPerVbyte,
);
RefundResponse resp = await BreezSDK().refund(req: req);
RefundResponse resp = await breezSDK.refund(req: req);
print(resp.refundTxId);
// ANCHOR_END: execute-refund
return resp;
Expand All @@ -56,14 +56,14 @@ Future<OpenChannelFeeResponse> getChannelOpeningFees({
}) async {
// ANCHOR: get-channel-opening-fees
OpenChannelFeeRequest req = OpenChannelFeeRequest(amountMsat: amountMsat, expiry: expiry);
OpenChannelFeeResponse resp = await BreezSDK().openChannelFee(req: req);
OpenChannelFeeResponse resp = await breezSDK.openChannelFee(req: req);
print(resp.feeMsat);
// ANCHOR_END: get-channel-opening-fees
return resp;
}

Future rescanSwaps() async {
// ANCHOR: rescan-swaps
await BreezSDK().rescanSwaps();
await breezSDK.rescanSwaps();
// ANCHOR_END: rescan-swaps
}
Loading

0 comments on commit b8bdd1d

Please sign in to comment.