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

Bugfix/cache swap providers #6979

Merged
merged 2 commits into from
May 30, 2024
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
5 changes: 5 additions & 0 deletions .changeset/itchy-adults-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

caching swap providers data
12 changes: 10 additions & 2 deletions libs/ledger-live-common/src/exchange/providers/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const swapProviders: Record<string, ProviderConfig & AdditionalProviderConfig> =
},
};

let providerDataCache: Record<string, ProviderConfig & AdditionalProviderConfig> | null = null;

export const getSwapProvider = async (
providerName: string,
): Promise<ProviderConfig & AdditionalProviderConfig> => {
Expand Down Expand Up @@ -161,22 +163,28 @@ export const getProvidersCDNData = async () => {
}
};

export const fetchAndMergeProviderData = async () => {
const fetchAndMergeProviderData = async () => {
if (providerDataCache) {
return providerDataCache;
}

try {
const [providersData, providersExtraData] = await Promise.all([
getProvidersData(),
getProvidersCDNData(),
]);

// Transform and merge fetched data
const transformedProvidersData = transformData(providersData);
const finalProvidersData = mergeProviderData(transformedProvidersData, providersExtraData);
providerDataCache = finalProvidersData;

return finalProvidersData;
} catch (error) {
console.error("Error fetching or processing provider data:", error);
const transformedProvidersData = transformData(swapProviders);
const finalProvidersData = mergeProviderData(transformedProvidersData, swapAdditionData);
providerDataCache = finalProvidersData;

return finalProvidersData;
}
};
Expand Down
Loading