Skip to content
Open
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
31 changes: 28 additions & 3 deletions components/Coin/CoinEarnBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ const getEarnOffer = (token: TokenBalance, tokens: TokenBalance[]): EarnOffer |
}
};

/** The idle cash to the cent, kept in token units: `$18.98`, or `<$0.01` under a cent. */
const formatIdleCash = (amount: TokenUnits, currency: string) =>
amount.raw * 100n < 10n ** BigInt(amount.decimals)
? `<${currency}0.01`
: `${currency}${formatTokenUnits(amount, 2, 2)}`;

/** The idle ETH kept short and in token units, the way `formatTokenAmount` shortens a number. */
const formatIdleEth = (amount: TokenUnits) => {
const one = 10n ** BigInt(amount.decimals);
if (amount.raw >= 1000n * one) return formatTokenUnits(amount, 2, 2);
if (amount.raw >= one) return formatTokenUnits(amount, 4, 2);
// About four significant digits: the leading zeros after the point, plus three.
const leadingPlaces = amount.decimals - amount.raw.toString().length + 1;
return formatTokenUnits(amount, Math.min(8, leadingPlaces + 3));
};

/** Figma 31247:16253 and 31247:16245. */
const EarnBannerRow = ({ vault, symbol, balance, balanceUsd }: EarnOffer) => {
const { width } = useWindowDimensions();
Expand All @@ -91,12 +107,17 @@ const EarnBannerRow = ({ vault, symbol, balance, balanceUsd }: EarnOffer) => {
? (balanceUsd * Number(apyText)) / 100 >= 1
: yearlyYield.raw >= 10n ** BigInt(yearlyYield.decimals);

// Someone who already holds this Yield product reads the idle amount itself.
const idleLabel = isEth
? `${formatIdleEth(balance)} ETH`
: formatIdleCash(balance, getVaultCurrencySymbol(vault.type));

let subtitle = `Put it in ${name}`;
if (savings) {
subtitle =
savings.balance > 0
? `Add it to your ${formatYieldAmount(savings.balance, vault.type)} in ${name}`
: `Add it to ${name}`;
? `Move it to ${name}, with your ${formatYieldAmount(savings.balance, vault.type)}`
: `Move it to ${name}`;
} else if (apyLabel && meetsEstimateMinimum) {
subtitle += isEth
? ` · ~${formatTokenAmount(Number(formatUnits(yearlyYield.raw, yearlyYield.decimals)))} ETH a year`
Expand Down Expand Up @@ -133,6 +154,10 @@ const EarnBannerRow = ({ vault, symbol, balance, balanceUsd }: EarnOffer) => {
</View>
<View className="min-w-0 flex-1 gap-0.5">
<Text
// Under 390pt the title is the only text, and it now carries an amount of any length.
numberOfLines={slim ? 1 : undefined}
adjustsFontSizeToFit={slim}
minimumFontScale={0.7}
className={
slim
? 'text-[14px] font-medium leading-[19px] text-brand'
Expand All @@ -145,7 +170,7 @@ const EarnBannerRow = ({ vault, symbol, balance, balanceUsd }: EarnOffer) => {
) : (
(apyLabel ?? 'yield')
)}
{savings ? ` on this ${symbol} too` : `${apyLabel ? ' a year' : ''} on your ${symbol}`}
{`${apyLabel ? ' a year' : ''} on ${savings ? `this ${idleLabel}` : `your ${symbol}`}`}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
</Text>
{!slim && <Text className="text-[14px] leading-5 text-white/[0.65]">{subtitle}</Text>}
</View>
Expand Down
61 changes: 59 additions & 2 deletions components/Coin/__tests__/CoinEarnBanner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import { arbitrum, base, fuse, mainnet } from 'viem/chains';

import CoinEarnBanner from '@/components/Coin/CoinEarnBanner';
import { useMaxAPY } from '@/hooks/useAnalytics';
import { useSavingsVaults } from '@/hooks/useSavingsVaults';
import { TokenBalance, TokenType, VaultType } from '@/lib/types';
import { getVaultDepositToken } from '@/lib/vaults';

const { act, create } = jest.requireActual('react-test-renderer');
jest.mock('expo-router', () => ({ router: { push: jest.fn() } }));
jest.mock('expo-image', () => ({ Image: 'Image' }));
let mockWidth = 419;
jest.mock('react-native/Libraries/Utilities/useWindowDimensions', () => ({
__esModule: true,
default: () => ({ width: 419 }),
default: () => ({ width: mockWidth }),
}));
jest.mock('@/hooks/useAnalytics', () => ({ useMaxAPY: jest.fn() }));
jest.mock('@/hooks/useSavingsVaults', () => ({ useSavingsVaults: () => ({ vaults: [] }) }));
jest.mock('@/hooks/useSavingsVaults', () => ({ useSavingsVaults: jest.fn() }));
jest.mock('@/components/ui/text', () => ({ Text: 'Text' }));
jest.mock('@/lib/utils', () => ({
cn: (...classes: string[]) => classes.filter(Boolean).join(' '),
Expand Down Expand Up @@ -51,13 +53,15 @@ const copy = () =>
.replace(/\s+/g, ' ');

beforeEach(() => {
(useSavingsVaults as jest.Mock).mockReturnValue({ vaults: [] });
(useMaxAPY as jest.Mock).mockImplementation((vault: VaultType) => ({
maxAPY: vault === VaultType.EUR ? 0 : vault === VaultType.ETH ? 2.9 : 4.5,
}));
});

afterEach(() => {
act(() => root.unmount());
mockWidth = 419;
jest.clearAllMocks();
});

Expand All @@ -73,6 +77,59 @@ it('projects the eligible USDC across networks and opens USD Yield', () => {
expect(router.push).toHaveBeenCalledWith({ pathname: '/savings', params: { vault: 'usdc' } });
});

it('names the idle amount and the USD Yield balance for someone who already holds USD Yield', () => {
(useSavingsVaults as jest.Mock).mockReturnValue({ vaults: [{ key: 'USD', balance: 36.17 }] });
render(heldToken('USDC', base.id, '18980000'));

expect(copy()).toContain('4.5%');
expect(copy()).toContain('a year on this $18.98');
expect(copy()).toContain('Move it to USD Yield, with your $36.17');
expect(copy()).not.toContain('too');
});

it('drops the balance from the subtitle once the USD Yield holding is empty', () => {
(useSavingsVaults as jest.Mock).mockReturnValue({ vaults: [{ key: 'USD', balance: 0 }] });
render(heldToken('USDC', base.id, '18980000'));

expect(copy()).toContain('a year on this $18.98');
expect(copy()).toMatch(/Move it to USD Yield$/);
});

it('shows the idle ETH in ETH, and under a cent of idle cash as less than a cent', () => {
(useSavingsVaults as jest.Mock).mockReturnValue({
vaults: [
{ key: 'ETH', balance: 1 },
{ key: 'USD', balance: 36.17 },
],
});
render(heldToken('ETH', mainnet.id, '150000000000000000'));
expect(copy()).toContain('a year on this 0.15 ETH');

act(() => root.unmount());
render(heldToken('ETH', mainnet.id, '1234567890123456789012'));
expect(copy()).toContain('a year on this 1,234.57 ETH');

act(() => root.unmount());
render(heldToken('ETH', mainnet.id, '9123456789012345'));
expect(copy()).toContain('a year on this 0.009123 ETH');

act(() => root.unmount());
render(heldToken('USDC', base.id, '9999'));
expect(copy()).toContain('a year on this <$0.01');
});

it('keeps the title on one line on a narrow phone, where it is the only text', () => {
mockWidth = 375;
(useSavingsVaults as jest.Mock).mockReturnValue({ vaults: [{ key: 'USD', balance: 36.17 }] });
render(heldToken('USDC', base.id, '12345670000'));

expect(copy()).toContain('a year on this $12,345.67');
expect(copy()).not.toContain('Move it to');
const title = root.root.findAllByType('Text')[0];
expect(title.props.numberOfLines).toBe(1);
expect(title.props.adjustsFontSizeToFit).toBe(true);
});

it('omits the offer when the wallet holds only an unsupported contract with the right ticker', () => {
render({ ...heldToken('USDT', mainnet.id, '500000000'), chainId: base.id });
expect(root.toJSON()).toBeNull();
Expand Down
Loading