Skip to content

Commit

Permalink
Add additional transaction balance change details
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlVS committed Dec 3, 2024
1 parent feb1587 commit 09a1820
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:decimal/decimal.dart';
import 'package:komodo_defi_types/komodo_defi_types.dart';

class TransactionInfo {
Expand Down Expand Up @@ -71,19 +70,4 @@ class TransactionInfo {
if (transactionFee != null) 'transaction_fee': transactionFee,
if (memo != null) 'memo': memo,
};

Transaction asTransaction(AssetId assetId) => Transaction(
id: txHash,
internalId: internalId,
assetId: assetId,
amount: Decimal.parse(myBalanceChange),
timestamp: DateTime.fromMillisecondsSinceEpoch(timestamp * 1000),
confirmations: confirmations,
blockHeight: blockHeight,
from: from,
to: to,
fee: feeDetails,
txHash: txHash,
memo: memo,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ class _WithdrawalScreenState extends State<WithdrawalScreen> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Amount: ${_preview!.totalAmount} ${widget.asset.id.id}'),
Text('To: ${_preview!.to.first}'),
Text('Amount: ${_preview!.balanceChanges.netChange} '
'${widget.asset.id.id}'),
Text('To: ${_preview!.to.join(', ')}'),
_buildFeeDetails(_preview!.fee),
if (_preview!.kmdRewards != null) ...[
const SizedBox(height: 8),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class WithdrawalManager {
.map(_mapStatusToProgress)
.forEach(controller.add);

// Send the raw transaction to the network if
// Send the raw transaction to the network if successful
if (lastProgress?.status == 'Ok' &&
lastProgress?.details is WithdrawResult) {
final details = lastProgress!.details as WithdrawResult;
Expand All @@ -51,12 +51,12 @@ class WithdrawalManager {
message: 'Withdrawal complete',
withdrawalResult: WithdrawalResult(
txHash: response.txHash,
amount: Decimal.parse(details.totalAmount),
balanceChanges: details.balanceChanges,
coin: parameters.asset,
toAddress: parameters.toAddress,
fee: details.fee,
kmdRewardsEligible: details.kmdRewards != null &&
(details.kmdRewards?.amount ?? '0') != '0',
Decimal.parse(details.kmdRewards!.amount) > Decimal.zero,
),
);
}
Expand Down Expand Up @@ -97,11 +97,12 @@ class WithdrawalManager {
message: 'Withdrawal generated. Sending transaction...',
withdrawalResult: WithdrawalResult(
txHash: result.txHash,
amount: Decimal.parse(result.totalAmount),
balanceChanges: result.balanceChanges,
coin: result.coin,
toAddress: result.to.first,
fee: result.fee,
kmdRewardsEligible: result.kmdRewards != null,
kmdRewardsEligible: result.kmdRewards != null &&
Decimal.parse(result.kmdRewards!.amount) > Decimal.zero,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:decimal/decimal.dart';
import 'package:equatable/equatable.dart';
import 'package:komodo_defi_types/komodo_defi_types.dart';

/// Represents the effect a transaction has on wallet balances
class BalanceChanges extends Equatable {
const BalanceChanges({
required this.netChange,
required this.receivedByMe,
required this.spentByMe,
required this.totalAmount,
});

factory BalanceChanges.fromJson(JsonMap json) => BalanceChanges(
netChange: Decimal.parse(json.value<String>('my_balance_change')),
receivedByMe: Decimal.parse(json.value<String>('received_by_me')),
spentByMe: Decimal.parse(json.value<String>('spent_by_me')),
totalAmount: Decimal.parse(json.value<String>('total_amount')),
);

/// The net change in the wallet's balance (positive for incoming,
/// negative for outgoing)
final Decimal netChange;

/// Amount received by my addresses in this transaction
final Decimal receivedByMe;

/// Amount spent from my addresses in this transaction
final Decimal spentByMe;

/// The total amount of coins transferred
final Decimal totalAmount;

bool get isIncoming => netChange > Decimal.zero;

@override
List<Object?> get props => [netChange, receivedByMe, spentByMe, totalAmount];

Map<String, dynamic> toJson() => {
'my_balance_change': netChange.toString(),
'received_by_me': receivedByMe.toString(),
'spent_by_me': spentByMe.toString(),
'total_amount': totalAmount.toString(),
};
}
59 changes: 45 additions & 14 deletions packages/komodo_defi_types/lib/src/transactions/transaction.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:decimal/decimal.dart';
import 'package:equatable/equatable.dart';
import 'package:komodo_defi_rpc_methods/komodo_defi_rpc_methods.dart';
import 'package:komodo_defi_types/komodo_defi_types.dart';

/// Domain model for a transaction, decoupled from the API representation
Expand All @@ -8,15 +9,15 @@ class Transaction extends Equatable {
required this.id,
required this.internalId,
required this.assetId,
required this.amount,
required this.balanceChanges,
required this.timestamp,
required this.confirmations,
required this.blockHeight,
required this.from,
required this.to,
required this.txHash,
required this.fee,
required this.memo,
this.fee,
this.memo,
});

factory Transaction.fromJson(Map<String, dynamic> json) => Transaction(
Expand All @@ -26,7 +27,7 @@ class Transaction extends Equatable {
json.value<JsonMap>('asset_id'),
knownIds: null,
),
amount: Decimal.parse(json.value<String>('my_balance_change')),
balanceChanges: BalanceChanges.fromJson(json),
timestamp: DateTime.parse(json.value<String>('timestamp')),
confirmations: json.value<int>('confirmations'),
blockHeight: json.value<int>('block_height'),
Expand All @@ -42,27 +43,28 @@ class Transaction extends Equatable {
final String id;
final String internalId;
final AssetId assetId;
final Decimal amount;
final BalanceChanges balanceChanges;
final DateTime timestamp;
final int confirmations;
final int blockHeight;
final List<String> from;
final List<String> to;

// Null for cases such as SIA coin. TODO: Consider if there is a better way
// represent this property usin
final String? txHash;
final FeeInfo? fee;
final String? memo;

bool get isIncoming => amount > Decimal.zero;
/// Convenience getter for the net balance change
Decimal get amount => balanceChanges.netChange;

/// Convenience getter for whether transaction is incoming
bool get isIncoming => balanceChanges.isIncoming;

@override
List<Object?> get props => [
id,
internalId,
assetId,
amount,
balanceChanges,
timestamp,
confirmations,
blockHeight,
Expand All @@ -77,14 +79,43 @@ class Transaction extends Equatable {
'id': id,
'internal_id': internalId,
'asset_id': assetId.toJson(),
'my_balance_change': amount.toString(),
...balanceChanges.toJson(),
'timestamp': timestamp.toIso8601String(),
'confirmations': confirmations,
'block_height': blockHeight,
'from': from,
'to': to,
'tx_hash': txHash,
'memo': memo,
'fee': fee.toString(),
if (txHash != null) 'tx_hash': txHash,
if (fee != null) 'fee': fee!.toJson(),
if (memo != null) 'memo': memo,
};
}

extension TransactionInfoExtension on TransactionInfo {
Transaction asTransaction(AssetId assetId) => Transaction(
id: txHash,
internalId: internalId,
assetId: assetId,
balanceChanges: BalanceChanges(
netChange: Decimal.parse(myBalanceChange),
receivedByMe: receivedByMe != null
? Decimal.parse(receivedByMe!)
: Decimal.zero,
spentByMe:
spentByMe != null ? Decimal.parse(spentByMe!) : Decimal.zero,
totalAmount: Decimal.parse(
// For historical transactions that don't have spent/received,
// use the absolute value of the balance change
receivedByMe ?? spentByMe ?? myBalanceChange.replaceAll('-', ''),
),
),
timestamp: DateTime.fromMillisecondsSinceEpoch(timestamp * 1000),
confirmations: confirmations,
blockHeight: blockHeight,
from: from,
to: to,
txHash: txHash,
fee: feeDetails,
memo: memo,
);
}
Loading

0 comments on commit 09a1820

Please sign in to comment.