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

:fix switch network improvements & wagmi to recognize wc session #3299

Merged
merged 33 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9a128d5
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 19, 2024
71f4058
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 20, 2024
bc7b24e
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 23, 2024
2b9a3db
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 24, 2024
3bdbaec
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 26, 2024
b60643a
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Sep 27, 2024
7db0f42
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Oct 2, 2024
34d19b4
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Oct 9, 2024
9ac3b69
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 13, 2024
e5d5f33
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 14, 2024
ecab080
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 14, 2024
2c7cd61
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 15, 2024
1065e16
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 15, 2024
9164df0
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 18, 2024
672b9e8
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 18, 2024
7715f95
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 19, 2024
3c67cb1
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 19, 2024
05646dd
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 20, 2024
7193992
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 20, 2024
7151a56
Merge branch 'main' of https://github.com/reown-com/appkit
svenvoskamp Nov 21, 2024
31814e2
wip
svenvoskamp Nov 21, 2024
d6deca4
Resolve cases where wagmi doesn't recognize wc session and unable to …
svenvoskamp Nov 21, 2024
ad88034
merge main
svenvoskamp Nov 21, 2024
3af2a70
improve switch network flow
svenvoskamp Nov 21, 2024
eb103e3
Merge branch 'main' into fix/switch-network-improvements
svenvoskamp Nov 21, 2024
ced239e
Merge branch 'main' into fix/switch-network-improvements
svenvoskamp Nov 22, 2024
a9b563a
set is chains to false
svenvoskamp Nov 22, 2024
ff5b727
Merge branch 'main' of https://github.com/reown-com/appkit into fix/s…
svenvoskamp Nov 22, 2024
7e1d915
Merge branch 'fix/switch-network-improvements' of https://github.com/…
svenvoskamp Nov 22, 2024
16ab1ca
fix uc
svenvoskamp Nov 22, 2024
0af2f69
request first then set to default
svenvoskamp Nov 22, 2024
44401e0
add comment
svenvoskamp Nov 22, 2024
5a7873d
Merge branch 'main' into fix/switch-network-improvements
svenvoskamp Nov 22, 2024
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
24 changes: 24 additions & 0 deletions .changeset/hungry-frogs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-scaffold-ui': patch
'@apps/laboratory': patch
'@reown/appkit': patch
'@reown/appkit-core': patch
'@reown/appkit-siwe': 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-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Resolve cases where wagmi doesn't recognize wc session and unable to switch from unsupported network"
1 change: 1 addition & 0 deletions apps/laboratory/src/components/Siwe/SiweData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { SIWESession } from '@reown/appkit-siwe'

export function SiweData() {
const { data, status } = useSession()

const session = data as unknown as SIWESession

return (
Expand Down
9 changes: 5 additions & 4 deletions packages/adapters/wagmi/src/connectors/UniversalConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ export function walletConnect(
}

try {
if (chainToSwitch?.caipNetworkId) {
provider.setDefaultChain(chainToSwitch?.caipNetworkId as string)
}

await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }]
})

if (chainToSwitch?.caipNetworkId) {
provider.setDefaultChain(chainToSwitch?.caipNetworkId as string)
}
config.emitter.emit('change', { chainId: Number(chainId) })

const requestedChains = await this.getRequestedChainsIds()
Expand Down Expand Up @@ -418,6 +418,7 @@ export function walletConnect(
}

const connectorChains = config.chains.map(x => x.id)

const namespaceChains = this.getNamespaceChainsIds()

