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: [lw-11844]: handle withdrawals in tx history #1555

Merged
merged 2 commits into from
Nov 27, 2024
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
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
Loading