accounts
Version:
Tempo Accounts SDK
116 lines • 4.53 kB
TypeScript
import { RpcRequest } from 'ox';
import { type Address, type Chain, type Transport } from 'viem';
import type { LocalAccount } from 'viem/accounts';
import { Transaction } from 'viem/tempo';
import { type Handler, from } from '../../Handler.js';
import * as Kv from '../../Kv.js';
import * as Tokenlist from '../tokenlist.js';
/**
* Instantiates a relay handler that proxies `eth_fillTransaction`
* with wallet-aware enrichment (fee token resolution, simulation,
* sponsorship, AMM resolution).
*
* @example
* ```ts
* import { Handler } from 'accounts/server'
*
* const handler = Handler.relay()
*
* // Plug handler into your server framework of choice:
* createServer(handler.listener) // Node.js
* Bun.serve(handler) // Bun
* Deno.serve(handler) // Deno
* app.use(handler.listener) // Express
* app.all('*', c => handler.fetch(c.req.raw)) // Hono
* export const GET = handler.fetch // Next.js
* export const POST = handler.fetch // Next.js
* ```
*
* @example
* With sponsorship
*
* ```ts
* import { privateKeyToAccount } from 'viem/accounts'
* import { Handler } from 'accounts/server'
*
* const handler = Handler.relay({
* feePayer: {
* account: privateKeyToAccount('0x...'),
* },
* })
* ```
*
* @param options - Options.
* @returns Request handler.
*/
export declare function relay(options?: relay.Options): Handler;
export declare namespace relay {
type Options = from.Options & {
/**
* Auto-swap options.
*/
autoSwap?: false | {
/** Slippage tolerance (e.g. 0.05 = 5%). @default 0.05 */
slippage?: number | undefined;
} | undefined;
/**
* TTL in seconds for cached `resolveTokens` results.
* @default 600 (10 minutes)
*/
cacheTtl?: number | undefined;
/**
* Supported chains. The handler resolves the client based on the
* `chainId` in the incoming transaction.
* @default [tempo, tempoModerato, tempoDevnet]
*/
chains?: readonly [Chain, ...Chain[]] | undefined;
/**
* Fee payer / sponsor configuration. When provided, the relay will
* sign `feePayerSignature` on the filled transaction.
*/
feePayer?: {
/** Account to use as the fee payer. */
account: LocalAccount;
/** Preferred fee token the sponsor wants to pay fees in. */
feeToken?: Address | undefined;
/**
* Validates whether to sponsor the transaction. When omitted, all
* transactions are sponsored. Return `false` to reject sponsorship.
*/
validate?: ((request: Transaction.TransactionRequest) => boolean | Promise<boolean>) | undefined;
/** Sponsor display name returned from `eth_fillTransaction`. */
name?: string | undefined;
/** Sponsor URL returned from `eth_fillTransaction`. */
url?: string | undefined;
} | undefined;
/**
* Kv store used to cache `resolveTokens` results across requests.
* Provide `Kv.cloudflare(env.KV)` for cross-instance caching, or omit
* for an in-process LRU.
* @default Kv.memory()
*/
kv?: Kv.Kv | undefined;
/**
* Resolves the list of known tokens for a chain. The relay checks
* `balanceOf` for each token and picks the one with the highest balance
* during fee token resolution.
* @default Fetches `https://tokenlist.tempo.xyz/list/:chainId`
*/
resolveTokens?: ((chainId: number) => readonly Tokenlist.Token[] | Promise<readonly Tokenlist.Token[]>) | undefined;
/**
* Relay features.
*
* - `'all'` — enables fee token resolution, auto-swap,
* fee payer, and simulation (balance diffs + fee breakdown).
* - `undefined` (default) — only fee payers.
*/
features?: 'all' | undefined;
/** Function to call before handling the request. */
onRequest?: ((request: RpcRequest.RpcRequest) => Promise<void>) | undefined;
/** Path to use for the handler. @default "/" */
path?: string | undefined;
/** Transports keyed by chain ID. Defaults to `http()` for each chain. */
transports?: Record<number, Transport> | undefined;
};
}
//# sourceMappingURL=relay.d.ts.map