From b1a8b22af2f231f3c960356ea4ce8c733c32ad7d Mon Sep 17 00:00:00 2001 From: chambaz Date: Wed, 27 Nov 2024 11:29:57 -0500 Subject: [PATCH] feat: add usd value to actionbox / improve numeral formatting --- .../mrgn-common/src/utils/formatters.utils.ts | 3 +++ .../components/action-input/action-input.tsx | 12 +++++++-- .../lending-action/lending-action.tsx | 4 +-- .../lend-box/components/preview/preview.tsx | 7 ++++++ .../actions/lend-box/lend-box.tsx | 1 + .../components/repay-action/repay-action.tsx | 10 ++++---- .../components/preview/preview.tsx | 9 +++++++ .../utils/stats-preview.utils.tsx | 25 +++++++++++++++++-- 8 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/mrgn-common/src/utils/formatters.utils.ts b/packages/mrgn-common/src/utils/formatters.utils.ts index 9950d657ed..d21ef3eaf5 100644 --- a/packages/mrgn-common/src/utils/formatters.utils.ts +++ b/packages/mrgn-common/src/utils/formatters.utils.ts @@ -105,6 +105,9 @@ const clampedNumeralFormatter = (value: number) => { }; const tokenPriceFormatter = (price: number, style: "currency" | "decimal" = "currency") => { + if (price === 0) { + return 0; + } const reformatNum = Number(price.toFixed(20)); if (reformatNum < 0.00000001) { diff --git a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/action-input/action-input.tsx b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/action-input/action-input.tsx index 844864766a..740f0f9521 100644 --- a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/action-input/action-input.tsx +++ b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/action-input/action-input.tsx @@ -2,6 +2,7 @@ import React from "react"; import { ActionType, ExtendedBankInfo } from "@mrgnlabs/marginfi-v2-ui-state"; import { formatAmount } from "@mrgnlabs/mrgn-utils"; +import { usdFormatter, tokenPriceFormatter } from "@mrgnlabs/mrgn-common"; import { Input } from "~/components/ui/input"; @@ -9,6 +10,7 @@ import { LendingAction, BankSelect } from "./components"; type ActionInputProps = { amountRaw: string; + amount: number | null; nativeSolBalance: number; walletAmount: number | undefined; maxAmount: number; @@ -38,6 +40,7 @@ export const ActionInput = ({ showTokenSelection, showTokenSelectionGroups, amountRaw, + amount, selectedBank, lendMode, setAmountRaw, @@ -85,7 +88,7 @@ export const ActionInput = ({ connected={connected} /> -
+
handleInputChange(e.target.value)} placeholder="0" - className="bg-transparent shadow-none min-w-[130px] text-right outline-none focus-visible:outline-none focus-visible:ring-0 border-none text-base font-medium" + className="bg-transparent shadow-none min-w-[130px] text-right h-auto py-0 pr-0 outline-none focus-visible:outline-none focus-visible:ring-0 border-none text-base font-medium" /> + {amount !== null && amount > 0 && selectedBank && ( + + {tokenPriceFormatter(amount * selectedBank.info.oraclePrice.priceRealtime.price.toNumber())} + + )}
- amount !== undefined ? `${clampedNumeralFormatter(amount)} ${symbol}` : "-"; + amount !== undefined ? `${dynamicNumeralFormatter(amount)} ${symbol}` : "-"; switch (lendMode) { case ActionType.Deposit: diff --git a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/preview/preview.tsx b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/preview/preview.tsx index c2076086ed..7ae0145aed 100644 --- a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/preview/preview.tsx +++ b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/components/preview/preview.tsx @@ -7,6 +7,7 @@ import { ActionStatItem } from "~/components/action-box-v2/components"; import type { HidePoolStats } from "~/components/action-box-v2"; import { getAmountStat, + getAmountUsdStat, getHealthStat, getLiquidationStat, getPoolSizeStat, @@ -78,6 +79,12 @@ function generateLendingStats( summary.actionPreview.positionAmount, bank.meta.tokenSymbol, summary.simulationPreview?.positionAmount + ), + getAmountUsdStat( + summary.actionPreview.positionAmount, + bank.meta.tokenSymbol, + bank.info.oraclePrice.priceRealtime.price.toNumber(), + summary.simulationPreview?.positionAmount ) ); } diff --git a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/lend-box.tsx b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/lend-box.tsx index 6adf36eab4..a685ef6122 100644 --- a/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/lend-box.tsx +++ b/packages/mrgn-ui/src/components/action-box-v2/actions/lend-box/lend-box.tsx @@ -476,6 +476,7 @@ export const LendBox = ({ nativeSolBalance={nativeSolBalance} walletAmount={walletAmount} amountRaw={amountRaw} + amount={debouncedAmount} maxAmount={maxAmount} connected={connected} selectedBank={selectedBank} diff --git a/packages/mrgn-ui/src/components/action-box-v2/actions/repay-collat-box/components/action-input/components/repay-action/repay-action.tsx b/packages/mrgn-ui/src/components/action-box-v2/actions/repay-collat-box/components/action-input/components/repay-action/repay-action.tsx index 50b150445d..cbb932eb61 100644 --- a/packages/mrgn-ui/src/components/action-box-v2/actions/repay-collat-box/components/action-input/components/repay-action/repay-action.tsx +++ b/packages/mrgn-ui/src/components/action-box-v2/actions/repay-collat-box/components/action-input/components/repay-action/repay-action.tsx @@ -2,7 +2,7 @@ import React from "react"; import { IconArrowRight } from "@tabler/icons-react"; import { ExtendedBankInfo } from "@mrgnlabs/marginfi-v2-ui-state"; -import { clampedNumeralFormatter, numeralFormatter } from "@mrgnlabs/mrgn-common"; +import { clampedNumeralFormatter, dynamicNumeralFormatter } from "@mrgnlabs/mrgn-common"; type RepayActionProps = { amountRaw: string; @@ -28,7 +28,7 @@ export const RepayAction = ({ showWalletIcon?: boolean; label?: string; } => { - const amountLeft = numeralFormatter(selectedBank?.isActive ? selectedBank.position.amount - repayAmount : 0); + const amountLeft = dynamicNumeralFormatter(selectedBank?.isActive ? selectedBank.position.amount - repayAmount : 0); return { amount: `${amountLeft} ${selectedBank?.meta.tokenSymbol}`, label: "Borrowed: ", @@ -43,7 +43,7 @@ export const RepayAction = ({