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

fix: ensure wallets are synced on transaction fetching #894

Merged
merged 3 commits into from
Jan 17, 2025
Merged
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
19 changes: 17 additions & 2 deletions src/domains/transaction/hooks/use-profile-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ const filterTransactions = ({ transactions }: FilterTransactionProperties) =>
return transaction.isConfirmed();
});

const syncWallets = async (wallets: Contracts.IReadWriteWallet[]) => {
await Promise.allSettled(
wallets.map((wallet) => {
if (wallet.hasSyncedWithNetwork()) {
return;
}

return wallet.synchroniser().identity();
}),
);
};

export const useProfileTransactions = ({
profile,
wallets,
Expand Down Expand Up @@ -184,11 +196,13 @@ export const useProfileTransactions = ({
);

const fetchTransactions = useCallback(
({ flush = false, mode = "all", wallets = [], transactionTypes = [] }: FetchTransactionProperties) => {
async ({ flush = false, mode = "all", wallets = [], transactionTypes = [] }: FetchTransactionProperties) => {
if (wallets.length === 0) {
return { hasMorePages: () => false, items: () => [] };
}

await syncWallets(wallets);

if (flush) {
profile.transactionAggregate().flush(mode);
}
Expand Down Expand Up @@ -218,6 +232,7 @@ export const useProfileTransactions = ({
cursor.current = cursor.current + 1;
setState((state) => ({ ...state, isLoadingMore: true }));

await syncWallets(wallets);
const response = await fetchTransactions({
cursor: cursor.current,
flush: false,
Expand All @@ -240,6 +255,7 @@ export const useProfileTransactions = ({
* Run periodically every 30 seconds to check for new transactions
*/
const checkNewTransactions = async () => {
await syncWallets(wallets);
const response = await fetchTransactions({
cursor: 1,
flush: true,
Expand Down Expand Up @@ -292,7 +308,6 @@ export const useProfileTransactions = ({
activeMode,
activeTransactionType,
fetchMore,
fetchTransactions,
hasEmptyResults,
hasFilter: (selectedTransactionTypes?.length ?? 0) < allTransactionTypes.length,
hasMore,
Expand Down
Loading