import {z} from 'zod' export type CreateApiAdaptersBridgeQuoteArgs = {'X-Steward-Request-Timestamp'?: string | undefined, 'X-Steward-Request-Expires-At'?: string | undefined, 'X-Steward-Signature'?: string | undefined, 'X-Steward-Signing-Key-Id'?: string | undefined, 'Idempotency-Key'?: string | undefined, body: {agentId?: string | undefined, fromChainId: number, toChainId: number, fromToken: {address: string, symbol?: string | undefined, decimals?: number | undefined}, toToken: {address: string, symbol?: string | undefined, decimals?: number | undefined}, amount: string, recipient: string, slippageBps?: number | undefined, estimatedUsd?: number | undefined}}; export type CreateApiAdaptersBridgeQuoteOptions = {baseUrl?: string | undefined, tenantApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAdaptersBridgeQuoteResponse = z.object({ok: z.literal(true), data: z.object({quote: z.object({provider: z.string(), quoteId: z.string(), fromChainId: z.number().int(), toChainId: z.number().int(), fromToken: z.object({address: z.string(), symbol: z.string().optional(), decimals: z.number().int().gte(0).lte(36).optional()}).optional(), toToken: z.object({address: z.string(), symbol: z.string().optional(), decimals: z.number().int().gte(0).lte(36).optional()}).optional(), amountIn: z.string(), amountOut: z.string(), minAmountOut: z.string().optional(), feeAmount: z.string().optional(), recipient: z.string().optional(), route: z.array(z.record(z.string(), z.unknown())).optional(), slippageBps: z.number().int().optional(), expiresAt: z.number().int().optional(), direction: z.string().optional(), executionMode: z.enum(["unsigned-transaction", "external-handoff"]).optional(), handoffUrl: z.string().optional(), feeBps: z.number().int().gte(0).lte(10000).optional(), feeScope: z.enum(["final", "global-estimate", "not-applicable"]).optional(), feeObservedSlot: z.number().int().gte(0).optional(), feeObservedAt: z.number().int().gte(0).optional(), notices: z.array(z.string()).optional()}).optional()})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Quote a bridge transfer * * POST /adapters/bridge/quote * * Also accepts auth: bearerAuth */ export const createApiAdaptersBridgeQuote = async (args: CreateApiAdaptersBridgeQuoteArgs, options?: CreateApiAdaptersBridgeQuoteOptions) => { const {baseUrl = "http://localhost:3200", fetch: fetchFn = fetch, tenantApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Steward-Request-Timestamp'] != null) headers.set("X-Steward-Request-Timestamp", String(args['X-Steward-Request-Timestamp'])) if (args['X-Steward-Request-Expires-At'] != null) headers.set("X-Steward-Request-Expires-At", String(args['X-Steward-Request-Expires-At'])) if (args['X-Steward-Signature'] != null) headers.set("X-Steward-Signature", String(args['X-Steward-Signature'])) if (args['X-Steward-Signing-Key-Id'] != null) headers.set("X-Steward-Signing-Key-Id", String(args['X-Steward-Signing-Key-Id'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (tenantApiKey !== undefined) headers.set("X-Steward-API-Key", tenantApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/adapters/bridge/quote${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAdaptersBridgeQuoteResponse, await res.json()) };