Skip to content

Commit

Permalink
refactor: send statusChange event to sync wagmi status with account c…
Browse files Browse the repository at this point in the history
…ontroller (#3329)

Co-authored-by: tomiir <rocchitomas@gmail.com>
  • Loading branch information
enesozturk and tomiir authored Nov 26, 2024
1 parent ba72eca commit 9f48a1d
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 94 deletions.
17 changes: 15 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.0.0/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "reown-com/appkit" }],
"changelog": ["@changesets/changelog-github",
{ "repo": "reown-com/appkit" }],
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["@examples/*", "@apps/gallery-new", "@apps/laboratory", "@apps/laboratory-new", "@reown/appkit-new", "@reown/appkit-scaffold-ui-new", "@reown/appkit-ui-new", "@reown/appkit-adapter-bitcoin"]
"ignore": [
"@examples/*",
"@apps/demo",
"@apps/gallery",
"@apps/gallery-new",
"@apps/laboratory",
"@apps/laboratory-new",
"@reown/appkit-new",
"@reown/appkit-scaffold-ui-new",
"@reown/appkit-ui-new",
"@reown/appkit-adapter-bitcoin",
"@reown/appkit-adapter-polkadot"
]
}
20 changes: 20 additions & 0 deletions .changeset/two-glasses-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': 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
---

Sync wagmi status with account controller status
41 changes: 41 additions & 0 deletions apps/laboratory/src/pages/library/wagmi-no-ssr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { WagmiProvider } from 'wagmi'
import { AppKitButtons } from '../../components/AppKitButtons'
import { WagmiTests } from '../../components/Wagmi/WagmiTests'
import { WagmiModalInfo } from '../../components/Wagmi/WagmiModalInfo'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { createAppKit } from '@reown/appkit/react'
import { ConstantsUtil } from '../../utils/ConstantsUtil'
import { ThemeStore } from '../../utils/StoreUtil'

const queryClient = new QueryClient()

const wagmiAdapter = new WagmiAdapter({
networks: ConstantsUtil.EvmNetworks,
projectId: ConstantsUtil.ProjectId
})

const modal = createAppKit({
adapters: [wagmiAdapter],
networks: ConstantsUtil.EvmNetworks,
projectId: ConstantsUtil.ProjectId,
features: {
analytics: true
},
customWallets: ConstantsUtil.CustomWallets
})

const config = wagmiAdapter.wagmiConfig
ThemeStore.setModal(modal)

export default function Wagmi() {
return (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<AppKitButtons />
<WagmiModalInfo />
<WagmiTests config={config} />
</QueryClientProvider>
</WagmiProvider>
)
}
13 changes: 8 additions & 5 deletions examples/next-ethers/src/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { EthersAdapter } from '@reown/appkit-adapter-ethers'
import { mainnet, arbitrum, avalanche, base, optimism, polygon } from '@reown/appkit/networks'
import { type ReactNode } from 'react'

const projectId = 'Your project ID'
const projectId = process.env['NEXT_PUBLIC_PROJECT_ID']
if (!projectId) {
throw new Error('NEXT_PUBLIC_PROJECT_ID is not set')
}

const ethersAdapter = new EthersAdapter()

Expand All @@ -14,10 +17,10 @@ createAppKit({
projectId,
networks: [mainnet, arbitrum, avalanche, base, optimism, polygon],
metadata: {
name: 'My App',
description: 'My app description',
url: 'https://myapp.com',
icons: ['https://myapp.com/favicon.ico']
name: 'AppKit Next.js Ethers Example',
description: 'Example Next.js application implementing AppKit with Ethers adapter',
url: 'https://reown.com/appkit',
icons: ['https://avatars.githubusercontent.com/u/179229932?s=200&v=4']
},
enableEIP6963: true,
enableCoinbase: true
Expand Down
6 changes: 1 addition & 5 deletions examples/next-wagmi/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cookieStorage, createStorage } from '@wagmi/core'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import {
mainnet,
Expand All @@ -22,10 +21,7 @@ export const networks = [mainnet, arbitrum, avalanche, base, optimism, polygon]
]

export const wagmiAdapter = new WagmiAdapter({
storage: createStorage({
storage: cookieStorage
}),
ssr: true,
ssr: false,
networks,
projectId
})
Expand Down
3 changes: 1 addition & 2 deletions packages/appkit-new/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"build": "tsc --build tsconfig.build.json",
"watch": "tsc --watch",
"typecheck": "tsc --noEmit",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"test": "vitest run --coverage.enabled=true -- coverage.reporter=json --coverage.reporter=json-summary --coverage.reportOnFailure=true"
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/exports/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PACKAGE_VERSION = '1.5.1'
export const PACKAGE_VERSION = '1.5.2'
6 changes: 6 additions & 0 deletions packages/appkit/src/adapters/ChainAdapterBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
} from '@reown/appkit-common'
import type { ChainAdapterConnector } from './ChainAdapterConnector.js'
import {
AccountController,
OptionsController,
ThemeController,
type AccountControllerState,
type Connector as AppKitConnector,
type AuthConnector,
type Metadata,
Expand Down Expand Up @@ -152,6 +154,10 @@ export abstract class AdapterBlueprint<
})
}

protected setStatus(status: AccountControllerState['status'], chainNamespace?: ChainNamespace) {
AccountController.setStatus(status, chainNamespace)
}

