Skip to content

Commit 6eb7c78

Browse files
Merge pull request #1088 from input-output-hk/fix/lw-9749-transactionSummaryInspector-detects-incorrectly-fees-as-unaccounted-balance
fix(core): transactionSummaryInspector now correctly accounts for transaction fees
2 parents 0ead9c0 + c55d71c commit 6eb7c78

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

packages/core/src/util/transactionSummaryInspector.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ const getUnaccountedFunds = async (
103103
tx: Cardano.Tx,
104104
resolvedInputs: ResolutionResult,
105105
implicitCoin: Cardano.Lovelace,
106+
fee: Cardano.Lovelace,
106107
implicitAssets: Cardano.TokenMap = new Map()
107108
): Promise<Cardano.Value> => {
108109
const totalInputs = totalInputsValue(resolvedInputs);
109110
const totalOutputs = totalOutputsValue(tx.body.outputs);
110111

111112
totalInputs.assets = coalesceTokenMaps([totalInputs.assets, implicitAssets]);
112113
totalInputs.coins += implicitCoin;
114+
totalOutputs.coins += fee;
113115

114116
return subtractValueQuantities([totalOutputs, totalInputs]);
115117
};
@@ -177,7 +179,7 @@ export const transactionSummaryInspector: TransactionSummaryInspector =
177179
returnedDeposit: implicit.reclaimDeposit || 0n,
178180
unresolved: {
179181
inputs: resolvedInputs.unresolvedInputs,
180-
value: await getUnaccountedFunds(tx, resolvedInputs, implicitCoin, implicitAssets)
182+
value: await getUnaccountedFunds(tx, resolvedInputs, implicitCoin, fee, implicitAssets)
181183
}
182184
};
183185
};

packages/core/test/util/transactionSummaryInspector.test.ts

+29-27
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ const auxiliaryData = {
118118
scripts: [mockScript2]
119119
};
120120

