accounts
Version:
Tempo Accounts SDK
275 lines • 9.97 kB
TypeScript
import { type Address, type Chain, type Transport } from 'viem';
import * as z from 'zod/mini';
import { type Handler, from } from '../../Handler.js';
import * as Kv from '../../Kv.js';
/** Zod schemas for the exchange handler's request and response payloads. */
export declare namespace schema {
/** Schemas for `POST /exchange/quote`. */
namespace quote {
/** Request body schema. */
const parameters: z.ZodMiniObject<{
amount: z.ZodMiniString<string>;
chainId: z.ZodMiniOptional<z.ZodMiniNumber<number>>;
pairToken: z.ZodMiniString<string>;
slippage: z.ZodMiniNumber<number>;
token: z.ZodMiniString<string>;
type: z.ZodMiniUnion<readonly [z.ZodMiniLiteral<"buy">, z.ZodMiniLiteral<"sell">]>;
}, z.core.$strip>;
/** Response body schema. */
const returns: z.ZodMiniObject<{
calls: z.ZodMiniReadonly<z.ZodMiniArray<z.ZodMiniObject<{
data: z.ZodMiniTemplateLiteral<`0x${string}`>;
to: z.ZodMiniTemplateLiteral<`0x${string}`>;
}, z.core.$strip>>>;
pairToken: z.ZodMiniObject<{
address: z.ZodMiniTemplateLiteral<`0x${string}`>;
amount: z.ZodMiniString<string>;
name: z.ZodMiniString<string>;
symbol: z.ZodMiniString<string>;
}, z.core.$strip>;
slippage: z.ZodMiniNumber<number>;
token: z.ZodMiniObject<{
address: z.ZodMiniTemplateLiteral<`0x${string}`>;
amount: z.ZodMiniString<string>;
name: z.ZodMiniString<string>;
symbol: z.ZodMiniString<string>;
}, z.core.$strip>;
type: z.ZodMiniUnion<readonly [z.ZodMiniLiteral<"buy">, z.ZodMiniLiteral<"sell">]>;
}, z.core.$strip>;
}
/** Schemas for `GET /exchange/tokens`. */
namespace tokens {
/** Query string schema. `chainId` is a decimal string when present. */
const parameters: z.ZodMiniObject<{
chainId: z.ZodMiniOptional<z.ZodMiniString<string>>;
}, z.core.$strip>;
/** Response body schema. */
const returns: z.ZodMiniObject<{
tokens: z.ZodMiniReadonly<z.ZodMiniArray<z.ZodMiniObject<{
address: z.ZodMiniTemplateLiteral<`0x${string}`>;
decimals: z.ZodMiniNumber<number>;
logoUri: z.ZodMiniOptional<z.ZodMiniString<string>>;
name: z.ZodMiniString<string>;
symbol: z.ZodMiniString<string>;
}, z.core.$strip>>>;
}, z.core.$strip>;
}
}
/**
* Instantiates a stablecoin-exchange handler that returns swap quotes plus
* the matching `calls` (approve + buy/sell) for the Tempo Stablecoin DEX.
*
* Exposes 2 endpoints:
* - `GET /exchange/tokens` — list known tokens for a chain (defaults to the
* first configured chain; pass `?chainId=N` to pick a different one).
* - `POST /exchange/quote` — return a quote and ready-to-submit calls.
*
* The returned value is a Hono app augmented with a Node `listener`. The full
* route schema is preserved on the type so consumers can derive a typed RPC
* client with `hc<ReturnType<typeof exchange>>(url)`.
*
* @example
* ```ts
* import { Handler } from 'accounts/server'
*
* const handler = Handler.exchange()
*
* // Plug handler into your server framework of choice:
* createServer(handler.listener)
* ```
*
* @example
* Typed RPC client
*
* ```ts
* import { hc } from 'hono/client'
* import { Handler } from 'accounts/server'
*
* const handler = Handler.exchange()
* type Handler = typeof handler
*
* const client = hc<Handler>('https://example.com')
* const res = await client.exchange.quote.$post({
* json: {
* amount: '1',
* pairToken: 'pathUSD',
* slippage: 0.01,
* token: 'USDC',
* type: 'sell',
* },
* })
* if (res.ok) {
* const { calls, pairToken, token } = await res.json()
* // fully typed
* }
* ```
*
* @param options - Options.
* @returns Request handler.
*/
export declare function exchange<const path extends string = '/exchange'>(options?: exchange.Options<path>): Handler<import("hono/hono-base").HonoBase<import("hono/types").BlankEnv, { [K in import("hono/types").MergePath<"/", `${path}/tokens`>]: {
$get: {
input: import("hono/types").AddParam<{
query: {
chainId?: string | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/tokens`>>;
output: {
error: string;
issues: {
path: string;
message: string;
}[];
};
outputFormat: "json";
status: 400;
} | {
input: import("hono/types").AddParam<{
query: {
chainId?: string | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/tokens`>>;
output: {
tokens: readonly {
address: `0x${string}`;
decimals: number;
name: string;
symbol: string;
logoUri?: string | undefined;
}[];
};
outputFormat: "json";
status: import("hono/utils/http-status").ContentfulStatusCode;
} | {
input: import("hono/types").AddParam<{
query: {
chainId?: string | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/tokens`>>;
output: {
error: string;
};
outputFormat: "json";
status: 400;
};
}; } & { [K_1 in import("hono/types").MergePath<"/", `${path}/quote`>]: {
$post: {
input: import("hono/types").AddParam<{
json: {
amount: string;
pairToken: string;
slippage: number;
token: string;
type: "buy" | "sell";
chainId?: number | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/quote`>>;
output: {
error: string;
issues: {
path: string;
message: string;
}[];
};
outputFormat: "json";
status: 400;
} | {
input: import("hono/types").AddParam<{
json: {
amount: string;
pairToken: string;
slippage: number;
token: string;
type: "buy" | "sell";
chainId?: number | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/quote`>>;
output: {
calls: readonly {
data: `0x${string}`;
to: `0x${string}`;
}[];
pairToken: {
address: `0x${string}`;
amount: string;
name: string;
symbol: string;
};
slippage: number;
token: {
address: `0x${string}`;
amount: string;
name: string;
symbol: string;
};
type: "buy" | "sell";
};
outputFormat: "json";
status: import("hono/utils/http-status").ContentfulStatusCode;
} | {
input: import("hono/types").AddParam<{
json: {
amount: string;
pairToken: string;
slippage: number;
token: string;
type: "buy" | "sell";
chainId?: number | undefined;
};
}, import("hono/types").MergePath<"/", `${path}/quote`>>;
output: {
error: string;
};
outputFormat: "json";
status: 400;
};
}; }, "/", import("hono/types").MergePath<"/", `${path}/quote`>>>;
export declare namespace exchange {
/** Options for `exchange()`. */
type Options<path extends string = string> = from.Options & {
/**
* TTL in seconds for cached tokenlist responses. On-chain token metadata
* is cached without expiry (immutable per address).
* @default 600 (10 minutes)
*/
cacheTtl?: number | undefined;
/**
* Supported chains. The first chain is used to resolve the client.
* @default [tempo, tempoModerato, tempoDevnet]
*/
chains?: readonly [Chain, ...Chain[]] | undefined;
/**
* Kv store used to cache network responses. Provide `Kv.cloudflare(env.KV)`
* for cross-instance caching, or omit for an in-process LRU.
* @default Kv.memory()
*/
kv?: Kv.Kv | undefined;
/** Function to call before handling the request. */
onRequest?: ((request: Request) => void | Promise<void>) | undefined;
/** Path prefix for the exchange endpoints. @default '/exchange' */
path?: path | undefined;
/**
* Resolves the list of known tokens for a chain. Used to resolve symbol
* references (e.g. `"USDC.e"`) to addresses + metadata. Address references
* are matched against this list first, falling back to on-chain metadata.
* @default Fetches `https://tokenlist.tempo.xyz/list/:chainId`
*/
resolveTokens?: ((chainId: number) => readonly Token[] | Promise<readonly Token[]>) | undefined;
/** Transports keyed by chain ID. Defaults to `http()` per chain. */
transports?: Record<number, Transport> | undefined;
};
/** Resolved token metadata. */
type Token = {
/** Token address. */
address: Address;
/** Token decimals. */
decimals: number;
/** Token logo URI. */
logoUri?: string | undefined;
/** Token name. */
name: string;
/** Token symbol. */
symbol: string;
};
}
//# sourceMappingURL=exchange.d.ts.map