Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document input parsing and amountless swaps #56

Merged
merged 7 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ on:
workflow_dispatch:
inputs:
sdk-ref:
description: 'sdk commit/tag/branch reference. Defaults to 0.5.0-rc8'
description: 'sdk commit/tag/branch reference. Defaults to 0.6.0-dev1'
required: false
type: string
default: 0.5.0-rc8
default: 0.6.0-dev1

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
Expand All @@ -26,9 +26,9 @@ jobs:
runs-on: ubuntu-latest
outputs:
# Used only for Rust snippets
sdk-ref: ${{ inputs.sdk-ref || '0.5.0-rc8' }}
sdk-ref: ${{ inputs.sdk-ref || '0.6.0-dev1' }}
# Used for RN and Flutter snippets
package-version: '0.5.0-rc8'
package-version: '0.6.0-dev1'
steps:
- run: echo "set pre-setup output variables"

Expand Down
15 changes: 8 additions & 7 deletions snippets/csharp/GettingStarted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public void FetchBalance(BindingLiquidSdk sdk)
try
{
var info = sdk.GetInfo();
var balanceSat = info?.balanceSat;
var pendingSendSat = info?.pendingSendSat;
var pendingReceiveSat = info?.pendingReceiveSat;
var balanceSat = info?.walletInfo?.balanceSat;
var pendingSendSat = info?.walletInfo?.pendingSendSat;
var pendingReceiveSat = info?.walletInfo?.pendingReceiveSat;
}
catch (Exception)
{
// Handle error
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
public class SdkLogger : Logger
{
Expand All @@ -55,7 +55,8 @@ public void Log(LogEntry l)
}
}

