diff --git a/apps/marginfi-v2-ui/src/components/AssetsList/AssetRow/AssetRow.tsx b/apps/marginfi-v2-ui/src/components/AssetsList/AssetRow/AssetRow.tsx index 86e918b019..04b9446add 100644 --- a/apps/marginfi-v2-ui/src/components/AssetsList/AssetRow/AssetRow.tsx +++ b/apps/marginfi-v2-ui/src/components/AssetsList/AssetRow/AssetRow.tsx @@ -31,7 +31,7 @@ import { useWalletContext } from "~/components/useWalletContext"; const CLOSE_BALANCE_TOAST_ID = "close-balance"; const BORROW_OR_LEND_TOAST_ID = "borrow-or-lend"; -const EMISSION_MINT_INFO_MAP = new Map([ +export const EMISSION_MINT_INFO_MAP = new Map([ [ "UXD", { diff --git a/apps/marginfi-v2-ui/src/components/Navbar/Navbar.tsx b/apps/marginfi-v2-ui/src/components/Navbar/Navbar.tsx index 0fe0a9a084..9ae52533e1 100644 --- a/apps/marginfi-v2-ui/src/components/Navbar/Navbar.tsx +++ b/apps/marginfi-v2-ui/src/components/Navbar/Navbar.tsx @@ -1,4 +1,4 @@ -import { FC, useEffect, useState } from "react"; +import { FC, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import Image from "next/image"; import AirdropZone from "./AirdropZone"; @@ -11,15 +11,20 @@ import { useRouter } from "next/router"; import { HotkeysEvent } from "react-hotkeys-hook/dist/types"; import { Badge } from "@mui/material"; import { useFirebaseAccount } from "../useFirebaseAccount"; -import { groupedNumberFormatterDyn, numeralFormatter } from "@mrgnlabs/mrgn-common"; +import { groupedNumberFormatterDyn, processTransaction } from "@mrgnlabs/mrgn-common"; import { useWalletContext } from "../useWalletContext"; import { Features, isActive } from "~/utils/featureGates"; +import { EMISSION_MINT_INFO_MAP } from "../AssetsList/AssetRow/AssetRow"; +import { PublicKey, Transaction } from "@solana/web3.js"; +import { useConnection } from "@solana/wallet-adapter-react"; +import { toast } from "react-toastify"; // @todo implement second pretty navbar row const Navbar: FC = () => { useFirebaseAccount(); - const { connected, walletAddress } = useWalletContext(); + const { connection } = useConnection(); + const { connected, walletAddress, wallet } = useWalletContext(); const router = useRouter(); const [accountSummary, selectedAccount, extendedBankInfos] = useMrgnlendStore((state) => [ state.accountSummary, @@ -37,6 +42,16 @@ const Navbar: FC = () => { const [isHotkeyMode, setIsHotkeyMode] = useState(false); const [currentRoute, setCurrentRoute] = useState(router.pathname); + const bankAddressesWithEmissions: PublicKey[] = useMemo(() => { + if (!selectedAccount) return []; + return [...EMISSION_MINT_INFO_MAP.keys()] + .map((bankMintSymbol) => { + const uxdBankInfo = extendedBankInfos?.find((b) => b.isActive && b.meta.tokenSymbol === bankMintSymbol); + return uxdBankInfo?.address; + }) + .filter((address) => address !== undefined) as PublicKey[]; + }, [selectedAccount, extendedBankInfos]); + useEffect(() => { if (!walletAddress) return; fetchPoints(walletAddress.toBase58()).catch(console.error); @@ -144,27 +159,29 @@ const Navbar: FC = () => { - {isActive(Features.STAKE) && - - stake - - } + + stake + + + )} {
{ - if (selectedAccount && extendedBankInfos?.find((b) => b.meta.tokenSymbol === "UXD")?.info.rawBank) { - selectedAccount!.withdrawEmissions( - extendedBankInfos.find((b) => b.meta.tokenSymbol === "UXD")!.address - ); - } - - if (selectedAccount && extendedBankInfos?.find((b) => b.meta.tokenSymbol === "bSOL")?.info.rawBank) { - selectedAccount!.withdrawEmissions( - extendedBankInfos.find((b) => b.meta.tokenSymbol === "bSOL")!.address - ); + className={`whitespace-nowrap hidden md:inline-flex ${ + bankAddressesWithEmissions.length > 0 ? "cursor-pointer hover:text-[#AAA]" : "cursor-not-allowed" + }`} + onClick={async () => { + if (!wallet || !selectedAccount || bankAddressesWithEmissions.length === 0) return; + const tx = new Transaction(); + const ixs = []; + const signers = []; + for (const bankAddress of bankAddressesWithEmissions) { + const ix = await selectedAccount.makeWithdrawEmissionsIx(bankAddress); + ixs.push(...ix.instructions); + signers.push(ix.keys); } + tx.add(...ixs); + await processTransaction(connection, wallet, tx); + toast.success("Withdrawal successful"); }} > withdraw all rewards + {bankAddressesWithEmissions.length > 0 && ( + + + + + )}