-
Notifications
You must be signed in to change notification settings - Fork 13
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
[WIP] feat: chain toggle changes wallet chain #530
base: develop
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -4,11 +4,12 @@ import React, { | |
useState, | ||
useMemo, | ||
useEffect, | ||
useCallback, | ||
} from 'react'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
import { CHAINS, isSDKSupportedL2Chain } from 'consts/chains'; | ||
import { useAccount } from 'wagmi'; | ||
import { useAccount, useSwitchChain } from 'wagmi'; | ||
import { config } from 'config'; | ||
import { useLidoSDK } from './lido-sdk'; | ||
import { wagmiChainMap } from './web3-provider'; | ||
|
@@ -20,7 +21,7 @@ export enum DAPP_CHAIN_TYPE { | |
|
||
type DappChainContextValue = { | ||
chainType: DAPP_CHAIN_TYPE; | ||
setChainType: React.Dispatch<React.SetStateAction<DAPP_CHAIN_TYPE>>; | ||
setChainType: React.Dispatch<DAPP_CHAIN_TYPE>; | ||
supportedChainIds: number[]; | ||
isChainTypeMatched: boolean; | ||
isChainTypeOnL2: boolean; | ||
|
@@ -119,6 +120,7 @@ export const useDappChain = (): UseDappChainValue => { | |
export const SupportL2Chains: React.FC<React.PropsWithChildren> = ({ | ||
children, | ||
}) => { | ||
const { switchChain } = useSwitchChain(); | ||
const { chainId: walletChainId, isConnected } = useAccount(); | ||
const [chainType, setChainType] = useState<DAPP_CHAIN_TYPE>( | ||
DAPP_CHAIN_TYPE.Ethereum, | ||
|
@@ -139,12 +141,25 @@ export const SupportL2Chains: React.FC<React.PropsWithChildren> = ({ | |
} | ||
}, [walletChainId, isConnected, setChainType]); | ||
|
||
const handleSetChainType = useCallback<React.Dispatch<DAPP_CHAIN_TYPE>>( | ||
(newChainType) => { | ||
setChainType(newChainType); | ||
|
||
const newChainId = | ||
getChainIdByChainType(newChainType, config.supportedChains) ?? | ||
config.defaultChain; | ||
|
||
switchChain({ chainId: newChainId }); | ||
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. We need to check if cuurent connector has 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. Also would love to experiment with async UX, where we on user selecting chain and if there is
|
||
}, | ||
[switchChain], | ||
); | ||
|
||
return ( | ||
<DappChainContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
chainType, | ||
setChainType, | ||
setChainType: handleSetChainType, | ||
supportedChainIds: config.supportedChains, | ||
isChainTypeMatched: | ||
chainType === getChainTypeByChainId(walletChainId), | ||
|
@@ -153,7 +168,7 @@ export const SupportL2Chains: React.FC<React.PropsWithChildren> = ({ | |
// or use an array or Set (for example with L2_DAPP_CHAINS_TYPE) | ||
isChainTypeOnL2: chainType === DAPP_CHAIN_TYPE.Optimism, | ||
}), | ||
[chainType, walletChainId], | ||
[chainType, handleSetChainType, walletChainId], | ||
)} | ||
> | ||
{children} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking we try something here:
Instead of chainType on selector, we select chainId. So for tesntent we would have Holesky and OP sepolia namings. No changes to prod UX. Also would allow in dev to select any chain you have in supported. This would also simpligy this code as chainId->chainType mapping is very clear.
ChainType still serves a purpose, as it allows us to get correct UI color and etc.