Skip to content

Commit

Permalink
fix(core): include withdrawals in txSummary spent coins
Browse files Browse the repository at this point in the history
Withdrawals are a type of "own input", which must
be accounted for when computing the wallet
spent amount
  • Loading branch information
mirceahasegan committed Dec 13, 2024
1 parent 3da1f4a commit b62c0b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/util/transactionSummaryInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion packages/core/test/util/transactionSummaryInspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down

0 comments on commit b62c0b7

Please sign in to comment.