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

chore: adapter client improvements #3484

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cf1e0b1
chore: adapter improvement
magiziz Dec 13, 2024
995ee1e
chore: create universal provider and auth provider after adapter crea…
magiziz Dec 16, 2024
90aa105
chore: remove provider creation on adapter level
magiziz Dec 16, 2024
0a6286c
fix: merge conflicts
magiziz Dec 16, 2024
1c748ee
chore: remove code from bitcoin adapter to prevent build issues for now
magiziz Dec 16, 2024
5b941a9
chore: await controllers
magiziz Dec 16, 2024
59c1480
revert: edit bitcoin adapter
magiziz Dec 16, 2024
9f00cd5
chore: migrate logic on chain blueprint level instead of adapter level
magiziz Dec 16, 2024
8779eee
chore: tweak wagmi code
magiziz Dec 16, 2024
2f89a13
chore: add comment for visibility
magiziz Dec 16, 2024
17ece78
chore: tweak comment
magiziz Dec 16, 2024
6c18680
revert: add setAuthProvider and setUniversalProvider undefined
magiziz Dec 16, 2024
a7901d4
chore: add mapToSIWX to AppKitSIWEClient
magiziz Dec 16, 2024
cfed5ba
chore: adding test for wagmi adapter
magiziz Dec 16, 2024
b709fb0
chore: add async operation for chain adapters
magiziz Dec 17, 2024
38f5768
chore: add tests
magiziz Dec 17, 2024
3493bc4
fix: merge conflicts
magiziz Dec 17, 2024
4fe8143
chore: tweak
magiziz Dec 17, 2024
c108ef3
chore: fix ci errors
magiziz Dec 17, 2024
0a8c9b0
chore: remove unused Connector type
magiziz Dec 17, 2024
e9524be
chore: check if auth provider exists
magiziz Dec 17, 2024
3152e62
chore: only fetch provider from auth connector
magiziz Dec 17, 2024
5dd7107
chore: fixes
magiziz Dec 17, 2024
2068b3c
chore: listen for sync connectors only
magiziz Dec 17, 2024
dd62ee4
revert: remove reconnect logic for wagmi during siwx login
magiziz Dec 17, 2024
7cfba60
chore: remove unused code
magiziz Dec 17, 2024
847cf4f
chore: mock connectors
magiziz Dec 17, 2024
c7dda55
Merge branch 'main' of github.com:reown-com/appkit into chore/adapter…
tomiir Dec 17, 2024
86ae60e
chore: prettier
magiziz Dec 17, 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
73 changes: 37 additions & 36 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type UniversalProvider from '@walletconnect/universal-provider'
import type { AppKitNetwork, BaseNetwork, CaipNetwork } from '@reown/appkit-common'
import type { AppKitNetwork, BaseNetwork, CaipNetwork, ChainNamespace } from '@reown/appkit-common'
import { AdapterBlueprint } from '@reown/appkit/adapters'
import { CoreHelperUtil } from '@reown/appkit-core'
import {
Expand Down Expand Up @@ -27,7 +27,8 @@ import {
getAccount,
prepareTransactionRequest,
reconnect,
watchPendingTransactions
watchPendingTransactions,
watchConnectors
} from '@wagmi/core'
import { type Chain } from '@wagmi/core/chains'

Expand Down Expand Up @@ -186,7 +187,6 @@ export class WagmiAdapter extends AdapterBlueprint {
}
}
})

