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: ProviderUtil.getProvider('eip155') returns undefined when using wagmi adapter #3672

Merged
merged 7 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions .changeset/selfish-rivers-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': 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
'@reown/appkit-wallet-button': patch
---

Fixed an issue where `walletProvider` from the `useAppKitProvider` hook was `undefined` when the wallet was connected. This issue occurred only when using wagmi adapter.
11 changes: 8 additions & 3 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class WagmiAdapter extends AdapterBlueprint {
return formatUnits(params.value, params.decimals)
}

private addWagmiConnector(connector: Connector, options: AppKitOptions) {
private async addWagmiConnector(connector: Connector, options: AppKitOptions) {
/*
* We don't need to set auth connector or walletConnect connector
* from wagmi since we already set it in chain adapter blueprint
Expand All @@ -422,6 +422,8 @@ export class WagmiAdapter extends AdapterBlueprint {
return
}

const provider = (await connector.getProvider().catch(() => undefined)) as Provider | undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have to use as Provider | undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like wagmi's provider type doesn't match with our provider type. It's throwing this error:

Type 'unknown' is not assignable to type 'Provider | W3mFrameProvider | UniversalProvider | undefined'.ts(2322)

Copy link
Collaborator

Choose a reason for hiding this comment

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

should we even add the connector if it's undefined? Seems like a case where things would bug out

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 we should add connector even if there is no provider. Because wagmi has internal logic when it comes to connecting to a connector since we do this in the wagmi adapter.

await connect(this.wagmiConfig, { connector })


this.addConnector({
id: connector.id,
explorerId: PresetsUtil.ConnectorExplorerIds[connector.id],
Expand All @@ -433,17 +435,20 @@ export class WagmiAdapter extends AdapterBlueprint {
connector.id === CommonConstantsUtil.CONNECTOR_ID.INJECTED
? undefined
: { rdns: connector.id },
provider,
chain: this.namespace as ChainNamespace,
chains: []
})
}

public syncConnectors(options: AppKitOptions, appKit: AppKit) {
public async syncConnectors(options: AppKitOptions, appKit: AppKit) {
// Add wagmi connectors
this.addWagmiConnectors(options, appKit)

// Add current wagmi connectors to chain adapter blueprint
this.wagmiConfig.connectors.forEach(connector => this.addWagmiConnector(connector, options))
await Promise.all(
this.wagmiConfig.connectors.map(connector => this.addWagmiConnector(connector, options))
)

/*
* Watch for new connectors. This is needed because some EIP6963
Expand Down
47 changes: 44 additions & 3 deletions packages/adapters/wagmi/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ const mockCaipNetworks = CaipNetworksUtil.extendCaipNetworks(mockNetworks, {
const mockWagmiConfig = {
connectors: [
{
id: 'test-connector'
id: 'test-connector',
getProvider() {
return Promise.resolve({ connect: vi.fn(), request: vi.fn() })
}
}
],
_internal: {
Expand Down Expand Up @@ -101,10 +104,13 @@ describe('WagmiAdapter', () => {
expect(adapter.namespace).toBe('eip155')
})

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

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

expect(adapter.connectors).toStrictEqual([
{
Expand All @@ -115,6 +121,10 @@ describe('WagmiAdapter', () => {
imageId: undefined,
imageUrl: undefined,
info: { rdns: 'test-connector' },
provider: {
connect: expect.any(Function),
request: expect.any(Function)
},
name: undefined,
type: 'EXTERNAL'
}
Expand Down Expand Up @@ -156,6 +166,37 @@ describe('WagmiAdapter', () => {
`https://cloudflare-eth.com`
)
})

it('should add connector with provider', async () => {
const mockConnector = {
id: 'injected',
name: 'Injected Wallet',
type: 'injected',
getProvider() {
return Promise.resolve({ connect: vi.fn(), request: vi.fn() })
}
} as unknown as wagmiCore.Connector

await (adapter as any).addWagmiConnector(mockConnector)
tomiir marked this conversation as resolved.
Show resolved Hide resolved

expect(adapter.connectors).toStrictEqual([
{
chain: 'eip155',
chains: [],
explorerId: undefined,
id: 'injected',
imageId: '07ba87ed-43aa-4adf-4540-9e6a2b9cae00',
imageUrl: undefined,
info: undefined,
name: 'Browser Wallet',
provider: {
connect: expect.any(Function),
request: expect.any(Function)
},
type: 'INJECTED'
}
])
})
})

describe('WagmiAdapter - signMessage', () => {
Expand Down
Loading