Skip to content

Commit

Permalink
fix: issues with mrgnlend provider fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 4, 2024
1 parent 86a4972 commit 0544c15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ export const MovePositionDialog = ({
} finally {
setIsExecutionLoading(false);
}
}, [marginfiClient, accountToMoveTo, actionTxns, broadcastType, priorityFees, fetchMrgnlendState, setIsOpen]);
}, [
marginfiClient,
accountToMoveTo,
actionTxns,
broadcastType,
priorityFees,
selectedAccount?.address,
fetchMrgnlendState,
setIsOpen,
]);

React.useEffect(() => {
if (!accountToMoveTo) return;
Expand Down
26 changes: 12 additions & 14 deletions apps/marginfi-v2-ui/src/context/MrgnlendProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const MrgnlendProvider: React.FC<{
}, [router.query]); // eslint-disable-line react-hooks/exhaustive-deps

React.useEffect(() => {
const fetchData = () => {
const initializeAndFetch = () => {
setIsRefreshingStore(true);
fetchPriorityFee(connection);
fetchMrgnlendState({
Expand All @@ -71,29 +71,27 @@ export const MrgnlendProvider: React.FC<{
}).catch(console.error);
};

const periodicFetch = () => {
console.log("🔄 Periodically fetching marginfi state");
setIsRefreshingStore(true);
fetchPriorityFee(connection);
fetchMrgnlendState().catch(console.error);
};

if (debounceId.current) {
clearTimeout(debounceId.current);
}

debounceId.current = setTimeout(() => {
fetchData();

const id = setInterval(() => {
setIsRefreshingStore(true);
fetchPriorityFee(connection);
fetchMrgnlendState().catch(console.error);
}, 30_000);
debounceId.current = setTimeout(initializeAndFetch, 1000);

return () => {
clearInterval(id);
clearTimeout(debounceId.current!);
};
}, 1000);
// Periodic updates without needing full configuration
const intervalId = setInterval(periodicFetch, 30_000);

return () => {
if (debounceId.current) {
clearTimeout(debounceId.current);
}
clearInterval(intervalId);
};
}, [wallet, isOverride]); // eslint-disable-line react-hooks/exhaustive-deps
// ^ crucial to omit both `connection` and `fetchMrgnlendState` from the dependency array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ const WalletProvider = ({ children }: { children: React.ReactNode }) => {
const [walletContextState, setWalletContextState] = React.useState<WalletContextStateOverride | WalletContextState>(
walletContextStateDefault
);
const walletQueryParam = query?.wallet as string;

// update wallet object, 3 potential sources: web3auth, anchor, override
const { wallet, isOverride }: { wallet: Wallet; isOverride: boolean } = React.useMemo(() => {
const override = query?.wallet as string;
// web3auth wallet
if (web3AuthWalletData && web3Auth?.connected) {
return {
Expand Down Expand Up @@ -192,11 +192,11 @@ const WalletProvider = ({ children }: { children: React.ReactNode }) => {

// wallet address override
// e.g simulating a wallet using ?wallet= query string
if (override) {
if (walletQueryParam) {
return {
wallet: {
...anchorWallet,
publicKey: new PublicKey(override) as PublicKey,
publicKey: new PublicKey(walletQueryParam),
signMessage: walletContextState?.signMessage,
signTransaction: walletContextState?.signTransaction as <T extends Transaction | VersionedTransaction>(
transactions: T
Expand Down Expand Up @@ -229,7 +229,7 @@ const WalletProvider = ({ children }: { children: React.ReactNode }) => {
isOverride: false,
};
}
}, [anchorWallet, web3AuthWalletData, query, web3Auth?.connected, walletContextState]);
}, [anchorWallet, web3AuthWalletData, walletQueryParam, web3Auth?.connected, walletContextState]);

// login to web3auth with specified social provider
const loginWeb3Auth = React.useCallback(
Expand Down

0 comments on commit 0544c15

Please sign in to comment.