From 13bd24d669550b31bf6fbe8d65b73a228e936d79 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:45:11 +0200 Subject: [PATCH] Dart snippets: rewrite to match Rust snippets --- snippets/dart_snippets/README.md | 2 + .../dart_snippets/lib/fiat_currencies.dart | 3 +- .../dart_snippets/lib/getting_started.dart | 64 +++++------------ snippets/dart_snippets/lib/list_payments.dart | 5 +- snippets/dart_snippets/lib/pay_onchain.dart | 50 ++++++------- .../dart_snippets/lib/receive_onchain.dart | 72 +++++++++---------- .../dart_snippets/lib/receive_payment.dart | 25 +++++-- snippets/dart_snippets/lib/send_payment.dart | 21 ++++-- snippets/dart_snippets/pubspec.lock | 32 --------- 9 files changed, 108 insertions(+), 166 deletions(-) diff --git a/snippets/dart_snippets/README.md b/snippets/dart_snippets/README.md index 26e925a..6fbbae1 100644 --- a/snippets/dart_snippets/README.md +++ b/snippets/dart_snippets/README.md @@ -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` \ No newline at end of file diff --git a/snippets/dart_snippets/lib/fiat_currencies.dart b/snippets/dart_snippets/lib/fiat_currencies.dart index 243bca3..805c40e 100644 --- a/snippets/dart_snippets/lib/fiat_currencies.dart +++ b/snippets/dart_snippets/lib/fiat_currencies.dart @@ -3,7 +3,8 @@ import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; Future> listFiatCurrencies() async { // ANCHOR: list-fiat-currencies - List fiatCurrencyList = await breezLiquidSDK.instance!.listFiatCurrencies(); + List fiatCurrencyList = await breezLiquidSDK.instance! + .listFiatCurrencies(); // ANCHOR_END: list-fiat-currencies return fiatCurrencyList; } diff --git a/snippets/dart_snippets/lib/getting_started.dart b/snippets/dart_snippets/lib/getting_started.dart index 161d615..6de24a5 100644 --- a/snippets/dart_snippets/lib/getting_started.dart +++ b/snippets/dart_snippets/lib/getting_started.dart @@ -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 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 = ""; // Create the default config - Uint8List seed = await breezSDK.mnemonicToSeed(""); - String inviteCode = ""; - String apiKey = ""; - 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 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 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 diff --git a/snippets/dart_snippets/lib/list_payments.dart b/snippets/dart_snippets/lib/list_payments.dart index 5efd096..9f2c9b2 100644 --- a/snippets/dart_snippets/lib/list_payments.dart +++ b/snippets/dart_snippets/lib/list_payments.dart @@ -1,10 +1,9 @@ +import 'package:dart_snippets/sdk_instance.dart'; import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; Future> listPayments() async { // ANCHOR: list-payments - ListPaymentsRequest req = ListPaymentsRequest(); - List paymentsList = await breezSDK.listPayments(req: req); - print(paymentsList); + List paymentsList = await breezLiquidSDK.instance!.listPayments(); // ANCHOR_END: list-payments return paymentsList; } diff --git a/snippets/dart_snippets/lib/pay_onchain.dart b/snippets/dart_snippets/lib/pay_onchain.dart index a5c4173..17c28b3 100644 --- a/snippets/dart_snippets/lib/pay_onchain.dart +++ b/snippets/dart_snippets/lib/pay_onchain.dart @@ -1,52 +1,42 @@ +import 'package:dart_snippets/sdk_instance.dart'; import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; Future 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 preparePayOnchain({ - required int amountSat, - required int satPerVbyte, -}) async { +Future 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 startReverseSwap({ - required String onchainRecipientAddress, - required PrepareOnchainPaymentResponse prepareRes, +Future 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> checkReverseSwapStatus() async { - // ANCHOR: check-reverse-swaps-status - List 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; -} diff --git a/snippets/dart_snippets/lib/receive_onchain.dart b/snippets/dart_snippets/lib/receive_onchain.dart index ea894f9..208928f 100644 --- a/snippets/dart_snippets/lib/receive_onchain.dart +++ b/snippets/dart_snippets/lib/receive_onchain.dart @@ -1,68 +1,62 @@ +import 'package:dart_snippets/sdk_instance.dart'; import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; -Future generateReceiveOnchainAddress() async { +Future 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 getInProgressSwap() async { - // ANCHOR: in-progress-swap - SwapInfo? swapInfo = await breezSDK.inProgressSwap(); - print(swapInfo); - // ANCHOR_END: in-progress-swap - return swapInfo; + return receiveOnchainResponse; } -Future> listRefundables() async { +Future> listRefundables() async { // ANCHOR: list-refundables - List refundables = await breezSDK.listRefundables(); - for (var refundable in refundables) { - print(refundable.bitcoinAddress); - } + List refundables = await breezLiquidSDK.instance!.listRefundables(); // ANCHOR_END: list-refundables return refundables; } Future 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 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 } diff --git a/snippets/dart_snippets/lib/receive_payment.dart b/snippets/dart_snippets/lib/receive_payment.dart index aa8ae30..5dc37ee 100644 --- a/snippets/dart_snippets/lib/receive_payment.dart +++ b/snippets/dart_snippets/lib/receive_payment.dart @@ -1,14 +1,27 @@ +import 'package:dart_snippets/sdk_instance.dart'; import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; Future 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; diff --git a/snippets/dart_snippets/lib/send_payment.dart b/snippets/dart_snippets/lib/send_payment.dart index 9eab8bc..7dbd068 100644 --- a/snippets/dart_snippets/lib/send_payment.dart +++ b/snippets/dart_snippets/lib/send_payment.dart @@ -1,13 +1,20 @@ +import 'package:dart_snippets/sdk_instance.dart'; import 'package:flutter_breez_liquid/flutter_breez_liquid.dart'; Future 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 = "