Skip to content

withFeePayer

Creates a transport that routes transactions to a fee payer service when a feePayer is requested on an action.

Usage

example.ts
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoTestnet } from 'viem/chains'
import { withFeePayer } from 'viem/tempo'
 
const client = createWalletClient({
  account: privateKeyToAccount('0x...'),
  chain: tempoTestnet,
  transport: withFeePayer(
    http(),                               // โ† Default Transport
    http('https://sponsor.example.com'),  // โ† Fee Payer Transport 
  ), 
})
 
// 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 { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoTestnet } from 'viem/chains'
import { withFeePayer } from 'viem/tempo'
 
const client = createWalletClient({
  account: privateKeyToAccount('0x...'),
  chain: tempoTestnet,
  transport: withFeePayer(
    http(), 
    http('http://localhost:3000'), 
  ), 
})
 
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.