Skip to content

Commit

Permalink
Dart snippets: rewrite to match Rust snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Jul 5, 2024
1 parent b0b3626 commit 13bd24d
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 166 deletions.
2 changes: 2 additions & 0 deletions snippets/dart_snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
2. Place the files in the folder `snippets/dart-snippets/packages/breez-liquid-sdk-flutter`
3. Happy coding

To use a local path to the flutter bindings, see the `dependency_overrides` section in `pubspec.yaml`.

## Nix
Use the command `nix develop`
3 changes: 2 additions & 1 deletion snippets/dart_snippets/lib/fiat_currencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<List<FiatCurrency>> listFiatCurrencies() async {
// ANCHOR: list-fiat-currencies
List<FiatCurrency> fiatCurrencyList = await breezLiquidSDK.instance!.listFiatCurrencies();
List<FiatCurrency> fiatCurrencyList = await breezLiquidSDK.instance!
.listFiatCurrencies();
// ANCHOR_END: list-fiat-currencies
return fiatCurrencyList;
}
Expand Down
64 changes: 16 additions & 48 deletions snippets/dart_snippets/lib/getting_started.dart
Original file line number Diff line number Diff line change
@@ -1,74 +1,42 @@
import 'dart:typed_data';

import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

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

// It is recommended to use a single instance of BreezSDK across your Dart/Flutter app.
// It is recommended to use a single instance of BreezSDKLiquid across your Dart/Flutter app.
//
// All of the snippets assume a BreezSDK object is created on entrypoint of the app as such:
// All of the snippets assume a BreezSDKLiquid object is created on entrypoint of the app as such:
//
// BreezSDK breezSDK = BreezSDK();
// ConnectRequest req = ConnectRequest(...);
// BindingLiquidSdk instance = await connect(req: req);
//
// 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`.
// Initialize library
await initialize();
// Create the default config
String mnemonic = "<mnemonic words>";

// Create the default config
Uint8List seed = await breezSDK.mnemonicToSeed("<mnemonic words>");
String inviteCode = "<invite code>";
String apiKey = "<api key>";
NodeConfig nodeConfig = NodeConfig.greenlight(
config: GreenlightNodeConfig(
partnerCredentials: null,
inviteCode: inviteCode,
),
);
Config config = await breezSDK.defaultConfig(
envType: EnvironmentType.Production,
apiKey: apiKey,
nodeConfig: nodeConfig,
Config config = defaultConfig(
network: LiquidNetwork.Mainnet
);

// Customize the config object according to your needs
config = config.copyWith(workingDir: "path to an existing directory");

// Connect to the Breez SDK make it ready for use
ConnectRequest connectRequest = ConnectRequest(config: config, seed: seed);
return await breezSDK.connect(req: connectRequest);
ConnectRequest connectRequest = ConnectRequest(mnemonic: mnemonic, config: config);
BindingLiquidSdk instance = await 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);
// ANCHOR_END: init-sdk-restore-only
}

