import {z} from 'zod' export type Speed = 'fastest' | 'fast' | 'average' | 'safeLow'; export type EvmTransactionRequest = {data?: string | undefined, gas_limit?: (number | null) | undefined, gas_price?: number | undefined, max_fee_per_gas?: number | undefined, max_priority_fee_per_gas?: number | undefined, speed?: Speed | undefined, to?: string | undefined, valid_until?: string | undefined, value: number}; export type SolanaAccountMeta = {is_signer: boolean, is_writable: boolean, pubkey: string}; export type SolanaInstructionSpec = {accounts: Array, data: string, program_id: string}; export type EncodedSerializedTransaction = string; export type SolanaTransactionRequest = {instructions?: (Array | null) | undefined, transaction?: (EncodedSerializedTransaction | null) | undefined, valid_until?: (string | null) | undefined}; export type MemoSpec = {type: 'none'} | {type: 'text', value: string} | {type: 'id', value: string} | {type: 'hash', value: Array} | {type: 'return', value: Array}; 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 StellarTransactionRequest = {fee_bump?: (boolean | null) | undefined, max_fee?: (number | null) | undefined, memo?: (MemoSpec | null) | undefined, network: string, operations?: (Array | null) | undefined, signed_auth_entry?: (string | null) | undefined, source_account?: (string | null) | undefined, transaction_xdr?: (string | null) | undefined, valid_until?: (string | null) | undefined}; export type NetworkTransactionRequest = EvmTransactionRequest | SolanaTransactionRequest | StellarTransactionRequest; export type CreateApiApiV1RelayersRelayerIdTransactionsArgs = {relayer_id: string, body: NetworkTransactionRequest}; export type CreateApiApiV1RelayersRelayerIdTransactionsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const evmTransactionDataSignature = z.object({r: z.string(), s: z.string(), sig: z.string(), v: z.number().int().gte(0)}); export const speed = z.enum(["fastest", "fast", "average", "safeLow"]); export const transactionStatus = z.enum(["canceled", "pending", "sent", "submitted", "mined", "confirmed", "failed", "expired"]); export const evmTransactionResponse = z.object({confirmed_at: z.string().optional(), created_at: z.string(), data: z.string().optional(), from: z.string(), gas_limit: z.number().int().gte(0).nullable().optional(), gas_price: z.string().optional(), hash: z.string().optional(), id: z.string(), is_canceled: z.boolean().optional(), max_fee_per_gas: z.string().optional(), max_priority_fee_per_gas: z.string().optional(), nonce: z.number().int().gte(0).optional(), relayer_id: z.string(), sent_at: z.string().optional(), signature: evmTransactionDataSignature.nullable().optional(), speed: speed.nullable().optional(), status: transactionStatus, status_reason: z.string().nullable().optional(), to: z.string().optional(), value: z.string()}); export const solanaAccountMeta = z.object({is_signer: z.boolean(), is_writable: z.boolean(), pubkey: z.string()}); export const solanaInstructionSpec = z.object({accounts: z.array(solanaAccountMeta), data: z.string(), program_id: z.string()}); export const solanaTransactionResponse = z.object({confirmed_at: z.string().optional(), created_at: z.string(), id: z.string(), instructions: z.array(solanaInstructionSpec).optional(), sent_at: z.string().optional(), signature: z.string().optional(), status: transactionStatus, status_reason: z.string().optional(), transaction: z.string()}); export const stellarTransactionResponse = z.object({confirmed_at: z.string().optional(), created_at: z.string(), fee: z.number().int().gte(0), hash: z.string().optional(), id: z.string(), relayer_id: z.string(), sent_at: z.string().optional(), sequence_number: z.string(), source_account: z.string(), status: transactionStatus, status_reason: z.string().nullable().optional(), transaction_result_xdr: z.string().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 apiResponseTransactionResponse = z.object({data: z.union([evmTransactionResponse, solanaTransactionResponse, stellarTransactionResponse]).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 }; /** * Sends a transaction through the specified relayer. * * POST /api/v1/relayers/{relayer_id}/transactions */ export const createApiApiV1RelayersRelayerIdTransactions = async (args: CreateApiApiV1RelayersRelayerIdTransactionsArgs, options: CreateApiApiV1RelayersRelayerIdTransactionsOptions) => { 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${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(apiResponseTransactionResponse, await res.json()) };