Skip to content

Commit

Permalink
fix: [lw-11844]: handle withdrawals in tx history (#1555)
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore authored Nov 27, 2024
1 parent a98a852 commit 62c4fa2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
69 changes: 49 additions & 20 deletions packages/nami/src/adapters/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,28 @@ const calculateAmount = ({
const amounts: Amount[] = [];

while (inputs.length > 0) {
const input = inputs.pop()!;
const outputIndex = outputs.findIndex(amount => amount.unit === input.unit);
let qty;

if (outputIndex > -1) {
qty =
(BigInt(input.quantity) - BigInt(outputs[outputIndex].quantity)) *
BigInt(-1);
outputs.splice(outputIndex, 1);
} else {
qty = BigInt(input.quantity) * BigInt(-1);
const input = inputs.pop();
if (input) {
const outputIndex = outputs.findIndex(
amount => amount.unit === input.unit,
);
let qty;

if (outputIndex > -1) {
qty =
(BigInt(input.quantity) - BigInt(outputs[outputIndex].quantity)) *
BigInt(-1);
outputs.splice(outputIndex, 1);
} else {
qty = BigInt(input.quantity) * BigInt(-1);
}

if (qty !== BigInt(0) || input.unit === 'lovelace')
amounts.push({
unit: input.unit,
quantity: qty,
});
}

if (qty !== BigInt(0) || input.unit === 'lovelace')
amounts.push({
unit: input.unit,
quantity: qty,
});
}

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

const getTotalWithdrawlAmount = ({
rewardAccountsAddresses,
withdrawals = [],
}: {
rewardAccountsAddresses: Set<Wallet.Cardano.RewardAccount>;
withdrawals: Wallet.Cardano.Withdrawal[];
}): bigint => {
let total = BigInt(0);

for (const withdrawal of withdrawals) {
if (rewardAccountsAddresses.has(withdrawal.stakeAddress)) {
total = total + BigInt(withdrawal.quantity.toString());
}
}

return total;
};

export const getTxInfo = async ({
tx,
getTxInputsValueAndAddress,
Expand Down Expand Up @@ -380,6 +402,10 @@ export const getTxInfo = async ({
Number.parseInt(implicitCoin.deposit?.toString() ?? '') > 0
? BigInt(implicitCoin.deposit?.toString() ?? 0)
: BigInt(0);
const totalWithdrawals = getTotalWithdrawlAmount({
rewardAccountsAddresses,
withdrawals: tx.body.withdrawals ?? [],
});

const info: TxInfo = {
txHash: tx.id.toString(),
Expand All @@ -402,9 +428,12 @@ export const getTxInfo = async ({
rewardAccountsAddresses,
}),
amounts: amounts,
lovelace: ['internalIn', 'externalIn', 'multisig'].includes(type)
? BigInt(lovelace.toString())
: BigInt(lovelace.toString()) + BigInt(tx.body.fee.toString()) + deposit,
lovelace:
(['internalIn', 'externalIn', 'multisig'].includes(type)
? BigInt(lovelace.toString())
: BigInt(lovelace.toString()) +
BigInt(tx.body.fee.toString()) +
deposit) - BigInt(totalWithdrawals.toString()),
assets: assets
.map(asset => {
const info = assetInfo?.get(Wallet.Cardano.AssetId(asset.unit));
Expand Down
6 changes: 3 additions & 3 deletions packages/nami/src/ui/app/components/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import TimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en';
import ReactDOMServer from 'react-dom/server';
import {
FaCoins,
FaPiggyBank,
FaTrashAlt,
FaRegEdit,
Expand Down Expand Up @@ -271,7 +270,6 @@ const TxIcon = ({
externalIn: TiArrowForward,
internalOut: TiArrowShuffle,
externalOut: TiArrowBack,
withdrawal: FaCoins,
delegation: FaPiggyBank,
stake: FaUserCheck,
unstake: IoRemoveCircleSharp,
Expand All @@ -282,7 +280,9 @@ const TxIcon = ({
contract: FaRegFileCode,
};

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

let style;
switch (type) {
Expand Down

0 comments on commit 62c4fa2

Please sign in to comment.