Skip to content
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

update chain on network change #2497

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,17 @@ export class Web3Modal extends Web3ModalScaffold {
this.syncNetwork(chainImages)
})

NetworkController.subscribeKey('caipNetwork', () => {
NetworkController.subscribeKey('caipNetwork', (newCaipChain: CaipNetwork | undefined) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for aligning naming, could we make it newCaipNetwork?

Suggested change
NetworkController.subscribeKey('caipNetwork', (newCaipChain: CaipNetwork | undefined) => {
NetworkController.subscribeKey('caipNetwork', (newCaipNetwork: CaipNetwork | undefined) => {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I guess you don't have to set type definitions since subscribeKey should already been set the type of the parameter.

Suggested change
NetworkController.subscribeKey('caipNetwork', (newCaipChain: CaipNetwork | undefined) => {
NetworkController.subscribeKey('caipNetwork', (newCaipChain) => {

const newChain = chains.find(_chain => _chain.chainId === newCaipChain?.id.split(':')[1])

if (!newChain) {
throw new Error('The selected chain is not a valid Solana chain')
}

if (NetworkController.state.caipNetwork && !SolStoreUtil.state.isConnected) {
SolStoreUtil.setCaipChainId(`solana:${chain.chainId}`)
SolStoreUtil.setCurrentChain(chain)
localStorage.setItem(SolConstantsUtil.CAIP_CHAIN_ID, `solana:${chain.chainId}`)
SolStoreUtil.setCaipChainId(`solana:${newChain.chainId}`)
SolStoreUtil.setCurrentChain(newChain)
localStorage.setItem(SolConstantsUtil.CAIP_CHAIN_ID, `solana:${newChain.chainId}`)
ApiController.reFetchWallets()
}
})
Expand Down
Loading