Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): include withdrawals in txSummary spent coins #1548

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice description. My first though here is that it would be better added to a type definition, so developers will see it when implementing, but it looks like we're just relying on inference here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I'll create an explicit type and add jsdocs for it 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I amended the commit, the only addition being the jsdoc: 6166bfc#diff-a9bcb5ce8c787fb688c6ba5c62595afae666260d468bd5b2ee1bd9a22e868b51R40

// `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
Loading