1
+ import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
1
2
import { createContext , useMemo } from 'react' ;
3
+ import { WagmiProvider } from 'wagmi' ;
2
4
import { ONCHAIN_KIT_CONFIG , setOnchainKitConfig } from './OnchainKitConfig' ;
5
+ import { createWagmiConfig } from './createWagmiConfig' ;
3
6
import { COINBASE_VERIFIED_ACCOUNT_SCHEMA_ID } from './identity/constants' ;
4
7
import { checkHashLength } from './internal/utils/checkHashLength' ;
5
8
import type { OnchainKitContextType , OnchainKitProviderReact } from './types' ;
9
+ import { useProviderDependencies } from './useProviderDependencies' ;
6
10
7
11
export const OnchainKitContext =
8
12
createContext < OnchainKitContextType > ( ONCHAIN_KIT_CONFIG ) ;
@@ -24,6 +28,7 @@ export function OnchainKitProvider({
24
28
throw Error ( 'EAS schemaId must be 64 characters prefixed with "0x"' ) ;
25
29
}
26
30
31
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ignore
27
32
const value = useMemo ( ( ) => {
28
33
const defaultPaymasterUrl = apiKey
29
34
? `https://api.developer.coinbase.com/rpc/v1/${ chain . name
@@ -36,8 +41,8 @@ export function OnchainKitProvider({
36
41
chain : chain ,
37
42
config : {
38
43
appearance : {
39
- name : config ?. appearance ?. name ,
40
- logo : config ?. appearance ?. logo ,
44
+ name : config ?. appearance ?. name ?? 'Dapp' ,
45
+ logo : config ?. appearance ?. logo ?? '' ,
41
46
mode : config ?. appearance ?. mode ?? 'auto' ,
42
47
theme : config ?. appearance ?. theme ?? 'default' ,
43
48
} ,
@@ -51,6 +56,47 @@ export function OnchainKitProvider({
51
56
return onchainKitConfig ;
52
57
} , [ address , apiKey , chain , config , projectId , rpcUrl , schemaId ] ) ;
53
58
59
+ // Check the React context for WagmiProvider and QueryClientProvider
60
+ const { providedWagmiConfig, providedQueryClient } =
61
+ useProviderDependencies ( ) ;
62
+
63
+ const defaultConfig = useMemo ( ( ) => {
64
+ // IMPORTANT: Don't create a new Wagmi configuration if one already exists
65
+ // This prevents the user-provided WagmiConfig from being overriden
66
+ return (
67
+ providedWagmiConfig ||
68
+ createWagmiConfig ( {
69
+ apiKey,
70
+ appName : value . config . appearance . name ,
71
+ appLogoUrl : value . config . appearance . logo ,
72
+ } )
73
+ ) ;
74
+ } , [
75
+ apiKey ,
76
+ providedWagmiConfig ,
77
+ value . config . appearance . name ,
78
+ value . config . appearance . logo ,
79
+ ] ) ;
80
+ const defaultQueryClient = useMemo ( ( ) => {
81
+ // IMPORTANT: Don't create a new QueryClient if one already exists
82
+ // This prevents the user-provided QueryClient from being overriden
83
+ return providedQueryClient || new QueryClient ( ) ;
84
+ } , [ providedQueryClient ] ) ;
85
+
86
+ // If both dependencies are missing, return a context with default parent providers
87
+ // If only one dependency is provided, expect the user to also provide the missing one
88
+ if ( ! providedWagmiConfig && ! providedQueryClient ) {
89
+ return (
90
+ < WagmiProvider config = { defaultConfig } >
91
+ < QueryClientProvider client = { defaultQueryClient } >
92
+ < OnchainKitContext . Provider value = { value } >
93
+ { children }
94
+ </ OnchainKitContext . Provider >
95
+ </ QueryClientProvider >
96
+ </ WagmiProvider >
97
+ ) ;
98
+ }
99
+
54
100
return (
55
101
< OnchainKitContext . Provider value = { value } >
56
102
{ children }
0 commit comments