Future<void> fetchBalance(String lspId) async {
// ANCHOR: fetch-balance
NodeState? nodeInfo = await breezSDK.nodeInfo();
if (nodeInfo != null) {
int lnBalance = nodeInfo.channelsBalanceMsat;
int onchainBalance = nodeInfo.onchainBalanceMsat;
print(lnBalance);
print(onchainBalance);
GetInfoResponse? nodeState = await breezLiquidSDK.instance!.getInfo();
if (nodeState != null) {
BigInt balanceSat = nodeState.balanceSat;
BigInt pendingSendSat = nodeState.pendingSendSat;
BigInt pendingReceiveSat = nodeState.pendingReceiveSat;
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
void onLogEntry(log) {
print("Received log ${log.level}]: ${log.line}");
}

void logging(BreezSDK breezSDK) {
breezSDK.logStream.listen(onLogEntry);
}
// ANCHOR_END: logging
5 changes: 2 additions & 3 deletions snippets/dart_snippets/lib/list_payments.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<List<Payment>> listPayments() async {
// ANCHOR: list-payments
ListPaymentsRequest req = ListPaymentsRequest();
List<Payment> paymentsList = await breezSDK.listPayments(req: req);
print(paymentsList);
List<Payment> paymentsList = await breezLiquidSDK.instance!.listPayments();
// ANCHOR_END: list-payments
return paymentsList;
}
50 changes: 20 additions & 30 deletions snippets/dart_snippets/lib/pay_onchain.dart
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<OnchainPaymentLimitsResponse> getCurrentLimits() async {
// ANCHOR: get-current-pay-onchain-limits
OnchainPaymentLimitsResponse currentLimits = await breezSDK.onchainPaymentLimits();
print("Minimum amount, in sats: ${currentLimits.minSat}");
print("Maximum amount, in sats: ${currentLimits.maxSat}");
OnchainPaymentLimitsResponse currentLimits = await breezLiquidSDK.instance!
.fetchOnchainLimits();
print("Minimum amount: ${currentLimits.send.minSat} sats");
print("Maximum amount: ${currentLimits.send.maxSat} sats");
// ANCHOR_END: get-current-pay-onchain-limits
return currentLimits;
}

Future<PrepareOnchainPaymentResponse> preparePayOnchain({
required int amountSat,
required int satPerVbyte,
}) async {
Future<PreparePayOnchainResponse> preparePayOnchain() async {
// ANCHOR: prepare-pay-onchain
PrepareOnchainPaymentRequest req = PrepareOnchainPaymentRequest(
amountSat: amountSat,
amountType: SwapAmountType.Send,
claimTxFeerate: satPerVbyte,
PreparePayOnchainRequest preparePayOnchainRequest = PreparePayOnchainRequest(
receiverAmountSat: 5000 as BigInt
);
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");
PreparePayOnchainResponse prepareRes = await breezLiquidSDK.instance!
.preparePayOnchain(req: preparePayOnchainRequest);

// Check if the fees are acceptable before proceeding
BigInt feesSat = prepareRes.feesSat;
// ANCHOR_END: prepare-pay-onchain

return prepareRes;
}

Future<PayOnchainResponse> startReverseSwap({
required String onchainRecipientAddress,
required PrepareOnchainPaymentResponse prepareRes,
Future<SendPaymentResponse> startReverseSwap({
required PreparePayOnchainResponse prepareRes,
}) async {
// ANCHOR: start-reverse-swap
String destinationAddress = "bc1..";

PayOnchainRequest req = PayOnchainRequest(
recipientAddress: onchainRecipientAddress,
address: destinationAddress,
prepareRes: prepareRes,
);
PayOnchainResponse res = await breezSDK.payOnchain(req: req);
SendPaymentResponse res = await breezLiquidSDK.instance!.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();
for (var inProgOnchainPayment in inProgOnchainPaymentList) {
print("Onchain payment ${inProgOnchainPayment.id} in progress, status is ${inProgOnchainPayment.status.name}");
}
// ANCHOR_END: check-reverse-swaps-status
return inProgOnchainPaymentList;
}
72 changes: 33 additions & 39 deletions snippets/dart_snippets/lib/receive_onchain.dart
Original file line number Diff line number Diff line change
@@ -1,68 +1,62 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<SwapInfo> generateReceiveOnchainAddress() async {
Future<ReceiveOnchainResponse> generateReceiveOnchainAddress() async {
// ANCHOR: generate-receive-onchain-address
ReceiveOnchainRequest req = const ReceiveOnchainRequest();
SwapInfo swapInfo = await breezSDK.receiveOnchain(req: req);
// Fetch the Onchain Receive limits
OnchainPaymentLimitsResponse currentLimits = await breezLiquidSDK.instance!
.fetchOnchainLimits();
print("Minimum amount: ${currentLimits.receive.minSat} sats");
print("Maximum amount: ${currentLimits.receive.maxSat} sats");

// Set the amount you wish the payer to send, which should be within the above limits
PrepareReceiveOnchainResponse prepareResponse = await breezLiquidSDK.instance!
.prepareReceiveOnchain(req: PrepareReceiveOnchainRequest (
payerAmountSat: 50000 as BigInt,
));

// If the fees are acceptable, continue to create the Onchain Receive Payment
BigInt receiveFeesSat = prepareResponse.feesSat;

ReceiveOnchainResponse receiveOnchainResponse = await breezLiquidSDK.instance!
.receiveOnchain(req: prepareResponse);

// Send your funds to the below bitcoin address
String address = swapInfo.bitcoinAddress;
print(address);
print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}");
print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}");
return swapInfo;
String address = receiveOnchainResponse.address;
String bip21 = receiveOnchainResponse.bip21;
// ANCHOR_END: generate-receive-onchain-address
}

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

Future<List<SwapInfo>> listRefundables() async {
Future<List<RefundableSwap>> listRefundables() async {
// ANCHOR: list-refundables
List<SwapInfo> refundables = await breezSDK.listRefundables();
for (var refundable in refundables) {
print(refundable.bitcoinAddress);
}
List<RefundableSwap> refundables = await breezLiquidSDK.instance!.listRefundables();
// ANCHOR_END: list-refundables
return refundables;
}

Future<RefundResponse> executeRefund({
required String swapAddress,
required String toAddress,
required int satPerVbyte,
required int refundTxFeeRate,
required RefundableSwap refundable
}) async {
// ANCHOR: execute-refund
String destinationAddress = "...";
int satPerVbyte = refundTxFeeRate;

RefundRequest req = RefundRequest(
swapAddress: swapAddress,
toAddress: toAddress,
swapAddress: refundable.swapAddress,
refundAddress: destinationAddress,
satPerVbyte: satPerVbyte,
);
RefundResponse resp = await breezSDK.refund(req: req);
RefundResponse resp = await breezLiquidSDK.instance!.refund(req: req);
print(resp.refundTxId);
// ANCHOR_END: execute-refund
return resp;
}

Future<OpenChannelFeeResponse> getChannelOpeningFees({
required int amountMsat,
int? expiry,
}) async {
// ANCHOR: get-channel-opening-fees
OpenChannelFeeRequest req = OpenChannelFeeRequest(amountMsat: amountMsat, expiry: expiry);
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 breezLiquidSDK.instance!.rescanOnchainSwaps();
// ANCHOR_END: rescan-swaps
}
25 changes: 19 additions & 6 deletions snippets/dart_snippets/lib/receive_payment.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<ReceivePaymentResponse> receivePayment() async {
// ANCHOR: receive-payment
ReceivePaymentRequest req = const ReceivePaymentRequest(
amountMsat: 3000000,
description: "Invoice for 3000 sats",
);
ReceivePaymentResponse receivePaymentResponse = await breezSDK.receivePayment(req: req);
// Fetch the Receive limits
LightningPaymentLimitsResponse currentLimits = await breezLiquidSDK.instance!
.fetchLightningLimits();
print("Minimum amount: ${currentLimits.receive.minSat} sats");
print("Maximum amount: ${currentLimits.receive.maxSat} sats");

print(receivePaymentResponse.lnInvoice);
// Set the amount you wish the payer to send
PrepareReceiveResponse prepareReceiveResponse = await breezLiquidSDK.instance!
.prepareReceivePayment(req: PrepareReceiveRequest (
payerAmountSat: 5000 as BigInt,
));

// If the fees are acceptable, continue to create the Receive Payment
BigInt receiveFeesSat = prepareReceiveResponse.feesSat;

ReceivePaymentResponse receivePaymentResponse = await breezLiquidSDK.instance!
.receivePayment(req: prepareReceiveResponse);

String invoice = receivePaymentResponse.invoice;
// ANCHOR_END: receive-payment

return receivePaymentResponse;
Expand Down
21 changes: 14 additions & 7 deletions snippets/dart_snippets/lib/send_payment.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<SendPaymentResponse> sendPayment({required String bolt11}) async {
// 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'.
int optionalAmountMsat = 3000000;
String optionalLabel = "<label>";
SendPaymentRequest req = SendPaymentRequest(bolt11: bolt11, amountMsat: optionalAmountMsat, label: optionalLabel);
SendPaymentResponse resp = await breezSDK.sendPayment(req: req);
// Set the BOLT11 invoice you wish to pay
PrepareSendResponse prepareSendResponse = await breezLiquidSDK.instance!
.prepareSendPayment(
req: PrepareSendRequest(invoice: "...")
);

// If the fees are acceptable, continue to create the Send Payment
BigInt sendFeesSat = prepareSendResponse.feesSat;

SendPaymentResponse sendPaymentResponse = await breezLiquidSDK.instance!
.sendPayment(req: prepareSendResponse);
Payment payment = sendPaymentResponse.payment;
// ANCHOR_END: send-payment
return resp;
return sendPaymentResponse;
}
Loading

0 comments on commit 13bd24d

Please sign in to comment.