-
Notifications
You must be signed in to change notification settings - Fork 1.5k
refactor: exposed switch network method and hooks #3164
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
Merged
enesozturk
merged 30 commits into
main
from
refactor/exposed-switch-network-method-and-hooks
Nov 1, 2024
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
d649a83
feat: add switchNetwork and addNetwork functions to useAppKitNetwork …
enesozturk 473b750
chore: linter issues
enesozturk c13ed22
fix: tests
enesozturk eea1cdc
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk f164e0b
chore: version const
enesozturk 367898c
refactor: update method names
enesozturk e4e63ee
chore: add tests for appkit functions
enesozturk b2ccddc
refactor: remove add network logics
enesozturk 168d8d0
chore: changeset file
enesozturk 0696e1d
chore: linter issues
enesozturk b1462a0
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk 33be069
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk beacd62
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk d8f1ea6
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk e2279fb
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk 2e178b1
fix: lab hydration issue
enesozturk 5a96859
chore: appkit hook hydration issue
enesozturk 20afd35
chore: hooks comp hydration
enesozturk 58506aa
chore: hydration
enesozturk ed766e2
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk 6a25c80
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk 15a4eca
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
tomiir d3eda44
fix: disable typing network name to switch network button
enesozturk 312b43c
refactor: swtich network button logics and styles
enesozturk 9b46d07
Merge branch 'main' into refactor/exposed-switch-network-method-and-h…
enesozturk f88749b
chore: skip new test
enesozturk 8635922
fix: wrong skipped test
enesozturk f020cad
chore: remove new hook button
enesozturk 6e23957
chore: revert switch network button
enesozturk 7801b98
chore: remove skip for test
enesozturk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,24 @@ | ||
--- | ||
'@reown/appkit-utils': patch | ||
'@apps/laboratory': patch | ||
'@reown/appkit': patch | ||
'@reown/appkit-core': patch | ||
'@apps/demo': patch | ||
'@apps/gallery': patch | ||
'@reown/appkit-adapter-ethers': patch | ||
'@reown/appkit-adapter-ethers5': patch | ||
'@reown/appkit-adapter-polkadot': patch | ||
'@reown/appkit-adapter-solana': patch | ||
'@reown/appkit-adapter-wagmi': patch | ||
'@reown/appkit-cdn': patch | ||
'@reown/appkit-common': patch | ||
'@reown/appkit-experimental': patch | ||
'@reown/appkit-polyfills': patch | ||
'@reown/appkit-scaffold-ui': patch | ||
'@reown/appkit-siwe': patch | ||
'@reown/appkit-siwx': patch | ||
'@reown/appkit-ui': patch | ||
'@reown/appkit-wallet': patch | ||
--- | ||
|
||
Adds switch network methods and hooks |
This file contains hidden or 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 hidden or 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,49 @@ | ||
import { useAppKit, useDisconnect, useAppKitAccount, useAppKitNetwork } from '@reown/appkit/react' | ||
import { polygon, mainnet, solana, solanaTestnet, type AppKitNetwork } from '@reown/appkit/networks' | ||
import { Heading, Box, Button } from '@chakra-ui/react' | ||
|
||
export function AppKitHooks() { | ||
const { open } = useAppKit() | ||
const { isConnected } = useAppKitAccount() | ||
const { caipNetwork, switchNetwork } = useAppKitNetwork() | ||
const { disconnect } = useDisconnect() | ||
|
||
function handleSwitchNetwork() { | ||
const isEIPNamespace = caipNetwork?.chainNamespace === 'eip155' | ||
// eslint-disable-next-line no-nested-ternary | ||
const networkToSwitch: AppKitNetwork = isEIPNamespace | ||
? caipNetwork?.id === polygon.id | ||
? mainnet | ||
: polygon | ||
: caipNetwork?.id === solana.id | ||
? solanaTestnet | ||
: solana | ||
|
||
switchNetwork(networkToSwitch) | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
Hooks Interactions | ||
</Heading> | ||
<Box display="flex" alignItems="center" columnGap={3} flexWrap="wrap"> | ||
<Button data-testid="w3m-open-hook-button" onClick={() => open()}> | ||
Open | ||
</Button> | ||
|
||
<Button | ||
data-testid="disconnect-hook-button" | ||
isDisabled={!isConnected} | ||
onClick={() => disconnect()} | ||
> | ||
Disconnect | ||
</Button> | ||
|
||
<Button data-testid="switch-network-hook-button" onClick={handleSwitchNetwork}> | ||
Switch Network | ||
</Button> | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ import { UniversalAdapterClient } from './universal-adapter/client.js' | |
import { CaipNetworksUtil, ErrorUtil } from '@reown/appkit-utils' | ||
import type { W3mFrameTypes } from '@reown/appkit-wallet' | ||
import { ProviderUtil } from './store/ProviderUtil.js' | ||
import type { AppKitNetwork } from '@reown/appkit/networks' | ||
|
||
// -- Export Controllers ------------------------------------------------------- | ||
export { AccountController } | ||
|
@@ -116,8 +117,16 @@ export class AppKit { | |
return ChainController.state.activeCaipNetwork?.id | ||
} | ||
|
||
public switchNetwork(caipNetwork: CaipNetwork) { | ||
return ChainController.switchActiveNetwork(caipNetwork) | ||
public switchNetwork(appKitNetwork: AppKitNetwork) { | ||
const network = this.caipNetworks.find(n => n.id === appKitNetwork.id) | ||
|
||
if (!network) { | ||
AlertController.open(ErrorUtil.ALERT_ERRORS.SWITCH_NETWORK_NOT_FOUND, 'error') | ||
|
||
Comment on lines
+123
to
+125
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. Nice 🙌 |
||
return | ||
} | ||
|
||
ChainController.switchActiveNetwork(network) | ||
} | ||
|
||
public getWalletProvider() { | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.