-
Notifications
You must be signed in to change notification settings - Fork 3
feat: morpho api backup #149
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,14 @@ | ||
| import { SupportedNetworks } from '@/utils/networks'; | ||
|
|
||
| /** | ||
| * Determines the primary data source for market details based on the network. | ||
| * Check if a network supports Morpho API as a data source | ||
| */ | ||
| export const getMarketDataSource = (network: SupportedNetworks): 'morpho' | 'subgraph' => { | ||
| export const supportsMorphoApi = (network: SupportedNetworks): boolean => { | ||
| switch (network) { | ||
| case SupportedNetworks.Mainnet: | ||
| return 'morpho'; | ||
| case SupportedNetworks.Base: | ||
| return 'morpho'; | ||
| return true; | ||
| default: | ||
| return 'subgraph'; // Default to Subgraph | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Determines the data source for historical market data. | ||
| * Assumes only Morpho API provides this, unless explicitly excluded. | ||
| */ | ||
| export const getHistoricalDataSource = (network: SupportedNetworks): 'morpho' | 'subgraph' => { | ||
| switch (network) { | ||
| case SupportedNetworks.Mainnet: | ||
| return 'morpho'; | ||
| case SupportedNetworks.Base: | ||
| return 'morpho'; | ||
| default: | ||
| return 'subgraph'; | ||
| return false; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,9 +21,10 @@ export const fetchMorphoTransactions = async ( | |||||||||||||||||||||||||||||||||||||
| chainId_in: filters.chainIds ?? [SupportedNetworks.Base, SupportedNetworks.Mainnet], | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (filters.marketUniqueKeys && filters.marketUniqueKeys.length > 0) { | ||||||||||||||||||||||||||||||||||||||
| whereClause.marketUniqueKey_in = filters.marketUniqueKeys; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| // disable cuz it's too long | ||||||||||||||||||||||||||||||||||||||
| // if (filters.marketUniqueKeys && filters.marketUniqueKeys.length > 0) { | ||||||||||||||||||||||||||||||||||||||
| // whereClause.marketUniqueKey_in = filters.marketUniqueKeys; | ||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ignoring
Options: -// disable cuz it's too long
-// if (filters.marketUniqueKeys && filters.marketUniqueKeys.length > 0) {
-// whereClause.marketUniqueKey_in = filters.marketUniqueKeys;
-// }
+// Morpho API rejects very long `IN` arrays (~5 KB limit). Chunk and merge.
+if (filters.marketUniqueKeys?.length) {
+ const CHUNK = 50;
+ const chunks = [];
+ for (let i = 0; i < filters.marketUniqueKeys.length; i += CHUNK) {
+ chunks.push({ ...whereClause, marketUniqueKey_in: filters.marketUniqueKeys.slice(i, i + CHUNK) });
+ }
+ return mergeResponses(
+ await Promise.all(chunks.map((w) => query(w)))
+ );
+}If the size limit is the real reason, chunking or a server-side alias is safer than dropping the filter. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| if (filters.timestampGte !== undefined && filters.timestampGte !== null) { | ||||||||||||||||||||||||||||||||||||||
| whereClause.timestamp_gte = filters.timestampGte; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useState, useEffect, useCallback } from 'react'; | ||
| import { getMarketDataSource } from '@/config/dataSources'; | ||
| import { supportsMorphoApi } from '@/config/dataSources'; | ||
| import { fetchMorphoApiLiquidatedMarketKeys } from '@/data-sources/morpho-api/liquidations'; | ||
| import { fetchSubgraphLiquidatedMarketKeys } from '@/data-sources/subgraph/liquidations'; | ||
| import { SupportedNetworks } from '@/utils/networks'; | ||
|
|
@@ -31,57 +31,73 @@ const useLiquidations = () => { | |
| await Promise.all( | ||
| networksToCheck.map(async (network) => { | ||
| try { | ||
| const dataSource = getMarketDataSource(network); | ||
| let networkLiquidatedKeys: Set<string>; | ||
|
|
||
| console.log(`Fetching liquidated markets for ${network} via ${dataSource}`); | ||
|
|
||
| if (dataSource === 'morpho') { | ||
| networkLiquidatedKeys = await fetchMorphoApiLiquidatedMarketKeys(network); | ||
| } else if (dataSource === 'subgraph') { | ||
| networkLiquidatedKeys = await fetchSubgraphLiquidatedMarketKeys(network); | ||
| // Try Morpho API first if supported | ||
| if (supportsMorphoApi(network)) { | ||
| try { | ||
| console.log(`Attempting to fetch liquidated markets via Morpho API for ${network}`); | ||
| networkLiquidatedKeys = await fetchMorphoApiLiquidatedMarketKeys(network); | ||
| } catch (morphoError) { | ||
| console.error(`Failed to fetch liquidated markets via Morpho API:`, morphoError); | ||
| // Continue to Subgraph fallback | ||
| networkLiquidatedKeys = new Set(); | ||
| } | ||
| } else { | ||
| console.warn(`No valid data source found for network ${network} for liquidations.`); | ||
| networkLiquidatedKeys = new Set<string>(); // Assume none if no source | ||
| networkLiquidatedKeys = new Set(); | ||
| } | ||
|
|
||
| // If Morpho API failed or not supported, try Subgraph | ||
| if (networkLiquidatedKeys.size === 0) { | ||
| try { | ||
| console.log(`Attempting to fetch liquidated markets via Subgraph for ${network}`); | ||
| networkLiquidatedKeys = await fetchSubgraphLiquidatedMarketKeys(network); | ||
| } catch (subgraphError) { | ||
| console.error(`Failed to fetch liquidated markets via Subgraph:`, subgraphError); | ||
| throw subgraphError; // Throw to be caught by outer catch | ||
| } | ||
| } | ||
|
Comment on lines
+36
to
59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Uninitialised variable risk
🤖 Prompt for AI Agents |
||
|
|
||
| // Add keys from this network to the combined set | ||
| // Add the keys to the combined set | ||
| networkLiquidatedKeys.forEach((key) => combinedLiquidatedKeys.add(key)); | ||
| } catch (networkError) { | ||
| console.error( | ||
| `Failed to fetch liquidated market keys for network ${network}:`, | ||
| `Failed to fetch liquidated markets for network ${network}:`, | ||
| networkError, | ||
| ); | ||
| fetchErrors.push(networkError); // Collect errors | ||
| fetchErrors.push(networkError); | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| setLiquidatedMarketKeys(combinedLiquidatedKeys); | ||
|
|
||
| // Set overall error if any network fetch failed | ||
| if (fetchErrors.length > 0) { | ||
| setError(fetchErrors[0]); // Or aggregate errors if needed | ||
| setError(fetchErrors[0]); | ||
| } | ||
| } catch (err) { | ||
| // Catch potential errors from Promise.all itself | ||
| console.error('Overall error fetching liquidations:', err); | ||
| console.error('Error fetching liquidated markets:', err); | ||
| setError(err); | ||
| } finally { | ||
| setLoading(false); | ||
| setIsRefetching(false); | ||
| if (isRefetch) { | ||
| setIsRefetching(false); | ||
| } else { | ||
| setLoading(false); | ||
| } | ||
| } | ||
| }, []); // Dependencies: None needed directly, fetchers are self-contained | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| fetchLiquidations().catch(console.error); | ||
| }, [fetchLiquidations]); | ||
|
|
||
| const refetch = useCallback(() => { | ||
| fetchLiquidations(true).catch(console.error); | ||
| }, [fetchLiquidations]); | ||
|
|
||
| return { loading, isRefetching, liquidatedMarketKeys, error, refetch }; | ||
| return { | ||
| loading, | ||
| isRefetching, | ||
| liquidatedMarketKeys, | ||
| error, | ||
| refetch: async () => fetchLiquidations(true), | ||
| }; | ||
| }; | ||
|
|
||
| export default useLiquidations; | ||
Uh oh!
There was an error while loading. Please reload this page.