Skip to content

Commit

Permalink
Use net amount calculations with clear names
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Dec 21, 2024
1 parent ff8e42d commit 5dff85b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 18 deletions.
14 changes: 12 additions & 2 deletions lib/_model/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ class Transaction with _$Transaction {
}
}

int getAmount({bool sentAsTotal = false}) {
/// Amount paid to the recipient of the transaction
int getNetAmountToPayee() {
try {
return sentAsTotal ? (sent! - received!) : (sent! - received! - fee!);
return sent! - received!;
} catch (e) {
return 0;
}
}

/// Amount spent by the wallet to effectuate the transaction
int getNetAmountIncludingFees() {
try {
return getNetAmountToPayee() - fee!;
} catch (e) {
return 0;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/_model/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,21 @@ class Wallet with _$Wallet {
}

int totalReceived() {
final txs = transactions.where((tx) => tx.getAmount() > 0).toList();
final txs =
transactions.where((tx) => tx.getNetAmountToPayee() > 0).toList();
int amt = 0;
for (final tx in txs) {
amt += tx.getAmount().abs();
amt += tx.getNetAmountToPayee().abs();
}
return amt;
}

int totalSent() {
final txs = transactions.where((tx) => tx.getAmount() < 0).toList();
final txs =
transactions.where((tx) => tx.getNetAmountIncludingFees() < 0).toList();
int amt = 0;
for (final tx in txs) {
amt += tx.getAmount(sentAsTotal: true).abs();
amt += tx.getNetAmountIncludingFees().abs();
}
return amt;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/home/transactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class HomeTxItem2 extends StatelessWidget {

final amount = context.select(
(CurrencyCubit x) => x.state
.getAmountInUnits(tx.getAmount(sentAsTotal: true), removeText: true),
.getAmountInUnits(tx.getNetAmountIncludingFees(), removeText: true),
);

final units = context.select(
Expand Down
2 changes: 1 addition & 1 deletion lib/send/psbt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PSBTPopUp extends StatelessWidget {

// final outAddresses = context.select((SendCubit cubit) => cubit.state.tx?.outAddresses ?? []);

final txamt = tx.getAmount();
final txamt = tx.getNetAmountToPayee();
final isSats =
context.select((CurrencyCubit cubit) => cubit.state.unitsInSats);
final txfee = context.select((SendCubit cubit) => cubit.state.tx?.fee ?? 0);
Expand Down
2 changes: 1 addition & 1 deletion lib/swap/receive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class _ReceivingSwapPageState extends State<ReceivingSwapPage>
(HomeCubit cubit) => cubit.state.getTxFromSwap(swapTx),
);

if (tx != null) amt = tx.getAmount();
if (tx != null) amt = tx.getNetAmountToPayee();

final isSats = context.select((CurrencyCubit _) => _.state.unitsInSats);
final amtDouble = isSats ? amt : amt / 100000000;
Expand Down
2 changes: 1 addition & 1 deletion lib/swap/swap_confirmation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _SwapConfirmationPageState extends State<SwapConfirmationPage> {
hiveStorage: locator<HiveStorage>(),
bbAPI: locator<BullBitcoinAPI>(),
defaultCurrencyCubit: context.read<CurrencyCubit>(),
)..updateAmountDirect(widget.send.state.tx!.getAmount(sentAsTotal: true));
)..updateAmountDirect(widget.send.state.tx!.getNetAmountIncludingFees());

super.initState();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/transaction/bump_fees.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class _Screen extends StatelessWidget {
final isSwapPending = tx.swapIdisTxid();

final txid = tx.txid;
final amt = tx.getAmount().abs();
final amt = tx.getNetAmountToPayee().abs();
final isReceived = tx.isReceived();
final fees = tx.fee ?? 0;
final amtStr = context.select(
Expand Down
6 changes: 3 additions & 3 deletions lib/transaction/transaction_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class _TxDetails extends StatelessWidget {

final txid = tx.txid;
final unblindedUrl = tx.unblindedUrl;
final amt = tx.getAmount().abs();
final amt = tx.getNetAmountToPayee().abs();
final isReceived = tx.isReceived();
final fees = tx.fee ?? 0;
final amtStr = context.select(
Expand Down Expand Up @@ -699,7 +699,7 @@ class _OnchainSwapDetails extends StatelessWidget {
final fromStatusStr = fromStatus ? 'Pending' : 'Confirmed';
final fromAmtStr = context.select(
(CurrencyCubit cubit) => cubit.state
.getAmountInUnits(tx.getAmount(sentAsTotal: true), removeText: true),
.getAmountInUnits(tx.getNetAmountIncludingFees(), removeText: true),
);
final fromUnits = context.select(
(CurrencyCubit cubit) => cubit.state.getUnitString(isLiquid: isLiq),
Expand Down Expand Up @@ -728,7 +728,7 @@ class _OnchainSwapDetails extends StatelessWidget {
if (receiveTx != null) {
toAmtStr = context.select(
(CurrencyCubit cubit) => cubit.state.getAmountInUnits(
receiveTx!.getAmount(sentAsTotal: true),
receiveTx!.getNetAmountIncludingFees(),
removeText: true,
),
);
Expand Down
6 changes: 2 additions & 4 deletions lib/wallet/wallet_txs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ class HomeTxItem extends StatelessWidget {

final amount = context.select(
(CurrencyCubit x) => x.state.getAmountInUnits(
tx.getAmount(
sentAsTotal: true,
),
tx.getNetAmountIncludingFees(),
isLiquid: tx.isLiquid,
),
);
Expand Down Expand Up @@ -175,7 +173,7 @@ class HomeTxItem extends StatelessWidget {
// color: Colors.red,
transformAlignment: Alignment.center,
transform: Matrix4.identity()
..rotateZ(tx.getAmount() > 0 ? 0 : 3.16),
..rotateZ(tx.getNetAmountToPayee() > 0 ? 0 : 3.16),
child: Image.asset(img),
),
),
Expand Down

0 comments on commit 5dff85b

Please sign in to comment.