Skip to content
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
16 changes: 16 additions & 0 deletions app/components/UI/Money/constants/activityStyles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,21 @@ describe('activityStyles', () => {
),
).toBe(false);
});

it('returns true for a Perps/Predict withdraw landing in the Money account', () => {
expect(
isIncomingMoneyTransactionMeta(
makeTx(TransactionType.batch, {
nestedTransactions: [
{ type: TransactionType.predictWithdraw } as TransactionMeta,
],
metamaskPay: {
tokenAddress: MUSD_TOKEN_ADDRESS,
chainId: MOCK_CHAIN,
},
}),
),
).toBe(true);
});
});
});
6 changes: 6 additions & 0 deletions app/components/UI/Money/constants/activityStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MUSD_DECIMALS,
MUSD_TOKEN,
} from '../../Earn/constants/musd';
import { isPerpsPredictMoneyWithdraw } from '../utils/moneyTransactionGuards';

function formatNumber(num: number): string {
return getIntlNumberFormatter(I18n.locale, {
Expand Down Expand Up @@ -185,6 +186,10 @@ export function getMusdDisplayAmountFromTransactionMeta(
}

export function isIncomingMoneyTransactionMeta(tx: TransactionMeta): boolean {
if (isPerpsPredictMoneyWithdraw(tx)) {
return true;
}

const t = tx.type;
if (
t === EvmTransactionType.incoming ||
Expand All @@ -194,6 +199,7 @@ export function isIncomingMoneyTransactionMeta(tx: TransactionMeta): boolean {
) {
return true;
}

// EIP-7702 batch deposits: moneyAccountDeposit sits in nestedTransactions
return (
tx.nestedTransactions?.some(
Expand Down
52 changes: 52 additions & 0 deletions app/components/UI/Money/constants/moneyActivityFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {

const MOCK_CHAIN: Hex = CHAIN_IDS.MONAD;
const OTHER_ERC20: Hex = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
// A MetaMask Pay token of mUSD on Monad marks a Perps/Predict ↔ Money transfer.
const MUSD_ON_MONAD = { tokenAddress: MUSD_TOKEN_ADDRESS, chainId: MOCK_CHAIN };

function tx(overrides: Partial<TransactionMeta>): TransactionMeta {
return {
Expand Down Expand Up @@ -103,6 +105,31 @@ describe('moneyActivityFilters', () => {
),
).toBe(false);
});

it('returns true for a Perps/Predict withdraw landing in the Money account (inflow)', () => {
expect(
isMoneyActivityDeposit(
tx({
type: TransactionType.batch,
nestedTransactions: [
{ type: TransactionType.predictWithdraw } as TransactionMeta,
],
metamaskPay: MUSD_ON_MONAD,
}),
),
).toBe(true);
});

it('returns false for a Perps/Predict deposit (outflow, not a deposit into Money)', () => {
expect(
isMoneyActivityDeposit(
tx({
type: TransactionType.perpsDeposit,
metamaskPay: MUSD_ON_MONAD,
}),
),
).toBe(false);
});
});

describe('isMoneyActivityTransfer', () => {
Expand Down Expand Up @@ -148,6 +175,31 @@ describe('moneyActivityFilters', () => {
),
).toBe(false);
});

it('returns true for a Perps/Predict deposit funded from the Money account (outflow)', () => {
expect(
isMoneyActivityTransfer(
tx({
type: TransactionType.perpsDeposit,
metamaskPay: MUSD_ON_MONAD,
}),
),
).toBe(true);
});

it('returns false for a Perps/Predict withdraw (inflow, bucketed as a deposit)', () => {
expect(
isMoneyActivityTransfer(
tx({
type: TransactionType.batch,
nestedTransactions: [
{ type: TransactionType.predictWithdraw } as TransactionMeta,
],
metamaskPay: MUSD_ON_MONAD,
}),
),
).toBe(false);
});
});

describe('isMusdErc20Transfer', () => {
Expand Down
11 changes: 9 additions & 2 deletions app/components/UI/Money/constants/moneyActivityFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
TransactionType,
} from '@metamask/transaction-controller';
import { isMusdOnMoneyAccountChain } from '../../Earn/constants/musd';
import {
isPerpsPredictMoneyDeposit,
isPerpsPredictMoneyWithdraw,
} from '../utils/moneyTransactionGuards';

const ERC20_TRANSFER_TYPES: TransactionType[] = [
TransactionType.tokenMethodTransfer,
Expand Down Expand Up @@ -32,10 +36,12 @@ export function isMoneyActivityDeposit(tx: TransactionMeta): boolean {
if (
t === TransactionType.incoming ||
t === TransactionType.moneyAccountDeposit ||
isMusdErc20Transfer(tx)
isMusdErc20Transfer(tx) ||
isPerpsPredictMoneyWithdraw(tx)
) {
return true;
}

// EIP-7702 batch deposits: moneyAccountDeposit sits in nestedTransactions
return (
tx.nestedTransactions?.some(
Expand All @@ -48,7 +54,8 @@ export function isMoneyActivityTransfer(tx: TransactionMeta): boolean {
const t = tx.type;
if (
t === TransactionType.moneyAccountWithdraw ||
t === TransactionType.simpleSend
t === TransactionType.simpleSend ||
isPerpsPredictMoneyDeposit(tx)
) {
return true;
}
Expand Down
48 changes: 48 additions & 0 deletions app/components/UI/Money/hooks/useMoneyAccountTransactions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,54 @@ describe('useMoneyAccountTransactions', () => {
expect(result.current.allTransactions).toHaveLength(0);
});

const MUSD_ON_MONAD = {
tokenAddress: MUSD_TOKEN_ADDRESS,
chainId: CHAIN_IDS.MONAD,
};

it('includes a Perps deposit funded from the Money account as a transfer (outflow)', () => {
const tx = makeTx(TransactionType.perpsDeposit, {
metamaskPay: MUSD_ON_MONAD as TransactionMeta['metamaskPay'],
});
const { result } = renderHookWithProvider(
() => useMoneyAccountTransactions(),
{ state: engineState({ moneyActivityMockDataEnabled: false }, [tx]) },
);
expect(result.current.allTransactions).toHaveLength(1);
expect(result.current.transfers).toHaveLength(1);
expect(result.current.deposits).toHaveLength(0);
});

it('includes a Predict withdraw landing in the Money account as a deposit (inflow)', () => {
const tx = makeTx(TransactionType.batch, {
nestedTransactions: [
{ type: TransactionType.predictWithdraw } as TransactionMeta,
],
metamaskPay: MUSD_ON_MONAD as TransactionMeta['metamaskPay'],
});
const { result } = renderHookWithProvider(
() => useMoneyAccountTransactions(),
{ state: engineState({ moneyActivityMockDataEnabled: false }, [tx]) },
);
expect(result.current.allTransactions).toHaveLength(1);
expect(result.current.deposits).toHaveLength(1);
expect(result.current.transfers).toHaveLength(0);
});

it('excludes a Perps deposit not funded from the Money account', () => {
const tx = makeTx(TransactionType.perpsDeposit, {
metamaskPay: {
tokenAddress: OTHER_ERC20,
chainId: CHAIN_IDS.ARBITRUM,
} as TransactionMeta['metamaskPay'],
});
const { result } = renderHookWithProvider(
() => useMoneyAccountTransactions(),
{ state: engineState({ moneyActivityMockDataEnabled: false }, [tx]) },
);
expect(result.current.allTransactions).toHaveLength(0);
});

it('includes inbound mUSD landing at the money account', () => {
const tx = makeTx(TransactionType.incoming, {
txParams: { from: OTHER_ADDRESS, to: MONEY_ADDRESS } as never,
Expand Down
8 changes: 8 additions & 0 deletions app/components/UI/Money/hooks/useMoneyAccountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ERC20_TRANSFER_FROM_CALLDATA_LENGTH,
} from '../constants/activityStyles';
import { getMoneyActivityStatus } from '../utils/classifyMoneyActivity';
import { isPerpsPredictMoneyActivity } from '../utils/moneyTransactionGuards';

// Statuses that should surface in money activity. `unapproved`/`approved`/
// `signed` are mid-compose and shouldn't appear yet; `rejected`/`dropped`/
Expand Down Expand Up @@ -137,6 +138,7 @@ export function useMoneyAccountTransactions(): UseMoneyAccountTransactionsResult
) {
return isMoneyAccountTxVisible(tx);
}

// EIP-7702 batch where a Money account call is a nested call.
if (
tx.nestedTransactions?.some(
Expand All @@ -147,6 +149,12 @@ export function useMoneyAccountTransactions(): UseMoneyAccountTransactionsResult
) {
return isMoneyAccountTxVisible(tx);
}

// Perps/Predict ↔ Money account transfers
if (isPerpsPredictMoneyActivity(tx)) {
return isMoneyAccountTxVisible(tx);
}

if (moneyAddress === undefined) return false;
// The inbound-mUSD and locally-signed mUSD branches below must skip
// mid-compose and explicitly-aborted rows, which would otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,3 +976,98 @@ describe('useMoneyTransactionDisplayInfo — per-kind subtitles', () => {
expect(result.current.description).toBe('mUSD');
});
});

// ---------------------------------------------------------------------------
// Perps/Predict ↔ Money transfers (matched via the mUSD pay token)
// ---------------------------------------------------------------------------

describe('useMoneyTransactionDisplayInfo — Perps/Predict ↔ Money', () => {
const MONAD: Hex = '0x8f';
const payOnMonad = (extra: Record<string, string>) => ({
tokenAddress: MUSD_TOKEN_ADDRESS,
chainId: MONAD,
...extra,
});

it('renders a money-funded Perps deposit as an outflow ("Sent")', () => {
// Outflow → the debit from the account, including the bridge fee = totalFiat.
const tx = makeTx(TransactionType.perpsDeposit, {
metamaskPay: payOnMonad({ totalFiat: '0.67157', targetFiat: '0.64' }),
});
const { result } = renderHookWithProvider(
() => useMoneyTransactionDisplayInfo(tx, undefined),
{ state: makeState({ currentCurrency: 'usd' }) },
);

expect(result.current.label).toBe('money.transaction.sent');
expect(result.current.icon).toBe(IconName.Arrow2UpRight);
expect(result.current.description).toBe(
'transaction_details.label.perps_account',
);
expect(result.current.isIncoming).toBe(false);
expect(result.current.primaryAmount).toBe('-0.67 mUSD');
expect(result.current.fiatAmount).toBe('-$0.67');
});

it('renders a Predict withdraw into the Money account as an inflow ("Deposited")', () => {
// Inflow → the net amount that lands = targetFiat. Withdraw sits in the
// EIP-7702 batch's nested calls.
const tx = makeTx(TransactionType.batch, {
nestedTransactions: [{ type: TransactionType.predictWithdraw }],
metamaskPay: payOnMonad({
totalFiat: '0.158879',
targetFiat: '0.099965',
}),
});
const { result } = renderHookWithProvider(
() => useMoneyTransactionDisplayInfo(tx, undefined),
{ state: makeState({ currentCurrency: 'usd' }) },
);

expect(result.current.label).toBe('money.transaction.deposited');
expect(result.current.icon).toBe(IconName.Add);
expect(result.current.description).toBe(
'transaction_details.label.predictions_account',
);
expect(result.current.isIncoming).toBe(true);
expect(result.current.primaryAmount).toBe('+0.10 mUSD');
expect(result.current.fiatAmount).toBe('+$0.10');
});

it('shows signed-zero (not the real amount) for a failed money-funded Perps deposit', () => {
// Failed rows must show signed-zero like every other kind; the perps amount
// block must not clobber it.
const tx = makeTx(TransactionType.perpsDeposit, {
status: TransactionStatus.failed,
metamaskPay: payOnMonad({ totalFiat: '0.67157', targetFiat: '0.64' }),
});
const { result } = renderHookWithProvider(
() => useMoneyTransactionDisplayInfo(tx, undefined),
{ state: makeState({ currentCurrency: 'usd' }) },
);

expect(result.current.label).toBe('money.transaction.send_failed');
expect(result.current.isIncoming).toBe(false);
expect(result.current.primaryAmount).toBe('-0.00 mUSD');
expect(result.current.fiatAmount).toBe('-$0.00');
});

it('shows signed-zero for a failed Predict withdraw into the Money account', () => {
const tx = makeTx(TransactionType.batch, {
status: TransactionStatus.failed,
nestedTransactions: [{ type: TransactionType.predictWithdraw }],
metamaskPay: payOnMonad({
totalFiat: '0.158879',
targetFiat: '0.099965',
}),
});
const { result } = renderHookWithProvider(
() => useMoneyTransactionDisplayInfo(tx, undefined),
{ state: makeState({ currentCurrency: 'usd' }) },
);

expect(result.current.isIncoming).toBe(true);
expect(result.current.primaryAmount).toBe('+0.00 mUSD');
expect(result.current.fiatAmount).toBe('+$0.00');
});
});
38 changes: 37 additions & 1 deletion app/components/UI/Money/hooks/useMoneyTransactionDisplayInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import {
MUSD_TOKEN,
} from '../../Earn/constants/musd';
import { MONEY_WITHDRAW_TOKEN_SYMBOL } from '../constants/moneyTokens';
import { isMoneyWithdrawTx } from '../utils/moneyTransactionGuards';
import {
isMoneyWithdrawTx,
isPerpsPredictMoneyActivity,
isPerpsPredictMoneyWithdraw,
perpsPredictServiceFamily,
} from '../utils/moneyTransactionGuards';
import type { MoneyActivityTransactionMeta } from '../constants/mockActivityData';
import {
classifyMoneyActivity,
Expand Down Expand Up @@ -111,6 +116,17 @@ function deriveSubtitle(
if (explicitSubtitle) {
return explicitSubtitle;
}

// Perps/Predict ↔ Money transfers (either direction) name the service account
// instead of a token pair / sender.
const serviceFamily = perpsPredictServiceFamily(tx);
if (serviceFamily === 'perps') {
return strings('transaction_details.label.perps_account');
}
if (serviceFamily === 'predict') {
return strings('transaction_details.label.predictions_account');
}

switch (kind) {
case 'converted':
return sourceTokenSymbol
Expand Down Expand Up @@ -244,6 +260,26 @@ export function useMoneyTransactionDisplayInfo(
}
}

// Perps/Predict ↔ Money transfers carry no `requiredAssets` and aren't token
// transfers, so neither amount path above resolves. Skip when failed so the
// signed-zero amount set above is preserved (as for every other failed row).
if (status !== 'failed' && isPerpsPredictMoneyActivity(tx)) {
const fiatStr = isPerpsPredictMoneyWithdraw(tx)
? tx.metamaskPay?.targetFiat
: tx.metamaskPay?.totalFiat;
const fiat = Number(fiatStr);
if (!isNaN(fiat) && fiat > 0) {
const amount = new BigNumber(fiat);
primaryAmount = formatMusdAmount(amount, isIncoming);
if (currentCurrency) {
fiatAmount = `${isIncoming ? '+' : '-'}${moneyFormatFiat(
amount,
currentCurrency,
)}`;
}
}
}

return {
label: moneyActivityLabel(kind, status),
description: deriveSubtitle(
Expand Down
Loading
Loading