Handler.feePayer
Creates a server handler that acts as a fee payer for transactions. This enables you to subsidize gas costs for your users by signing transactions with a dedicated fee payer account on your backend.
Usage
server.ts
import { Handler } from 'tempo.ts/server'
import { tempo } from 'tempo.ts/chains'
import { http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
const handler = Handler.feePayer({
account: privateKeyToAccount('0x...'),
chain: tempo({ feeToken: '0x20c0...0001' }),
path: '/fee-payer',
transport: http(),
})Then plug handler into your server framework of choice. For example:
createServer(handler.listener) // Node.js
Bun.serve(handler) // Bun
Deno.serve(handler) // Deno
app.all('*', c => handler.fetch(c.request)) // Elysia
app.use(handler.listener) // Express
app.use(c => handler.fetch(c.req.raw)) // Hono
export const GET = handler.fetch // Next.js
export const POST = handler.fetch // Next.jsHow It Works
The fee payer handler intercepts RPC requests and handles the following methods:
eth_signTransaction- Signs a transaction with the fee payer accounteth_signRawTransaction- Signs a pre-serialized transaction with the fee payer accounteth_sendRawTransaction- Signs and broadcasts a transaction with the fee payer accounteth_sendRawTransactionSync- Signs, broadcasts, and waits for confirmation
For each of these methods, the handler:
- Deserializes the transaction (if necessary)
- Signs the transaction with your fee payer account
- Returns the signed transaction or transaction hash
Parameters
account
- Type:
LocalAccount - Required: Yes
The account to use as the fee payer. This account will sign all transactions and pay the gas fees.
chain
- Type:
Chain
Chain (and fee token) to use.
transport
- Type:
Transport
Transport to use.
path
- Type:
string - Default:
'/' - Optional
The path where the handler will listen for requests.
onRequest
- Type:
(request: RpcRequest) => Promise<void> - Optional
A callback function that's called before processing each request. Useful for logging, rate limiting, or custom validation.