import {z} from 'zod' export type RpcConfig = {url: string, weight: number}; export type RelayerNetworkType = 'evm' | 'solana' | 'stellar'; export type RelayerEvmPolicy = {eip1559_pricing?: (boolean | null) | undefined, gas_limit_estimation?: (boolean | null) | undefined, gas_price_cap?: (number | null) | undefined, include_revert_data?: (boolean | null) | undefined, min_balance?: (number | null) | undefined, private_transactions?: (boolean | null) | undefined, whitelist_receivers?: (Array | null) | undefined}; export type SolanaAllowedTokensSwapConfig = {max_amount?: number | undefined, min_amount?: number | undefined, retain_min_amount?: number | undefined, slippage_percentage?: number | undefined}; export type SolanaAllowedTokensPolicy = {decimals?: number | undefined, max_allowed_fee?: number | undefined, mint: string, swap_config?: SolanaAllowedTokensSwapConfig | undefined, symbol?: string | undefined}; export type SolanaFeePaymentStrategy = 'user' | 'relayer'; export type JupiterSwapOptions = {dynamic_compute_unit_limit?: boolean | undefined, priority_fee_max_lamports?: number | undefined, priority_level?: string | undefined}; export type SolanaSwapStrategy = 'jupiter-swap' | 'jupiter-ultra' | 'noop'; export type RelayerSolanaSwapConfig = {cron_schedule?: string | undefined, jupiter_swap_options?: JupiterSwapOptions | undefined, min_balance_threshold?: number | undefined, strategy?: SolanaSwapStrategy | undefined}; export type RelayerSolanaPolicy = {allowed_accounts?: (Array | null) | undefined, allowed_programs?: (Array | null) | undefined, allowed_tokens?: (Array | null) | undefined, disallowed_accounts?: (Array | null) | undefined, fee_margin_percentage?: (number | null) | undefined, fee_payment_strategy?: SolanaFeePaymentStrategy | undefined, max_allowed_fee_lamports?: (number | null) | undefined, max_signatures?: (number | null) | undefined, max_tx_data_size?: (number | null) | undefined, min_balance?: (number | null) | undefined, swap_config?: RelayerSolanaSwapConfig | undefined}; export type StellarTokenKind = 'native' | {classic: {code: string, issuer: string}} | {contract: {contract_id: string}}; export type StellarTokenMetadata = {canonical_asset_id: string, decimals: number, kind: StellarTokenKind}; export type StellarAllowedTokensSwapConfig = {max_amount?: number | undefined, min_amount?: number | undefined, retain_min_amount?: number | undefined, slippage_percentage?: number | undefined}; export type StellarAllowedTokensPolicy = {asset: string, max_allowed_fee?: number | undefined, metadata?: StellarTokenMetadata | undefined, swap_config?: StellarAllowedTokensSwapConfig | undefined}; export type StellarFeePaymentStrategy = 'user' | 'relayer'; export type StellarSwapStrategy = 'order-book' | 'soroswap'; export type RelayerStellarSwapConfig = {cron_schedule?: string | undefined, min_balance_threshold?: number | undefined, strategies?: Array | undefined}; export type RelayerStellarPolicy = {allowed_tokens?: (Array | null) | undefined, concurrent_transactions?: (boolean | null) | undefined, fee_margin_percentage?: (number | null) | undefined, fee_payment_strategy?: StellarFeePaymentStrategy | undefined, max_fee?: (number | null) | undefined, min_balance?: (number | null) | undefined, slippage_percentage?: (number | null) | undefined, swap_config?: RelayerStellarSwapConfig | undefined, timeout_seconds?: (number | null) | undefined}; export type CreateRelayerPolicyRequest = RelayerEvmPolicy | RelayerSolanaPolicy | RelayerStellarPolicy; export type CreateRelayerRequest = {custom_rpc_urls?: Array | undefined, id?: string | undefined, name: string, network: string, network_type: RelayerNetworkType, notification_id?: string | undefined, paused: boolean, policies?: CreateRelayerPolicyRequest | undefined, signer_id: string}; export type CreateApiApiV1RelayersArgs = {body: CreateRelayerRequest}; export type CreateApiApiV1RelayersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const maskedRpcConfig = z.object({url: z.string(), weight: z.number().int().gte(0).lte(100)}); export const disabledReason = z.union([z.object({details: z.string(), type: z.literal("NonceSyncFailed")}), z.object({details: z.string(), type: z.literal("RpcValidationFailed")}), z.object({details: z.string(), type: z.literal("BalanceCheckFailed")}), z.object({details: z.string(), type: z.literal("SequenceSyncFailed")}), z.object({details: z.array(z.string()), type: z.literal("Multiple")})]); export const relayerNetworkType = z.enum(["evm", "solana", "stellar"]); export const evmPolicyResponse = z.object({eip1559_pricing: z.boolean().optional(), gas_limit_estimation: z.boolean().optional(), gas_price_cap: z.number().int().gte(0).optional(), include_revert_data: z.boolean().optional(), min_balance: z.number().int().gte(0).optional(), private_transactions: z.boolean().optional(), whitelist_receivers: z.array(z.string()).optional()}); export const stellarTokenKind = z.union([z.literal("native"), z.object({classic: z.object({code: z.string(), issuer: z.string()})}), z.object({contract: z.object({contract_id: z.string()})})]); export const stellarTokenMetadata = z.object({canonical_asset_id: z.string(), decimals: z.number().int().gte(0), kind: stellarTokenKind}); export const stellarAllowedTokensSwapConfig = z.object({max_amount: z.number().int().gte(0).optional(), min_amount: z.number().int().gte(0).optional(), retain_min_amount: z.number().int().gte(0).optional(), slippage_percentage: z.number().optional()}); export const stellarAllowedTokensPolicy = z.object({asset: z.string(), max_allowed_fee: z.number().int().gte(0).optional(), metadata: stellarTokenMetadata.optional(), swap_config: stellarAllowedTokensSwapConfig.optional()}); export const stellarFeePaymentStrategy = z.enum(["user", "relayer"]); export const stellarSwapStrategy = z.enum(["order-book", "soroswap"]); export const relayerStellarSwapConfig = z.object({cron_schedule: z.string().optional(), min_balance_threshold: z.number().int().gte(0).optional(), strategies: z.array(stellarSwapStrategy).optional()}); export const stellarPolicyResponse = z.object({allowed_tokens: z.array(stellarAllowedTokensPolicy).optional(), concurrent_transactions: z.boolean().optional(), fee_margin_percentage: z.number().optional(), fee_payment_strategy: stellarFeePaymentStrategy.optional(), max_fee: z.number().int().gte(0).optional(), min_balance: z.number().int().gte(0).optional(), slippage_percentage: z.number().optional(), swap_config: relayerStellarSwapConfig.optional(), timeout_seconds: z.number().int().gte(0).optional()}); export const solanaAllowedTokensSwapConfig = z.object({max_amount: z.number().int().gte(0).optional(), min_amount: z.number().int().gte(0).optional(), retain_min_amount: z.number().int().gte(0).optional(), slippage_percentage: z.number().optional()}); export const solanaAllowedTokensPolicy = z.object({decimals: z.number().int().gte(0).optional(), max_allowed_fee: z.number().int().gte(0).optional(), mint: z.string(), swap_config: solanaAllowedTokensSwapConfig.optional(), symbol: z.string().optional()}); export const solanaFeePaymentStrategy = z.enum(["user", "relayer"]); export const jupiterSwapOptions = z.object({dynamic_compute_unit_limit: z.boolean().optional(), priority_fee_max_lamports: z.number().int().gte(0).optional(), priority_level: z.string().optional()}); export const solanaSwapStrategy = z.enum(["jupiter-swap", "jupiter-ultra", "noop"]); export const relayerSolanaSwapConfig = z.object({cron_schedule: z.string().optional(), jupiter_swap_options: jupiterSwapOptions.optional(), min_balance_threshold: z.number().int().gte(0).optional(), strategy: solanaSwapStrategy.optional()}); export const solanaPolicyResponse = z.object({allowed_accounts: z.array(z.string()).optional(), allowed_programs: z.array(z.string()).optional(), allowed_tokens: z.array(solanaAllowedTokensPolicy).optional(), disallowed_accounts: z.array(z.string()).optional(), fee_margin_percentage: z.number().optional(), fee_payment_strategy: solanaFeePaymentStrategy.optional(), max_allowed_fee_lamports: z.number().int().gte(0).optional(), max_signatures: z.number().int().gte(0).optional(), max_tx_data_size: z.number().int().gte(0).optional(), min_balance: z.number().int().gte(0).optional(), swap_config: relayerSolanaSwapConfig.optional()}); export const relayerNetworkPolicyResponse = z.union([evmPolicyResponse, stellarPolicyResponse, solanaPolicyResponse]); 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 apiResponseRelayerResponse = z.object({data: z.object({address: z.string().optional(), custom_rpc_urls: z.array(maskedRpcConfig).optional(), disabled_reason: disabledReason.optional(), id: z.string(), name: z.string(), network: z.string(), network_type: relayerNetworkType, notification_id: z.string().optional(), paused: z.boolean(), policies: relayerNetworkPolicyResponse.optional(), signer_id: z.string(), system_disabled: z.boolean().optional()}).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 }; /** * Creates a new relayer. * * POST /api/v1/relayers */ export const createApiApiV1Relayers = async (args: CreateApiApiV1RelayersArgs, options: CreateApiApiV1RelayersOptions) => { 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${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(apiResponseRelayerResponse, await res.json()) };