-
Notifications
You must be signed in to change notification settings - Fork 158
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
feat: OnchainKitProvider
default dependencies
#1589
base: main
Are you sure you want to change the base?
Changes from 1 commit
6c46896
367e1af
147d8eb
35688c1
6c0a5f2
4a51003
463edb7
82d355b
e8d109e
976f60e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { createConfig } from 'wagmi'; | ||
import { http } from 'wagmi'; | ||
import { base, baseSepolia } from 'wagmi/chains'; | ||
import { coinbaseWallet } from 'wagmi/connectors'; | ||
import { createWagmiConfig } from './createWagmiConfig'; | ||
|
||
// Mock the imported modules | ||
vi.mock('wagmi', async () => { | ||
const actual = await vi.importActual('wagmi'); | ||
return { | ||
...actual, | ||
createConfig: vi.fn(), | ||
createStorage: vi.fn(), | ||
}; | ||
}); | ||
|
||
vi.mock('wagmi/chains', async () => { | ||
const actual = await vi.importActual('wagmi/chains'); | ||
return { | ||
...actual, | ||
base: { id: 8453 }, | ||
baseSepolia: { id: 84532 }, | ||
}; | ||
}); | ||
|
||
vi.mock('wagmi/connectors', async () => { | ||
const actual = await vi.importActual('wagmi/connectors'); | ||
return { | ||
...actual, | ||
coinbaseWallet: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe('createWagmiConfig', () => { | ||
it('should create config with default values when no parameters are provided', () => { | ||
createWagmiConfig({}); | ||
expect(createConfig).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
chains: [base, baseSepolia], | ||
ssr: true, | ||
transports: { | ||
[base.id]: expect.any(Function), | ||
[baseSepolia.id]: expect.any(Function), | ||
}, | ||
}), | ||
); | ||
expect(coinbaseWallet).toHaveBeenCalledWith({ | ||
appName: undefined, | ||
appLogoUrl: undefined, | ||
preference: 'smartWalletOnly', | ||
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. Is this going to force the components to use smart wallet ? thinking this might be related to the issue i'm having where i'm unable to connect to EOA |
||
}); | ||
}); | ||
|
||
it('should create config with custom values when parameters are provided', () => { | ||
const customConfig = { | ||
appearance: { | ||
name: 'Custom App', | ||
logo: 'https://example.com/logo.png', | ||
}, | ||
}; | ||
createWagmiConfig({ | ||
apiKey: 'test-api-key', | ||
appName: customConfig.appearance.name, | ||
appLogoUrl: customConfig.appearance.logo, | ||
}); | ||
expect(createConfig).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
chains: [base, baseSepolia], | ||
ssr: true, | ||
transports: { | ||
[base.id]: expect.any(Function), | ||
[baseSepolia.id]: expect.any(Function), | ||
}, | ||
}), | ||
); | ||
expect(coinbaseWallet).toHaveBeenCalledWith({ | ||
appName: 'Custom App', | ||
appLogoUrl: 'https://example.com/logo.png', | ||
preference: 'smartWalletOnly', | ||
}); | ||
}); | ||
|
||
it('should use API key in transports when provided', () => { | ||
const testApiKey = 'test-api-key'; | ||
const result = createWagmiConfig({ apiKey: testApiKey }); | ||
expect(result).toContain( | ||
http(`https://api.developer.coinbase.com/rpc/v1/base/${testApiKey}`), | ||
); | ||
expect(result).toContain( | ||
http( | ||
`https://api.developer.coinbase.com/rpc/v1/base-sepolia/${testApiKey}`, | ||
), | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { http, cookieStorage, createConfig, createStorage } from 'wagmi'; | ||
import { base, baseSepolia } from 'wagmi/chains'; | ||
import { coinbaseWallet } from 'wagmi/connectors'; | ||
import type { CreateWagmiConfigParams } from './types'; | ||
|
||
// createWagmiConfig returns a WagmiConfig (https://wagmi.sh/react/api/createConfig) using OnchainKit provided settings. | ||
// This function should only be used if the user does not provide WagmiProvider as a parent in the React context. | ||
export const createWagmiConfig = ({ | ||
apiKey, | ||
appName, | ||
appLogoUrl, | ||
}: CreateWagmiConfigParams) => { | ||
return createConfig({ | ||
chains: [base, baseSepolia], | ||
connectors: [ | ||
coinbaseWallet({ | ||
appName, | ||
appLogoUrl, | ||
preference: 'smartWalletOnly', | ||
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. should we set this to 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. seems like it'd be a more flexible implementation, unless we just really want to push to smart wallet. 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. we need to add a second connector for EOA |
||
}), | ||
], | ||
storage: createStorage({ | ||
storage: cookieStorage, | ||
}), | ||
ssr: true, | ||
transports: { | ||
[base.id]: apiKey | ||
? http(`https://api.developer.coinbase.com/rpc/v1/base/${apiKey}`) | ||
: http(), | ||
[baseSepolia.id]: apiKey | ||
? http( | ||
`https://api.developer.coinbase.com/rpc/v1/base-sepolia/${apiKey}`, | ||
) | ||
: http(), | ||
Comment on lines
+27
to
+34
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. uses Base networks by default and sets the RPC as the CDP Node |
||
}, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { type QueryClient, useQueryClient } from '@tanstack/react-query'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { type Config, WagmiProviderNotFoundError, useConfig } from 'wagmi'; | ||
import { useProviderDependencies } from './useProviderDependencies'; | ||
|
||
// Mock the wagmi and react-query hooks | ||
vi.mock('wagmi', async () => { | ||
const actual = await vi.importActual('wagmi'); | ||
return { | ||
...actual, | ||
useConfig: vi.fn(), | ||
}; | ||
}); | ||
|
||
vi.mock('@tanstack/react-query', async () => { | ||
const actual = await vi.importActual('@tanstack/react-query'); | ||
return { | ||
...actual, | ||
useQueryClient: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe('useProviderDependencies', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return both configs when they exist', () => { | ||
const mockWagmiConfig = { testWagmi: true } as unknown as Config; | ||
const mockQueryClient = { testQuery: true } as unknown as QueryClient; | ||
vi.mocked(useConfig).mockReturnValue(mockWagmiConfig); | ||
vi.mocked(useQueryClient).mockReturnValue(mockQueryClient); | ||
const { result } = renderHook(() => useProviderDependencies()); | ||
expect(result.current).toEqual({ | ||
providedWagmiConfig: mockWagmiConfig, | ||
providedQueryClient: mockQueryClient, | ||
}); | ||
}); | ||
|
||
it('should handle missing WagmiProvider gracefully', () => { | ||
vi.mocked(useConfig).mockImplementation(() => { | ||
throw new WagmiProviderNotFoundError(); | ||
}); | ||
const mockQueryClient = { testQuery: true } as unknown as QueryClient; | ||
vi.mocked(useQueryClient).mockReturnValue(mockQueryClient); | ||
const { result } = renderHook(() => useProviderDependencies()); | ||
expect(result.current).toEqual({ | ||
providedWagmiConfig: null, | ||
providedQueryClient: mockQueryClient, | ||
}); | ||
}); | ||
|
||
it('should handle missing QueryClient gracefully', () => { | ||
const mockWagmiConfig = { testWagmi: true } as unknown as Config; | ||
vi.mocked(useConfig).mockReturnValue(mockWagmiConfig); | ||
vi.mocked(useQueryClient).mockImplementation(() => { | ||
throw new Error('No QueryClient set, use QueryClientProvider to set one'); | ||
}); | ||
const { result } = renderHook(() => useProviderDependencies()); | ||
expect(result.current).toEqual({ | ||
providedWagmiConfig: mockWagmiConfig, | ||
providedQueryClient: null, | ||
}); | ||
}); | ||
|
||
it('should log non-WagmiProvider errors and return null config', () => { | ||
const consoleErrorSpy = vi | ||
.spyOn(console, 'error') | ||
.mockImplementation(() => {}); | ||
const error = new Error('Different error'); | ||
vi.mocked(useConfig).mockImplementation(() => { | ||
throw error; | ||
}); | ||
const mockQueryClient = { testQuery: true } as unknown as QueryClient; | ||
vi.mocked(useQueryClient).mockReturnValue(mockQueryClient); | ||
const { result } = renderHook(() => useProviderDependencies()); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'Error fetching WagmiProvider, using default:', | ||
error, | ||
); | ||
expect(result.current).toEqual({ | ||
providedWagmiConfig: null, | ||
providedQueryClient: mockQueryClient, | ||
}); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should log non-QueryClient provider errors and return null client', () => { | ||
const consoleErrorSpy = vi | ||
.spyOn(console, 'error') | ||
.mockImplementation(() => {}); | ||
const mockWagmiConfig = { testWagmi: true } as unknown as Config; | ||
vi.mocked(useConfig).mockReturnValue(mockWagmiConfig); | ||
const error = new Error('Different query error'); | ||
vi.mocked(useQueryClient).mockImplementation(() => { | ||
throw error; | ||
}); | ||
const { result } = renderHook(() => useProviderDependencies()); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'Error fetching QueryClient, using default:', | ||
error, | ||
); | ||
expect(result.current).toEqual({ | ||
providedWagmiConfig: mockWagmiConfig, | ||
providedQueryClient: null, | ||
}); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since Wagmi requires QueryClient, the only time 1 dep is missing should be when there is a QueryClient, but no Wagmi? Seems like an edge case, but would be possible to support this usecase, no? or, should we adjust the console.errors at least to only state using default if both don't exist and otherwise prompt the user to fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if 1 dep is missing, we shouldn't modify the default behavior and allow the app to display the typical error message instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in my mind, if you provide 1 of the 2 deps then it should default to the same logic/behavior as before this PR which is
Unhandled Runtime Error