11import { getWagmiConnectorV2 } from '@binance/w3w-wagmi-connector-v2'
2- import { getEntryGatewayUrl } from '@universe/api'
32import {
43 createObservableTransport ,
5- createRpcConfigResolver ,
6- createUniRpcConfigResolver ,
4+ createUniRpcRoutedTransport ,
75 createUniRpcTransportFactory ,
86 getRpcObserver ,
97} from '@universe/chains'
108import { isE2eTestEnv , isTestEnv } from '@universe/environment'
11- import { FeatureFlags , getFeatureFlag } from '@universe/gating'
129import { UNISWAP_LOGO } from 'ui/src/assets'
1310import { UNISWAP_WEB_URL } from 'uniswap/src/constants/urls'
1411import { CONNECTION_PROVIDER_IDS } from 'uniswap/src/constants/web3'
1512import type { getChainInfo } from 'uniswap/src/features/chains/chainInfo'
1613import { ORDERED_EVM_CHAINS } from 'uniswap/src/features/chains/chainInfo'
1714import { RPCType } from 'uniswap/src/features/chains/types'
1815import { isTestnetChain } from 'uniswap/src/features/chains/utils'
19- import { selectRpcUrl } from 'uniswap/src/features/providers/rpcUrlSelector '
16+ import { defaultResolveRpcConfig } from 'uniswap/src/features/providers/resolveRpcConfig '
2017import { logger } from 'utilities/src/logger/logger'
2118import { getNonEmptyArrayOrThrow } from 'utilities/src/primitives/array'
2219import type { Chain } from 'viem'
@@ -111,16 +108,9 @@ function createWagmiConnectors(params: {
111108 : baseConnectors
112109}
113110
114- const webResolveRpcConfig = createRpcConfigResolver ( {
115- resolveUniRpcConfig : createUniRpcConfigResolver ( {
116- getFeatureFlag : ( ) => getFeatureFlag ( FeatureFlags . UniRpcEnabled ) ,
117- getEntryGatewayUrl,
118- requestSource : 'uniswap-web' ,
119- credentials : 'include' ,
120- } ) ,
121- selectLegacyRpcUrl : selectRpcUrl ,
122- } )
123-
111+ // Cookie-session UniRPC transport factory (web's injected session strategy).
112+ // The gating decision lives in the shared `defaultResolveRpcConfig` resolver;
113+ // this only constructs the UniRPC transport once that resolver says to use it.
124114const buildWebUniRpcTransport = createUniRpcTransportFactory ( {
125115 session : { type : 'cookies' } ,
126116} )
@@ -138,40 +128,42 @@ function createWagmiConfig(params: {
138128 chains : getNonEmptyArrayOrThrow ( ORDERED_EVM_CHAINS ) ,
139129 connectors,
140130 client ( { chain } ) {
141- const rpcConfig = webResolveRpcConfig ( { chainId : chain . id , rpcType : RPCType . Public } )
142- // Branch on the explicit `isUniRpc` flag — header presence used to be
143- // the implicit signal, which would have routed any legacy provider with
144- // static headers through the UniRPC transport by accident.
145- if ( rpcConfig ?. isUniRpc ) {
146- return createClient ( {
147- chain,
148- batch : { multicall : true } ,
149- pollingInterval : 12_000 ,
150- transport : createObservableTransport ( {
151- baseTransportFactory : buildWebUniRpcTransport ( {
152- config : { rpcUrl : rpcConfig . rpcUrl , headers : rpcConfig . headers ?? { } } ,
153- } ) ,
154- observer : getRpcObserver ( ) ,
155- meta : { chainId : chain . id , url : rpcConfig . rpcUrl } ,
156- } ) ,
157- } )
158- }
159-
131+ // wagmi builds this client once per chain and caches it for the session,
132+ // so the UniRPC-vs-legacy choice must NOT be snapshotted here: on app
133+ // start the gate behind `isUniRpc` is usually still unresolved (Statsig
134+ // inits async; a cold load has no cached value), and a snapshot would pin
135+ // the chain to the legacy Infura/QuickNode providers for the whole
136+ // session even after the gate turns on. createUniRpcRoutedTransport
137+ // re-reads the shared resolver per request, so the cached client
138+ // self-heals onto UniRPC the moment the gate resolves — same guarantee
139+ // ViemClientManager gets by re-resolving per `getViemClient` call.
160140 return createClient ( {
161141 chain,
162142 batch : { multicall : true } ,
163143 pollingInterval : 12_000 ,
164- transport : fallback (
165- orderedTransportUrls ( chain ) . map ( ( url ) =>
144+ transport : createUniRpcRoutedTransport ( {
145+ resolveRpcConfig : ( ) => defaultResolveRpcConfig ( { chainId : chain . id , rpcType : RPCType . Public } ) ,
146+ buildUniRpcTransport : ( rpcConfig ) =>
166147 createObservableTransport ( {
167- baseTransportFactory : http ( url , {
168- onFetchResponse : ( response ) => onFetchResponse ( response , chain , url ) ,
148+ baseTransportFactory : buildWebUniRpcTransport ( {
149+ config : { rpcUrl : rpcConfig . rpcUrl , headers : rpcConfig . headers ?? { } } ,
169150 } ) ,
170151 observer : getRpcObserver ( ) ,
171- meta : { chainId : chain . id , url } ,
152+ meta : { chainId : chain . id , url : rpcConfig . rpcUrl } ,
172153 } ) ,
173- ) ,
174- ) ,
154+ buildLegacyTransport : ( ) =>
155+ fallback (
156+ orderedTransportUrls ( chain ) . map ( ( url ) =>
157+ createObservableTransport ( {
158+ baseTransportFactory : http ( url , {
159+ onFetchResponse : ( response ) => onFetchResponse ( response , chain , url ) ,
160+ } ) ,
161+ observer : getRpcObserver ( ) ,
162+ meta : { chainId : chain . id , url } ,
163+ } ) ,
164+ ) ,
165+ ) ,
166+ } ) ,
175167 } )
176168 } ,
177169 } )
0 commit comments