Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically reload supply table after deposit/withdraw #851

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions earn/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useEffect } from 'react';
import React, { Suspense, useEffect, useState } from 'react';

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/react-hooks';
import * as Sentry from '@sentry/react';
Expand Down Expand Up @@ -144,13 +144,14 @@ function AppBodyWrapper() {
}

function App() {
const [activeChain, setActiveChain] = React.useState<Chain>(DEFAULT_CHAIN);
const [activeChain, setActiveChain] = useState<Chain>(DEFAULT_CHAIN);
const [accountRisk, setAccountRisk] = useSafeState<AccountRiskResult>({ isBlocked: false, isLoading: true });
const [geoFencingInfo, setGeoFencingInfo] = useSafeState<GeoFencingInfo>({
isAllowed: false,
isLoading: true,
});
const [lendingPairs, setLendingPairs] = useChainDependentState<LendingPair[] | null>(null, activeChain.id);
const [shouldFetchLendingPairs, setShouldFetchLendingPairs] = useChainDependentState(true, activeChain.id);
haydenshively marked this conversation as resolved.
Show resolved Hide resolved

const { address: userAddress } = useAccount();
const provider = useProvider({ chainId: activeChain.id });
Expand Down Expand Up @@ -187,15 +188,20 @@ function App() {
let mounted = true;

(async () => {
if (!shouldFetchLendingPairs) return;

const chainId = (await provider.getNetwork()).chainId;
const res = await getAvailableLendingPairs(chainId, provider);
if (mounted) setLendingPairs(res);
if (mounted) {
setShouldFetchLendingPairs(false);
setLendingPairs(res);
}
})();

return () => {
mounted = false;
};
}, [provider, setLendingPairs]);
}, [provider, setLendingPairs, shouldFetchLendingPairs, setShouldFetchLendingPairs]);

return (
<>
Expand All @@ -204,7 +210,7 @@ function App() {
<AccountRiskContext.Provider value={accountRisk}>
<GeoFencingContext.Provider value={geoFencingInfo}>
<ChainContext.Provider value={value}>
<LendingPairsContext.Provider value={lendingPairs}>
<LendingPairsContext.Provider value={{ lendingPairs, refetch: () => setShouldFetchLendingPairs(true) }}>
<ScrollToTop />
<AppBodyWrapper />
</LendingPairsContext.Provider>
Expand Down
13 changes: 8 additions & 5 deletions earn/src/data/hooks/UseLendingPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { Address } from 'wagmi';

import { LendingPair } from '../LendingPair';

export const LendingPairsContext = createContext<LendingPair[] | null>(null);
export const LendingPairsContext = createContext<{ lendingPairs: LendingPair[] | null; refetch?: () => void }>({
lendingPairs: null,
});

export function useLendingPairs() {
const ctxt = useContext(LendingPairsContext);

return useMemo(
() => ({
isLoading: ctxt === null,
lendingPairs: ctxt ?? [],
isLoading: ctxt.lendingPairs === null,
lendingPairs: ctxt.lendingPairs ?? [],
refetch: ctxt.refetch,
}),
[ctxt]
);
Expand All @@ -23,8 +26,8 @@ export function useLendingPair(token0?: Address, token1?: Address) {

const { lendingPairs } = useMemo(
() => ({
isLoading: ctxt === null,
lendingPairs: ctxt ?? [],
isLoading: ctxt.lendingPairs === null,
lendingPairs: ctxt.lendingPairs ?? [],
}),
[ctxt]
);
Expand Down
7 changes: 5 additions & 2 deletions earn/src/pages/MarketsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function MarketsPage() {
}, [searchParams]);

// MARK: custom hooks
const { lendingPairs } = useLendingPairs();
const { lendingPairs, refetch: refetchLendingPairs } = useLendingPairs();

// NOTE: Instead of `useAvailablePools()`, we're able to compute `availablePools` from `lendingPairs`.
// This saves a lot of data.
Expand Down Expand Up @@ -446,7 +446,10 @@ export default function MarketsPage() {
}}
onConfirm={() => {
setIsPendingTxnModalOpen(false);
setTimeout(() => refetch(), 100);
setTimeout(() => {
refetchLendingPairs?.();
refetch();
}, 100);
}}
status={pendingTxnModalStatus}
/>
Expand Down