121+
const fee = 170_000n;
122+
121123
const buildMockTx = (
122124
args: {
123125
inputs?: Cardano.HydratedTxIn[];
@@ -143,7 +145,7 @@ const buildMockTx = (
143145
certificates: args.certificates,
144146
collateralReturn: args.collateralReturn ?? undefined,
145147
collaterals: args.collaterals ?? undefined,
146-
fee: 170_000n,
148+
fee,
147149
inputs: args.inputs ?? [
148150
{
149151
address: addresses[0],
@@ -255,7 +257,7 @@ describe('Transaction Summary Inspector', () => {
255257
address: externalAddress1,
256258
value: {
257259
assets: new Map([[AssetIds.TSLA, 5n]]),
258-
coins: 5_000_000n
260+
coins: 5_000_000n - fee // In this TX the fee is coming out of one of the external addresses.
259261
}
260262
},
261263
{
@@ -346,7 +348,7 @@ describe('Transaction Summary Inspector', () => {
346348
coins: -10_000_000n,
347349
collateral: 0n,
348350
deposit: 0n,
349-
fee: 170_000n,
351+
fee,
350352
returnedDeposit: 0n,
351353
unresolved: {
352354
inputs: [],
@@ -382,7 +384,7 @@ describe('Transaction Summary Inspector', () => {
382384
address: addresses[0],
383385
value: {
384386
assets: new Map([[AssetIds.TSLA, 5n]]),
385-
coins: 5_000_000n
387+
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
386388
}
387389
}
388390
]
@@ -433,10 +435,10 @@ describe('Transaction Summary Inspector', () => {
433435
// Assert
434436
expect(summary).toEqual({
435437
assets: new Map(),
436-
coins: 0n,
438+
coins: -fee,
437439
collateral: 10_000_000n,
438440
deposit: 0n,
439-
fee: 170_000n,
441+
fee,
440442
returnedDeposit: 0n,
441443
unresolved: {
442444
inputs: [],
@@ -473,7 +475,7 @@ describe('Transaction Summary Inspector', () => {
473475
address: addresses[0],
474476
value: {
475477
assets: new Map([[AssetIds.TSLA, 5n]]),
476-
coins: 27_000_000n
478+
coins: 27_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
477479
}
478480
}
479481
],
@@ -525,10 +527,10 @@ describe('Transaction Summary Inspector', () => {
525527
// Assert
526528
expect(summary).toEqual({
527529
assets: new Map(),
528-
coins: 0n,
530+
coins: -fee,
529531
collateral: 25_000_000n,
530532
deposit: 0n,
531-
fee: 170_000n,
533+
fee,
532534
returnedDeposit: 0n,
533535
unresolved: {
534536
inputs: [],
@@ -564,7 +566,7 @@ describe('Transaction Summary Inspector', () => {
564566
address: addresses[0],
565567
value: {
566568
assets: new Map([[AssetIds.TSLA, 5n]]),
567-
coins: 5_000_000n
569+
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
568570
}
569571
}
570572
]
@@ -609,10 +611,10 @@ describe('Transaction Summary Inspector', () => {
609611
// Assert
610612
expect(summary).toEqual({
611613
assets: new Map(),
612-
coins: 0n,
614+
coins: -fee,
613615
collateral: 5_000_000n,
614616
deposit: 0n,
615-
fee: 170_000n,
617+
fee,
616618
returnedDeposit: 0n,
617619
unresolved: {
618620
inputs: [],
@@ -637,7 +639,7 @@ describe('Transaction Summary Inspector', () => {
637639
address: addresses[0],
638640
value: {
639641
assets: new Map([[AssetIds.TSLA, 5n]]),
640-
coins: 3_000_000n
642+
coins: 3_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
641643
}
642644
}
643645
]
@@ -676,10 +678,10 @@ describe('Transaction Summary Inspector', () => {
676678
// Assert
677679
expect(summary).toEqual({
678680
assets: new Map(),
679-
coins: -2_000_000n,
681+
coins: -2_000_000n - fee,
680682
collateral: 0n,
681683
deposit: 2_000_000n,
682-
fee: 170_000n,
684+
fee,
683685
returnedDeposit: 0n,
684686
unresolved: {
685687
inputs: [],
@@ -704,7 +706,7 @@ describe('Transaction Summary Inspector', () => {
704706
address: addresses[0],
705707
value: {
706708
assets: new Map([[AssetIds.TSLA, 5n]]),
707-
coins: 5_000_000n
709+
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
708710
}
709711
}
710712
]
@@ -743,10 +745,10 @@ describe('Transaction Summary Inspector', () => {
743745
// Assert
744746
expect(summary).toEqual({
745747
assets: new Map(),
746-
coins: 2_000_000n,
748+
coins: 2_000_000n - fee,
747749
collateral: 0n,
748750
deposit: 0n,
749-
fee: 170_000n,
751+
fee,
750752
returnedDeposit: 2_000_000n,
751753
unresolved: {
752754
inputs: [],
@@ -780,7 +782,7 @@ describe('Transaction Summary Inspector', () => {
780782
address: addresses[0],
781783
value: {
782784
assets: new Map([[AssetIds.TSLA, 5n]]),
783-
coins: 5_000_000n
785+
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
784786
}
785787
}
786788
]
@@ -819,10 +821,10 @@ describe('Transaction Summary Inspector', () => {
819821
// Assert
820822
expect(summary).toEqual({
821823
assets: new Map(),
822-
coins: -15_000_000n,
824+
coins: -15_000_000n - fee,
823825
collateral: 0n,
824826
deposit: 15_000_000n,
825-
fee: 170_000n,
827+
fee,
826828
returnedDeposit: 0n,
827829
unresolved: {
828830
inputs: [],
@@ -856,7 +858,7 @@ describe('Transaction Summary Inspector', () => {
856858
address: addresses[0],
857859
value: {
858860
assets: new Map([[AssetIds.TSLA, 5n]]),
859-
coins: 20_000_000n
861+
coins: 20_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
860862
}
861863
}
862864
]
@@ -895,10 +897,10 @@ describe('Transaction Summary Inspector', () => {
895897
// Assert
896898
expect(summary).toEqual({
897899
assets: new Map(),
898-
coins: 15_000_000n,
900+
coins: 15_000_000n - fee,
899901
collateral: 0n,
900902
deposit: 0n,
901-
fee: 170_000n,
903+
fee,
902904
returnedDeposit: 15_000_000n,
903905
unresolved: {
904906
inputs: [],
@@ -932,7 +934,7 @@ describe('Transaction Summary Inspector', () => {
932934
address: addresses[0],
933935
value: {
934936
assets: new Map([[AssetIds.TSLA, 5n]]),
935-
coins: 20_000_000n
937+
coins: 20_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
936938
}
937939
},
938940
{
@@ -986,10 +988,10 @@ describe('Transaction Summary Inspector', () => {
986988
[assetInfos[AssetInfoIdx.PXL], 1n],
987989
[assetInfos[AssetInfoIdx.Unit], 1n]
988990
]),
989-
coins: 100_000_000n,
991+
coins: 100_000_000n - fee,
990992
collateral: 0n,
991993
deposit: 0n,
992-
fee: 170_000n,
994+
fee,
993995
returnedDeposit: 0n,
994996
unresolved: {
995997
inputs: [

0 commit comments

Comments
 (0)