-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from lidofinance/develop
Develop to Main
- Loading branch information
Showing
34 changed files
with
683 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { useExternalConfigContext } from './use-external-config-context'; | ||
export type { | ||
ManifestConfig, | ||
Manifest, | ||
ManifestEntry, | ||
ExternalConfig, | ||
} from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { DexWithdrawalApi } from 'features/withdrawals/request/withdrawal-rates'; | ||
import { SWRResponse } from 'swr'; | ||
|
||
export type Manifest = Record<string, ManifestEntry>; | ||
|
||
export type ManifestEntry = { | ||
cid?: string; | ||
ens?: string; | ||
leastSafeVersion?: string; | ||
config: ManifestConfig; | ||
}; | ||
|
||
export type ManifestConfig = { | ||
enabledWithdrawalDexes: DexWithdrawalApi[]; | ||
}; | ||
|
||
export type ExternalConfig = Omit<ManifestEntry, 'config'> & | ||
ManifestConfig & { | ||
fetchMeta: SWRResponse<ManifestEntry>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useMemo } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import { STRATEGY_LAZY } from 'consts/swr-strategies'; | ||
import { getConfig } from '../get-config'; | ||
import { standardFetcher } from 'utils/standardFetcher'; | ||
import { IPFS_MANIFEST_URL } from 'consts/external-links'; | ||
import { | ||
getBackwardCompatibleConfig, | ||
isManifestEntryValid, | ||
useFallbackManifestEntry, | ||
} from './utils'; | ||
|
||
import type { ExternalConfig, ManifestEntry } from './types'; | ||
|
||
const onFetchError = (error: unknown) => { | ||
console.warn( | ||
'[useExternalConfigContext] while fetching external config:', | ||
error, | ||
); | ||
}; | ||
|
||
export const useExternalConfigContext = ( | ||
prefetchedManifest?: unknown, | ||
): ExternalConfig => { | ||
const { defaultChain } = getConfig(); | ||
const fallbackData = useFallbackManifestEntry( | ||
prefetchedManifest, | ||
defaultChain, | ||
); | ||
|
||
const swr = useSWR<ManifestEntry>( | ||
['swr:external-config', defaultChain], | ||
async () => { | ||
const result = await standardFetcher<Record<string, any>>( | ||
IPFS_MANIFEST_URL, | ||
{ | ||
headers: { Accept: 'application/json' }, | ||
}, | ||
); | ||
const entry = result[defaultChain.toString()]; | ||
if (isManifestEntryValid(entry)) return entry; | ||
throw new Error( | ||
'[useExternalConfigContext] received invalid manifest', | ||
result, | ||
); | ||
}, | ||
{ | ||
...STRATEGY_LAZY, | ||
onError: onFetchError, | ||
}, | ||
); | ||
|
||
return useMemo(() => { | ||
const { config, ...rest } = swr.data ?? fallbackData; | ||
const cleanConfig = getBackwardCompatibleConfig(config); | ||
return { ...cleanConfig, ...rest, fetchMeta: swr }; | ||
}, [swr, fallbackData]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useMemo } from 'react'; | ||
import type { Manifest, ManifestEntry } from './types'; | ||
import { getDexConfig } from 'features/withdrawals/request/withdrawal-rates'; | ||
|
||
import FallbackLocalManifest from 'IPFS.json' assert { type: 'json' }; | ||
|
||
// TODO: refactor on config expansion | ||
export const isManifestEntryValid = ( | ||
entry?: unknown, | ||
): entry is ManifestEntry => { | ||
if ( | ||
// entry = {} | ||
entry && | ||
typeof entry === 'object' && | ||
// entry.config = {} | ||
'config' in entry && | ||
typeof entry.config === 'object' && | ||
entry.config | ||
) { | ||
const config = entry.config; | ||
if ( | ||
'enabledWithdrawalDexes' in config && | ||
Array.isArray(config.enabledWithdrawalDexes) | ||
) { | ||
const enabledWithdrawalDexes = config.enabledWithdrawalDexes; | ||
return ( | ||
new Set(enabledWithdrawalDexes).size === enabledWithdrawalDexes.length | ||
); | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
|
||
export const getBackwardCompatibleConfig = ( | ||
config: ManifestEntry['config'], | ||
): ManifestEntry['config'] => { | ||
return { | ||
enabledWithdrawalDexes: config.enabledWithdrawalDexes.filter( | ||
(dex) => !!getDexConfig(dex), | ||
), | ||
}; | ||
}; | ||
|
||
export const isManifestValid = ( | ||
manifest: unknown, | ||
chain: number, | ||
): manifest is Manifest => { | ||
const stringChain = chain.toString(); | ||
if (manifest && typeof manifest === 'object' && stringChain in manifest) | ||
return isManifestEntryValid( | ||
(manifest as Record<string, unknown>)[stringChain], | ||
); | ||
return false; | ||
}; | ||
|
||
export const useFallbackManifestEntry = ( | ||
prefetchedManifest: unknown, | ||
chain: number, | ||
): ManifestEntry => { | ||
return useMemo(() => { | ||
const isValid = isManifestValid(prefetchedManifest, chain); | ||
return isValid | ||
? prefetchedManifest[chain] | ||
: (FallbackLocalManifest as unknown as Manifest)[chain]; | ||
}, [prefetchedManifest, chain]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { ManifestConfig, ManifestEntry } from 'config/external-config'; | ||
|
||
export const DEFAULT_REVALIDATION = 60 * 15; // 15 minutes | ||
export const ERROR_REVALIDATION_SECONDS = 60; // 1 minute | ||
|
||
export const FALLBACK_CONFIG: ManifestConfig = { | ||
enabledWithdrawalDexes: [], | ||
}; | ||
|
||
export const FALLBACK_MANIFEST_ENTRY: ManifestEntry = { | ||
cid: undefined, | ||
ens: undefined, | ||
leastSafeVersion: undefined, | ||
config: FALLBACK_CONFIG, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.