public void SetLogger(SdkLogger logger) {
public void SetLogger(SdkLogger logger)
{
try
{
BreezSdkLiquidMethods.SetLogger(logger);
Expand All @@ -70,7 +71,7 @@ public void SetLogger(SdkLogger logger) {
// ANCHOR: add-event-listener
public class SdkListener : EventListener
{
public void OnEvent(SdkEvent e)
public void OnEvent(SdkEvent e)
{
Console.WriteLine($"Received event {e}");
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public void RemoveEventListener(BindingLiquidSdk sdk, String listenerId)
}
}
// ANCHOR_END: remove-event-listener

// ANCHOR: disconnect
public void Disconnect(BindingLiquidSdk sdk)
{
Expand Down
2 changes: 1 addition & 1 deletion snippets/csharp/LnurlAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void LnurlAuth(BindingLiquidSdk sdk)

try
{
var input = BreezSdkLiquidMethods.Parse(lnurlAuthUrl);
var input = sdk.Parse(lnurlAuthUrl);
if (input is InputType.LnUrlAuth lnurla)
{
var result = sdk.LnurlAuth(lnurla.data);
Expand Down
2 changes: 1 addition & 1 deletion snippets/csharp/LnurlPay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void PrepareLnurlPay(BindingLiquidSdk sdk)

try
{
var input = BreezSdkLiquidMethods.Parse(lnurlPayUrl);
var input = sdk.Parse(lnurlPayUrl);
if (input is InputType.LnUrlPay lnurlp)
{
var amountMsat = lnurlp.data.minSendable;
Expand Down
2 changes: 1 addition & 1 deletion snippets/csharp/LnurlWithdraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void LnurlWithdraw(BindingLiquidSdk sdk)

try
{
var input = BreezSdkLiquidMethods.Parse(lnurlWithdrawUrl);
var input = sdk.Parse(lnurlWithdrawUrl);
if (input is InputType.LnUrlWithdraw lnurlw)
{
var amountMsat = lnurlw.data.minWithdrawable;
Expand Down
88 changes: 88 additions & 0 deletions snippets/csharp/ParsingInputs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Breez.Sdk.Liquid;

public class ParsingInputsSnippets
{
public void ParseInput(BindingLiquidSdk sdk)
{
// ANCHOR: parse-inputs
var input = "an input to be parsed...";

try
{
var parsed = sdk.Parse(input);

switch (parsed)
{
case InputType.BitcoinAddress bitcoinAddress:
Console.WriteLine($"Input is Bitcoin address {bitcoinAddress.address}");
break;

case InputType.Bolt11 bolt11:
var amount = bolt11.invoice.amountMsat.HasValue
? bolt11.invoice.amountMsat.Value.ToString()
: "unknown";
Console.WriteLine($"Input is BOLT11 invoice for {amount} msats");
break;

case InputType.LnUrlPay lnUrlPay:
Console.WriteLine(
$"Input is LNURL-Pay/Lightning address accepting min/max {lnUrlPay.data.minSendable}/{lnUrlPay.data.maxSendable} msats"
);
break;

case InputType.LnUrlWithdraw lnUrlWithdraw:
Console.WriteLine(
$"Input is LNURL-Withdraw for min/max {lnUrlWithdraw.data.minWithdrawable}/{lnUrlWithdraw.data.maxWithdrawable} msats"
);
break;
default:
// Other input types are available
break;
}
}
catch (Exception)
{
// Handle error
}
// ANCHOR_END: parse-inputs
}

public void ConfigureParsers()
{
// ANCHOR: configure-external-parser
var mnemonic = "<mnemonic words>";

// Create the default config, providing your Breez API key
var config = BreezSdkLiquidMethods.DefaultConfig(
LiquidNetwork.Mainnet,
"<your-Breez-API-key>"
) with
{
// Configure external parsers
externalInputParsers = new List<ExternalInputParser>
{
new(
providerId: "provider_a",
inputRegex: "^provider_a",
parserUrl: "https://parser-domain.com/parser?input=<input>"
),
new(
providerId: "provider_b",
inputRegex: "^provider_b",
parserUrl: "https://parser-domain.com/parser?input=<input>"
)
}
};

try
{
var connectRequest = new ConnectRequest(config, mnemonic);
var sdk = BreezSdkLiquidMethods.Connect(connectRequest);
}
catch (Exception)
{
// Handle error
}
// ANCHOR_END: configure-external-parser
}
}
35 changes: 33 additions & 2 deletions snippets/csharp/ReceiveOnchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void ExecuteRefund(BindingLiquidSdk sdk, uint refundTxFeeRate, Refundable
{
sdk.Refund(
new RefundRequest(
refundable.swapAddress,
destinationAddress,
refundable.swapAddress,
destinationAddress,
feeRateSatPerVbyte));
}
catch (Exception)
Expand Down Expand Up @@ -63,4 +63,35 @@ public void RecommendedFees(BindingLiquidSdk sdk)
}
// ANCHOR_END: recommended-fees
}

public void HandlePaymentsWaitingFeeAcceptance(BindingLiquidSdk sdk)
{
// ANCHOR: handle-payments-waiting-fee-acceptance
// Payments on hold waiting for fee acceptance have the state WaitingFeeAcceptance
var paymentsWaitingFeeAcceptance = sdk.ListPayments(
new ListPaymentsRequest()
{
states = new List<PaymentState>() { PaymentState.WaitingFeeAcceptance }
});

foreach (var payment in paymentsWaitingFeeAcceptance)
{
if (payment.details is not PaymentDetails.Bitcoin bitcoinDetails)
{
// Only Bitcoin payments can be `WaitingFeeAcceptance`
continue;
}

var fetchFeesResponse = sdk.FetchPaymentProposedFees(
new FetchPaymentProposedFeesRequest(bitcoinDetails.swapId));

Console.WriteLine(
$"Payer sent {fetchFeesResponse.payerAmountSat} and currently proposed fees are {fetchFeesResponse.feesSat}");

// If the user is ok with the fees, accept them, allowing the payment to proceed
sdk.AcceptPaymentProposedFees(
new AcceptPaymentProposedFeesRequest(fetchFeesResponse));
}
// ANCHOR_END: handle-payments-waiting-fee-acceptance
}
}
2 changes: 1 addition & 1 deletion snippets/csharp/Snippets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
<PackageReference Include="Breez.Sdk.Liquid" Version="*" />
</ItemGroup>

</Project>
</Project>
6 changes: 3 additions & 3 deletions snippets/dart_snippets/lib/getting_started.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Future<void> initializeSDK() async {
Future<void> fetchBalance(String lspId) async {
// ANCHOR: fetch-balance
GetInfoResponse? info = await breezSDKLiquid.instance!.getInfo();
BigInt balanceSat = info.balanceSat;
BigInt pendingSendSat = info.pendingSendSat;
BigInt pendingReceiveSat = info.pendingReceiveSat;
BigInt balanceSat = info.walletInfo.balanceSat;
BigInt pendingSendSat = info.walletInfo.pendingSendSat;
BigInt pendingReceiveSat = info.walletInfo.pendingReceiveSat;
// ANCHOR_END: fetch-balance
print(balanceSat);
print(pendingSendSat);
Expand Down
2 changes: 1 addition & 1 deletion snippets/dart_snippets/lib/lnurl_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Future<void> lnurlAuth() async {
String lnurlAuthUrl =
"lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu";

InputType inputType = await parse(input: lnurlAuthUrl);
InputType inputType = await breezSDKLiquid.instance!.parse(input: lnurlAuthUrl);
if (inputType is InputType_LnUrlAuth) {
LnUrlCallbackStatus result = await breezSDKLiquid.instance!.lnurlAuth(reqData: inputType.data);
if (result is LnUrlCallbackStatus_Ok) {
Expand Down
2 changes: 1 addition & 1 deletion snippets/dart_snippets/lib/lnurl_pay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Future<void> prepareLnurlPay() async {
/// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf
String lnurlPayUrl = "[email protected]";

InputType inputType = await parse(input: lnurlPayUrl);
InputType inputType = await breezSDKLiquid.instance!.parse(input: lnurlPayUrl);
if (inputType is InputType_LnUrlPay) {
BigInt amountMsat = inputType.data.minSendable;
String optionalComment = "<comment>";
Expand Down
2 changes: 1 addition & 1 deletion snippets/dart_snippets/lib/lnurl_withdraw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Future<void> lnurlWithdraw() async {
String lnurlWithdrawUrl =
"lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk";

InputType inputType = await parse(input: lnurlWithdrawUrl);
InputType inputType = await breezSDKLiquid.instance!.parse(input: lnurlWithdrawUrl);
if (inputType is InputType_LnUrlWithdraw) {
BigInt amountMsat = inputType.data.minWithdrawable;
LnUrlWithdrawRequest req = LnUrlWithdrawRequest(
Expand Down
60 changes: 60 additions & 0 deletions snippets/dart_snippets/lib/parsing_inputs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:dart_snippets/sdk_instance.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';

Future<void> parseInput() async {
// ANCHOR: parse-inputs
String input = "an input to be parsed...";

InputType inputType = await breezSDKLiquid.instance!.parse(input: input);
if (inputType is InputType_BitcoinAddress) {
print("Input is Bitcoin address ${inputType.address.address}");
} else if (inputType is InputType_Bolt11) {
String amountStr = inputType.invoice.amountMsat != null
? inputType.invoice.amountMsat.toString()
: "unknown";
print("Input is BOLT11 invoice for $amountStr msats");
} else if (inputType is InputType_LnUrlPay) {
print(
"Input is LNURL-Pay/Lightning address accepting min/max ${inputType.data.minSendable}/${inputType.data.maxSendable} msats");
} else if (inputType is InputType_LnUrlWithdraw) {
print(
"Input is LNURL-Withdraw for min/max ${inputType.data.minWithdrawable}/${inputType.data.maxWithdrawable} msats");
} else {
// Other input types are available
}
// ANCHOR_END: parse-inputs
}

Future<void> configureParsers() async {
// ANCHOR: configure-external-parser
// Create the default config
String mnemonic = "<mnemonic words>";

// Create the default config, providing your Breez API key
Config config = defaultConfig(
network: LiquidNetwork.mainnet,
breezApiKey: "<your-Breez-API-key>"
);

// Configure external parsers
config = config.copyWith(
externalInputParsers: [
ExternalInputParser(
providerId: "provider_a",
inputRegex: "^provider_a",
parserUrl: "https://parser-domain.com/parser?input=<input>",
),
ExternalInputParser(
providerId: "provider_b",
inputRegex: "^provider_b",
parserUrl: "https://parser-domain.com/parser?input=<input>",
),
],
);

ConnectRequest connectRequest = ConnectRequest(mnemonic: mnemonic, config: config);

await breezSDKLiquid.connect(req: connectRequest);

// ANCHOR_END: configure-external-parser
}
37 changes: 37 additions & 0 deletions snippets/dart_snippets/lib/receive_onchain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,40 @@ Future recommendedFees() async {
// ANCHOR_END: recommended-fees
print(fees);
}

Future<void> handlePaymentsWaitingFeeAcceptance() async {
// ANCHOR: handle-payments-waiting-fee-acceptance
// Payments on hold waiting for fee acceptance have the state WaitingFeeAcceptance
List<Payment> paymentsWaitingFeeAcceptance = await breezSDKLiquid.instance!.listPayments(
req: ListPaymentsRequest(
states: [PaymentState.waitingFeeAcceptance],
),
);

for (Payment payment in paymentsWaitingFeeAcceptance) {
if (payment.details is! PaymentDetails_Bitcoin) {
// Only Bitcoin payments can be `WaitingFeeAcceptance`
continue;
}

PaymentDetails_Bitcoin details = payment.details as PaymentDetails_Bitcoin;
FetchPaymentProposedFeesResponse fetchFeesResponse =
await breezSDKLiquid.instance!.fetchPaymentProposedFees(
req: FetchPaymentProposedFeesRequest(
swapId: details.swapId,
),
);

print(
"Payer sent ${fetchFeesResponse.payerAmountSat} and currently proposed fees are ${fetchFeesResponse.feesSat}",
);

// If the user is ok with the fees, accept them, allowing the payment to proceed
await breezSDKLiquid.instance!.acceptPaymentProposedFees(
req: AcceptPaymentProposedFeesRequest(
response: fetchFeesResponse,
),
);
}
// ANCHOR_END: handle-payments-waiting-fee-acceptance
}
Loading
Loading