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

Add missing unit information to amounts #898

Merged
merged 1 commit into from
Jul 16, 2024
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
52 changes: 26 additions & 26 deletions lib/bloc/account/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:rxdart/rxdart.dart';

const maxPaymentAmount = 4294967;
const maxPaymentAmountSat = 4294967;
const nodeSyncInterval = 60;

final _log = Logger("AccountBloc");
Expand Down Expand Up @@ -160,7 +160,7 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
Future<sdk.LnUrlWithdrawResult> lnurlWithdraw({
required sdk.LnUrlWithdrawRequest req,
}) async {
_log.info("lnurlWithdraw amount: req: $req");
_log.info("lnurlWithdraw req: $req");
try {
return await _breezSDK.lnurlWithdraw(req: req);
} catch (e) {
Expand All @@ -170,7 +170,7 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
}

Future<sdk.LnUrlPayResult> lnurlPay({required req}) async {
_log.info("lnurlPay amount: req: $req");
_log.info("lnurlPay req: $req");
try {
return await _breezSDK.lnurlPay(req: req);
} catch (e) {
Expand All @@ -182,7 +182,7 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
Future<sdk.LnUrlCallbackStatus> lnurlAuth({
required sdk.LnUrlAuthRequestData reqData,
}) async {
_log.info("lnurlAuth amount: reqData: $reqData");
_log.info("lnurlAuth reqData: $reqData");
try {
return await _breezSDK.lnurlAuth(reqData: reqData);
} catch (e) {
Expand Down Expand Up @@ -238,38 +238,38 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
// validatePayment is used to validate that outgoing/incoming payments meet the liquidity
// constraints.
void validatePayment(
int amount,
int amountSat,
bool outgoing,
bool channelCreationPossible, {
int? channelMinimumFee,
int? channelMinimumFeeSat,
}) {
_log.info("validatePayment: $amount, $outgoing, $channelMinimumFee");
_log.info("validatePayment: $amountSat, $outgoing, $channelMinimumFeeSat");
var accState = state;
if (amount > accState.maxPaymentAmount) {
_log.info("Amount $amount is bigger than maxPaymentAmount ${accState.maxPaymentAmount}");
throw PaymentExceededLimitError(accState.maxPaymentAmount);
if (amountSat > accState.maxPaymentAmountSat) {
_log.info("Amount $amountSat is bigger than maxPaymentAmount ${accState.maxPaymentAmountSat}");
throw PaymentExceededLimitError(accState.maxPaymentAmountSat);
}

if (!outgoing) {
if (!channelCreationPossible && accState.maxInboundLiquidity == 0) {
throw NoChannelCreationZeroLiqudityError();
} else if (!channelCreationPossible && accState.maxInboundLiquidity < amount) {
throw PaymentExcededLiqudityChannelCreationNotPossibleError(accState.maxInboundLiquidity);
} else if (channelMinimumFee != null &&
(amount > accState.maxInboundLiquidity && amount <= channelMinimumFee)) {
throw PaymentBelowSetupFeesError(channelMinimumFee);
} else if (channelMinimumFee == null && amount > accState.maxInboundLiquidity) {
throw PaymentExceedLiquidityError(accState.maxInboundLiquidity);
} else if (amount > accState.maxAllowedToReceive) {
throw PaymentExceededLimitError(accState.maxAllowedToReceive);
if (!channelCreationPossible && accState.maxInboundLiquiditySat == 0) {
throw NoChannelCreationZeroLiquidityError();
} else if (!channelCreationPossible && accState.maxInboundLiquiditySat < amountSat) {
throw PaymentExceededLiquidityChannelCreationNotPossibleError(accState.maxInboundLiquiditySat);
} else if (channelMinimumFeeSat != null &&
(amountSat > accState.maxInboundLiquiditySat && amountSat <= channelMinimumFeeSat)) {
throw PaymentBelowSetupFeesError(channelMinimumFeeSat);
} else if (channelMinimumFeeSat == null && amountSat > accState.maxInboundLiquiditySat) {
throw PaymentExceededLiquidityError(accState.maxInboundLiquiditySat);
} else if (amountSat > accState.maxAllowedToReceiveSat) {
throw PaymentExceededLimitError(accState.maxAllowedToReceiveSat);
}
}

if (outgoing && amount > accState.maxAllowedToPay) {
_log.info("Outgoing but amount $amount is bigger than ${accState.maxAllowedToPay}");
if (accState.reserveAmount > 0) {
_log.info("Reserve amount ${accState.reserveAmount}");
throw PaymentBelowReserveError(accState.reserveAmount);
if (outgoing && amountSat > accState.maxAllowedToPaySat) {
_log.info("Outgoing but amount $amountSat is bigger than ${accState.maxAllowedToPaySat}");
if (accState.reserveAmountSat > 0) {
_log.info("Reserve amount ${accState.reserveAmountSat}");
throw PaymentBelowReserveError(accState.reserveAmountSat);
}
throw const InsufficientLocalBalanceError();
}
Expand Down
109 changes: 51 additions & 58 deletions lib/bloc/account/account_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ class AccountState {
final String? id;
final bool initial;
final int blockheight;
final int balance;
final int walletBalance;
final int maxAllowedToPay;
final int maxAllowedToReceive;
final int maxPaymentAmount;
final int maxChanReserve;
final int balanceSat;
final int walletBalanceSat;
final int maxAllowedToPaySat;
final int maxAllowedToReceiveSat;
final int maxPaymentAmountSat;
final int maxChanReserveSat;
final List<String> connectedPeers;
final int maxInboundLiquidity;
final int onChainFeeRate;
final int maxInboundLiquiditySat;
final List<PaymentMinutiae> payments;
final PaymentFilters paymentFilters;
final ConnectionStatus? connectionStatus;
Expand All @@ -31,15 +30,14 @@ class AccountState {
required this.id,
required this.initial,
required this.blockheight,
required this.balance,
required this.walletBalance,
required this.maxAllowedToPay,
required this.maxAllowedToReceive,
required this.maxPaymentAmount,
required this.maxChanReserve,
required this.balanceSat,
required this.walletBalanceSat,
required this.maxAllowedToPaySat,
required this.maxAllowedToReceiveSat,
required this.maxPaymentAmountSat,
required this.maxChanReserveSat,
required this.connectedPeers,
required this.maxInboundLiquidity,
required this.onChainFeeRate,
required this.maxInboundLiquiditySat,
required this.payments,
required this.paymentFilters,
required this.connectionStatus,
Expand All @@ -51,15 +49,14 @@ class AccountState {
id: null,
initial: true,
blockheight: 0,
maxAllowedToPay: 0,
maxAllowedToReceive: 0,
maxPaymentAmount: 0,
maxChanReserve: 0,
maxAllowedToPaySat: 0,
maxAllowedToReceiveSat: 0,
maxPaymentAmountSat: 0,
maxChanReserveSat: 0,
connectedPeers: List.empty(),
maxInboundLiquidity: 0,
onChainFeeRate: 0,
balance: 0,
walletBalance: 0,
maxInboundLiquiditySat: 0,
balanceSat: 0,
walletBalanceSat: 0,
payments: [],
paymentFilters: PaymentFilters.initial(),
connectionStatus: null,
Expand All @@ -70,15 +67,14 @@ class AccountState {
String? id,
bool? initial,
int? blockheight,
int? balance,
int? walletBalance,
int? maxAllowedToPay,
int? maxAllowedToReceive,
int? maxPaymentAmount,
int? maxChanReserve,
int? balanceSat,
int? walletBalanceSat,
int? maxAllowedToPaySat,
int? maxAllowedToReceiveSat,
int? maxPaymentAmountSat,
int? maxChanReserveSat,
List<String>? connectedPeers,
int? maxInboundLiquidity,
int? onChainFeeRate,
int? maxInboundLiquiditySat,
List<PaymentMinutiae>? payments,
PaymentFilters? paymentFilters,
ConnectionStatus? connectionStatus,
Expand All @@ -87,41 +83,39 @@ class AccountState {
return AccountState(
id: id ?? this.id,
initial: initial ?? this.initial,
balance: balance ?? this.balance,
walletBalance: walletBalance ?? this.walletBalance,
maxAllowedToPay: maxAllowedToPay ?? this.maxAllowedToPay,
maxAllowedToReceive: maxAllowedToReceive ?? this.maxAllowedToReceive,
maxPaymentAmount: maxPaymentAmount ?? this.maxPaymentAmount,
balanceSat: balanceSat ?? this.balanceSat,
walletBalanceSat: walletBalanceSat ?? this.walletBalanceSat,
maxAllowedToPaySat: maxAllowedToPaySat ?? this.maxAllowedToPaySat,
maxAllowedToReceiveSat: maxAllowedToReceiveSat ?? this.maxAllowedToReceiveSat,
maxPaymentAmountSat: maxPaymentAmountSat ?? this.maxPaymentAmountSat,
blockheight: blockheight ?? this.blockheight,
maxChanReserve: maxChanReserve ?? this.maxChanReserve,
maxChanReserveSat: maxChanReserveSat ?? this.maxChanReserveSat,
connectedPeers: connectedPeers ?? this.connectedPeers,
maxInboundLiquidity: maxInboundLiquidity ?? this.maxInboundLiquidity,
onChainFeeRate: onChainFeeRate ?? this.onChainFeeRate,
maxInboundLiquiditySat: maxInboundLiquiditySat ?? this.maxInboundLiquiditySat,
payments: payments ?? this.payments,
paymentFilters: paymentFilters ?? this.paymentFilters,
connectionStatus: connectionStatus ?? this.connectionStatus,
verificationStatus: verificationStatus ?? this.verificationStatus,
);
}

int get reserveAmount => balance - maxAllowedToPay;
int get reserveAmountSat => balanceSat - maxAllowedToPaySat;

bool get isFeesApplicable => maxAllowedToReceive > maxInboundLiquidity;
bool get isFeesApplicable => maxAllowedToReceiveSat > maxInboundLiquiditySat;

// TODO: Add payments toJson
Map<String, dynamic>? toJson() {
return {
"id": id,
"initial": initial,
"blockheight": blockheight,
"balance": balance,
"walletBalance": walletBalance,
"maxAllowedToPay": maxAllowedToPay,
"maxAllowedToReceive": maxAllowedToReceive,
"maxPaymentAmount": maxPaymentAmount,
"maxChanReserve": maxChanReserve,
"maxInboundLiquidity": maxInboundLiquidity,
"onChainFeeRate": onChainFeeRate,
"balanceSat": balanceSat,
"walletBalanceSat": walletBalanceSat,
"maxAllowedToPaySat": maxAllowedToPaySat,
"maxAllowedToReceiveSat": maxAllowedToReceiveSat,
"maxPaymentAmountSat": maxPaymentAmountSat,
"maxChanReserveSat": maxChanReserveSat,
"maxInboundLiquiditySat": maxInboundLiquiditySat,
"paymentFilters": paymentFilters.toJson(),
"connectionStatus": connectionStatus?.index,
"verificationStatus": verificationStatus?.index,
Expand All @@ -134,15 +128,14 @@ class AccountState {
id: json["id"],
initial: json["initial"],
blockheight: json["blockheight"],
balance: json["balance"],
walletBalance: json["walletBalance"],
maxAllowedToPay: json["maxAllowedToPay"],
maxAllowedToReceive: json["maxAllowedToReceive"],
maxPaymentAmount: json["maxPaymentAmount"],
maxChanReserve: json["maxChanReserve"],
balanceSat: json["balanceSat"],
walletBalanceSat: json["walletBalanceSat"],
maxAllowedToPaySat: json["maxAllowedToPaySat"],
maxAllowedToReceiveSat: json["maxAllowedToReceiveSat"],
maxPaymentAmountSat: json["maxPaymentAmountSat"],
maxChanReserveSat: json["maxChanReserveSat"],
connectedPeers: <String>[],
maxInboundLiquidity: json["maxInboundLiquidity"] ?? 0,
onChainFeeRate: (json["onChainFeeRate"]),
maxInboundLiquiditySat: json["maxInboundLiquiditySat"] ?? 0,
payments: [],
paymentFilters: PaymentFilters.fromJson(json["paymentFilters"]),
connectionStatus: json["connectionStatus"] != null
Expand Down
15 changes: 7 additions & 8 deletions lib/bloc/account/account_state_assembler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ AccountState? assembleAccountState(
id: nodeState.id,
initial: false,
blockheight: nodeState.blockHeight,
balance: nodeState.channelsBalanceMsat.toInt() ~/ 1000,
walletBalance: nodeState.onchainBalanceMsat ~/ 1000,
maxAllowedToPay: nodeState.maxPayableMsat ~/ 1000,
maxAllowedToReceive: nodeState.maxReceivableMsat ~/ 1000,
maxPaymentAmount: maxPaymentAmount,
maxChanReserve: (nodeState.channelsBalanceMsat.toInt() - nodeState.maxPayableMsat.toInt()) ~/ 1000,
balanceSat: nodeState.channelsBalanceMsat.toInt() ~/ 1000,
walletBalanceSat: nodeState.onchainBalanceMsat ~/ 1000,
maxAllowedToPaySat: nodeState.maxPayableMsat ~/ 1000,
maxAllowedToReceiveSat: nodeState.maxReceivableMsat ~/ 1000,
maxPaymentAmountSat: maxPaymentAmountSat,
maxChanReserveSat: (nodeState.channelsBalanceMsat.toInt() - nodeState.maxPayableMsat.toInt()) ~/ 1000,
connectedPeers: nodeState.connectedPeers,
onChainFeeRate: 0,
maxInboundLiquidity: nodeState.inboundLiquidityMsats ~/ 1000,
maxInboundLiquiditySat: nodeState.inboundLiquidityMsats ~/ 1000,
payments: payments.map((e) => PaymentMinutiae.fromPayment(e, texts)).toList(),
paymentFilters: paymentFilters,
connectionStatus: ConnectionStatus.CONNECTED,
Expand Down
34 changes: 11 additions & 23 deletions lib/bloc/account/payment_error.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
class PaymentExceededLimitError implements Exception {
final int limitSat;

const PaymentExceededLimitError(
this.limitSat,
);
const PaymentExceededLimitError(this.limitSat);
}

class PaymentBelowLimitError implements Exception {
final int limitSat;

const PaymentBelowLimitError(
this.limitSat,
);
const PaymentBelowLimitError(this.limitSat);
}

class PaymentBelowReserveError implements Exception {
final int reserveAmount;
final int reserveAmountSat;

const PaymentBelowReserveError(
this.reserveAmount,
);
const PaymentBelowReserveError(this.reserveAmountSat);
}

class InsufficientLocalBalanceError implements Exception {
const InsufficientLocalBalanceError();
}

class PaymentBelowSetupFeesError implements Exception {
final int setupFees;
final int setupFeesSat;

const PaymentBelowSetupFeesError(
this.setupFees,
);
const PaymentBelowSetupFeesError(this.setupFeesSat);
}

class PaymentExceedLiquidityError implements Exception {
class PaymentExceededLiquidityError implements Exception {
final int limitSat;

const PaymentExceedLiquidityError(
this.limitSat,
);
const PaymentExceededLiquidityError(this.limitSat);
}

class PaymentExcededLiqudityChannelCreationNotPossibleError implements Exception {
class PaymentExceededLiquidityChannelCreationNotPossibleError implements Exception {
final int limitSat;

const PaymentExcededLiqudityChannelCreationNotPossibleError(
this.limitSat,
);
const PaymentExceededLiquidityChannelCreationNotPossibleError(this.limitSat);
}

class NoChannelCreationZeroLiqudityError implements Exception {}
class NoChannelCreationZeroLiquidityError implements Exception {}
13 changes: 7 additions & 6 deletions lib/bloc/currency/currency_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class CurrencyState {
final Map<String, Rate> exchangeRates;
final List<FiatCurrency> fiatCurrenciesData;

CurrencyState(
{this.fiatCurrenciesData = const [],
this.exchangeRates = const {},
this.preferredCurrencies = const ["USD", "EUR", "GBP", "JPY"],
this.fiatId = "USD",
this.bitcoinTicker = "SAT"});
CurrencyState({
this.fiatCurrenciesData = const [],
this.exchangeRates = const {},
this.preferredCurrencies = const ["USD", "EUR", "GBP", "JPY"],
this.fiatId = "USD",
this.bitcoinTicker = "SAT",
});

CurrencyState.initial() : this();

Expand Down
4 changes: 2 additions & 2 deletions lib/bloc/refund/refund_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ class RefundBloc extends Cubit<RefundState> {
toAddress: toAddress,
satPerVbyte: recommendedFee,
);
final fee = await _prepareRefund(req);
final refundTxFeeSat = await _prepareRefund(req);

return RefundFeeOption(
processingSpeed: ProcessingSpeed.values.elementAt(index),
txFeeSat: fee,
txFeeSat: refundTxFeeSat,
satPerVbyte: recommendedFee,
);
}),
Expand Down
Loading
Loading