diff --git a/.changeset/thirty-spies-wink.md b/.changeset/thirty-spies-wink.md new file mode 100644 index 0000000000..0c12f395ce --- /dev/null +++ b/.changeset/thirty-spies-wink.md @@ -0,0 +1,5 @@ +--- +"@venusprotocol/evm": minor +--- + +create reusable chains package diff --git a/apps/evm/package.json b/apps/evm/package.json index aeeba7af2e..f166cc0b36 100644 --- a/apps/evm/package.json +++ b/apps/evm/package.json @@ -21,7 +21,6 @@ "generate-subgraph-types": "npm-run-all --parallel generate-subgraph-types:isolated-pools generate-subgraph-types:governance", "generate-contracts": "rimraf src/libs/contracts/generated/getters && rimraf src/libs/contracts/generated/infos && src/libs/contracts/scripts/generateContractRecords/index.ts && yarn biome format --vcs-use-ignore-file=false --write src/libs/contracts/generated", "generate-pancake-swap-tokens": "src/libs/tokens/scripts/generatePancakeSwapTokenRecords/index.ts && yarn biome format --vcs-use-ignore-file=false --write src/libs/tokens/generated/pancakeSwapTokens/bscMainnet.ts", - "generate-token-accent-colors": "src/libs/tokens/scripts/generateTokenAccentColors/index.ts && yarn biome format --vcs-use-ignore-file=false --write src/libs/tokens/generated/tokenAccentColors.ts", "generate-version-files": "genversion --es6 --semi src/constants/version.ts && src/scripts/generatePublicVersionFile.ts && yarn biome format --vcs-use-ignore-file=false --write public/version.json", "generate": "npm-run-all --parallel generate-version-files generate-contracts generate-subgraph-types && yarn generate-pancake-swap-tokens", "storybook": "storybook dev -p 6006", @@ -157,15 +156,7 @@ "whatwg-fetch": "^3.6.18" }, "browserslist": { - "production": [ - ">0.2%", - "not dead", - "not op_mini all" - ], - "development": [ - "last 1 chrome version", - "last 1 firefox version", - "last 1 safari version" - ] + "production": [">0.2%", "not dead", "not op_mini all"], + "development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"] } } diff --git a/apps/evm/src/clients/api/queries/getProposal/useGetProposal.ts b/apps/evm/src/clients/api/queries/getProposal/useGetProposal.ts index eb1cb8237f..1fa2ceb329 100644 --- a/apps/evm/src/clients/api/queries/getProposal/useGetProposal.ts +++ b/apps/evm/src/clients/api/queries/getProposal/useGetProposal.ts @@ -1,8 +1,8 @@ import { type QueryObserverOptions, useQuery } from '@tanstack/react-query'; +import { chainMetadata } from '@venusprotocol/chains'; import getProposal from 'clients/api/queries/getProposal'; import type { GetProposalInput, GetProposalOutput } from 'clients/api/queries/getProposal/types'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { DEFAULT_REFETCH_INTERVAL_MS } from 'constants/defaultRefetchInterval'; import FunctionKey from 'constants/functionKey'; import { governanceChain } from 'libs/wallet'; @@ -18,7 +18,7 @@ type Options = QueryObserverOptions< >; const useGetProposal = (params: GetProposalInput, options?: Partial) => { - const { blockTimeMs } = CHAIN_METADATA[governanceChain.id]; + const { blockTimeMs } = chainMetadata[governanceChain.id]; return useQuery({ queryKey: [FunctionKey.GET_PROPOSAL, params], diff --git a/apps/evm/src/clients/api/queries/getProposalPreviews/useGetProposalPreviews.ts b/apps/evm/src/clients/api/queries/getProposalPreviews/useGetProposalPreviews.ts index e1ab7cb015..535b8b30a4 100644 --- a/apps/evm/src/clients/api/queries/getProposalPreviews/useGetProposalPreviews.ts +++ b/apps/evm/src/clients/api/queries/getProposalPreviews/useGetProposalPreviews.ts @@ -1,5 +1,6 @@ import { type QueryObserverOptions, useQuery } from '@tanstack/react-query'; +import { chainMetadata } from '@venusprotocol/chains'; import useGetBlockNumber from 'clients/api/queries/getBlockNumber/useGetBlockNumber'; import { useGetProposalMinQuorumVotes } from 'clients/api/queries/getProposalMinQuorumVotes/useGetProposalMinQuorumVotes'; import { @@ -7,7 +8,6 @@ import { type GetProposalPreviewsOutput, getProposalPreviews, } from 'clients/api/queries/getProposalPreviews'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { DEFAULT_REFETCH_INTERVAL_MS } from 'constants/defaultRefetchInterval'; import FunctionKey from 'constants/functionKey'; import { governanceChain } from 'libs/wallet'; @@ -45,7 +45,7 @@ export const useGetProposalPreviews = ( }); const currentBlockNumber = getBlockNumberData?.blockNumber; - const { blockTimeMs, proposalExecutionGracePeriodMs } = CHAIN_METADATA[governanceChain.id]; + const { blockTimeMs, proposalExecutionGracePeriodMs } = chainMetadata[governanceChain.id]; const sanitizedInput: TrimmedGetProposalPreviewsInput = { ...input, diff --git a/apps/evm/src/clients/api/queries/getProposalState/useGetProposalState.ts b/apps/evm/src/clients/api/queries/getProposalState/useGetProposalState.ts index 0a170c8fd4..276266c43b 100644 --- a/apps/evm/src/clients/api/queries/getProposalState/useGetProposalState.ts +++ b/apps/evm/src/clients/api/queries/getProposalState/useGetProposalState.ts @@ -1,10 +1,10 @@ import { type QueryObserverOptions, useQuery } from '@tanstack/react-query'; +import { chainMetadata } from '@venusprotocol/chains'; import getProposalState, { type GetProposalStateInput, type GetProposalStateOutput, } from 'clients/api/queries/getProposalState'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { DEFAULT_REFETCH_INTERVAL_MS } from 'constants/defaultRefetchInterval'; import FunctionKey from 'constants/functionKey'; import { useGetGovernorBravoDelegateContract } from 'libs/contracts'; @@ -22,7 +22,7 @@ type Options = QueryObserverOptions< >; const useGetProposalState = (input: TrimmedGetProposalStateInput, options?: Partial) => { - const { blockTimeMs } = CHAIN_METADATA[governanceChain.id]; + const { blockTimeMs } = chainMetadata[governanceChain.id]; const governorBravoDelegateContract = useGetGovernorBravoDelegateContract({ chainId: governanceChain.id, }); diff --git a/apps/evm/src/clients/api/queries/getVenusVaiVaultDailyRate/index.spec.ts b/apps/evm/src/clients/api/queries/getVenusVaiVaultDailyRate/index.spec.ts index 5a6975f42d..57179da943 100644 --- a/apps/evm/src/clients/api/queries/getVenusVaiVaultDailyRate/index.spec.ts +++ b/apps/evm/src/clients/api/queries/getVenusVaiVaultDailyRate/index.spec.ts @@ -1,7 +1,7 @@ import BigNumber from 'bignumber.js'; import { BigNumber as BN } from 'ethers'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; +import { chainMetadata } from '@venusprotocol/chains'; import type { LegacyPoolComptroller } from 'libs/contracts'; import { ChainId } from 'types'; @@ -25,7 +25,7 @@ describe('api/queries/getVenusVaiVaultDailyRate', () => { expect(venusVaiVaultRateMock).toHaveBeenCalledTimes(1); expect(response).toEqual({ dailyRateMantissa: new BigNumber(fakeOutput.toString()).times( - CHAIN_METADATA[ChainId.BSC_TESTNET].blocksPerDay!, + chainMetadata[ChainId.BSC_TESTNET].blocksPerDay!, ), }); }); diff --git a/apps/evm/src/config/index.ts b/apps/evm/src/config/index.ts index f54f91423f..760ccee79e 100644 --- a/apps/evm/src/config/index.ts +++ b/apps/evm/src/config/index.ts @@ -1,5 +1,5 @@ +import { chainMetadata } from '@venusprotocol/chains'; import { apiUrls } from 'constants/api'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { ChainId, type Environment, type Network } from 'types'; import { extractEnumValues } from 'utilities/extractEnumValues'; @@ -34,26 +34,26 @@ const chainIds = extractEnumValues(ChainId); const { rpcUrls, marketsSubgraphUrls, governanceSubgraphUrls } = chainIds.reduce( (acc, chainId) => { const chainKey = ChainId[chainId]; - const chainMetadata = CHAIN_METADATA[chainId]; + const chain = chainMetadata[chainId]; return { rpcUrls: { ...acc.rpcUrls, [chainId]: ENV_VARIABLES[`VITE_RPC_HTTP_URL_${chainKey}` as keyof typeof ENV_VARIABLES] || - chainMetadata.rpcUrl, + chain.rpcUrl, }, marketsSubgraphUrls: { ...acc.marketsSubgraphUrls, [chainId]: ENV_VARIABLES[`VITE_SUBGRAPH_MARKETS_URL_${chainKey}` as keyof typeof ENV_VARIABLES] || - chainMetadata.marketsSubgraphUrl, + chain.marketsSubgraphUrl, }, governanceSubgraphUrls: { ...acc.governanceSubgraphUrls, [chainId]: ENV_VARIABLES[`VITE_SUBGRAPH_GOVERNANCE_URL_${chainKey}` as keyof typeof ENV_VARIABLES] || - chainMetadata.governanceSubgraphUrl, + chain.governanceSubgraphUrl, }, }; }, diff --git a/apps/evm/src/containers/Layout/Header/TopBar/ChainSelect/index.tsx b/apps/evm/src/containers/Layout/Header/TopBar/ChainSelect/index.tsx index 12603a92cc..21ab0eed48 100644 --- a/apps/evm/src/containers/Layout/Header/TopBar/ChainSelect/index.tsx +++ b/apps/evm/src/containers/Layout/Header/TopBar/ChainSelect/index.tsx @@ -1,5 +1,5 @@ +import { chainMetadata } from '@venusprotocol/chains'; import { Select, type SelectOption, type SelectProps } from 'components'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { useUserChainSettings } from 'hooks/useUserChainSettings'; import { useTranslation } from 'libs/translations'; import { chains, useChainId, useSwitchChain } from 'libs/wallet'; @@ -16,7 +16,7 @@ const getOptions = ({ }: { isGaslessTransactionsSettingEnabled: boolean }) => chains.map>(chain => ({ label: ({ isRenderedInButton }) => { - const metadata = CHAIN_METADATA[chain.id as ChainId]; + const metadata = chainMetadata[chain.id as ChainId]; return (
{metadata.name} diff --git a/apps/evm/src/hooks/useGetChainMetadata/__tests__/index.spec.tsx b/apps/evm/src/hooks/useGetChainMetadata/__tests__/index.spec.tsx index d4e1aa2a63..4d5d70962b 100644 --- a/apps/evm/src/hooks/useGetChainMetadata/__tests__/index.spec.tsx +++ b/apps/evm/src/hooks/useGetChainMetadata/__tests__/index.spec.tsx @@ -2,7 +2,7 @@ import type Vi from 'vitest'; import { renderHook } from 'testUtils/render'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; +import { chainMetadata } from '@venusprotocol/chains'; import { useChainId } from 'libs/wallet'; import { ChainId } from 'types'; @@ -16,6 +16,6 @@ describe('useGetChainMetadata', () => { const { result } = renderHook(() => useGetChainMetadata()); - expect(result.current).toBe(CHAIN_METADATA[ChainId.BSC_TESTNET]); + expect(result.current).toBe(chainMetadata[ChainId.BSC_TESTNET]); }); }); diff --git a/apps/evm/src/hooks/useGetChainMetadata/index.tsx b/apps/evm/src/hooks/useGetChainMetadata/index.tsx index ab4641d8ec..13b1b2b88f 100644 --- a/apps/evm/src/hooks/useGetChainMetadata/index.tsx +++ b/apps/evm/src/hooks/useGetChainMetadata/index.tsx @@ -1,7 +1,7 @@ -import { CHAIN_METADATA } from 'constants/chainMetadata'; +import { chainMetadata } from '@venusprotocol/chains'; import { useChainId } from 'libs/wallet'; export const useGetChainMetadata = () => { const { chainId } = useChainId(); - return CHAIN_METADATA[chainId]; + return chainMetadata[chainId]; }; diff --git a/apps/evm/src/libs/wallet/Web3Wrapper/config.ts b/apps/evm/src/libs/wallet/Web3Wrapper/config.ts index 44b5cabe87..7ef277b761 100644 --- a/apps/evm/src/libs/wallet/Web3Wrapper/config.ts +++ b/apps/evm/src/libs/wallet/Web3Wrapper/config.ts @@ -3,12 +3,12 @@ import { http, createConfig } from 'wagmi'; import localConfig from 'config'; import type { ChainId } from 'types'; -import type { Transport } from 'viem'; +import type { Chain, Transport } from 'viem'; import { chains } from '../chains'; import { WALLET_CONNECT_PROJECT_ID } from '../constants'; const connectKitConfig = getDefaultConfig({ - chains, + chains: chains as [Chain, ...Chain[]], transports: chains.reduce((acc, chain) => { const url = localConfig.rpcUrls[chain.id as ChainId]; diff --git a/apps/evm/src/libs/wallet/chains.ts b/apps/evm/src/libs/wallet/chains.ts index e8cd3ac6d8..f2800a891f 100644 --- a/apps/evm/src/libs/wallet/chains.ts +++ b/apps/evm/src/libs/wallet/chains.ts @@ -1,3 +1,4 @@ +import { ChainId, MainnetChainId, TestnetChainId } from '@venusprotocol/chains'; import { type Chain, arbitrum as arbitrumOne, @@ -15,23 +16,36 @@ import { } from 'wagmi/chains'; import localConfig from 'config'; +import { extractEnumValues } from 'utilities/extractEnumValues'; -const getSupportedChains = (): [Chain, ...Chain[]] => { - if (localConfig.network === 'testnet') { - return [ - bscTestnet, - opBNBTestnet, - sepolia, - arbitrumSepolia, - zksyncSepoliaTestnet, - optimismSepolia, - ]; - } - - return [bscMainnet, ethereum, opBNBMainnet, arbitrumOne, zksyncMainnet, optimismMainnet]; +const chainMapping = { + [ChainId.BSC_MAINNET]: bscMainnet, + [ChainId.BSC_TESTNET]: bscTestnet, + [ChainId.ETHEREUM]: ethereum, + [ChainId.SEPOLIA]: sepolia, + [ChainId.OPBNB_MAINNET]: opBNBMainnet, + [ChainId.OPBNB_TESTNET]: opBNBTestnet, + [ChainId.ARBITRUM_ONE]: arbitrumOne, + [ChainId.ARBITRUM_SEPOLIA]: arbitrumSepolia, + [ChainId.ZKSYNC_MAINNET]: zksyncMainnet, + [ChainId.ZKSYNC_SEPOLIA]: zksyncSepoliaTestnet, + [ChainId.OPTIMISM_MAINNET]: optimismMainnet, + [ChainId.OPTIMISM_SEPOLIA]: optimismSepolia, +} as const satisfies Record; + +const getSupportedChains = () => { + const chainIds = + localConfig.network === 'testnet' + ? extractEnumValues(TestnetChainId) + : extractEnumValues(MainnetChainId); + + const chains: Chain[] = chainIds.map(chainId => chainMapping[chainId]); + + return chains; }; -export const governanceChain = localConfig.network === 'testnet' ? bscTestnet : bscMainnet; +export const governanceChain = + chainMapping[localConfig.network === 'testnet' ? ChainId.BSC_TESTNET : ChainId.BSC_MAINNET]; export const chains = getSupportedChains(); diff --git a/apps/evm/src/pages/Account/Settings/index.tsx b/apps/evm/src/pages/Account/Settings/index.tsx index 48da9dc1fa..9cabd9b6a9 100644 --- a/apps/evm/src/pages/Account/Settings/index.tsx +++ b/apps/evm/src/pages/Account/Settings/index.tsx @@ -1,15 +1,13 @@ import { Card, Icon, Toggle } from 'components'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; +import { useGetChainMetadata } from 'hooks/useGetChainMetadata'; import { useIsFeatureEnabled } from 'hooks/useIsFeatureEnabled'; import { useUserChainSettings } from 'hooks/useUserChainSettings'; import { useTranslation } from 'libs/translations'; -import { useChainId } from 'libs/wallet'; import Section from '../Section'; export const Settings: React.FC = () => { const { t } = useTranslation(); - const { chainId } = useChainId(); - const { name: chainName } = CHAIN_METADATA[chainId]; + const chainMetadata = useGetChainMetadata(); const isGaslessTransactionsFeatureEnabled = useIsFeatureEnabled({ name: 'gaslessTransactions' }); const [{ gaslessTransactions }, setUserChainSettings] = useUserChainSettings(); @@ -31,7 +29,11 @@ export const Settings: React.FC = () => {
-

{t('account.settings.gaslessTransactions.switchLabel', { chainName })}

+

+ {t('account.settings.gaslessTransactions.switchLabel', { + chainName: chainMetadata.name, + })} +

diff --git a/apps/evm/src/pages/Bridge/ChainSelect/index.tsx b/apps/evm/src/pages/Bridge/ChainSelect/index.tsx index d629f47b1c..94a08fdd97 100644 --- a/apps/evm/src/pages/Bridge/ChainSelect/index.tsx +++ b/apps/evm/src/pages/Bridge/ChainSelect/index.tsx @@ -1,13 +1,13 @@ import { forwardRef } from 'react'; +import { chainMetadata } from '@venusprotocol/chains'; import { Select, type SelectOption, type SelectProps } from 'components'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { chains } from 'libs/wallet'; import type { ChainId } from 'types'; export const getOptionsFromChainsList = (chainsList: typeof chains) => chainsList.map(chain => { - const metadata = CHAIN_METADATA[chain.id as ChainId]; + const metadata = chainMetadata[chain.id as ChainId]; const option: SelectOption = { label: (
diff --git a/apps/evm/src/pages/Bridge/__tests__/index.spec.tsx b/apps/evm/src/pages/Bridge/__tests__/index.spec.tsx index 1aa6e58f1c..bbafd007f6 100644 --- a/apps/evm/src/pages/Bridge/__tests__/index.spec.tsx +++ b/apps/evm/src/pages/Bridge/__tests__/index.spec.tsx @@ -129,7 +129,7 @@ describe('Bridge', () => { await waitFor(() => expect((getByTestId(TEST_IDS.toChainIdSelect) as HTMLInputElement).value).toEqual( - String(ChainId.OPBNB_TESTNET), + String(ChainId.SEPOLIA), ), ); @@ -160,7 +160,7 @@ describe('Bridge', () => { ), ); expect((getByTestId(TEST_IDS.toChainIdSelect) as HTMLInputElement).value).toEqual( - String(ChainId.OPBNB_TESTNET), + String(ChainId.SEPOLIA), ); // Click on switch button @@ -168,13 +168,13 @@ describe('Bridge', () => { await waitFor(() => expect(switchChainMock).toHaveBeenCalledTimes(1)); expect(switchChainMock).toHaveBeenCalledWith({ - chainId: ChainId.OPBNB_TESTNET, + chainId: ChainId.SEPOLIA, callback: expect.any(Function), }); await waitFor(() => expect((getByTestId(TEST_IDS.fromChainIdSelect) as HTMLInputElement).value).toEqual( - String(ChainId.OPBNB_TESTNET), + String(ChainId.SEPOLIA), ), ); expect((getByTestId(TEST_IDS.toChainIdSelect) as HTMLInputElement).value).toEqual( @@ -204,7 +204,7 @@ describe('Bridge', () => { const fakeBridgeXvsParams = { accountAddress: fakeAccountAddress, amountMantissa: fakeBalanceMantissa, - destinationChainId: ChainId.OPBNB_TESTNET, + destinationChainId: ChainId.SEPOLIA, nativeCurrencyFeeMantissa: fakeBridgeFeeMantissa, }; @@ -262,7 +262,7 @@ describe('Bridge', () => { // Check the warning shown to the user await waitFor(() => expect(getByTestId(TEST_IDS.notice).textContent).toMatchInlineSnapshot( - '"You cannot bridge more than 0 XVS ($0) on the destination chain in a single transaction"', + `"You cannot bridge more than 0 XVS ($0) on the destination chain in a single transaction"`, ), ); @@ -306,7 +306,7 @@ describe('Bridge', () => { // Check the warning shown to the user await waitFor(() => expect(getByTestId(TEST_IDS.notice).textContent).toMatchInlineSnapshot( - '"You cannot bridge more than 0 XVS ($0) on the destination chain due to the 24-hour limit. This limit will be reset on 13 Jan 2024 12:00 PM"', + `"You cannot bridge more than 0 XVS ($0) on the destination chain due to the 24-hour limit. This limit will be reset on 13 Jan 2024 12:00 PM"`, ), ); diff --git a/apps/evm/src/pages/IsolatedPools/PoolTable/index.spec.tsx b/apps/evm/src/pages/IsolatedPools/PoolTable/index.spec.tsx index 1612e5bba5..0d938244da 100644 --- a/apps/evm/src/pages/IsolatedPools/PoolTable/index.spec.tsx +++ b/apps/evm/src/pages/IsolatedPools/PoolTable/index.spec.tsx @@ -3,8 +3,8 @@ import type Vi from 'vitest'; import { poolData } from '__mocks__/models/pools'; import { renderComponent } from 'testUtils/render'; +import { chainMetadata } from '@venusprotocol/chains'; import { useGetIsolatedPools } from 'clients/api'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { useGetChainMetadata } from 'hooks/useGetChainMetadata'; import { ChainId } from 'types'; @@ -14,7 +14,7 @@ vi.mock('hooks/useGetChainMetadata'); describe('PoolTable', () => { beforeEach(() => { - (useGetChainMetadata as Vi.Mock).mockImplementation(() => CHAIN_METADATA[ChainId.BSC_TESTNET]); + (useGetChainMetadata as Vi.Mock).mockImplementation(() => chainMetadata[ChainId.BSC_TESTNET]); }); it('renders without crashing', () => { @@ -28,7 +28,7 @@ describe('PoolTable', () => { { ...poolData[0], comptrollerContractAddress: - CHAIN_METADATA[ChainId.BSC_TESTNET].corePoolComptrollerContractAddress, + chainMetadata[ChainId.BSC_TESTNET].corePoolComptrollerContractAddress, }, ], }, diff --git a/apps/evm/src/pages/Proposal/Commands/Command/Cta/index.tsx b/apps/evm/src/pages/Proposal/Commands/Command/Cta/index.tsx index 95be70348b..127858ad5b 100644 --- a/apps/evm/src/pages/Proposal/Commands/Command/Cta/index.tsx +++ b/apps/evm/src/pages/Proposal/Commands/Command/Cta/index.tsx @@ -1,5 +1,5 @@ +import { chainMetadata } from '@venusprotocol/chains'; import { Button } from 'components'; -import { CHAIN_METADATA } from 'constants/chainMetadata'; import { useTranslation } from 'libs/translations'; import { useSwitchChain } from 'libs/wallet'; import { useMemo } from 'react'; @@ -34,7 +34,7 @@ export const Cta: React.FC = ({ ...otherProps }) => { const { t } = useTranslation(); - const chainMetadata = CHAIN_METADATA[chainId]; + const chain = chainMetadata[chainId]; const { isOnWrongChain, hasFailedExecution } = useCommand({ chainId, @@ -67,7 +67,7 @@ export const Cta: React.FC = ({ const buttonLabel = useMemo(() => { if (isOnWrongChain) { return t('voteProposalUi.command.cta.wrongChain', { - chainName: chainMetadata.name, + chainName: chain.name, }); } @@ -76,7 +76,7 @@ export const Cta: React.FC = ({ } return t('voteProposalUi.command.cta.execute'); - }, [t, isOnWrongChain, hasFailedExecution, chainMetadata.name]); + }, [t, isOnWrongChain, hasFailedExecution, chain.name]); return (