Skip to content

Commit

Permalink
Merge pull request #458 from lidofinance/feature/add-multichain-to-co…
Browse files Browse the repository at this point in the history
…nfig

feat: use config for multichain banner
  • Loading branch information
Jeday authored Sep 6, 2024
2 parents 4665a1f + 96cf9f7 commit c7731f7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 29 deletions.
8 changes: 6 additions & 2 deletions IPFS.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"cid": "bafybeib3zmyqlmantvdd6i5q4ehmo4larvorgquyanne3uoqdbedwgh3aq",
"leastSafeVersion": "0.36.1",
"config": {
"enabledWithdrawalDexes": ["one-inch", "paraswap", "bebop"]
"enabledWithdrawalDexes": ["one-inch", "paraswap", "bebop"],
"multiChainBanner": [324, 10, 42161, 137, 8453, 5000, 59144, 534352, 56]
}
},
"5": {
Expand All @@ -16,7 +17,10 @@
"cid": "bafybeibbsoqlofslw273b4ih2pdxfaz2zbjmred2ijog725tcmfoewix7y",
"leastSafeVersion": "0.36.1",
"config": {
"enabledWithdrawalDexes": ["one-inch", "paraswap", "bebop"]
"enabledWithdrawalDexes": ["one-inch", "paraswap", "bebop"],
"multiChainBanner": [
324, 10, 42161, 137, 8453, 5000, 59144, 534352, 56, 34443
]
}
}
}
1 change: 1 addition & 0 deletions config/external-config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ManifestEntry = {

export type ManifestConfig = {
enabledWithdrawalDexes: DexWithdrawalApi[];
multiChainBanner: number[];
};

export type ExternalConfig = Omit<ManifestEntry, 'config'> &
Expand Down
55 changes: 44 additions & 11 deletions config/external-config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,45 @@ import { getDexConfig } from 'features/withdrawals/request/withdrawal-rates';

import FallbackLocalManifest from 'IPFS.json' assert { type: 'json' };

// TODO: refactor on config expansion
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;
};

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 isManifestEntryValid = (
entry?: unknown,
): entry is ManifestEntry => {
Expand All @@ -18,16 +56,10 @@ export const isManifestEntryValid = (
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 [isEnabledDexesValid, isMultiChainBannerValid]
.map((validator) => validator(config))
.every((isValid) => isValid);
}
return false;
};
Expand All @@ -39,6 +71,7 @@ export const getBackwardCompatibleConfig = (
enabledWithdrawalDexes: config.enabledWithdrawalDexes.filter(
(dex) => !!getDexConfig(dex),
),
multiChainBanner: config.multiChainBanner ?? [],
};
};

Expand Down
13 changes: 0 additions & 13 deletions config/groups/revalidation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
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,
};
2 changes: 1 addition & 1 deletion consts/external-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const LINK_ADD_NFT_GUIDE = `${config.helpOrigin}/en/articles/7858367-how-
export const OPEN_OCEAN_REFERRAL_ADDRESS =
'0xbb1263222b2c020f155d409dba05c4a3861f18f8';

// for dev and local testing you can set to 'http:/localhost:3000/runtime/IPFS.json' and have file at /public/runtime/IPFS.json
// for dev and local testing you can set to 'http://localhost:3000/runtime/IPFS.json' and have file at /public/runtime/IPFS.json
export const IPFS_MANIFEST_URL =
'https://raw.githubusercontent.com/lidofinance/ethereum-staking-widget/main/IPFS.json';
9 changes: 7 additions & 2 deletions shared/hooks/use-dapp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import { useAccount } from 'wagmi';
import { LIDO_MULTICHAIN_CHAINS } from 'consts/chains';

import { useIsSupportedChain } from './use-is-supported-chain';
import { useConfig } from 'config';

export const useDappStatus = () => {
const { multiChainBanner } = useConfig().externalConfig;
const { chainId, isConnected: isWalletConnected } = useAccount();
const isSupportedChain = useIsSupportedChain();

const isLidoMultichainChain = useMemo(
() => !!chainId && !!LIDO_MULTICHAIN_CHAINS[chainId],
[chainId],
() =>
!!chainId &&
!!LIDO_MULTICHAIN_CHAINS[chainId] &&
multiChainBanner.includes(chainId),
[chainId, multiChainBanner],
);

const isDappActive = useMemo(() => {
Expand Down

0 comments on commit c7731f7

Please sign in to comment.