/**
* Adds an event listener for a specific event.
* @template T
Expand Down
62 changes: 42 additions & 20 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
type EstimateGasTransactionArgs,
type AccountControllerState,
type AdapterNetworkState,
SIWXUtil
SIWXUtil,
type ConnectionStatus
} from '@reown/appkit-core'
import {
AccountController,
Expand Down Expand Up @@ -380,6 +381,7 @@ export class AppKit {
}

public setStatus: (typeof AccountController)['setStatus'] = (status, chain) => {
StorageUtil.setConnectionStatus(status as ConnectionStatus)
AccountController.setStatus(status, chain)
}

Expand Down Expand Up @@ -946,15 +948,15 @@ export class AppKit {
const providerType =
ProviderUtil.state.providerIds[ChainController.state.activeChain as ChainNamespace]

if (providerType === 'AUTH') {
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_AUTH) {
try {
ChainController.state.activeChain = caipNetwork.chainNamespace
await this.connectionControllerClient?.connectExternal?.({
id: 'ID_AUTH',
id: UtilConstantsUtil.AUTH_CONNECTOR_ID,
provider: this.authProvider,
chain: caipNetwork.chainNamespace,
chainId: caipNetwork.id,
type: 'AUTH',
type: UtilConstantsUtil.CONNECTOR_TYPE_AUTH as ConnectorType,
caipNetwork
})
} catch (error) {
Expand Down Expand Up @@ -1013,21 +1015,13 @@ export class AppKit {
this.setLoading(true)
const isLoginEmailUsed = provider.getLoginEmailUsed()
this.setLoading(isLoginEmailUsed)
const { isConnected } = await provider.isConnected()
if (isConnected && this.connectionControllerClient?.connectExternal) {
this.connectionControllerClient?.connectExternal({
id: 'ID_AUTH',
info: {
name: 'ID_AUTH'
},
type: 'AUTH',
provider,
chainId: ChainController.state.activeCaipNetwork?.id
})
} else {
this.setLoading(false)

if (isLoginEmailUsed) {
this.setStatus('connecting', ChainController.state.activeChain as ChainNamespace)
}

const { isConnected } = await provider.isConnected()

provider.onRpcRequest((request: W3mFrameTypes.RPCRequest) => {
if (W3mFrameHelpers.checkIfRequestExists(request)) {
if (!W3mFrameHelpers.checkIfRequestIsSafe(request)) {
Expand Down Expand Up @@ -1068,7 +1062,7 @@ export class AppKit {
})
provider.onNotConnected(() => {
const connectedConnector = SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR)
const isConnectedWithAuth = connectedConnector === 'ID_AUTH'
const isConnectedWithAuth = connectedConnector === UtilConstantsUtil.AUTH_CONNECTOR_ID
if (!isConnected && isConnectedWithAuth) {
this.setCaipAddress(undefined, ChainController.state.activeChain as ChainNamespace)
this.setLoading(false)
Expand All @@ -1079,9 +1073,9 @@ export class AppKit {
})
provider.onConnect(async user => {
this.syncProvider({
type: 'AUTH',
type: UtilConstantsUtil.CONNECTOR_TYPE_AUTH as ConnectorType,
provider,
id: 'ID_AUTH',
id: UtilConstantsUtil.AUTH_CONNECTOR_ID,
chainNamespace: ChainController.state.activeChain as ChainNamespace
})

Expand Down Expand Up @@ -1127,6 +1121,21 @@ export class AppKit {
ChainController.state.activeChain as ChainNamespace
)
})

if (isConnected && this.connectionControllerClient?.connectExternal) {
await this.connectionControllerClient?.connectExternal({
id: UtilConstantsUtil.AUTH_CONNECTOR_ID,
info: { name: UtilConstantsUtil.AUTH_CONNECTOR_ID },
type: UtilConstantsUtil.CONNECTOR_TYPE_AUTH as ConnectorType,
provider,
chainId: ChainController.state.activeCaipNetwork?.id
})
this.setLoading(false)
this.setStatus('connected', ChainController.state.activeChain as ChainNamespace)
} else {
this.setLoading(false)
this.setStatus('disconnected', ChainController.state.activeChain as ChainNamespace)
}
}

private listenWalletConnect() {
Expand Down Expand Up @@ -1176,6 +1185,13 @@ export class AppKit {

private listenAdapter(chainNamespace: ChainNamespace) {
const adapter = this.getAdapter(chainNamespace)
const connectionStatus = StorageUtil.getConnectionStatus()

if (connectionStatus === 'connected') {
this.setStatus('connecting', chainNamespace)
} else {
this.setStatus(connectionStatus, chainNamespace)
}

adapter?.on('switchNetwork', ({ address, chainId }) => {
if (chainId && this.caipNetworks?.find(n => n.id === chainId)) {
Expand Down Expand Up @@ -1526,6 +1542,7 @@ export class AppKit {
connectedConnector !== UtilConstantsUtil.CONNECTOR_TYPE_W3M_AUTH &&
connectedNamespace
) {
this.setStatus('connecting', connectedNamespace as ChainNamespace)
const adapter = this.getAdapter(connectedNamespace as ChainNamespace)
const res = await adapter?.syncConnection({
id: connectedConnector,
Expand All @@ -1537,11 +1554,16 @@ export class AppKit {
if (res) {
this.syncProvider({ ...res, chainNamespace: connectedNamespace as ChainNamespace })
await this.syncAccount({ ...res, chainNamespace: connectedNamespace as ChainNamespace })
this.setStatus('connected', connectedNamespace as ChainNamespace)
} else {
this.setStatus('disconnected', connectedNamespace as ChainNamespace)
}

if (!this.caipNetworks?.some(network => network.id === res?.chainId)) {
ChainController.showUnsupportedChainUI()
}
} else if (connectedConnector !== UtilConstantsUtil.CONNECTOR_TYPE_W3M_AUTH) {
this.setStatus('disconnected', ChainController.state.activeChain as ChainNamespace)
}
}

Expand Down
Loading

0 comments on commit 9f48a1d

Please sign in to comment.