diff --git a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBox.tsx b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBox.tsx index befe03660b..a45c4d98ee 100644 --- a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBox.tsx +++ b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBox.tsx @@ -45,13 +45,15 @@ export const ActionBox = () => { const maxAmount = React.useMemo(() => { switch (selectedMode) { case ActionType.Deposit: - return selectedToken?.userInfo.maxDeposit; + return selectedToken?.userInfo.maxDeposit ?? 0; case ActionType.Withdraw: - return selectedToken?.userInfo.maxWithdraw; + return selectedToken?.userInfo.maxWithdraw ?? 0; case ActionType.Borrow: - return selectedToken?.userInfo.maxBorrow; + return selectedToken?.userInfo.maxBorrow ?? 0; case ActionType.Repay: - return selectedToken?.userInfo.maxRepay; + return selectedToken?.userInfo.maxRepay ?? 0; + default: + return 0; } }, [selectedToken, selectedMode]); const isInputDisabled = React.useMemo(() => maxAmount === 0 && !showCloseBalance, [maxAmount, showCloseBalance]); @@ -65,18 +67,19 @@ export const ActionBox = () => { const hasActivePosition = React.useMemo( () => selectedToken?.isActive && - ((!selectedToken.isLending && lendingMode === LendingModes.LEND) || - (selectedToken.isLending && lendingMode === LendingModes.BORROW)), + ((!selectedToken.position.isLending && lendingMode === LendingModes.LEND) || + (selectedToken.position.isLending && lendingMode === LendingModes.BORROW)), [selectedToken, lendingMode] ); React.useEffect(() => { + setAmount(0); if (lendingMode === LendingModes.LEND) { setSelectMode(ActionType.Deposit); } else if (lendingMode === LendingModes.BORROW) { setSelectMode(ActionType.Borrow); } - }, [lendingMode, setSelectMode, selectedToken]); + }, [lendingMode, setSelectMode, setAmount, selectedToken]); React.useEffect(() => { if (!selectedToken || !amount) { @@ -108,7 +111,7 @@ export const ActionBox = () => { if (!selectedToken || !amountInputRef.current) return; setAmount(0); amountInputRef.current.focus(); - }, [selectedToken]); + }, [selectedToken, setAmount]); const executeLendingActionCb = React.useCallback( async ({ @@ -146,6 +149,9 @@ export const ActionBox = () => { const handleCloseBalance = React.useCallback(async () => { try { + if (!selectedToken || !selectedAccount) { + throw new Error(); + } await closeBalance({ marginfiAccount: selectedAccount, bank: selectedToken }); } catch (error) { return; @@ -164,6 +170,9 @@ export const ActionBox = () => { const handleLendingAction = React.useCallback(async () => { // TODO implement LST dialog + if (!selectedMode || !selectedToken || !selectedAccount || !amount) { + return; + } await executeLendingActionCb({ mfiClient, @@ -240,7 +249,7 @@ export const ActionBox = () => { - {(walletAmount > 0.01 ? numeralFormatter(walletAmount) : "< 0.01").concat( + {(walletAmount && walletAmount > 0.01 ? numeralFormatter(walletAmount) : "< 0.01").concat( " ", selectedToken?.meta.tokenSymbol )} @@ -265,9 +274,9 @@ export const ActionBox = () => { (showCloseBalance ? handleCloseBalance() : handleLendingAction())} /> {selectedToken !== null && amount !== null && preview.length > 0 && ( diff --git a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxActions.tsx b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxActions.tsx index ff19e707e1..a6b57396b7 100644 --- a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxActions.tsx +++ b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxActions.tsx @@ -7,7 +7,7 @@ import { useUiStore } from "~/store"; import { Button } from "~/components/ui/button"; type ActionBoxActionsProps = { - selectedMode: ActionType; + selectedMode?: ActionType; amount: number; maxAmount: number; showCloseBalance: boolean; diff --git a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxTokens.tsx b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxTokens.tsx index 588dd56265..c05179a481 100644 --- a/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxTokens.tsx +++ b/apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBoxTokens.tsx @@ -27,11 +27,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke state.extendedBankInfos, state.nativeSolBalance, ]); - const [lendingMode, selectedToken, setSelectedToken] = useUiStore((state) => [ - state.lendingMode, - state.selectedToken, - state.setSelectedToken, - ]); + const [lendingMode] = useUiStore((state) => [state.lendingMode]); const [searchQuery, setSearchQuery] = React.useState(""); const [isTokenPopoverOpen, setIsTokenPopoverOpen] = React.useState(false); @@ -62,7 +58,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke return extendedBankInfos .filter((bankInfo) => bankInfo.meta.tokenSymbol.toLowerCase().includes(lowerCaseSearchQuery)) .filter((bankInfo) => bankInfo.isActive && bankInfo.position?.isLending) - .sort((a, b) => b.userInfo.position?.amount - a.userInfo.position?.amount); + .sort((a, b) => (b.isActive ? b?.position?.amount : 0) - (a.isActive ? a?.position?.amount : 0)); }, [extendedBankInfos, searchQuery]); const filteredBanksActiveBorrowing = React.useMemo(() => { @@ -70,7 +66,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke return extendedBankInfos .filter((bankInfo) => bankInfo.meta.tokenSymbol.toLowerCase().includes(lowerCaseSearchQuery)) .filter((bankInfo) => bankInfo.isActive && !bankInfo.position?.isLending) - .sort((a, b) => b.userInfo.position?.amount - a.userInfo.position?.amount); + .sort((a, b) => (b.isActive ? b?.position?.amount : 0) - (a.isActive ? a?.position?.amount : 0)); }, [extendedBankInfos, searchQuery]); const filteredBanksUserOwns = React.useMemo(() => { @@ -131,7 +127,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke lendingMode === LendingModes.BORROW && "text-error" )} > - {calculateRate(selectedToken) + ` ${lendingMode === LendingModes.LEND ? "APY" : "APR"}`} + {calculateRate(currentToken) + ` ${lendingMode === LendingModes.LEND ? "APY" : "APR"}`}

@@ -150,9 +146,9 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke - setSelectedToken(extendedBankInfos.find((bank) => bank.address.toString() === value) || selectedToken) + setCurrentToken(extendedBankInfos.find((bank) => bank.address.toString() === value) || currentToken) } >