if (namespaceChains.length && !namespaceChains.some(id => connectorChains.includes(id))) {
Expand Down
15 changes: 11 additions & 4 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ export class AppKit {
>(ChainController.state.activeChain as ChainNamespace)
const providerType =
ProviderUtil.state.providerIds[ChainController.state.activeChain as ChainNamespace]

await adapter?.switchNetwork({ caipNetwork, provider, providerType })
this.setCaipNetwork(caipNetwork)
await this.syncAccount({
Expand Down Expand Up @@ -1282,10 +1283,16 @@ export class AppKit {
chainId: ChainController.state.activeCaipNetwork?.id as string | number
})
} catch (error) {
adapter?.switchNetwork({
provider: this.universalProvider,
caipNetwork: ChainController.state.activeCaipNetwork as CaipNetwork
})
/**
* Handle edge case where wagmi detects existing connection but lacks to complete UniversalProvider instance.
* Connection attempt fails due to already connected state - reconnect to restore provider state.
*/
if (adapter?.reconnect) {
adapter?.reconnect({
id: 'walletConnect',
type: 'WALLET_CONNECT'
})
Comment on lines +1290 to +1294
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we add a comment on why this is required? not sure why this or a switch network would be required if a connection fails? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment:

Handle edge case where wagmi detects existing connection but lacks to complete UniversalProvider instance. Connection attempt fails due to already connected state - reconnect to restore provider state.

}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/SIWXUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const SIWXUtil = {

try {
const sessions = await siwx.getSessions(`${network}:${chainId}` as CaipNetworkId, address)

if (sessions.length) {
return
}
Expand Down
7 changes: 6 additions & 1 deletion packages/scaffold-ui/src/modal/w3m-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export class W3mModal extends LitElement {
const prevCaipNetworkId = this.caipNetwork?.caipNetworkId?.toString()
const nextNetworkId = nextCaipNetwork?.caipNetworkId?.toString()

if (prevCaipNetworkId && nextNetworkId && prevCaipNetworkId !== nextNetworkId) {
if (
prevCaipNetworkId &&
nextNetworkId &&
prevCaipNetworkId !== nextNetworkId &&
this.caipNetwork?.name !== 'Unknown Network'
) {
RouterController.goBack()
}
this.caipNetwork = nextCaipNetwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,16 @@ export class W3mUnsupportedChainView extends LitElement {
'supportsAllNetworks',
network.chainNamespace
)
const caipNetwork = ChainController.state.activeCaipNetwork

const routerData = RouterController.state.data

if (caipAddress && caipNetwork?.caipNetworkId !== network.caipNetworkId) {
if (caipAddress) {
if (approvedCaipNetworkIds?.includes(network.caipNetworkId)) {
await ChainController.switchActiveNetwork(network)
} else if (supportsAllNetworks) {
RouterController.push('SwitchNetwork', { ...routerData, network })
} else {
RouterController.push('SwitchNetwork', { ...routerData, network })
}
} else if (!caipAddress) {
ChainController.setActiveCaipNetwork(network)
Expand Down
17 changes: 14 additions & 3 deletions packages/siwe/src/mapToSIWX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ export function mapToSIWX(siwe: AppKitSIWEClient): SIWXConfig {
}

const session = await getSession()
const isDifferentAddress =
session && session.address !== CoreHelperUtil.getPlainAddress(activeCaipAddress)

const compareSessionAddress = session?.address.toLowerCase()
const compareCaipAddress = CoreHelperUtil?.getPlainAddress(activeCaipAddress)?.toLowerCase()

const isDifferentAddress = session && compareSessionAddress !== compareCaipAddress

if (isDifferentAddress) {
await siwe.methods.signOut()
Expand Down Expand Up @@ -146,7 +149,15 @@ export function mapToSIWX(siwe: AppKitSIWEClient): SIWXConfig {
const siweSession = await getSession()

const siweCaipNetworkId = `eip155:${siweSession?.chainId}`
if (!siweSession || siweSession.address !== address || siweCaipNetworkId !== chainId) {

const compareSessionAddress = siweSession?.address.toLowerCase()
const compareCaipAddress = address?.toLowerCase()

if (
!siweSession ||
compareSessionAddress !== compareCaipAddress ||
siweCaipNetworkId !== chainId
) {
return []
}

Expand Down
Loading