cake_wallet/lib/view_model/lightning_view_model.dart
2024-04-16 15:23:48 -07:00

130 lines
3.9 KiB
Dart

import 'dart:async';
import 'package:breez_sdk/breez_sdk.dart';
import 'package:breez_sdk/bridge_generated.dart' as BZG;
import 'package:mobx/mobx.dart';
import 'package:cw_lightning/.secrets.g.dart' as secrets;
part 'lightning_view_model.g.dart';
class LightningViewModel = LightningViewModelBase with _$LightningViewModel;
abstract class LightningViewModelBase with Store {
LightningViewModelBase() {}
Future<ReceiveOnchainResult> receiveOnchain() async {
final sdk = await BreezSDK();
BZG.ReceiveOnchainRequest req = const BZG.ReceiveOnchainRequest();
BZG.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}");
int fee = 0;
double feePercent = 0;
try {
final nodeState = (await sdk.nodeInfo())!;
int inboundLiquidity = nodeState.inboundLiquidityMsats ~/ 1000;
final openingFees = await sdk.openChannelFee(
req: BZG.OpenChannelFeeRequest(amountMsat: inboundLiquidity + 1));
feePercent = (openingFees.feeParams.proportional * 100) / 1000000;
fee = openingFees.feeParams.minMsat ~/ 1000;
} catch (_) {}
return ReceiveOnchainResult(
bitcoinAddress: swapInfo.bitcoinAddress,
minAllowedDeposit: swapInfo.minAllowedDeposit,
maxAllowedDeposit: swapInfo.maxAllowedDeposit,
feePercent: feePercent,
fee: fee,
);
}
Future<String> createInvoice({required String amountSats, String? description}) async {
final sdk = await BreezSDK();
final req = BZG.ReceivePaymentRequest(
amountMsat: (double.parse(amountSats) * 1000).round(),
description: description ?? '',
);
final res = await sdk.receivePayment(req: req);
return res.lnInvoice.bolt11;
}
Future<InvoiceSoftLimitsResult> invoiceSoftLimitsSats() async {
double feePercent = 0.4;
int minFee = (2500 * 1000) ~/ 1000; // 2500 sats
int inboundLiquidity = 1000000000 * 1000 * 10; // 10 BTC
int balance = 0;
final sdk = await BreezSDK();
try {
final nodeState = (await sdk.nodeInfo())!;
inboundLiquidity = nodeState.inboundLiquidityMsats ~/ 1000;
final openingFees = await sdk.openChannelFee(
req: BZG.OpenChannelFeeRequest(amountMsat: inboundLiquidity + 1));
feePercent = (openingFees.feeParams.proportional * 100) / 1000000;
minFee = openingFees.feeParams.minMsat ~/ 1000;
balance = nodeState.channelsBalanceMsat ~/ 1000;
} catch (_) {}
return InvoiceSoftLimitsResult(
minFee: minFee,
inboundLiquidity: inboundLiquidity,
feePercent: feePercent,
balance: balance,
);
}
Future<int> getBalanceSats() async {
try {
final sdk = await BreezSDK();
final nodeState = (await sdk.nodeInfo())!;
return nodeState.channelsBalanceMsat ~/ 1000;
} catch (_) {
return 0;
}
}
Future<BZG.HealthCheckStatus> serviceHealthCheck() async {
try {
final sdk = await BreezSDK();
BZG.ServiceHealthCheckResponse response =
await sdk.serviceHealthCheck(apiKey: secrets.breezApiKey);
return response.status;
} catch (_) {
return BZG.HealthCheckStatus.ServiceDisruption;
}
}
}
class ReceiveOnchainResult {
final String bitcoinAddress;
final int minAllowedDeposit;
final int maxAllowedDeposit;
final int fee;
final double feePercent;
ReceiveOnchainResult({
required this.bitcoinAddress,
required this.minAllowedDeposit,
required this.maxAllowedDeposit,
required this.fee,
required this.feePercent,
});
}
class InvoiceSoftLimitsResult {
final double feePercent;
final int minFee;
final int inboundLiquidity;
final int balance;
InvoiceSoftLimitsResult({
required this.inboundLiquidity,
required this.feePercent,
required this.minFee,
required this.balance,
});
}