cake_wallet/lib/view_model/lightning_view_model.dart

85 lines
2.6 KiB
Dart
Raw Normal View History

2024-02-14 17:34:52 +00:00
import 'dart:async';
import 'package:breez_sdk/breez_sdk.dart';
import 'package:breez_sdk/bridge_generated.dart';
import 'package:mobx/mobx.dart';
part 'lightning_view_model.g.dart';
class LightningViewModel = LightningViewModelBase with _$LightningViewModel;
abstract class LightningViewModelBase with Store {
LightningViewModelBase() {}
2024-02-14 18:21:37 +00:00
Future<List<String>> receiveOnchain() async {
final sdk = await BreezSDK();
ReceiveOnchainRequest req = const ReceiveOnchainRequest();
SwapInfo swapInfo = await sdk.receiveOnchain(req: req);
print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}");
print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}");
2024-02-27 21:50:26 +00:00
int fee = swapInfo.channelOpeningFees?.minMsat ?? 2000;
fee = fee ~/ 1000;
2024-02-14 18:21:37 +00:00
return [
swapInfo.bitcoinAddress,
swapInfo.minAllowedDeposit.toString(),
2024-02-27 19:24:02 +00:00
swapInfo.maxAllowedDeposit.toString(),
2024-02-27 21:50:26 +00:00
fee.toString(),
2024-02-14 18:21:37 +00:00
];
}
2024-02-14 17:34:52 +00:00
2024-02-14 18:21:37 +00:00
Future<String> createInvoice({required String amount, String? description}) async {
final sdk = await BreezSDK();
final req = ReceivePaymentRequest(
2024-02-27 21:50:26 +00:00
amountMsat: (double.parse(amount) * 1000).round(),
2024-02-14 18:21:37 +00:00
description: description ?? '',
);
final res = await sdk.receivePayment(req: req);
return res.lnInvoice.bolt11;
}
2024-02-14 17:34:52 +00:00
2024-02-26 19:46:05 +00:00
Future<List<String>> invoiceLimitsSats() async {
2024-02-14 18:21:37 +00:00
final sdk = await BreezSDK();
2024-02-29 20:39:49 +00:00
ReceivePaymentRequest? req = null;
req = ReceivePaymentRequest(
amountMsat: 10000 * 1000, // 10000 sats
2024-02-26 18:01:39 +00:00
description: "limits",
2024-02-14 18:21:37 +00:00
);
2024-02-22 03:03:19 +00:00
final res = await sdk.receivePayment(req: req);
2024-02-27 21:50:26 +00:00
int min = (res.openingFeeMsat ?? (2500 * 1000)) ~/ 1000;
2024-02-29 20:39:49 +00:00
int max = 1000000000 * 1000 * 10; // 10 BTC
2024-02-26 18:01:39 +00:00
return [min.toString(), max.toString()];
2024-02-14 17:34:52 +00:00
}
2024-03-01 16:49:24 +00:00
2024-03-01 17:08:32 +00:00
Future<List<int>> invoiceSoftLimitsSats() async {
2024-03-01 16:49:24 +00:00
final sdk = await BreezSDK();
ReceivePaymentRequest? req = null;
req = ReceivePaymentRequest(
amountMsat: 10000 * 1000, // 10000 sats
description: "limits",
);
final res = await sdk.receivePayment(req: req);
int min = (res.openingFeeMsat ?? (2500 * 1000)) ~/ 1000;
int max = 1000000000 * 1000 * 10; // 10 BTC
2024-03-01 17:08:32 +00:00
int balance = 0;
2024-03-01 16:49:24 +00:00
try {
final nodeState = (await sdk.nodeInfo())!;
max = nodeState.inboundLiquidityMsats ~/ 1000;
2024-03-01 17:08:32 +00:00
balance = nodeState.channelsBalanceMsat ~/ 1000;
2024-03-01 16:49:24 +00:00
} catch (_) {}
2024-03-01 17:08:32 +00:00
return [min, max, balance];
}
Future<int> getBalanceSats() async {
try {
final sdk = await BreezSDK();
final nodeState = (await sdk.nodeInfo())!;
return nodeState.channelsBalanceMsat ~/ 1000;
} catch (_) {
return 0;
}
2024-03-01 16:49:24 +00:00
}
2024-02-14 17:34:52 +00:00
}