diff --git a/components/Card/RainSunset/RainClosingWithdraw.tsx b/components/Card/RainSunset/RainClosingWithdraw.tsx index de3612516..56a8a41e8 100644 --- a/components/Card/RainSunset/RainClosingWithdraw.tsx +++ b/components/Card/RainSunset/RainClosingWithdraw.tsx @@ -11,7 +11,6 @@ import Toast from 'react-native-toast-message'; import { Image } from 'expo-image'; import { useRouter } from 'expo-router'; import { useQueryClient } from '@tanstack/react-query'; -import { formatUnits } from 'viem'; import PageLayout from '@/components/PageLayout'; import RenderTokenIcon from '@/components/RenderTokenIcon'; @@ -29,9 +28,11 @@ import { showFailureSheet } from '@/lib/errors'; import getTokenIcon from '@/lib/getTokenIcon'; import { isCancelledByUser } from '@/lib/utils/passkeyErrors'; import { + closingWithdrawalPlan, floorCents, formatClosingBalance, formatClosingDate, + formatTokenUnits, remainderCents, tokenWithdrawal, unitsToCents, @@ -76,6 +77,11 @@ type Phase = * reason to leave anything on it. Each token is its own collateral withdrawal — * today's `useWithdrawRainCollateral`, one `withdrawAsset` per token — so a card * holding two tokens asks for the passkey twice, and the screen says so. + * + * The amounts shown are what the withdrawal will move (`closingWithdrawalPlan`), + * never the card's whole collateral: the part of it set aside for card payments + * already made stays on the card, and the screen says how much that is. Showing + * the collateral put "Withdraw $11.14" on a card with $0.24 to withdraw. */ export function RainClosingWithdraw() { const router = useRouter(); @@ -93,7 +99,15 @@ export function RainClosingWithdraw() { ), [collateral], ); - const totalUsd = tokens.reduce((sum, token) => sum + token.balanceUsd, 0); + // What the withdrawal will move under the card's spending power, and what + // that leaves on the card; the rows, the total and the button all read this. + const plan = useMemo( + () => closingWithdrawalPlan(tokens, collateral?.spendingPowerUsd), + [tokens, collateral?.spendingPowerUsd], + ); + const movable = plan.rows.filter(row => row.units > 0n); + const totalUsd = Number(plan.movableCents) / 100; + const heldUsd = Number(plan.heldCents) / 100; const closesOn = formatClosingDate(sunset?.closesAt); // No answer at all is not an empty card: say so and offer to read it again. const unreadable = !collateral && isError; @@ -200,9 +214,10 @@ export function RainClosingWithdraw() { }; if (phase.kind === 'done' && phase.leftUsd > 0) { - // Rain capped the withdrawal below the card balance (it still holds some - // against pending payments). Say what moved and what did not, and lead - // back to the review, which shows what is still on the card. + // Rain capped the withdrawal below the card's collateral: the rest is set + // aside, nearly always for card payments already made. Say what moved and + // what did not, without promising the rest, and offer the review, which + // re-reads the card. return ( @@ -216,19 +231,20 @@ export function RainClosingWithdraw() { {formatClosingBalance(phase.usd)} is in your wallet - {formatClosingBalance(phase.leftUsd)} is still on your card, held for pending - payments. Withdraw it once they settle. + {formatClosingBalance(phase.leftUsd)} is still on your card. It is set aside, usually + for card payments you already made, and can’t be withdrawn. + - @@ -284,10 +300,18 @@ export function RainClosingWithdraw() { } const withdrawing = phase.kind === 'withdrawing'; + // One passkey confirmation per token that actually moves. const confirmations = - tokens.length > 1 - ? `You'll confirm with your passkey, once for each of the ${tokens.length} tokens.` + movable.length > 1 + ? `You'll confirm with your passkey, once for each of the ${movable.length} tokens.` : "You'll confirm with your passkey."; + // The collateral past the cap: named, so the total above is not read as an error. + const held = + heldUsd > 0 + ? movable.length > 0 + ? `Another ${formatClosingBalance(heldUsd)} on your card is set aside, usually for card payments you already made, and can’t be withdrawn.` + : `The ${formatClosingBalance(heldUsd)} on your card is set aside, usually for card payments you already made, and can’t be withdrawn.` + : null; const stays = closesOn ? ` Your card keeps working until ${closesOn.long}.` : ' Your card keeps working until it closes.'; @@ -318,29 +342,28 @@ export function RainClosingWithdraw() { {formatClosingBalance(totalUsd)} )} - All of it moves to your wallet + {movable.length > 0 || !held ? ( + All of it moves to your wallet + ) : null} - {tokens.length > 0 ? ( + {movable.length > 0 ? ( From your card - {tokens.map((token, index) => ( + {movable.map(({ token, index, units, cents }, position) => ( - {index > 0 ? : null} + {position > 0 ? : null} - + {token.symbol} - {Number( - formatUnits(BigInt(token.rawBalance || '0'), token.decimals), - ).toFixed(2)}{' '} - {token.symbol} + {formatTokenUnits(units, token.decimals)} {token.symbol} - {formatClosingBalance(token.balanceUsd)} + {formatClosingBalance(Number(cents) / 100)} {withdrawing && phase.step === index ? ( @@ -367,6 +390,8 @@ export function RainClosingWithdraw() { + {held ? {held} : null} + {confirmations} {stays} @@ -377,7 +402,7 @@ export function RainClosingWithdraw() {