Skip to content

Commit

Permalink
fix: improved store refreshing for all actionboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 16, 2024
1 parent d6d9255 commit 7b8d1f3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 87 deletions.
11 changes: 6 additions & 5 deletions apps/marginfi-v2-trading/src/components/common/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ const navItems = [

export const Header = () => {
const { connection } = useConnection();
const [initialized, userDataFetched, nativeSolBalance, fetchTradeState, referralCode, banksByBankPk] =
useTradeStoreV2((state) => [
const [initialized, userDataFetched, nativeSolBalance, fetchUserData, referralCode, banksByBankPk] = useTradeStoreV2(
(state) => [
state.initialized,
state.userDataFetched,
state.nativeSolBalance,
state.fetchTradeState,
state.fetchUserData,
state.referralCode,
state.banksByBankPk,
]);
]
);
const { priorityType, broadcastType, priorityFees, maxCap, maxCapType, setTransactionSettings } = useUiStore(
(state) => ({
priorityType: state.priorityType,
Expand Down Expand Up @@ -169,7 +170,7 @@ export const Header = () => {
extendedBankInfos={extendedBankInfos}
nativeSolBalance={nativeSolBalance}
refreshState={() =>
fetchTradeState({
fetchUserData({
connection,
wallet,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export const PositionActionButtons = ({
const [multiStepToast, setMultiStepToast] = React.useState<MultiStepToastHandle | null>(null);
const [isClosing, setIsClosing] = React.useState(false);

const [fetchTradeState, refreshGroup, setIsRefreshingStore, nativeSolBalance] = useTradeStoreV2((state) => [
state.fetchTradeState,
const [refreshGroup, setIsRefreshingStore, nativeSolBalance] = useTradeStoreV2((state) => [
state.refreshGroup,
state.setIsRefreshingStore,
state.nativeSolBalance,
Expand Down Expand Up @@ -283,9 +282,11 @@ export const PositionActionButtons = ({
});
},
onComplete: () => {
fetchTradeState({
refreshGroup({
connection,
wallet,
groupPk: arenaPool.groupPk,
banks: [arenaPool.tokenBank.address, arenaPool.quoteBank.address],
});
},
}}
Expand Down Expand Up @@ -326,9 +327,11 @@ export const PositionActionButtons = ({
});
},
onComplete: () => {
fetchTradeState({
refreshGroup({
connection,
wallet,
groupPk: arenaPool.groupPk,
banks: [arenaPool.tokenBank.address, arenaPool.quoteBank.address],
});
},
}}
Expand Down
15 changes: 12 additions & 3 deletions apps/marginfi-v2-trading/src/components/common/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Swap = ({ onLoad, initialInputMint }: SwapProps) => {
const { walletContextState, wallet } = useWallet();
const { connection } = useConnection();
const [loadTimestamp, setLoadTimestamp] = React.useState(0);
const [fetchTradeState] = useTradeStoreV2((state) => [state.fetchTradeState]);
const [fetchUserData] = useTradeStoreV2((state) => [state.fetchUserData]);
const router = useRouter();

const initialMint = React.useMemo(() => {
Expand All @@ -46,7 +46,7 @@ export const Swap = ({ onLoad, initialInputMint }: SwapProps) => {
endpoint: config.rpcEndpoint,
passThroughWallet: walletContextState.wallet,
onSuccess: ({ txid }: { txid: string }) => {
fetchTradeState({
fetchUserData({
connection,
wallet,
});
Expand All @@ -69,7 +69,16 @@ export const Swap = ({ onLoad, initialInputMint }: SwapProps) => {
setTimeout(() => {
onLoad && onLoad();
}, delay);
}, [initialInputMint, loadTimestamp, onLoad, walletContextState.wallet, initialMint]);
}, [
walletContextState.wallet,
initialInputMint,
initialMint,
loadTimestamp,
fetchUserData,
connection,
wallet,
onLoad,
]);

React.useEffect(() => {
if (!initialInputMint) {
Expand Down
37 changes: 21 additions & 16 deletions apps/marginfi-v2-trading/src/components/common/Yield/YieldRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ interface props {
export const YieldRow = ({ pool }: props) => {
const { connection } = useConnection();
const { connected, wallet } = useWallet();
const [nativeSolBalance, fetchTradeState] = useTradeStoreV2((state) => [
state.nativeSolBalance,
state.fetchTradeState,
]);
const [nativeSolBalance, refreshStore] = useTradeStoreV2((state) => [state.nativeSolBalance, state.refreshGroup]);

return (
<div key={pool.groupPk.toBase58()} className="relative bg-background border rounded-xl mb-12 pt-5 pb-2 px-4">
Expand Down Expand Up @@ -72,7 +69,14 @@ export const YieldRow = ({ pool }: props) => {
connection={connection}
wallet={wallet}
nativeSolBalance={nativeSolBalance}
fetchTradeState={fetchTradeState}
refreshStore={() =>
refreshStore({
connection,
wallet,
groupPk: pool.groupPk,
banks: [pool.tokenBank.address, pool.quoteBank.address],
})
}
/>

<YieldItem
Expand All @@ -83,7 +87,14 @@ export const YieldRow = ({ pool }: props) => {
connection={connection}
wallet={wallet}
nativeSolBalance={nativeSolBalance}
fetchTradeState={fetchTradeState}
refreshStore={() =>
refreshStore({
connection,
wallet,
groupPk: pool.groupPk,
banks: [pool.tokenBank.address, pool.quoteBank.address],
})
}
/>
</div>
);
Expand All @@ -97,7 +108,7 @@ const YieldItem = ({
connection,
wallet,
nativeSolBalance,
fetchTradeState,
refreshStore,
}: {
pool: ArenaPoolV2Extended;
bankType: "COLLATERAL" | "TOKEN";
Expand All @@ -106,7 +117,7 @@ const YieldItem = ({
connection: Connection;
wallet: Wallet;
nativeSolBalance: number;
fetchTradeState: (args: { connection: Connection; wallet: Wallet }) => void;
refreshStore: () => void;
}) => {
const { marginfiClient, wrappedAccount, accountSummary } = useActionBoxProps(pool.groupPk, [
pool.tokenBank,
Expand Down Expand Up @@ -195,10 +206,7 @@ const YieldItem = ({
});
},
onComplete: () => {
fetchTradeState({
connection,
wallet,
});
refreshStore();
},
}}
dialogProps={{
Expand Down Expand Up @@ -256,10 +264,7 @@ const YieldItem = ({
});
},
onComplete: () => {
fetchTradeState({
connection,
wallet,
});
refreshStore();
},
}}
dialogProps={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ interface InfoMessagesProps {
isActiveWithCollat: boolean;
actionMethods: ActionMessageType[];
setIsWalletOpen: (value: boolean) => void;
fetchTradeState: ({
connection,
wallet,
refresh,
}: {
connection?: Connection;
wallet?: Wallet;
refresh?: boolean;
}) => Promise<void>;
refreshStore: () => Promise<void>;
refreshSimulation: () => void;
connection: any;
wallet: any;
Expand All @@ -37,7 +29,7 @@ export const InfoMessages = ({
isActiveWithCollat,
actionMethods = [],
setIsWalletOpen,
fetchTradeState,
refreshStore,
connection,
wallet,
refreshSimulation,
Expand Down Expand Up @@ -143,7 +135,7 @@ export const InfoMessages = ({
requestedBank: actionMethod.action.bank,
showAvailableCollateral: false,
captureEvent: () => console.log("Position added"),
onComplete: () => fetchTradeState({ connection, wallet }),
onComplete: () => refreshStore(),
}}
dialogProps={{
trigger: (
Expand Down Expand Up @@ -174,7 +166,7 @@ export const InfoMessages = ({
requestedBank: activePool.quoteBank,
showAvailableCollateral: false,
captureEvent: () => console.log("Deposit Collateral"),
onComplete: () => fetchTradeState({ connection, wallet }),
onComplete: () => refreshStore(),
}}
dialogProps={{
trigger: <Button className="w-full">Deposit Collateral</Button>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ export const TradeBoxV2 = ({ activePool, side = "long" }: TradeBoxV2Props) => {
state.setPreviousTxn,
]);
const [setIsWalletOpen] = useWalletStore((state) => [state.setIsWalletOpen]);
const [fetchTradeState, nativeSolBalance, setIsRefreshingStore, refreshGroup] = useTradeStoreV2((state) => [
state.fetchTradeState,
const [nativeSolBalance, setIsRefreshingStore, refreshGroup] = useTradeStoreV2((state) => [
state.nativeSolBalance,
state.setIsRefreshingStore,
state.refreshGroup,
Expand Down Expand Up @@ -474,7 +473,14 @@ export const TradeBoxV2 = ({ activePool, side = "long" }: TradeBoxV2Props) => {
isActiveWithCollat={isActiveWithCollat}
actionMethods={actionMethods}
setIsWalletOpen={setIsWalletOpen}
fetchTradeState={fetchTradeState}
refreshStore={() =>
refreshGroup({
connection,
wallet,
groupPk: activePoolExtended.groupPk,
banks: [activePoolExtended.tokenBank.address, activePoolExtended.quoteBank.address],
})
}
connection={connection}
wallet={wallet}
refreshSimulation={refreshSimulation}
Expand Down
46 changes: 2 additions & 44 deletions apps/marginfi-v2-trading/src/store/tradeStoreV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ import { create, StateCreator } from "zustand";
import { AddressLookupTableAccount, Connection, PublicKey, RpcResponseAndContext } from "@solana/web3.js";
import { AnchorProvider, Program } from "@coral-xyz/anchor";
import Fuse, { FuseResult } from "fuse.js";
import {
ExtendedBankInfo,
makeExtendedBankInfo,
fetchTokenAccounts,
TokenAccountMap,
TokenAccount,
} from "@mrgnlabs/marginfi-v2-ui-state";
import { TokenAccountMap } from "@mrgnlabs/marginfi-v2-ui-state";
import {
getConfig,
Bank,
OraclePrice,
MARGINFI_IDL,
MarginfiIdlType,
MarginfiProgram,
BankRaw,
MarginfiGroup,
MarginfiAccount,
MintDataMap,
MintData,
} from "@mrgnlabs/marginfi-client-v2";
import {
Wallet,
TokenMetadata,
getValueInsensitive,
BankMetadata,
chunkedGetRawMultipleAccountInfoOrdered,
} from "@mrgnlabs/mrgn-common";
import { Wallet, chunkedGetRawMultipleAccountInfoOrdered } from "@mrgnlabs/mrgn-common";

import { POOLS_PER_PAGE } from "~/config/trade";
import { TokenData } from "~/types";
Expand All @@ -45,10 +30,6 @@ import {
import { PositionData } from "@mrgnlabs/mrgn-utils";
import { ArenaBank, ArenaPoolSummary, ArenaPoolV2, BankData } from "~/types/trade-store.types";

type TradeGroupsCache = {
[group: string]: [string, string];
};

export enum TradePoolFilterStates {
TIMESTAMP = "timestamp",
PRICE_MOVEMENT_ASC = "price-movement-asc",
Expand All @@ -61,18 +42,6 @@ export enum TradePoolFilterStates {
APY_DESC = "apy-desc",
}

// export type TokenData = {
// price: number;
// priceChange24hr: number;
// volume24hr: number;
// volumeChange24hr: number;
// marketCap: number;
// };

// new types

// api calls

type TradeStoreV2State = {
// keep track of store state
initialized: boolean;
Expand Down Expand Up @@ -135,15 +104,6 @@ type TradeStoreV2State = {
}) => Promise<void>;

// fetch groups / banks
fetchTradeState: ({
connection,
wallet,
refresh,
}: {
connection?: Connection;
wallet?: Wallet;
refresh?: boolean;
}) => Promise<void>;
fetchUserData: ({ connection, wallet }: { connection?: Connection; wallet?: Wallet }) => Promise<void>;
refreshGroup: ({
groupPk,
Expand Down Expand Up @@ -538,8 +498,6 @@ const stateCreator: StateCreator<TradeStoreV2State, [], []> = (set, get) => ({
});
},

fetchTradeState: async (args) => {},

refreshGroup: async (args) => {
const connection = args.connection || get().connection;
const wallet = args.wallet || get().wallet;
Expand Down

0 comments on commit 7b8d1f3

Please sign in to comment.