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

852: Fixed loading transactions from localStorage #877

Merged
1 commit merged into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/components/@widgets/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const SwapWidget: FC = () => {
baseTokenInfo,
baseAmount
);
const shouldApprove = !hasSufficientAllowance && swapType !== 'wrapOrUnwrap';
const shouldApprove = !hasSufficientAllowance && swapType !== "wrapOrUnwrap";

const hasApprovalPending = useApprovalPending(baseToken);
const maxAmount = useMaxAmount(baseToken);
Expand Down
9 changes: 8 additions & 1 deletion src/features/metadata/metadataSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,17 @@ export const subscribeToSavedTokenChangesForLocalStoragePersisting = () => {
) {
transactionCache[wallet.address!][wallet.chainId!] =
mostRecentTransactions;

// Filter out processing transactions to prevent them being stuck perpetually, we should enable this again once we have a better solution
// TODO: https://github.com/airswap/airswap-web/issues/876
const updatedLocalStorageTransactions = mostRecentTransactions.filter(
(tx) => tx.status !== "processing"
);

localStorage.setItem(
getTransactionsLocalStorageKey(wallet.address!, wallet.chainId!),
JSON.stringify({
all: mostRecentTransactions,
all: updatedLocalStorageTransactions,
})
);
}
Expand Down
22 changes: 18 additions & 4 deletions src/features/transactions/useHistoricalTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";

import { useCustomCompareEffect } from "@react-hookz/web/esm";
import { useWeb3React } from "@web3-react/core";
Expand All @@ -10,6 +10,7 @@ import Weth9 from "../../constants/Weth9";
import {
checkPendingTransactionState,
getSwapArgsFromWrappedSwapForLog,
getTransactionsLocalStorageKey,
SwapEventArgs,
} from "./transactionUtils";
import {
Expand All @@ -23,13 +24,14 @@ import useTransactionsFromLocalStorage from "./useTransactionsFromLocalStorage";
const useHistoricalTransactions = () => {
const { chainId, library, account } = useWeb3React();
const { result: swapLogs, status: swapLogStatus } = useSwapLogs();
const transactions = useAppSelector(selectTransactions);
const {
transactions: localStorageTransactions,
setTransactions: setLocalStorageTransactions,
} = useTransactionsFromLocalStorage();
const dispatch = useAppDispatch();

const [activeLocalStorageKey, setActiveLocalStorageKey] = useState<string>();

useCustomCompareEffect(
() => {
if (swapLogStatus === "loading" || swapLogStatus === "not-executed")
Expand Down Expand Up @@ -170,8 +172,20 @@ const useHistoricalTransactions = () => {
);

useEffect(() => {
setLocalStorageTransactions({ all: transactions });
}, [transactions]);
if (!chainId || !account) {
return;
}

const localStorageKey = getTransactionsLocalStorageKey(account, chainId);

if (localStorageKey === activeLocalStorageKey) {
return;
}

setActiveLocalStorageKey(localStorageKey);

dispatch(setTransactions(localStorageTransactions?.all || []));
}, [localStorageTransactions]);
};

export default useHistoricalTransactions;
Loading