diff --git a/components/Coin/CoinEarnBanner.tsx b/components/Coin/CoinEarnBanner.tsx index 0aef26a30..40a36af19 100644 --- a/components/Coin/CoinEarnBanner.tsx +++ b/components/Coin/CoinEarnBanner.tsx @@ -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(); @@ -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` @@ -133,6 +154,10 @@ 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}`}`} {!slim && {subtitle}} diff --git a/components/Coin/__tests__/CoinEarnBanner.test.tsx b/components/Coin/__tests__/CoinEarnBanner.test.tsx index b8d9381fd..a9758a0af 100644 --- a/components/Coin/__tests__/CoinEarnBanner.test.tsx +++ b/components/Coin/__tests__/CoinEarnBanner.test.tsx @@ -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(' '), @@ -51,6 +53,7 @@ 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, })); @@ -58,6 +61,7 @@ beforeEach(() => { afterEach(() => { act(() => root.unmount()); + mockWidth = 419; jest.clearAllMocks(); }); @@ -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();