11import { type Chain , type Client , createClient , http } from 'viem'
2+ import { withFeePayer } from 'viem/tempo'
23import type { MaybePromise } from '../internal/types.js'
34
45export function getResolver (
56 parameters : getResolver . Parameters & {
67 /** Default chain to use if not provided. */
78 chain ?: Chain | undefined
9+ /** Fee payer relay URL. When set, the transport is wrapped with `withFeePayer`. */
10+ feePayerUrl ?: string | undefined
811 /** RPC URLs keyed by chain ID. */
912 rpcUrl ?: ( { [ chainId : number ] : string } & object ) | undefined
1013 } ,
1114) : ( parameters : { chainId ?: number | undefined } ) => MaybePromise < Client > {
12- const { chain, getClient, rpcUrl } = parameters
15+ const { chain, feePayerUrl , getClient, rpcUrl } = parameters
1316
1417 if ( getClient ) {
1518 // When a default chain with serializers is provided (e.g. Tempo chain config),
1619 // ensure user-provided clients inherit those serializers. Without this, clients
1720 // created without the Tempo chain config will use the default viem serializer,
1821 // causing errors like "maxFeePerGas is not a valid Legacy Transaction attribute".
19- if ( ! chain ?. serializers ) return getClient
22+ if ( ! chain ?. serializers && ! feePayerUrl ) return getClient
2023 return async ( params ) => {
2124 const client = await getClient ( params )
22- if ( client . chain ?. serializers ?. transaction ) return client
25+
26+ // Wrap the client's transport with `withFeePayer` when a fee payer URL is provided.
27+ if ( feePayerUrl && client . transport . key !== 'feePayer' ) {
28+ const url = ( client . transport as { url ?: string } ) . url
29+ if ( url ) {
30+ const wrapped = createClient ( {
31+ chain : client . chain ,
32+ transport : withFeePayer ( http ( url ) , http ( feePayerUrl ) ) ,
33+ } )
34+ Object . assign ( client , { transport : wrapped . transport , request : wrapped . request } )
35+ }
36+ }
37+
38+ if ( ! chain ?. serializers || client . chain ?. serializers ?. transaction ) return client
2339 return Object . assign ( { } , client , {
2440 chain : {
2541 ...chain ,
@@ -40,9 +56,10 @@ export function getResolver(
4056 const resolvedChainId = chainId || Number ( Object . keys ( rpcUrl ) [ 0 ] ) !
4157 const url = rpcUrl [ resolvedChainId as keyof typeof rpcUrl ]
4258 if ( ! url ) throw new Error ( `No \`rpcUrl\` configured for \`chainId\` (${ resolvedChainId } ).` )
59+ const transport = feePayerUrl ? withFeePayer ( http ( url ) , http ( feePayerUrl ) ) : http ( url )
4360 return createClient ( {
4461 chain : { ...chain , id : resolvedChainId } as never ,
45- transport : http ( url ) ,
62+ transport,
4663 } )
4764 }
4865}
0 commit comments