diff --git a/apps/marginfi-v2-trading/src/components/common/ActionBox/ActionBox.tsx b/apps/marginfi-v2-trading/src/components/common/ActionBox/ActionBox.tsx index 0af2480272..6ce0edcfd8 100644 --- a/apps/marginfi-v2-trading/src/components/common/ActionBox/ActionBox.tsx +++ b/apps/marginfi-v2-trading/src/components/common/ActionBox/ActionBox.tsx @@ -61,16 +61,16 @@ export const ActionBox = ({ isTokenSelectable, handleCloseDialog, }: ActionBoxProps) => { - const [isInitialized, setIsRefreshingStore, activeGroupPk, groupMap, mfiClient, nativeSolBalance, refreshGroup] = - useTradeStore((state) => [ + const [isInitialized, setIsRefreshingStore, activeGroupPk, groupMap, nativeSolBalance, refreshGroup] = useTradeStore( + (state) => [ state.initialized, state.setIsRefreshingStore, state.activeGroup, state.groupMap, - state.marginfiClient, state.nativeSolBalance, state.refreshGroup, - ]); + ] + ); const activeGroup = React.useMemo(() => { const group = activeGroupPk ? groupMap.get(activeGroupPk.toBase58()) || null : null; @@ -363,6 +363,7 @@ export const ActionBox = ({ setIsActionComplete, setPreviousTxn, setIsRefreshingStore, + refreshGroup, connection, wallet, activeGroup?.groupPk, @@ -443,7 +444,7 @@ export const ActionBox = ({ }; const handleLstAction = React.useCallback(async () => { - if ((!selectedBank && !selectedStakingAccount) || !mfiClient || !lstData) { + if ((!selectedBank && !selectedStakingAccount) || !activeGroup?.client || !lstData) { return; } @@ -473,7 +474,7 @@ export const ActionBox = ({ const txnSig = await executeLstAction({ actionMode, - marginfiClient: mfiClient, + marginfiClient: activeGroup.client, amount, connection, wallet, @@ -538,7 +539,8 @@ export const ActionBox = ({ }, [ selectedBank, selectedStakingAccount, - mfiClient, + activeGroup?.client, + activeGroup?.groupPk, lstData, lstQuoteMeta, setIsLoading, @@ -553,17 +555,18 @@ export const ActionBox = ({ setIsActionComplete, setPreviousTxn, setIsRefreshingStore, - activeGroup?.groupPk, + refreshGroup, ]); const handleLendingAction = React.useCallback(async () => { - if (!actionMode || !selectedBank || (!amount && !repayAmount)) { + console.log({ activeGroup }); + if (!actionMode || !activeGroup?.client || !selectedBank || (!amount && !repayAmount)) { return; } const action = async () => { const params = { - mfiClient, + mfiClient: activeGroup.client, actionType: actionMode, bank: selectedBank, amount, @@ -589,10 +592,10 @@ export const ActionBox = ({ await action(); }, [ actionMode, + activeGroup?.client, selectedBank, amount, repayAmount, - mfiClient, nativeSolBalance, selectedAccount, walletContextState, diff --git a/apps/marginfi-v2-trading/src/components/common/ActionBox/components/ActionBoxPreview/Components/LendingPreview/LendingPreview.tsx b/apps/marginfi-v2-trading/src/components/common/ActionBox/components/ActionBoxPreview/Components/LendingPreview/LendingPreview.tsx index 3c52ef4e85..7d91c06002 100644 --- a/apps/marginfi-v2-trading/src/components/common/ActionBox/components/ActionBoxPreview/Components/LendingPreview/LendingPreview.tsx +++ b/apps/marginfi-v2-trading/src/components/common/ActionBox/components/ActionBoxPreview/Components/LendingPreview/LendingPreview.tsx @@ -30,10 +30,8 @@ export const LendingPreview = ({ addAdditionalsPopup, children, }: ActionBoxPreviewProps) => { - const [marginfiClient] = useTradeStore((state) => [state.marginfiClient]); - const { preview, previewStats, isLoading, actionMethod } = useLendingPreview({ - marginfiClient: activeGroup?.client ?? marginfiClient, + marginfiClient: activeGroup?.client ?? null, accountSummary: activeGroup?.accountSummary ?? DEFAULT_ACCOUNT_SUMMARY, actionMode, account: activeGroup?.selectedAccount ?? null, diff --git a/apps/marginfi-v2-trading/src/components/common/ActiveGroup/ActiveGroup.tsx b/apps/marginfi-v2-trading/src/components/common/ActiveGroup/ActiveGroup.tsx index cae6431c9b..54c5b5bd97 100644 --- a/apps/marginfi-v2-trading/src/components/common/ActiveGroup/ActiveGroup.tsx +++ b/apps/marginfi-v2-trading/src/components/common/ActiveGroup/ActiveGroup.tsx @@ -9,11 +9,7 @@ import { useTradeStore } from "~/store"; import { Button } from "~/components/ui/button"; export const ActiveGroup = () => { - const [activeGroupPk, groupMap, marginfiClient] = useTradeStore((state) => [ - state.activeGroup, - state.groupMap, - state.marginfiClient, - ]); + const [activeGroupPk, groupMap] = useTradeStore((state) => [state.activeGroup, state.groupMap]); const activeGroup = React.useMemo(() => { return (activeGroupPk ? groupMap.get(activeGroupPk.toBase58()) : null) ?? null; @@ -31,8 +27,8 @@ export const ActiveGroup = () => { return (
- {(!activeGroup || !activeGroup.pool.token || !marginfiClient) &&

No active group

} - {activeGroup && activeGroup.pool.token && marginfiClient && isOpen && ( + {(!activeGroup || !activeGroup.pool.token || !activeGroup.client) &&

No active group

} + {activeGroup && activeGroup.pool.token && activeGroup.client && isOpen && ( <> )} - + diff --git a/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionActionButtons.tsx b/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionActionButtons.tsx index 029f4eeb16..07a71fa046 100644 --- a/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionActionButtons.tsx +++ b/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionActionButtons.tsx @@ -145,8 +145,8 @@ export const PositionActionButtons = ({ collateralBank, marginfiClient, bank.meta.tokenSymbol, - borrowBank, depositBanks, + borrowBank, slippageBps, connection, priorityFee, @@ -154,6 +154,8 @@ export const PositionActionButtons = ({ groupsCache, wallet, setIsRefreshingStore, + refreshGroup, + activeGroup?.groupPk, ]); return ( diff --git a/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionList.tsx b/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionList.tsx index 1bc6dbc0c6..21784b8165 100644 --- a/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionList.tsx +++ b/apps/marginfi-v2-trading/src/components/common/Portfolio/PositionList.tsx @@ -13,14 +13,14 @@ import { PositionActionButtons } from "~/components/common/Portfolio"; import { Table, TableBody, TableHead, TableCell, TableHeader, TableRow } from "~/components/ui/table"; import { Badge } from "~/components/ui/badge"; import { GroupData } from "~/store/tradeStore"; +import { PublicKey } from "@solana/web3.js"; -export const PositionList = () => { +export const PositionList = ({ activeGroupPk }: { activeGroupPk: PublicKey }) => { const [portfolio] = useTradeStore((state) => [state.portfolio]); const portfolioCombined = React.useMemo(() => { if (!portfolio) return []; - - const isActiveGroupPosition = (item: GroupData) => item.pool.token.isActive; + const isActiveGroupPosition = (item: GroupData) => item.groupPk.equals(activeGroupPk); const activeGroupPosition = [...portfolio.long, ...portfolio.short].find(isActiveGroupPosition); @@ -28,7 +28,7 @@ export const PositionList = () => { const sortedShorts = portfolio.short.filter((item) => !isActiveGroupPosition(item)); return [...(activeGroupPosition ? [activeGroupPosition] : []), ...sortedLongs, ...sortedShorts]; - }, [portfolio]); + }, [activeGroupPk, portfolio]); if (!portfolio) return null; diff --git a/apps/marginfi-v2-trading/src/components/common/TradingBox/TradingBox.tsx b/apps/marginfi-v2-trading/src/components/common/TradingBox/TradingBox.tsx index e3c1bc2481..2311ee72e1 100644 --- a/apps/marginfi-v2-trading/src/components/common/TradingBox/TradingBox.tsx +++ b/apps/marginfi-v2-trading/src/components/common/TradingBox/TradingBox.tsx @@ -59,16 +59,16 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { } }, [tradeState, loopingObject]); - const [activeGroupPk, setActiveGroup, marginfiClient, groupMap, setIsRefreshingStore, refreshGroup, fetchTradeState] = - useTradeStore((state) => [ + const [activeGroupPk, setActiveGroup, groupMap, setIsRefreshingStore, refreshGroup, fetchTradeState] = useTradeStore( + (state) => [ state.activeGroup, state.setActiveGroup, - state.marginfiClient, state.groupMap, state.setIsRefreshingStore, state.refreshGroup, state.fetchTradeState, - ]); + ] + ); const [slippageBps, priorityFee, platformFeeBps, setSlippageBps, setIsActionComplete, setPreviousTxn] = useUiStore( (state) => [ @@ -157,7 +157,7 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { const loadStats = React.useCallback( async (simulationResult: SimulationResult | null, looping: LoopingObject, isAccountInitialized: boolean) => { - if (!marginfiClient || !activeGroup) { + if (!activeGroup?.client || !activeGroup) { return; } setStats( @@ -171,19 +171,19 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { ) ); }, - [activeGroup, marginfiClient] + [activeGroup] ); const handleSimulation = React.useCallback( async (looping: LoopingObject, bank: ExtendedBankInfo, selectedAccount: MarginfiAccountWrapper | null) => { - if (!marginfiClient) { + if (!activeGroup?.client) { return; } let simulationResult: SimulationResult | null = null; try { simulationResult = await simulateLooping({ - marginfiClient, + marginfiClient: activeGroup.client, account: selectedAccount, bank: bank, loopingTxn: looping.loopingTxn, @@ -200,11 +200,11 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { loadStats(simulationResult, looping, !!selectedAccount); } }, - [loadStats, marginfiClient] + [loadStats, activeGroup?.client] ); const loadLoopingVariables = React.useCallback(async () => { - if (marginfiClient && activeGroup) { + if (activeGroup?.client && activeGroup) { try { if (Number(amount) === 0 || leverage <= 1) { throw new Error("Amount is 0"); @@ -226,7 +226,7 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { const amountParsed = isNaN(Number.parseFloat(strippedAmount)) ? 0 : Number.parseFloat(strippedAmount); const result = await calculateLoopingParams({ - marginfiClient, + marginfiClient: activeGroup?.client, marginfiAccount: activeGroup.selectedAccount, depositBank, borrowBank, @@ -260,7 +260,6 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { } } }, [ - marginfiClient, activeGroup, amount, leverage, @@ -291,7 +290,7 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { const leverageActionCb = React.useCallback( async (depositBank: ExtendedBankInfo, borrowBank: ExtendedBankInfo) => { const sig = await executeLeverageAction({ - marginfiClient, + marginfiClient: activeGroup?.client ?? null, marginfiAccount: activeGroup?.selectedAccount ?? null, depositBank, borrowBank, @@ -306,21 +305,11 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { return sig; }, - [ - amount, - connection, - loopingObject, - marginfiClient, - priorityFee, - activeGroup, - slippageBps, - tradeState, - walletContextState, - ] + [amount, connection, loopingObject, priorityFee, activeGroup, slippageBps, tradeState, walletContextState] ); const handleLeverageAction = React.useCallback(async () => { - if (loopingObject && marginfiClient && collateralBank) { + if (loopingObject && activeGroup?.client && collateralBank) { try { setIsLoading(true); let depositBank: ExtendedBankInfo, borrowBank: ExtendedBankInfo; @@ -389,7 +378,7 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { leverage, leverageActionCb, loopingObject, - marginfiClient, + refreshGroup, setIsActionComplete, setIsRefreshingStore, setPreviousTxn, @@ -544,6 +533,7 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => {

{actionMethod.description}

{actionMethod.action && ( @@ -601,7 +591,11 @@ export const TradingBox = ({ side = "long" }: TradingBoxProps) => { {Stats} ) : ( - + )} diff --git a/apps/marginfi-v2-trading/src/pages/_app.tsx b/apps/marginfi-v2-trading/src/pages/_app.tsx index e15ecd4c62..ca42a55b00 100644 --- a/apps/marginfi-v2-trading/src/pages/_app.tsx +++ b/apps/marginfi-v2-trading/src/pages/_app.tsx @@ -122,6 +122,5 @@ MrgnApp.getInitialProps = async (appContext: AppContext): Promise bank.groupAddress === groupAddress); } - console.log(path); return { ...appProps, path: path || "/", bank }; }; diff --git a/apps/marginfi-v2-trading/src/pages/pools/[symbol].tsx b/apps/marginfi-v2-trading/src/pages/pools/[symbol].tsx index cf2ce010e4..7d366893f6 100644 --- a/apps/marginfi-v2-trading/src/pages/pools/[symbol].tsx +++ b/apps/marginfi-v2-trading/src/pages/pools/[symbol].tsx @@ -99,8 +99,8 @@ export default function TradeSymbolPage() {
- - + +
diff --git a/apps/marginfi-v2-trading/src/pages/trade/[symbol].tsx b/apps/marginfi-v2-trading/src/pages/trade/[symbol].tsx index 938a0ce3a1..daf52eac3c 100644 --- a/apps/marginfi-v2-trading/src/pages/trade/[symbol].tsx +++ b/apps/marginfi-v2-trading/src/pages/trade/[symbol].tsx @@ -355,7 +355,7 @@ export default function TradeSymbolPage() {
- +
diff --git a/apps/marginfi-v2-trading/src/pages/yield.tsx b/apps/marginfi-v2-trading/src/pages/yield.tsx index 23196ecd4d..90c3e701f3 100644 --- a/apps/marginfi-v2-trading/src/pages/yield.tsx +++ b/apps/marginfi-v2-trading/src/pages/yield.tsx @@ -280,6 +280,7 @@ export default function PortfolioPage() { group.pool.token.position.isLending && group.selectedAccount && ( )} @@ -354,6 +356,7 @@ export default function PortfolioPage() {
{collateralBank.isActive && collateralBank.position.isLending && group.selectedAccount && ( )} @@ -492,13 +496,21 @@ export default function PortfolioPage() { )}
{group.pool.token.isActive && group.pool.token.position.isLending && ( - + )} - + @@ -560,13 +572,21 @@ export default function PortfolioPage() { )}
{collateralBank.isActive && collateralBank.position.isLending && ( - + )} - + diff --git a/apps/marginfi-v2-trading/src/store/tradeStore.ts b/apps/marginfi-v2-trading/src/store/tradeStore.ts index 9ebd36e41e..de73b0ecc4 100644 --- a/apps/marginfi-v2-trading/src/store/tradeStore.ts +++ b/apps/marginfi-v2-trading/src/store/tradeStore.ts @@ -120,9 +120,6 @@ type TradeStoreState = { sortBy: TradePoolFilterStates; - // marginfi client, initialized when viewing an active group - marginfiClient: MarginfiClient | null; - // active group, currently being viewed / traded activeGroup: PublicKey | null; // user native sol balance @@ -190,7 +187,6 @@ const stateCreator: StateCreator = (set, get) => ({ currentPage: 1, totalPages: 0, sortBy: TradePoolFilterStates.TIMESTAMP, - marginfiClient: null, activeGroup: null, nativeSolBalance: 0, tokenAccountMap: null, @@ -441,7 +437,6 @@ const stateCreator: StateCreator = (set, get) => ({ } set({ - marginfiClient, portfolio, groupMap, wallet: wallet, @@ -475,7 +470,6 @@ const stateCreator: StateCreator = (set, get) => ({ set({ activeGroup: args.groupPk, - marginfiClient, }); }, @@ -483,7 +477,6 @@ const stateCreator: StateCreator = (set, get) => ({ set((state) => { return { ...state, - marginfiClient: null, selectedAccount: null, activeGroup: null, }; diff --git a/apps/marginfi-v2-trading/src/utils/mrgnActions.ts b/apps/marginfi-v2-trading/src/utils/mrgnActions.ts index e1d82a2cb1..aa700d0947 100644 --- a/apps/marginfi-v2-trading/src/utils/mrgnActions.ts +++ b/apps/marginfi-v2-trading/src/utils/mrgnActions.ts @@ -101,7 +101,6 @@ export async function executeLendingAction({ if (actionType === ActionType.Repay) { if (repayWithCollatOptions) { - console.log({ amount, repay: repayWithCollatOptions.repayAmount }); txnSig = await repayWithCollat({ marginfiClient: mfiClient, marginfiAccount,