-
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.
refactor: move IPFS.json utils to config/external-config
- Loading branch information
Showing
7 changed files
with
165 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useMemo } from 'react'; | ||
import { | ||
Manifest, | ||
ManifestConfig, | ||
ManifestConfigPage, | ||
ManifestConfigPageList, | ||
ManifestEntry, | ||
isManifestValid, | ||
} from 'config/external-config'; | ||
import { getDexConfig } from 'features/withdrawals/request/withdrawal-rates'; | ||
|
||
import FallbackLocalManifest from 'IPFS.json' assert { type: 'json' }; | ||
|
||
export const getBackwardCompatibleConfig = ( | ||
config: ManifestEntry['config'], | ||
): ManifestEntry['config'] => { | ||
let pages: ManifestConfig['pages']; | ||
const configPages = config.pages; | ||
if (configPages) { | ||
pages = (Object.keys(configPages) as ManifestConfigPage[]) | ||
.filter((key) => ManifestConfigPageList.has(key)) | ||
.reduce( | ||
(acc, key) => { | ||
if (acc) { | ||
acc[key] = { ...configPages[key] }; | ||
} | ||
|
||
return acc; | ||
}, | ||
{} as ManifestConfig['pages'], | ||
); | ||
} | ||
|
||
return { | ||
enabledWithdrawalDexes: config.enabledWithdrawalDexes.filter( | ||
(dex) => !!getDexConfig(dex), | ||
), | ||
featureFlags: { ...(config.featureFlags ?? {}) }, | ||
multiChainBanner: config.multiChainBanner ?? [], | ||
pages, | ||
}; | ||
}; | ||
|
||
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 |
---|---|---|
@@ -1,54 +1,105 @@ | ||
import { useMemo } from 'react'; | ||
import { | ||
Manifest, | ||
ManifestConfig, | ||
ManifestConfigPage, | ||
ManifestConfigPageList, | ||
ManifestConfigPageEnum, | ||
ManifestEntry, | ||
} from './types'; | ||
import { isManifestValid } from 'utils/validate-ipfs-json'; | ||
import { getDexConfig } from 'features/withdrawals/request/withdrawal-rates'; | ||
|
||
import FallbackLocalManifest from 'IPFS.json' assert { type: 'json' }; | ||
|
||
export const getBackwardCompatibleConfig = ( | ||
config: ManifestEntry['config'], | ||
): ManifestEntry['config'] => { | ||
let pages: ManifestConfig['pages']; | ||
const configPages = config.pages; | ||
if (configPages) { | ||
pages = (Object.keys(configPages) as ManifestConfigPage[]) | ||
.filter((key) => ManifestConfigPageList.has(key)) | ||
.reduce( | ||
(acc, key) => { | ||
if (acc) { | ||
acc[key] = { ...configPages[key] }; | ||
} | ||
|
||
return acc; | ||
}, | ||
{} as ManifestConfig['pages'], | ||
); | ||
|
||
export const isMultiChainBannerValid = (config: object) => { | ||
// allow empty config | ||
if (!('multiChainBanner' in config) || !config.multiChainBanner) return true; | ||
|
||
if (!Array.isArray(config.multiChainBanner)) return false; | ||
|
||
const multiChainBanner = config.multiChainBanner; | ||
|
||
if ( | ||
!multiChainBanner.every( | ||
(chainId) => typeof chainId === 'number' && chainId > 0, | ||
) | ||
) | ||
return false; | ||
|
||
return !(new Set(multiChainBanner).size !== multiChainBanner.length); | ||
}; | ||
|
||
export const isFeatureFlagsValid = (config: object) => { | ||
// allow empty config | ||
if (!('featureFlags' in config) || !config.featureFlags) return true; | ||
|
||
// only objects | ||
return !(typeof config.featureFlags !== 'object'); | ||
}; | ||
|
||
export const isEnabledDexesValid = (config: object) => { | ||
if ( | ||
!( | ||
'enabledWithdrawalDexes' in config && | ||
Array.isArray(config.enabledWithdrawalDexes) | ||
) | ||
) | ||
return false; | ||
|
||
const enabledWithdrawalDexes = config.enabledWithdrawalDexes; | ||
|
||
if ( | ||
!enabledWithdrawalDexes.every( | ||
(dex) => typeof dex === 'string' && dex !== '', | ||
) | ||
) | ||
return false; | ||
|
||
return new Set(enabledWithdrawalDexes).size === enabledWithdrawalDexes.length; | ||
}; | ||
|
||
export const isPagesValid = (config: object) => { | ||
if (!('pages' in config)) { | ||
return true; | ||
} | ||
|
||
const pages = config.pages as ManifestConfig['pages']; | ||
if (pages && typeof pages === 'object') { | ||
// INFO: exclude possible issue when stack interface can be deactivated | ||
return !pages[ManifestConfigPageEnum.Stake]?.shouldDisable; | ||
} | ||
|
||
return { | ||
enabledWithdrawalDexes: config.enabledWithdrawalDexes.filter( | ||
(dex) => !!getDexConfig(dex), | ||
), | ||
featureFlags: { ...(config.featureFlags ?? {}) }, | ||
multiChainBanner: config.multiChainBanner ?? [], | ||
pages, | ||
}; | ||
return false; | ||
}; | ||
|
||
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; | ||
|
||
return [ | ||
isEnabledDexesValid, | ||
isMultiChainBannerValid, | ||
isFeatureFlagsValid, | ||
isPagesValid, | ||
] | ||
.map((validator) => validator(config)) | ||
.every((isValid) => isValid); | ||
} | ||
return false; | ||
}; | ||
|
||
export const useFallbackManifestEntry = ( | ||
prefetchedManifest: unknown, | ||
export const isManifestValid = ( | ||
manifest: unknown, | ||
chain: number, | ||
): ManifestEntry => { | ||
return useMemo(() => { | ||
const isValid = isManifestValid(prefetchedManifest, chain); | ||
return isValid | ||
? prefetchedManifest[chain] | ||
: (FallbackLocalManifest as unknown as Manifest)[chain]; | ||
}, [prefetchedManifest, chain]); | ||
): 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; | ||
}; |
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 was deleted.
Oops, something went wrong.
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