Skip to content

withFeePayer

Creates a transport that routes transactions to a fee payer service when a feePayer is requested on an action. Learn more about Fee Sponsorship

Usage

example.ts
import { createClient, http, walletActions } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempo } from 'tempo.ts/chains'
import { withFeePayer } from 'tempo.ts/viem'
 
const client = createClient({
  account: privateKeyToAccount('0x...'),
  chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
  transport: withFeePayer( 
    http(), 
    http('http://localhost:3000'), 
    { policy: 'sign-only' }, 
  ), 
}).extend(walletActions)
 
// Regular transaction
const receipt1 = await client.sendTransactionSync({
  to: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
})
 
// Sponsored transaction 
const receipt2 = await client.sendTransactionSync({ 
  feePayer: true, 
  to: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb', 
}) 

Example Fee Payer Service

Below is an end-to-end example of a client/server fee payer setup.

See server.ts for the server-side implementation. It uses Handler.feePayer provided by tempo.ts/server to handle fee payer requests.

client.ts
import { createClient, http, walletActions } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempo } from 'tempo.ts/chains'
import { withFeePayer } from 'tempo.ts/viem'
 
const client = createClient({
  account: privateKeyToAccount('0x...'),
  chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
  transport: withFeePayer( 
    http(), 
    http('http://localhost:3000'), 
    { policy: 'sign-only' }, 
  ), 
}).extend(walletActions)
 
const hash = await client.sendTransactionSync({
  feePayer: true,
  to: '0x0000000000000000000000000000000000000000',
})

Return Type

type ReturnType = Transport<'feePayer'>

Parameters

defaultTransport

  • Type: Transport

The default transport to use for regular (non-sponsored) transactions.

relayTransport

  • Type: Transport

The relay transport to use for sponsored transactions. This should point to a fee payer service that will sign and submit the transaction with a fee payer signature.

Parameters (optional)

  • Type: withFeePayer.Parameters

Options for withFeePayer usage.

policy (optional)

  • Type: 'sign-only' | 'sign-and-broadcast'
  • Default: 'sign-only'

Controls how the fee payer handles sponsored transactions:

  • 'sign-only': Fee payer co-signs the transaction and returns it to the client transport, which then broadcasts it via the default transport.

  • 'sign-and-broadcast': Fee payer co-signs and broadcasts the transaction directly. The fee payer service handles both signing and submission to the blockchain.