import {z} from 'zod' export type Chains = 'ARBITRUM_MAINNET' | 'ARBITRUM_TESTNET' | 'AVALANCHE_MAINNET' | 'AVALANCHE_TESTNET' | 'BASE_MAINNET' | 'BASE_TESTNET' | 'STARKNET_MAINNET' | 'STARKNET_TESTNET' | 'BSC_MAINNET' | 'ETHEREUM_MAINNET' | 'ETHEREUM_TESTNET' | 'OPTIMISM_MAINNET' | 'OPTIMISM_TESTNET' | 'MONAD_MAINNET' | 'MONAD_TESTNET' | 'POLYGON_MAINNET' | 'HYPEREVM_MAINNET' | 'LISK_MAINNET' | 'SOLANA_MAINNET' | 'SOLANA_TESTNET' | 'TRON_MAINNET'; export type CreateIntentDto = {sender?: string | undefined, paymentLinkId?: string | undefined, amount: string, amountSymbol: string, tokenIn: string, source_chain: Chains, destination_chain: Chains, recipient: string, refund_address?: string | undefined, metadata?: Record | undefined}; export type CreateApiIntentsArgs = {body: CreateIntentDto}; export type CreateApiIntentsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiIntentsResponse = z.object({id: z.number().optional(), client_id: z.string().optional(), sender: z.string().optional(), initialAmount: z.string().optional(), fees_in_usd: z.string().optional(), app_fee_in_usd: z.string().optional(), total_amount_in_usd: z.string().optional(), total_amount_in_asset_token: z.string().optional(), fees_in_asset_token: z.string().optional(), app_fee_in_asset_token: z.string().optional(), asset_token_symbol: z.string().optional(), asset_token_decimals: z.number().optional(), slippage: z.string().optional(), tokenIn: z.string().optional(), tokenOut: z.string().optional(), intent_address: z.string().optional(), source_chain: z.string().optional(), destination_chain: z.string().optional(), recipient: z.string().optional(), refund_address: z.string().optional(), relayer: z.string().optional(), coordinator: z.string().optional(), bridger: z.string().optional(), bridgeExtraData: z.string().optional(), intent_nonce: z.number().optional(), intent_status: z.enum(["PENDING", "PROCESSING", "COMPLETED", "FAILED"]).optional(), tx_hash: z.string().nullable().optional(), needs_relay: z.boolean().optional(), expires_at: z.string().optional(), metadata: z.object({}).optional(), created_at: z.string().optional(), updated_at: z.string().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 }; /** * Create new intent * * POST /intents */ export const createApiIntents = async (args: CreateApiIntentsArgs, options?: CreateApiIntentsOptions) => { const {baseUrl = "https://api.chainrails.io/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/intents${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(createApiIntentsResponse, await res.json()) };