import {z} from 'zod' export type EncodedSerializedTransaction = string; export type SolanaFeeEstimateRequestParams = {fee_token: string, transaction: EncodedSerializedTransaction}; export type AssetSpec = {type: 'native'} | {code: string, issuer: string, type: 'credit4'} | {code: string, issuer: string, type: 'credit12'}; export type AuthSpec = {type: 'none'} | {type: 'source_account'} | {signers: Array, type: 'addresses'} | {entries: Array, type: 'xdr'}; export type ContractSource = {address: string, from: 'address'} | {contract: string, from: 'contract'}; export type WasmSource = {hex: string} | {base64: string}; export type OperationSpec = {amount: string, asset: AssetSpec, destination: string, type: 'payment'} | {args: Array, auth?: (AuthSpec | null) | undefined, contract_address: string, function_name: string, type: 'invoke_contract'} | {auth?: (AuthSpec | null) | undefined, constructor_args?: (Array | null) | undefined, salt?: (string | null) | undefined, source: ContractSource, type: 'create_contract', wasm_hash: string} | {auth?: (AuthSpec | null) | undefined, type: 'upload_wasm', wasm: WasmSource}; export type StellarFeeEstimateRequestParams = {fee_token: string, operations?: (Array | null) | undefined, source_account?: (string | null) | undefined, transaction_xdr?: (string | null) | undefined}; export type SponsoredTransactionQuoteRequest = SolanaFeeEstimateRequestParams | StellarFeeEstimateRequestParams; export type CreateApiApiV1RelayersRelayerIdTransactionsSponsoredQuoteArgs = {relayer_id: string, body: SponsoredTransactionQuoteRequest}; export type CreateApiApiV1RelayersRelayerIdTransactionsSponsoredQuoteOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const solanaFeeEstimateResult = z.object({conversion_rate: z.string(), estimated_fee: z.string()}); export const stellarFeeEstimateResult = z.object({conversion_rate: z.string(), fee_in_token: z.string(), fee_in_token_ui: z.string(), max_fee_in_token: z.string().nullable().optional(), max_fee_in_token_ui: z.string().nullable().optional()}); export const logLevel = z.enum(["log", "info", "error", "warn", "debug", "result"]); export const logEntry = z.object({level: logLevel, message: z.string()}); export const pluginMetadata = z.object({logs: z.array(logEntry).nullable().optional(), traces: z.array(z.unknown()).nullable().optional()}); export const paginationMeta = z.object({current_page: z.number().int().gte(0), per_page: z.number().int().gte(0), total_items: z.number().int().gte(0)}); export const apiResponseSponsoredTransactionQuoteResponse = z.object({data: z.union([solanaFeeEstimateResult, stellarFeeEstimateResult]).optional(), error: z.string().optional(), metadata: pluginMetadata.optional(), pagination: paginationMeta.optional(), success: z.boolean()}); 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 }; /** * Estimates fees for a sponsored (gasless) transaction. * * POST /api/v1/relayers/{relayer_id}/transactions/sponsored/quote */ export const createApiApiV1RelayersRelayerIdTransactionsSponsoredQuote = async (args: CreateApiApiV1RelayersRelayerIdTransactionsSponsoredQuoteArgs, options: CreateApiApiV1RelayersRelayerIdTransactionsSponsoredQuoteOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/relayers/${encodeURIComponent(String(args.relayer_id))}/transactions/sponsored/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(apiResponseSponsoredTransactionQuoteResponse, await res.json()) };