Skip to content

Commit

Permalink
Merge pull request #1548 from input-output-hk/fix/lw-11936-include-wi…
Browse files Browse the repository at this point in the history
…thdrawals-in-tx-summary

fix(core): include withdrawals in txSummary spent coins
  • Loading branch information
mirceahasegan authored Dec 20, 2024
2 parents fee2064 + d70e5cd commit ec6900f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 12 additions & 2 deletions packages/core/src/util/transactionSummaryInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ interface TransactionSummaryInspectorArgs {

export type TransactionSummaryInspection = {
assets: Map<Cardano.AssetId, AssetInfoWithAmount>;
/**
* Spent amount from the wallet's perspective, computed as `own outputs - (own inputs + withdrawals)`.
* Withdrawals are a type of "own input", and are accounted for when computing the wallet spent amount.
* Positive when the wallet is receiving funds, negative when the wallet is sending funds.
*/
coins: Cardano.Lovelace;
collateral: Cardano.Lovelace;
deposit: Cardano.Lovelace;
Expand Down Expand Up @@ -201,14 +206,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 {
Expand Down
17 changes: 15 additions & 2 deletions packages/core/test/util/transactionSummaryInspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,21 @@ describe('Transaction Summary Inspector', () => {
]),
coins: 6_000_000n
}
},
{
address: addresses[0],
value: {
coins: 2_000_000n
}
},
{
address: externalAddress1,
value: {
coins: 1_000_000n
}
}
]
],
withdrawals: [{ quantity: 3_000_000n, stakeAddress: rewardAccounts[0] }]
});

const histTx: Cardano.HydratedTx[] = [
Expand Down Expand Up @@ -335,7 +348,7 @@ describe('Transaction Summary Inspector', () => {
[assetInfos[AssetInfoIdx.PXL], -6n],
[assetInfos[AssetInfoIdx.Unit], -7n]
]),
coins: -10_000_000n,
coins: -11_000_000n,
collateral: 0n,
deposit: 0n,
fee,
Expand Down

0 comments on commit ec6900f

Please sign in to comment.