Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mfi-ui): actionbox values #981

Merged
merged 1 commit into from
Nov 27, 2024
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
3 changes: 3 additions & 0 deletions packages/mrgn-common/src/utils/formatters.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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";

import { LendingAction, BankSelect } from "./components";

type ActionInputProps = {
amountRaw: string;
amount: number | null;
nativeSolBalance: number;
walletAmount: number | undefined;
maxAmount: number;
Expand Down Expand Up @@ -38,6 +40,7 @@ export const ActionInput = ({
showTokenSelection,
showTokenSelectionGroups,
amountRaw,
amount,
selectedBank,
lendMode,
setAmountRaw,
Expand Down Expand Up @@ -85,7 +88,7 @@ export const ActionInput = ({
connected={connected}
/>
</div>
<div className="flex-auto">
<div className="flex-auto flex flex-col gap-0 items-end">
<Input
type="text"
ref={amountInputRef}
Expand All @@ -94,8 +97,13 @@ export const ActionInput = ({
disabled={isInputDisabled}
onChange={(e) => 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 && (
<span className="text-xs text-muted-foreground font-normal">
{tokenPriceFormatter(amount * selectedBank.info.oraclePrice.priceRealtime.price.toNumber())}
</span>
)}
</div>
</div>
<LendingAction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { ActionType, ExtendedBankInfo } from "@mrgnlabs/marginfi-v2-ui-state";
import { clampedNumeralFormatter } from "@mrgnlabs/mrgn-common";
import { dynamicNumeralFormatter } from "@mrgnlabs/mrgn-common";

type LendingActionProps = {
walletAmount: number | undefined;
Expand Down Expand Up @@ -35,7 +35,7 @@ export const LendingAction = ({
}

const formatAmount = (amount?: number, symbol?: string) =>
amount !== undefined ? `${clampedNumeralFormatter(amount)} ${symbol}` : "-";
amount !== undefined ? `${dynamicNumeralFormatter(amount)} ${symbol}` : "-";

switch (lendMode) {
case ActionType.Deposit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ export const LendBox = ({
nativeSolBalance={nativeSolBalance}
walletAmount={walletAmount}
amountRaw={amountRaw}
amount={debouncedAmount}
maxAmount={maxAmount}
connected={connected}
selectedBank={selectedBank}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: ",
Expand All @@ -43,7 +43,7 @@ export const RepayAction = ({
<ul className="flex flex-col gap-0.5 mt-4 text-xs w-full text-muted-foreground">
<li className="flex justify-between items-center gap-1.5">
<strong className="mr-auto">{maxLabel.label}</strong>
<div className="flex space-x-1">
<div className="flex space-x-1 items-center">
{selectedBank?.isActive && !isUnchanged && (
<div>{clampedNumeralFormatter(selectedBank.position.amount)}</div>
)}
Expand All @@ -64,11 +64,11 @@ export const RepayAction = ({
<strong>Deposited:</strong>

<div className="flex space-x-1.5 items-center">
{selectedSecondaryBank?.isActive ? selectedSecondaryBank.position.amount : 0}
{selectedSecondaryBank?.isActive ? dynamicNumeralFormatter(selectedSecondaryBank.position.amount) : 0}
{selectedSecondaryBank?.isActive && !isUnchanged && <IconArrowRight width={12} height={12} />}
{selectedSecondaryBank?.isActive &&
!isUnchanged &&
selectedSecondaryBank.position.amount - Number(amountRaw)}{" "}
dynamicNumeralFormatter(selectedSecondaryBank.position.amount - Number(amountRaw))}{" "}
{selectedSecondaryBank?.meta.tokenSymbol}
</div>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cn } from "@mrgnlabs/mrgn-utils";
import { ActionStatItem } from "~/components/action-box-v2/components";
import {
getAmountStat,
getAmountUsdStat,
getHealthStat,
getLiquidationStat,
getPoolSizeStat,
Expand Down Expand Up @@ -65,6 +66,14 @@ function generateRepayCollatStats(summary: ActionSummary, bank: ExtendedBankInfo
summary.simulationPreview?.positionAmount
)
);
stats.push(
getAmountUsdStat(
summary.actionPreview.positionAmount,
bank.meta.tokenSymbol,
bank.info.oraclePrice.priceRealtime.price.toNumber(),
summary.simulationPreview?.positionAmount
)
);
if (summary.actionPreview.priceImpactPct) stats.push(getPriceImpactStat(summary.actionPreview.priceImpactPct));
if (summary.actionPreview.slippageBps) stats.push(getSlippageStat(summary.actionPreview.slippageBps));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { getPriceWithConfidence } from "@mrgnlabs/marginfi-client-v2";
import { ActiveBankInfo, ExtendedBankInfo } from "@mrgnlabs/marginfi-v2-ui-state";
import {
clampedNumeralFormatter,
dynamicNumeralFormatter,
numeralFormatter,
percentFormatter,
percentFormatterDyn,
tokenPriceFormatter,
usdFormatter,
usdFormatterDyn,
} from "@mrgnlabs/mrgn-common";
import { cn } from "@mrgnlabs/mrgn-utils";

Expand Down Expand Up @@ -36,9 +39,27 @@ export function getAmountStat(currentAmount: number, symbol: string, simulatedAm
label: "Your amount",
value: () => (
<>
{clampedNumeralFormatter(currentAmount)} {symbol}
{dynamicNumeralFormatter(currentAmount)} {symbol}
{simulatedAmount !== undefined ? <IconArrowRight width={12} height={12} /> : <></>}
{simulatedAmount !== undefined ? clampedNumeralFormatter(simulatedAmount) + " " + symbol : <></>}
{simulatedAmount !== undefined ? dynamicNumeralFormatter(simulatedAmount) + " " + symbol : <></>}
</>
),
};
}

export function getAmountUsdStat(
currentAmount: number,
symbol: string,
price: number,
simulatedAmount?: number
): PreviewStat {
return {
label: "USD Value",
value: () => (
<>
{tokenPriceFormatter(currentAmount)}
{simulatedAmount !== undefined ? <IconArrowRight width={12} height={12} /> : <></>}
{simulatedAmount !== undefined ? tokenPriceFormatter(simulatedAmount * price) : <></>}
</>
),
};
Expand Down