2024-02-14 17:34:52 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:breez_sdk/breez_sdk.dart';
|
2024-03-06 18:38:11 +00:00
|
|
|
import 'package:breez_sdk/bridge_generated.dart' as BZG;
|
|
|
|
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
|
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
|
|
|
import 'package:cake_wallet/lightning/lightning.dart';
|
|
|
|
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2024-02-14 17:34:52 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'lightning_view_model.g.dart';
|
|
|
|
|
|
|
|
class LightningViewModel = LightningViewModelBase with _$LightningViewModel;
|
|
|
|
|
|
|
|
abstract class LightningViewModelBase with Store {
|
2024-03-07 06:10:40 +00:00
|
|
|
LightningViewModelBase() {}
|
2024-03-06 18:38:11 +00:00
|
|
|
|
2024-03-07 06:10:40 +00:00
|
|
|
Future<ReceiveOnchainResult> receiveOnchain() async {
|
2024-02-14 18:21:37 +00:00
|
|
|
final sdk = await BreezSDK();
|
|
|
|
|
2024-03-06 18:38:11 +00:00
|
|
|
BZG.ReceiveOnchainRequest req = const BZG.ReceiveOnchainRequest();
|
|
|
|
BZG.SwapInfo swapInfo = await sdk.receiveOnchain(req: req);
|
2024-02-14 18:21:37 +00:00
|
|
|
print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}");
|
|
|
|
print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}");
|
2024-03-07 18:17:56 +00:00
|
|
|
|
|
|
|
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));
|
2024-03-26 16:20:05 +00:00
|
|
|
feePercent = (openingFees.feeParams.proportional * 100) / 1000000;
|
|
|
|
fee = openingFees.feeParams.minMsat ~/ 1000;
|
2024-03-07 18:17:56 +00:00
|
|
|
} catch (_) {}
|
2024-03-25 19:13:54 +00:00
|
|
|
|
2024-03-07 06:10:40 +00:00
|
|
|
return ReceiveOnchainResult(
|
|
|
|
bitcoinAddress: swapInfo.bitcoinAddress,
|
|
|
|
minAllowedDeposit: swapInfo.minAllowedDeposit,
|
|
|
|
maxAllowedDeposit: swapInfo.maxAllowedDeposit,
|
2024-03-07 18:17:56 +00:00
|
|
|
feePercent: feePercent,
|
2024-03-07 06:10:40 +00:00
|
|
|
fee: fee,
|
|
|
|
);
|
2024-02-14 18:21:37 +00:00
|
|
|
}
|
2024-02-14 17:34:52 +00:00
|
|
|
|
2024-03-04 17:47:39 +00:00
|
|
|
Future<String> createInvoice({required String amountSats, String? description}) async {
|
2024-02-14 18:21:37 +00:00
|
|
|
final sdk = await BreezSDK();
|
2024-03-06 18:38:11 +00:00
|
|
|
final req = BZG.ReceivePaymentRequest(
|
2024-03-04 17:47:39 +00:00
|
|
|
amountMsat: (double.parse(amountSats) * 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-03-07 06:10:40 +00:00
|
|
|
Future<InvoiceSoftLimitsResult> invoiceSoftLimitsSats() async {
|
2024-03-07 17:26:23 +00:00
|
|
|
double feePercent = 0.4;
|
|
|
|
int minFee = (2500 * 1000) ~/ 1000; // 2500 sats
|
|
|
|
int inboundLiquidity = 1000000000 * 1000 * 10; // 10 BTC
|
2024-03-01 17:08:32 +00:00
|
|
|
int balance = 0;
|
|
|
|
|
2024-03-07 17:26:23 +00:00
|
|
|
final sdk = await BreezSDK();
|
|
|
|
|
2024-03-01 16:49:24 +00:00
|
|
|
try {
|
|
|
|
final nodeState = (await sdk.nodeInfo())!;
|
2024-03-07 17:26:23 +00:00
|
|
|
inboundLiquidity = nodeState.inboundLiquidityMsats ~/ 1000;
|
|
|
|
|
|
|
|
final openingFees = await sdk.openChannelFee(
|
|
|
|
req: BZG.OpenChannelFeeRequest(amountMsat: inboundLiquidity + 1));
|
|
|
|
|
2024-03-26 16:20:05 +00:00
|
|
|
feePercent = (openingFees.feeParams.proportional * 100) / 1000000;
|
|
|
|
minFee = openingFees.feeParams.minMsat ~/ 1000;
|
2024-03-07 17:26:23 +00:00
|
|
|
balance = nodeState.channelsBalanceMsat ~/ 1000;
|
2024-03-01 16:49:24 +00:00
|
|
|
} catch (_) {}
|
2024-03-07 06:10:40 +00:00
|
|
|
return InvoiceSoftLimitsResult(
|
2024-03-07 17:26:23 +00:00
|
|
|
minFee: minFee,
|
|
|
|
inboundLiquidity: inboundLiquidity,
|
|
|
|
feePercent: feePercent,
|
2024-03-07 06:10:40 +00:00
|
|
|
balance: balance,
|
|
|
|
);
|
2024-03-01 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-25 19:13:54 +00:00
|
|
|
|
|
|
|
Future<BZG.HealthCheckStatus> serviceHealthCheck() async {
|
|
|
|
try {
|
|
|
|
final sdk = await BreezSDK();
|
|
|
|
BZG.ServiceHealthCheckResponse response = await sdk.serviceHealthCheck();
|
|
|
|
return response.status;
|
|
|
|
} catch (_) {
|
|
|
|
return BZG.HealthCheckStatus.ServiceDisruption;
|
|
|
|
}
|
|
|
|
}
|
2024-02-14 17:34:52 +00:00
|
|
|
}
|
2024-03-07 06:10:40 +00:00
|
|
|
|
|
|
|
class ReceiveOnchainResult {
|
|
|
|
final String bitcoinAddress;
|
|
|
|
final int minAllowedDeposit;
|
|
|
|
final int maxAllowedDeposit;
|
|
|
|
final int fee;
|
2024-03-07 18:17:56 +00:00
|
|
|
final double feePercent;
|
2024-03-07 06:10:40 +00:00
|
|
|
|
|
|
|
ReceiveOnchainResult({
|
|
|
|
required this.bitcoinAddress,
|
|
|
|
required this.minAllowedDeposit,
|
|
|
|
required this.maxAllowedDeposit,
|
|
|
|
required this.fee,
|
2024-03-07 18:17:56 +00:00
|
|
|
required this.feePercent,
|
2024-03-07 06:10:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class InvoiceSoftLimitsResult {
|
2024-03-07 17:26:23 +00:00
|
|
|
final double feePercent;
|
|
|
|
final int minFee;
|
|
|
|
final int inboundLiquidity;
|
2024-03-07 06:10:40 +00:00
|
|
|
final int balance;
|
|
|
|
|
|
|
|
InvoiceSoftLimitsResult({
|
2024-03-07 17:26:23 +00:00
|
|
|
required this.inboundLiquidity,
|
|
|
|
required this.feePercent,
|
|
|
|
required this.minFee,
|
2024-03-07 06:10:40 +00:00
|
|
|
required this.balance,
|
|
|
|
});
|
2024-03-07 17:26:23 +00:00
|
|
|
}
|