diff --git a/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts b/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts index b84a60c1b..f62cabbcf 100644 --- a/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts +++ b/apps/browser-extension-wallet/src/stores/slices/transaction-detail-slice.ts @@ -15,7 +15,6 @@ import { getTransactionTotalOutput } from '../../utils/get-transaction-total-out import { inspectTxValues } from '@src/utils/tx-inspection'; import { firstValueFrom } from 'rxjs'; import { getAssetsInformation } from '@src/utils/get-assets-information'; -import { getRewardsAmount } from '@src/views/browser-view/features/activity/helpers'; import { MAX_POOLS_COUNT } from '@lace/staking'; import { TransactionType } from '@lace/core'; @@ -169,31 +168,14 @@ const getTransactionDetail = transaction = { ...transaction, pools: pools.map((pool) => ({ - name: pool.metadata.name || '-', - ticker: pool.metadata.ticker || '-', + name: pool.metadata?.name || '-', + ticker: pool.metadata?.ticker || '-', id: pool.id.toString() })) }; } } - /* - as we need to divide the transaction that withdrawn rewards in to two records, - now we have a type rewards. - When the record is of type rewards we will need to calculate the amount of rewards withdrawn - and then adds this property rewards to the transaction information - */ - if (type === 'rewards') { - const rewards = getRewardsAmount( - tx?.body?.withdrawals, - walletInfo.addresses.map((addr) => addr.rewardAccount) - ); - transaction = { - ...transaction, - rewards: Wallet.util.lovelacesToAdaString(rewards) - }; - } - set({ fetchingTransactionInfo: false }); return { tx: transaction, blocks, status, assetAmount, type }; }; diff --git a/apps/browser-extension-wallet/src/stores/slices/wallet-activities-slice.ts b/apps/browser-extension-wallet/src/stores/slices/wallet-activities-slice.ts index 5c41a3fca..20c9ea541 100644 --- a/apps/browser-extension-wallet/src/stores/slices/wallet-activities-slice.ts +++ b/apps/browser-extension-wallet/src/stores/slices/wallet-activities-slice.ts @@ -233,7 +233,7 @@ const getWalletActivitiesObservable = async ({ /** * 1. Listens for time settings - * 2. Passes it to hisorical transactions and pending transactions lists + * 2. Passes it to historical transactions and pending transactions lists * 3. Emits both lists combined and sets current state for Zustand */ return combineLatest([eraSummaries$, pendingTransactions$, historicalTransactions$]).pipe( diff --git a/apps/browser-extension-wallet/src/utils/__tests__/inspectTxType.test.ts b/apps/browser-extension-wallet/src/utils/__tests__/inspectTxType.test.ts index 996991c9c..2464dc1c9 100644 --- a/apps/browser-extension-wallet/src/utils/__tests__/inspectTxType.test.ts +++ b/apps/browser-extension-wallet/src/utils/__tests__/inspectTxType.test.ts @@ -33,7 +33,6 @@ describe('testing tx-inspection utils', () => { expect(getTxDirection({ type: 'incoming' })).toEqual(TxDirections.Incoming); expect(getTxDirection({ type: 'rewards' })).toEqual(TxDirections.Outgoing); expect(getTxDirection({ type: 'outgoing' })).toEqual(TxDirections.Outgoing); - expect(getTxDirection({ type: 'self-rewards' })).toEqual(TxDirections.Self); expect(getTxDirection({ type: 'self' })).toEqual(TxDirections.Self); }); }); @@ -258,7 +257,7 @@ describe('testing tx-inspection utils', () => { ] as Wallet.KeyManagement.GroupedAddress[] }); - expect(result).toBe('self-rewards'); + expect(result).toBe('self'); }); test('is self', () => { diff --git a/apps/browser-extension-wallet/src/utils/tx-inspection.ts b/apps/browser-extension-wallet/src/utils/tx-inspection.ts index 81c41bacd..b940dc594 100644 --- a/apps/browser-extension-wallet/src/utils/tx-inspection.ts +++ b/apps/browser-extension-wallet/src/utils/tx-inspection.ts @@ -18,10 +18,10 @@ const hasWalletStakeAddress = ( ) => withdrawals.some((item) => item.stakeAddress === stakeAddress); interface TxTypeProps { - type: TransactionType | 'self-rewards'; + type: TransactionType; } -export const getTxDirection = ({ type }: TxTypeProps): TxDirection => { +export const getTxDirection = ({ type }: TxTypeProps): TxDirections => { switch (type) { case 'incoming': return TxDirections.Incoming; @@ -29,8 +29,6 @@ export const getTxDirection = ({ type }: TxTypeProps): TxDirection => { return TxDirections.Outgoing; case 'outgoing': return TxDirections.Outgoing; - case 'self-rewards': - return TxDirections.Self; case 'self': return TxDirections.Self; } @@ -49,7 +47,7 @@ export const inspectTxType = ({ }: { walletAddresses: Wallet.KeyManagement.GroupedAddress[]; tx: Wallet.Cardano.HydratedTx; -}): TransactionType | 'self-rewards' => { +}): Exclude => { const { paymentAddresses, rewardAccounts } = walletAddresses.reduce( (acc, curr) => ({ paymentAddresses: [...acc.paymentAddresses, curr.address], @@ -74,7 +72,7 @@ export const inspectTxType = ({ inspectionProperties.totalWithdrawals > BigInt(0) && walletAddresses.some((addr) => hasWalletStakeAddress(tx.body.withdrawals, addr.rewardAccount)); - if (inspectionProperties.sent.inputs.length > 0) { + if (inspectionProperties.sent.inputs.length > 0 || withRewardsWithdrawal) { switch (true) { case !!inspectionProperties.delegation[0]?.poolId: return 'delegation'; @@ -82,10 +80,6 @@ export const inspectTxType = ({ return 'delegationRegistration'; case inspectionProperties.stakeKeyDeregistration.length > 0: return 'delegationDeregistration'; - case withRewardsWithdrawal && inspectionProperties.selfTransaction: - return 'self-rewards'; - case withRewardsWithdrawal: - return 'rewards'; case inspectionProperties.selfTransaction: return 'self'; default: @@ -93,7 +87,6 @@ export const inspectTxType = ({ } } - if (withRewardsWithdrawal) return 'rewards'; return 'incoming'; }; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts index 4b3e0dd12..572f7f8a7 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts @@ -5,12 +5,13 @@ let actualLovelacesToAdaString: any; /* eslint-disable import/imports-first */ /* eslint-disable max-len */ /* eslint-disable no-magic-numbers */ -import { pendingTxTransformer, getFormattedFiatAmount } from '../pending-tx-transformer'; +import { pendingTxTransformer } from '../pending-tx-transformer'; import { Wallet } from '@lace/cardano'; import { cardanoCoin } from '@utils/constants'; import { TxCBOR } from '@cardano-sdk/core'; import { DEFAULT_TIME_FORMAT, formatTime } from '@src/utils/format-date'; import BigNumber from 'bignumber.js'; +import { getFormattedFiatAmount } from '../common-tx-transformer'; jest.mock('@lace/cardano', () => { const actual = jest.requireActual('@lace/cardano'); diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/tx-history-transformer.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/tx-history-transformer.test.ts index 7389e2756..a3de4bfd7 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/tx-history-transformer.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/tx-history-transformer.test.ts @@ -4,15 +4,14 @@ const mockGetFormattedAmount = jest.fn(); /* eslint-disable max-len */ /* eslint-disable no-magic-numbers */ /* eslint-disable import/imports-first */ +import * as txTransformers from '../common-tx-transformer'; import * as txHistoryTransformers from '../tx-history-transformer'; import { Wallet } from '@lace/cardano'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { cardanoCoin } from '@src/utils/constants'; import * as txInspection from '@src/utils/tx-inspection'; -import * as pendingTxTransformer from '../pending-tx-transformer'; -import { TxDirection } from '@src/types'; -import { AssetActivityItemProps } from '@lace/core'; +import type { TxDirections } from '@src/types'; jest.mock('@lace/cardano', () => { const actual = jest.requireActual('@lace/cardano'); @@ -73,7 +72,12 @@ describe('Testing txHistoryTransformer function', () => { invalidBefore: Wallet.Cardano.Slot(1), invalidHereafter: Wallet.Cardano.Slot(2) }, - withdrawals: [{ quantity: BigInt(2) } as Wallet.Cardano.Withdrawal] + withdrawals: [ + { + quantity: BigInt(2), + stakeAddress: Wallet.Cardano.RewardAccount('stake_test1uq7g7kqeucnqfweqzgxk3dw34e8zg4swnc7nagysug2mm4cm77jrx') + } as Wallet.Cardano.Withdrawal + ] }, witness: { signatures: new Map() @@ -105,8 +109,8 @@ describe('Testing txHistoryTransformer function', () => { protocolParameters: { poolDeposit: 3, stakeKeyDeposit: 2 } as Wallet.ProtocolParameters, cardanoCoin }); - expect(result.status).toBe('success'); - expect(result.amount).toBe('20.00 ADA'); + expect(result[0].status).toBe('success'); + expect(result[0].amount).toBe('20.00 ADA'); }); test('should return parsed outgoing tx', async () => { @@ -132,31 +136,21 @@ describe('Testing txHistoryTransformer function', () => { protocolParameters: { poolDeposit: 3, stakeKeyDeposit: 2 } as Wallet.ProtocolParameters, cardanoCoin }); - expect(result.status).toBe('success'); - expect(result.amount).toBe('30.00 ADA'); + expect(result[0].status).toBe('success'); + expect(result[0].amount).toBe('30.00 ADA'); }); - test('should return tx of reward type', async () => { + test('should return outgoing tx with withdrawal only as outgoing tx', async () => { const direction = 'tx-direction'; - const rewardsAmount = '10'; const formattedAmount = 'getFormattedAmount'; const formattedFiatAmount = 'getFormattedFiatAmount'; - const transformedTx = { - date: 'date', - timestamp: 'timestamp' - }; mockGetFormattedAmount.mockReturnValueOnce(formattedAmount); const getFormattedFiatAmountSpy = jest - .spyOn(pendingTxTransformer, 'getFormattedFiatAmount') + .spyOn(txTransformers, 'getFormattedFiatAmount') .mockReturnValueOnce(formattedFiatAmount); - const txTransformerSpy = jest - .spyOn(pendingTxTransformer, 'txTransformer') - .mockReturnValueOnce(transformedTx as unknown as Omit); - const inspectTxTypeSpy = jest.spyOn(txInspection, 'inspectTxType').mockReturnValueOnce('self-rewards'); - const getTxDirectionSpy = jest.spyOn(txInspection, 'getTxDirection').mockReturnValueOnce(direction as TxDirection); - const getRewardsAmountSpy = jest - .spyOn(txHistoryTransformers, 'getRewardsAmount') - .mockReturnValueOnce(rewardsAmount); + const txTransformerSpy = jest.spyOn(txTransformers, 'txTransformer'); + const inspectTxTypeSpy = jest.spyOn(txInspection, 'inspectTxType'); + const getTxDirectionSpy = jest.spyOn(txInspection, 'getTxDirection').mockReturnValueOnce(direction as TxDirections); const props = { tx: txHistory, @@ -186,12 +180,8 @@ describe('Testing txHistoryTransformer function', () => { tx: props.tx }); expect(getTxDirectionSpy).toBeCalledWith({ - type: 'self-rewards' + type: 'outgoing' }); - expect(getRewardsAmountSpy).toBeCalledWith( - props.tx?.body?.withdrawals, - props.walletAddresses.map((addr) => addr.rewardAccount) - ); expect(txTransformerSpy).toBeCalledWith({ tx: props.tx, walletAddresses: props.walletAddresses, @@ -204,24 +194,12 @@ describe('Testing txHistoryTransformer function', () => { direction, date: '01 February 2022' }); - expect(result).toEqual([ - { ...transformedTx, type: 'self' }, - { - type: 'rewards', - direction: 'Incoming', - amount: formattedAmount, - fiatAmount: formattedFiatAmount, - status: Wallet.TransactionStatus.SPENDABLE, - date: transformedTx.date, - assets: [], - timestamp: transformedTx.timestamp - } - ]); + expect(result[0].status).toBe('success'); + expect(result[0].type).toBe('outgoing'); txTransformerSpy.mockRestore(); inspectTxTypeSpy.mockRestore(); getTxDirectionSpy.mockRestore(); getFormattedFiatAmountSpy.mockRestore(); - getRewardsAmountSpy.mockRestore(); }); }); diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-transformers.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-transformers.ts deleted file mode 100644 index 7ad629927..000000000 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-transformers.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { AssetActivityItemProps } from '@lace/core'; -import { inspectTxType } from '../../../../../utils/tx-inspection'; - -type TransformedTx = Omit; - -export const splitDelegationWithDeregistrationIntoTwoActions = (tx: TransformedTx): [TransformedTx, TransformedTx] => [ - { - ...tx, - type: 'delegation', - // Reclaimed deposit already shown in the delegationDeregistration - depositReclaim: undefined - }, - { - ...tx, - type: 'delegationDeregistration', - // Let de-registration show just the returned deposit, - // and the other transaction show fee to avoid duplicity - fee: '0' - } -]; - -// If a delegation transaction has also any reclaimed deposit (deregistration certificates), -// we want to split it into two separate actions for clarity -export const isDelegationWithDeregistrationTx = (tx: TransformedTx, type: ReturnType): boolean => - type === 'delegation' && !!tx.depositReclaim; - -export const splitDelegationWithRegistrationIntoTwoActions = (tx: TransformedTx): [TransformedTx, TransformedTx] => [ - { - ...tx, - type: 'delegation', - // Deposit already shown in the delegationDeregistration - deposit: undefined - }, - { - ...tx, - type: 'delegationRegistration', - // Let registration show just the deposit, - // and the other transaction show fee to avoid duplicity - fee: '0' - } -]; - -// If a delegation transaction has also any deposit (registration certificates), -// we want to split it into two separate actions for clarity -export const isDelegationWithRegistrationTx = (tx: TransformedTx, type: ReturnType): boolean => - type === 'delegation' && !!tx.deposit; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts new file mode 100644 index 000000000..1e40facbc --- /dev/null +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts @@ -0,0 +1,150 @@ +import BigNumber from 'bignumber.js'; +import { Wallet } from '@lace/cardano'; +import { CurrencyInfo, TxDirections } from '@types'; +import { inspectTxValues, inspectTxType } from '@src/utils/tx-inspection'; +import { formatTime } from '@src/utils/format-date'; +import type { TransformedTx } from './types'; + +export interface TxTransformerInput { + tx: Wallet.TxInFlight | Wallet.Cardano.HydratedTx; + walletAddresses: Wallet.KeyManagement.GroupedAddress[]; + fiatCurrency: CurrencyInfo; + fiatPrice?: number; + protocolParameters: Wallet.ProtocolParameters; + cardanoCoin: Wallet.CoinId; + time: Date; + direction?: TxDirections; + status?: Wallet.TransactionStatus; + date?: string; +} + +export const getFormattedFiatAmount = ({ + amount, + fiatPrice, + fiatCurrency +}: { + amount: BigNumber; + fiatPrice: number; + fiatCurrency: CurrencyInfo; +}): string => { + const fiatAmount = fiatPrice + ? Wallet.util.lovelacesToAdaString(amount.times(new BigNumber(fiatPrice)).toString()) + : ''; + return fiatAmount ? `${fiatAmount} ${fiatCurrency.code}` : '-'; +}; + +const splitDelegationTx = (tx: TransformedTx): TransformedTx[] => { + if (tx.deposit) { + return [ + { + ...tx, + type: 'delegation', + // Deposit already shown in the delegationRegistration + deposit: undefined + }, + { + ...tx, + type: 'delegationRegistration', + // Let registration show just the deposit, + // and the other transaction show fee to avoid duplicity + fee: '0' + } + ]; + } else if (tx.depositReclaim) { + return [ + { + ...tx, + type: 'delegation', + // Reclaimed deposit already shown in the delegationDeregistration + depositReclaim: undefined + }, + { + ...tx, + type: 'delegationDeregistration', + // Let de-registration show just the returned deposit, + // and the other transaction show fee to avoid duplicity + fee: '0' + } + ]; + } + + return []; +}; + +/** + Simplifies the transaction object to be used in the activity list + + @param tx the transaction object + @param walletAddresses the addresses of the wallet and the reward account + @param fiatCurrency the fiat currency details + @param fiatPrice the fiat price of ADA + @param protocolParameters the protocol parameters + @param cardanoCoin the ADA coin details + @param time the time of the transaction + @param direction the direction of the transaction + @param status the status of the transaction + @param date the date of the transaction + */ + +export const txTransformer = ({ + tx, + walletAddresses, + fiatCurrency, + fiatPrice, + protocolParameters, + cardanoCoin, + time, + date, + direction, + status +}: TxTransformerInput): TransformedTx[] => { + const type = inspectTxType({ walletAddresses, tx: tx as unknown as Wallet.Cardano.HydratedTx }); + const implicitCoin = Wallet.Cardano.util.computeImplicitCoin(protocolParameters, tx.body); + const deposit = implicitCoin.deposit ? Wallet.util.lovelacesToAdaString(implicitCoin.deposit.toString()) : undefined; + const depositReclaimValue = Wallet.util.calculateDepositReclaim(implicitCoin); + const depositReclaim = depositReclaimValue + ? Wallet.util.lovelacesToAdaString(depositReclaimValue.toString()) + : undefined; + const { coins, assets } = inspectTxValues({ + addresses: walletAddresses, + tx: tx as unknown as Wallet.Cardano.HydratedTx, + direction + }); + const outputAmount = new BigNumber(coins.toString()); + const timestamp = formatTime({ + date: time, + type: 'local' + }); + + const assetsEntries = assets + ? [...assets.entries()] + .map(([id, val]) => ({ id: id.toString(), val: val.toString() })) + .sort((a, b) => Number(b.val) - Number(a.val)) + : []; + + const baseTransformedTx = { + id: tx.id.toString(), + deposit, + depositReclaim, + fee: Wallet.util.lovelacesToAdaString(tx.body.fee.toString()), + status, + amount: Wallet.util.getFormattedAmount({ amount: outputAmount.toString(), cardanoCoin }), + fiatAmount: getFormattedFiatAmount({ amount: outputAmount, fiatCurrency, fiatPrice }), + assets: assetsEntries, + assetsNumber: (assets?.size ?? 0) + 1, + date, + timestamp + }; + + if (type === 'delegation') { + return splitDelegationTx(baseTransformedTx); + } + + return [ + { + ...baseTransformedTx, + type, + direction + } + ]; +}; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/index.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/index.ts index 5c1368360..023ec9375 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/index.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/index.ts @@ -2,3 +2,4 @@ export * from './pending-tx-transformer'; export * from './tx-history-transformer'; export * from './is-tx-with-assets'; export * from './filter-outputs-by-tx-direction'; +export * from './types'; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts index a97ef0bdf..7b985cad8 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/pending-tx-transformer.ts @@ -1,110 +1,13 @@ -import BigNumber from 'bignumber.js'; -import { AssetActivityItemProps, TransactionType } from '@lace/core'; +import { TxTransformerInput, txTransformer } from './common-tx-transformer'; import { Wallet } from '@lace/cardano'; -import { CurrencyInfo, TxDirections } from '@types'; -import { inspectTxValues, inspectTxType } from '@src/utils/tx-inspection'; +import type { TransformedTx } from './types'; import capitalize from 'lodash/capitalize'; -import { formatTime } from '@src/utils/format-date'; -import { - isDelegationWithDeregistrationTx, - isDelegationWithRegistrationTx, - splitDelegationWithDeregistrationIntoTwoActions, - splitDelegationWithRegistrationIntoTwoActions -} from './common-transformers'; +import { TxDirections } from '@types'; -export interface TxTransformerInput { +interface TxHistoryTransformerInput extends Omit { tx: Wallet.TxInFlight; - walletAddresses: Wallet.KeyManagement.GroupedAddress[]; - fiatCurrency: CurrencyInfo; - fiatPrice?: number; - protocolParameters: Wallet.ProtocolParameters; - cardanoCoin: Wallet.CoinId; - time: Date; - direction?: TxDirections; - status?: Wallet.TransactionStatus; - date?: string; } -export const getFormattedFiatAmount = ({ - amount, - fiatPrice, - fiatCurrency -}: { - amount: BigNumber; - fiatPrice: number; - fiatCurrency: CurrencyInfo; -}): string => { - const fiatAmount = fiatPrice - ? Wallet.util.lovelacesToAdaString(amount.times(new BigNumber(fiatPrice)).toString()) - : ''; - return fiatAmount ? `${fiatAmount} ${fiatCurrency.code}` : '-'; -}; - -/** - Simplifies the transaction object to be used in the activity list - - @param tx the transaction object - @param walletAddresses the addresses of the wallet and the reward account - @param fiatCurrency the fiat currency details - @param fiatPrice the fiat price of ADA - @param protocolParameters the protocol parameters - @param cardanoCoin the ADA coin details - @param time the time of the transaction - @param direction the direction of the transaction - @param status the status of the transaction - @param date the date of the transaction - */ - -export const txTransformer = ({ - tx, - walletAddresses, - fiatCurrency, - fiatPrice, - protocolParameters, - cardanoCoin, - time, - date, - direction, - status -}: TxTransformerInput): Omit => { - const implicitCoin = Wallet.Cardano.util.computeImplicitCoin(protocolParameters, tx.body); - const deposit = implicitCoin.deposit ? Wallet.util.lovelacesToAdaString(implicitCoin.deposit.toString()) : undefined; - const depositReclaimValue = Wallet.util.calculateDepositReclaim(implicitCoin); - const depositReclaim = depositReclaimValue - ? Wallet.util.lovelacesToAdaString(depositReclaimValue.toString()) - : undefined; - const { coins, assets } = inspectTxValues({ - addresses: walletAddresses, - tx: tx as unknown as Wallet.Cardano.HydratedTx, - direction - }); - const outputAmount = new BigNumber(coins.toString()); - const timestamp = formatTime({ - date: time, - type: 'local' - }); - - const assetsEntries = assets - ? [...assets.entries()] - .map(([id, val]) => ({ id: id.toString(), val: val.toString() })) - .sort((a, b) => Number(b.val) - Number(a.val)) - : []; - - return { - id: tx.id.toString(), - deposit, - depositReclaim, - fee: Wallet.util.lovelacesToAdaString(tx.body.fee.toString()), - status, - amount: Wallet.util.getFormattedAmount({ amount: outputAmount.toString(), cardanoCoin }), - fiatAmount: getFormattedFiatAmount({ amount: outputAmount, fiatCurrency, fiatPrice }), - assets: assetsEntries, - assetsNumber: (assets?.size ?? 0) + 1, - date, - timestamp - }; -}; - export const pendingTxTransformer = ({ tx, walletAddresses, @@ -113,9 +16,8 @@ export const pendingTxTransformer = ({ protocolParameters, cardanoCoin, time -}: TxTransformerInput): Array> | Omit => { - const type = inspectTxType({ walletAddresses, tx: tx as unknown as Wallet.Cardano.HydratedTx }); - const transformedTx = txTransformer({ +}: TxHistoryTransformerInput): TransformedTx[] => + txTransformer({ tx, walletAddresses, fiatCurrency, @@ -127,17 +29,3 @@ export const pendingTxTransformer = ({ direction: TxDirections.Outgoing, date: capitalize(Wallet.TransactionStatus.PENDING) }); - - if (isDelegationWithRegistrationTx(transformedTx, type)) { - return splitDelegationWithRegistrationIntoTwoActions(transformedTx); - } - - if (isDelegationWithDeregistrationTx(transformedTx, type)) { - return splitDelegationWithDeregistrationIntoTwoActions(transformedTx); - } - - return { - ...transformedTx, - type: type as TransactionType - }; -}; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/tx-history-transformer.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/tx-history-transformer.ts index 3d6dd00fd..24192fc12 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/tx-history-transformer.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/tx-history-transformer.ts @@ -1,42 +1,14 @@ -/* eslint-disable complexity */ -/* eslint-disable no-magic-numbers */ -import BigNumber from 'bignumber.js'; import { Wallet } from '@lace/cardano'; -import { AssetActivityItemProps } from '@lace/core'; import dayjs from 'dayjs'; import { formatDate } from '@src/utils/format-date'; import { getTxDirection, inspectTxType } from '@src/utils/tx-inspection'; -import { getFormattedFiatAmount, txTransformer, TxTransformerInput } from './pending-tx-transformer'; -import { TxDirections } from '@types'; -import { - isDelegationWithDeregistrationTx, - isDelegationWithRegistrationTx, - splitDelegationWithDeregistrationIntoTwoActions, - splitDelegationWithRegistrationIntoTwoActions -} from './common-transformers'; +import { txTransformer, TxTransformerInput } from './common-tx-transformer'; +import type { TransformedTx } from './types'; interface TxHistoryTransformerInput extends Omit { tx: Wallet.Cardano.HydratedTx; } -/** - calculates the amount of rewards withdrawn by the wallet - - @param withdrawals the withdrawal list in the transaction - @param rewardAccount needed to verify if the withdrawal was made to the wallet stake address -*/ -export const getRewardsAmount = ( - withdrawals: Wallet.Cardano.Withdrawal[], - rewardAccounts: Wallet.Cardano.RewardAccount[] -): string => - withdrawals - ?.reduce( - (total, { quantity, stakeAddress }) => - rewardAccounts.includes(stakeAddress) ? total.plus(quantity.toString()) : total, - new BigNumber(0) - ) - ?.toString() || '0'; - export const txHistoryTransformer = ({ tx, walletAddresses, @@ -45,14 +17,12 @@ export const txHistoryTransformer = ({ time, protocolParameters, cardanoCoin -}: TxHistoryTransformerInput): - | Array> - | Omit => { +}: TxHistoryTransformerInput): TransformedTx[] => { const type = inspectTxType({ walletAddresses, tx }); const direction = getTxDirection({ type }); - const transformedTx = txTransformer({ - tx: tx as unknown as Wallet.TxInFlight, + return txTransformer({ + tx, walletAddresses, fiatCurrency, fiatPrice, @@ -60,54 +30,7 @@ export const txHistoryTransformer = ({ protocolParameters, cardanoCoin, status: Wallet.TransactionStatus.SUCCESS, - direction: direction as TxDirections, + direction, date: dayjs().isSame(time, 'day') ? 'Today' : formatDate({ date: time, format: 'DD MMMM YYYY', type: 'local' }) }); - - if (isDelegationWithRegistrationTx(transformedTx, type)) { - return splitDelegationWithRegistrationIntoTwoActions(transformedTx); - } - - if (isDelegationWithDeregistrationTx(transformedTx, type)) { - return splitDelegationWithDeregistrationIntoTwoActions(transformedTx); - } - - /* - whenever the wallet have withdrawn rewards, we will need need to create a new record Rewards and add it to the transaction history list - given this, we will keep the original send transaction type and we will add this new Rewards record - */ - // TODO: extract to common-transformers, apply in pending-tx-transformer - if (type === 'rewards' || type === 'self-rewards') { - const rewardsAmount = getRewardsAmount( - tx?.body?.withdrawals, - walletAddresses.map((addr) => addr.rewardAccount) - ); - // TODO: create a type for this and replace AssetActivityItemProps type in other place (JIRA: LW-5490) - return [ - { - ...transformedTx, - type: type === 'self-rewards' ? 'self' : 'outgoing' - }, - { - type: 'rewards', - direction: 'Incoming', - amount: Wallet.util.getFormattedAmount({ amount: rewardsAmount, cardanoCoin }), - fiatAmount: getFormattedFiatAmount({ - amount: new BigNumber(tx?.body?.withdrawals[0]?.quantity?.toString() || '0'), - fiatCurrency, - fiatPrice - }), - status: Wallet.TransactionStatus.SPENDABLE, - date: transformedTx.date, - assets: [], - timestamp: transformedTx.timestamp - } - ] as Array>; - } - - return { - ...transformedTx, - type, - direction - }; }; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/types.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/types.ts new file mode 100644 index 000000000..ce438b040 --- /dev/null +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/types.ts @@ -0,0 +1,3 @@ +import type { AssetActivityItemProps } from '@lace/core'; + +export type TransformedTx = Omit; diff --git a/packages/core/src/ui/components/Transactions/TransactionDetailBrowser.tsx b/packages/core/src/ui/components/Transactions/TransactionDetailBrowser.tsx index 45c0c1c29..fcdc1a7f4 100644 --- a/packages/core/src/ui/components/Transactions/TransactionDetailBrowser.tsx +++ b/packages/core/src/ui/components/Transactions/TransactionDetailBrowser.tsx @@ -2,7 +2,7 @@ import React from 'react'; import styles from './TransactionDetailBrowser.module.scss'; import { TransactionDetailHeaderBrowser } from './TransactionDetailHeaderBrowser'; import { TransactionStatus } from '../Activity/AssetActivityItem'; -import { RewardDetails, RewardDetailsProps } from './RewardDetails'; +import { RewardDetailsProps } from './RewardDetails'; import { Transaction, TransactionProps } from './Transaction'; import { TransactionType } from './TransactionType'; import { useTranslate } from '@src/ui/hooks'; @@ -53,6 +53,8 @@ export const TransactionDetailBrowser = ({ isPopupView }; + // temporarily unused as part of (LW-8315), rewards to be reintroduced with LW-8751 + /* const rewardProps: RewardDetailsProps = { name, status, @@ -62,12 +64,15 @@ export const TransactionDetailBrowser = ({ coinSymbol, rewards }; + */ return (
{status === TransactionStatus.SPENDABLE ? ( - + // temporarily unused as part of (LW-8315), rewards to be reintroduced with LW-8751 + // + <> ) : ( )}