Skip to content

Commit 62c4fa2

Browse files
authored
fix: [lw-11844]: handle withdrawals in tx history (#1555)
1 parent a98a852 commit 62c4fa2

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

packages/nami/src/adapters/transactions.ts

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,24 +189,28 @@ const calculateAmount = ({
189189
const amounts: Amount[] = [];
190190

191191
while (inputs.length > 0) {
192-
const input = inputs.pop()!;
193-
const outputIndex = outputs.findIndex(amount => amount.unit === input.unit);
194-
let qty;
195-
196-
if (outputIndex > -1) {
197-
qty =
198-
(BigInt(input.quantity) - BigInt(outputs[outputIndex].quantity)) *
199-
BigInt(-1);
200-
outputs.splice(outputIndex, 1);
201-
} else {
202-
qty = BigInt(input.quantity) * BigInt(-1);
192+
const input = inputs.pop();
193+
if (input) {
194+
const outputIndex = outputs.findIndex(
195+
amount => amount.unit === input.unit,
196+
);
197+
let qty;
198+
199+
if (outputIndex > -1) {
200+
qty =
201+
(BigInt(input.quantity) - BigInt(outputs[outputIndex].quantity)) *
202+
BigInt(-1);
203+
outputs.splice(outputIndex, 1);
204+
} else {
205+
qty = BigInt(input.quantity) * BigInt(-1);
206+
}
207+
208+
if (qty !== BigInt(0) || input.unit === 'lovelace')
209+
amounts.push({
210+
unit: input.unit,
211+
quantity: qty,
212+
});
203213
}
204-
205-
if (qty !== BigInt(0) || input.unit === 'lovelace')
206-
amounts.push({
207-
unit: input.unit,
208-
quantity: qty,
209-
});
210214
}
211215

212216
return amounts.concat(outputs);
@@ -325,6 +329,24 @@ export const encodeToCbor = (args: EncodeToCborArgs): Serialization.TxCBOR => {
325329
return transaction.toCbor();
326330
};
327331

332+
const getTotalWithdrawlAmount = ({
333+
rewardAccountsAddresses,
334+
withdrawals = [],
335+
}: {
336+
rewardAccountsAddresses: Set<Wallet.Cardano.RewardAccount>;
337+
withdrawals: Wallet.Cardano.Withdrawal[];
338+
}): bigint => {
339+
let total = BigInt(0);
340+
341+
for (const withdrawal of withdrawals) {
342+
if (rewardAccountsAddresses.has(withdrawal.stakeAddress)) {
343+
total = total + BigInt(withdrawal.quantity.toString());
344+
}
345+
}
346+
347+
return total;
348+
};
349+
328350
export const getTxInfo = async ({
329351
tx,
330352
getTxInputsValueAndAddress,
@@ -380,6 +402,10 @@ export const getTxInfo = async ({
380402
Number.parseInt(implicitCoin.deposit?.toString() ?? '') > 0
381403
? BigInt(implicitCoin.deposit?.toString() ?? 0)
382404
: BigInt(0);
405+
const totalWithdrawals = getTotalWithdrawlAmount({
406+
rewardAccountsAddresses,
407+
withdrawals: tx.body.withdrawals ?? [],
408+
});
383409

384410
const info: TxInfo = {
385411
txHash: tx.id.toString(),
@@ -402,9 +428,12 @@ export const getTxInfo = async ({
402428
rewardAccountsAddresses,
403429
}),
404430
amounts: amounts,
405-
lovelace: ['internalIn', 'externalIn', 'multisig'].includes(type)
406-
? BigInt(lovelace.toString())
407-
: BigInt(lovelace.toString()) + BigInt(tx.body.fee.toString()) + deposit,
431+
lovelace:
432+
(['internalIn', 'externalIn', 'multisig'].includes(type)
433+
? BigInt(lovelace.toString())
434+
: BigInt(lovelace.toString()) +
435+
BigInt(tx.body.fee.toString()) +
436+
deposit) - BigInt(totalWithdrawals.toString()),
408437
assets: assets
409438
.map(asset => {
410439
const info = assetInfo?.get(Wallet.Cardano.AssetId(asset.unit));

packages/nami/src/ui/app/components/transaction.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import TimeAgo from 'javascript-time-ago';
2121
import en from 'javascript-time-ago/locale/en';
2222
import ReactDOMServer from 'react-dom/server';
2323
import {
24-
FaCoins,
2524
FaPiggyBank,
2625
FaTrashAlt,
2726
FaRegEdit,
@@ -271,7 +270,6 @@ const TxIcon = ({
271270
externalIn: TiArrowForward,
272271
internalOut: TiArrowShuffle,
273272
externalOut: TiArrowBack,
274-
withdrawal: FaCoins,
275273
delegation: FaPiggyBank,
276274
stake: FaUserCheck,
277275
unstake: IoRemoveCircleSharp,
@@ -282,7 +280,9 @@ const TxIcon = ({
282280
contract: FaRegFileCode,
283281
};
284282

285-
const type = extra.length > 0 ? extra[0] : txType;
283+
// there is no withdrawal type of tx in lace (see LW-11844)
284+
const filteredExtra = extra.filter(e => e !== 'withdrawal');
285+
const type = filteredExtra.length > 0 ? filteredExtra[0] : txType;
286286

287287
let style;
288288
switch (type) {

0 commit comments

Comments
 (0)