From b62c0b7bc20ea4c321958ba0e1455ff23b394e34 Mon Sep 17 00:00:00 2001 From: Mircea Hasegan Date: Fri, 13 Dec 2024 11:11:07 +0100 Subject: [PATCH] fix(core): include withdrawals in txSummary spent coins Withdrawals are a type of "own input", which must be accounted for when computing the wallet spent amount --- packages/core/src/util/transactionSummaryInspector.ts | 9 +++++++-- .../core/test/util/transactionSummaryInspector.test.ts | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/core/src/util/transactionSummaryInspector.ts b/packages/core/src/util/transactionSummaryInspector.ts index e81e4934200..3151b8949c3 100644 --- a/packages/core/src/util/transactionSummaryInspector.ts +++ b/packages/core/src/util/transactionSummaryInspector.ts @@ -201,14 +201,19 @@ export const transactionSummaryInspector: TransactionSummaryInspector = const collateral = await getCollateral(tx, inputResolver, addresses); + const withdrawals = implicit.withdrawals || 0n; const totalOutputValue = await totalAddressOutputsValueInspector(addresses)(tx); const totalInputValue = await totalAddressInputsValueInspector(addresses, inputResolver)(tx); - const implicitCoin = (implicit.withdrawals || 0n) + (implicit.reclaimDeposit || 0n) - (implicit.deposit || 0n); + const implicitCoin = withdrawals + (implicit.reclaimDeposit || 0n) - (implicit.deposit || 0n); const implicitAssets = await getImplicitAssets(tx); const diff = { assets: subtractTokenMaps([totalOutputValue.assets, totalInputValue.assets]), - coins: totalOutputValue.coins - totalInputValue.coins + // Withdrawals are a type of "own input", which must be accounted for when computing the wallet spent amount. + // `coins` represents the actual spent coins from the wallets perspective, using the formula `ownOutputs - ownInputs`. + // deposit is like a foreign output, and reclaimDeposit is like a foreignInput, + // so they do not need to be included in the computation. + coins: totalOutputValue.coins - (totalInputValue.coins + withdrawals) }; return { diff --git a/packages/core/test/util/transactionSummaryInspector.test.ts b/packages/core/test/util/transactionSummaryInspector.test.ts index fd5a21e82d6..2c50945932c 100644 --- a/packages/core/test/util/transactionSummaryInspector.test.ts +++ b/packages/core/test/util/transactionSummaryInspector.test.ts @@ -271,8 +271,15 @@ describe('Transaction Summary Inspector', () => { ]), coins: 6_000_000n } + }, + { + address: addresses[0], + value: { + coins: 3_000_000n + } } - ] + ], + withdrawals: [{ quantity: 3_000_000n, stakeAddress: rewardAccounts[0] }] }); const histTx: Cardano.HydratedTx[] = [