watchConnections(this.wagmiConfig, {
onChange: connections => {
if (connections.length === 0) {
Expand Down Expand Up @@ -232,9 +232,7 @@ export class WagmiAdapter extends AdapterBlueprint {
customConnectors.push(
authConnector({
chains: this.wagmiChains,
options: { projectId: options.projectId },
provider: this.availableConnectors.find(c => c.id === ConstantsUtil.AUTH_CONNECTOR_ID)
?.provider as W3mFrameProvider
options: { projectId: options.projectId }
})
)
}
Expand Down Expand Up @@ -358,40 +356,43 @@ export class WagmiAdapter extends AdapterBlueprint {
return formatUnits(params.value, params.decimals)
}

public syncConnectors(options: AppKitOptions, appKit: AppKit) {
this.addWagmiConnectors(options, appKit)
private addWagmiConnector(connector: Connector, options: AppKitOptions) {
/*
* We don't need to set auth connector from wagmi
* since we already set it in chain adapter blueprint
*/
if (connector.id === ConstantsUtil.AUTH_CONNECTOR_ID) {
return
}

const connectors = this.wagmiConfig.connectors.map(connector => ({
...connector,
chain: this.namespace
}))
this.addConnector({
id: connector.id,
explorerId: PresetsUtil.ConnectorExplorerIds[connector.id],
imageUrl: options?.connectorImages?.[connector.id] ?? connector.icon,
name: PresetsUtil.ConnectorNamesMap[connector.id] ?? connector.name,
imageId: PresetsUtil.ConnectorImageIds[connector.id],
type: PresetsUtil.ConnectorTypesMap[connector.type] ?? 'EXTERNAL',
info:
connector.id === ConstantsUtil.INJECTED_CONNECTOR_ID ? undefined : { rdns: connector.id },
chain: this.namespace as ChainNamespace,
chains: []
})
}

const uniqueIds = new Set()
const filteredConnectors = connectors.filter(item => {
const isDuplicate = uniqueIds.has(item.id)
uniqueIds.add(item.id)
public syncConnectors(options: AppKitOptions, appKit: AppKit) {
// Add wagmi connectors
this.addWagmiConnectors(options, appKit)

return !isDuplicate
})
// Add current wagmi connectors to chain adapter blueprint
this.wagmiConfig.connectors.forEach(connector => this.addWagmiConnector(connector, options))

filteredConnectors.forEach(connector => {
const shouldSkip = ConstantsUtil.AUTH_CONNECTOR_ID === connector.id

const injectedConnector = connector.id === ConstantsUtil.INJECTED_CONNECTOR_ID

if (!shouldSkip && this.namespace) {
this.addConnector({
id: connector.id,
explorerId: PresetsUtil.ConnectorExplorerIds[connector.id],
imageUrl: options?.connectorImages?.[connector.id] ?? connector.icon,
name: PresetsUtil.ConnectorNamesMap[connector.id] ?? connector.name,
imageId: PresetsUtil.ConnectorImageIds[connector.id],
type: PresetsUtil.ConnectorTypesMap[connector.type] ?? 'EXTERNAL',
info: injectedConnector ? undefined : { rdns: connector.id },
chain: this.namespace,
chains: []
})
}
/*
* Watch for new connectors. This is needed because some EIP6963 connectors
* that are added later in the process the initial setup
*/
watchConnectors(this.wagmiConfig, {
onChange: connectors =>
connectors.forEach(connector => this.addWagmiConnector(connector, options))
})
}

Expand Down
1 change: 0 additions & 1 deletion packages/adapters/wagmi/src/connectors/AuthConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface W3mFrameProviderOptions {
export type AuthParameters = {
chains?: CreateConfigParameters['chains']
options: W3mFrameProviderOptions
provider: W3mFrameProvider
}

// -- Connector ------------------------------------------------------------------------------------
Expand Down
13 changes: 1 addition & 12 deletions packages/adapters/wagmi/src/connectors/AuthConnectorExport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { CreateConfigParameters } from '@wagmi/core'
import { authConnector as authConnectorWagmi } from './AuthConnector.js'
import { ErrorUtil } from '@reown/appkit-utils'
import { AlertController } from '@reown/appkit-core'
import { W3mFrameProviderSingleton } from '@reown/appkit/auth-provider'

interface W3mFrameProviderOptions {
projectId: string
Expand All @@ -14,13 +11,5 @@ export type AuthParameters = {
}

export function authConnector(parameters: AuthParameters) {
return authConnectorWagmi({
...parameters,
provider: W3mFrameProviderSingleton.getInstance({
projectId: parameters.options.projectId,
onTimeout: () => {
AlertController.open(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT, 'error')
}
})
})
return authConnectorWagmi(parameters)
}
22 changes: 22 additions & 0 deletions packages/adapters/wagmi/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {
watchPendingTransactions,
http
} from '@wagmi/core'
import * as wagmiCore from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { CaipNetworksUtil } from '@reown/appkit-utils'
import type UniversalProvider from '@walletconnect/universal-provider'
import { mockAppKit } from './mocks/AppKit'

vi.mock('@wagmi/core', async () => {
const actual = await vi.importActual('@wagmi/core')
Expand Down Expand Up @@ -97,6 +99,26 @@ describe('WagmiAdapter', () => {
expect(adapter.namespace).toBe('eip155')
})

it('should set wagmi connectors', () => {
vi.spyOn(wagmiCore, 'watchConnectors').mockImplementation(vi.fn())

adapter.syncConnectors({ networks: [mainnet], projectId: 'YOUR_PROJECT_ID' }, mockAppKit)

expect(adapter.connectors).toStrictEqual([
{
chain: 'eip155',
chains: [],
explorerId: undefined,
id: 'test-connector',
imageId: undefined,
imageUrl: undefined,
info: { rdns: 'test-connector' },
name: undefined,
type: 'EXTERNAL'
}
])
})

it('should not set info property for injected connector', () => {
const mockConnectors = [
{
Expand Down
10 changes: 9 additions & 1 deletion packages/appkit/src/adapters/ChainAdapterBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ import type { AppKitOptions } from '../utils/index.js'
import type { AppKit } from '../client.js'
import { snapshot } from 'valtio/vanilla'

type EventName = 'disconnect' | 'accountChanged' | 'switchNetwork' | 'pendingTransactions'
type EventName =
| 'disconnect'
| 'accountChanged'
| 'switchNetwork'
| 'connectors'
| 'pendingTransactions'
type EventData = {
disconnect: () => void
accountChanged: { address: string; chainId?: number | string }
switchNetwork: { address?: string; chainId: number | string }
connectors: ChainAdapterConnector[]
pendingTransactions: () => void
}
type EventCallback<T extends EventName> = (data: EventData[T]) => void
Expand Down Expand Up @@ -155,6 +161,8 @@ export abstract class AdapterBlueprint<

return true
})

this.emit('connectors', this.availableConnectors)
Copy link
Collaborator

Choose a reason for hiding this comment

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

what happens if multiple adapters emit this at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's fine since we already filter them here and it ConnectorController as well.

}

protected setStatus(status: AccountControllerState['status'], chainNamespace?: ChainNamespace) {
Expand Down
1 change: 1 addition & 0 deletions packages/appkit/src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { AdapterBlueprint } from './ChainAdapterBlueprint.js'
export type { ChainAdapterConnector } from './ChainAdapterConnector.js'
Loading
Loading