-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dart snippets: rewrite to match Rust snippets
- Loading branch information
Showing
9 changed files
with
108 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.