Skip to content

Commit

Permalink
Merge pull request #1088 from input-output-hk/fix/lw-9749-transaction…
Browse files Browse the repository at this point in the history
…SummaryInspector-detects-incorrectly-fees-as-unaccounted-balance

fix(core): transactionSummaryInspector now correctly accounts for transaction fees
  • Loading branch information
AngelCastilloB authored Feb 7, 2024
2 parents 0ead9c0 + c55d71c commit 6eb7c78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/util/transactionSummaryInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ const getUnaccountedFunds = async (
tx: Cardano.Tx,
resolvedInputs: ResolutionResult,
implicitCoin: Cardano.Lovelace,
fee: Cardano.Lovelace,
implicitAssets: Cardano.TokenMap = new Map()
): Promise<Cardano.Value> => {
const totalInputs = totalInputsValue(resolvedInputs);
const totalOutputs = totalOutputsValue(tx.body.outputs);

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

return subtractValueQuantities([totalOutputs, totalInputs]);
};
Expand Down Expand Up @@ -177,7 +179,7 @@ export const transactionSummaryInspector: TransactionSummaryInspector =
returnedDeposit: implicit.reclaimDeposit || 0n,
unresolved: {
inputs: resolvedInputs.unresolvedInputs,
value: await getUnaccountedFunds(tx, resolvedInputs, implicitCoin, implicitAssets)
value: await getUnaccountedFunds(tx, resolvedInputs, implicitCoin, fee, implicitAssets)
}
};
};
56 changes: 29 additions & 27 deletions packages/core/test/util/transactionSummaryInspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const auxiliaryData = {
scripts: [mockScript2]
};

const fee = 170_000n;

const buildMockTx = (
args: {
inputs?: Cardano.HydratedTxIn[];
Expand All @@ -143,7 +145,7 @@ const buildMockTx = (
certificates: args.certificates,
collateralReturn: args.collateralReturn ?? undefined,
collaterals: args.collaterals ?? undefined,
fee: 170_000n,
fee,
inputs: args.inputs ?? [
{
address: addresses[0],
Expand Down Expand Up @@ -255,7 +257,7 @@ describe('Transaction Summary Inspector', () => {
address: externalAddress1,
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 5_000_000n
coins: 5_000_000n - fee // In this TX the fee is coming out of one of the external addresses.
}
},
{
Expand Down Expand Up @@ -346,7 +348,7 @@ describe('Transaction Summary Inspector', () => {
coins: -10_000_000n,
collateral: 0n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -382,7 +384,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 5_000_000n
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -433,10 +435,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: 0n,
coins: -fee,
collateral: 10_000_000n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -473,7 +475,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 27_000_000n
coins: 27_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
],
Expand Down Expand Up @@ -525,10 +527,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: 0n,
coins: -fee,
collateral: 25_000_000n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -564,7 +566,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 5_000_000n
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -609,10 +611,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: 0n,
coins: -fee,
collateral: 5_000_000n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand All @@ -637,7 +639,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 3_000_000n
coins: 3_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -676,10 +678,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: -2_000_000n,
coins: -2_000_000n - fee,
collateral: 0n,
deposit: 2_000_000n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand All @@ -704,7 +706,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 5_000_000n
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -743,10 +745,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: 2_000_000n,
coins: 2_000_000n - fee,
collateral: 0n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 2_000_000n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -780,7 +782,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 5_000_000n
coins: 5_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -819,10 +821,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: -15_000_000n,
coins: -15_000_000n - fee,
collateral: 0n,
deposit: 15_000_000n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -856,7 +858,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 20_000_000n
coins: 20_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
}
]
Expand Down Expand Up @@ -895,10 +897,10 @@ describe('Transaction Summary Inspector', () => {
// Assert
expect(summary).toEqual({
assets: new Map(),
coins: 15_000_000n,
coins: 15_000_000n - fee,
collateral: 0n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 15_000_000n,
unresolved: {
inputs: [],
Expand Down Expand Up @@ -932,7 +934,7 @@ describe('Transaction Summary Inspector', () => {
address: addresses[0],
value: {
assets: new Map([[AssetIds.TSLA, 5n]]),
coins: 20_000_000n
coins: 20_000_000n - fee // In this TX the fee is coming out of one of our own addresses.
}
},
{
Expand Down Expand Up @@ -986,10 +988,10 @@ describe('Transaction Summary Inspector', () => {
[assetInfos[AssetInfoIdx.PXL], 1n],
[assetInfos[AssetInfoIdx.Unit], 1n]
]),
coins: 100_000_000n,
coins: 100_000_000n - fee,
collateral: 0n,
deposit: 0n,
fee: 170_000n,
fee,
returnedDeposit: 0n,
unresolved: {
inputs: [
Expand Down

0 comments on commit 6eb7c78

Please sign in to comment.